import styled from 'styled-components'; import { Button } from '../buttonv2/Buttonv2.component'; import { Icon } from '../icon/Icon.component'; import { Input, InputProps } from '../inputv2/inputv2'; import { Modal } from '../modal/Modal.component'; import { useToast } from '../toast/ToastProvider'; import { useForm } from 'react-hook-form'; import { UseMutationResult } from 'react-query'; import { Text } from '../text/Text.component'; import { useState } from 'react'; import { Stack, Wrap } from '../../spacing'; const UnderlinedText = styled(Text)` text-decoration-line: underline; text-decoration-style: dashed; cursor: text; `; type InlineInputForm = { value: string; }; type InlineInputProps = { defaultValue?: string; confirmationModal?: { title: JSX.Element; body: JSX.Element; }; changeMutation: UseMutationResult; } & InputProps; export const InlineInput = ({ defaultValue, confirmationModal, changeMutation, ...props }: InlineInputProps) => { const { register, handleSubmit, watch, reset } = useForm({ defaultValues: { value: defaultValue, }, }); const [isConfirmationModalOpened, setIsConfirmationModalOpened] = useState(false); const handleSuccess = () => { setIsConfirmationModalOpened(false); setIsEditing(false); setIsHover(false); }; const onSubmit = (data: InlineInputForm) => { if (confirmationModal) { setIsConfirmationModalOpened(true); } else { changeMutation.mutate(data, { onSuccess: () => { handleSuccess(); }, onError: () => { showToast({ open: true, status: 'error', message: 'An error occurred while updating the value', }); }, }); } }; const { showToast } = useToast(); const [isHover, setIsHover] = useState(false); const [isEditing, setIsEditing] = useState(false); const handleReset = () => { reset(); setIsEditing(false); setIsHover(false); }; //handle esc key to cancel editing const handleKeyDown = (event: React.KeyboardEvent) => { if (event.key === 'Escape') { handleReset(); } }; if (isEditing) { return ( <>