import { Box, Button, Flex, FormControl, FormErrorMessage, FormLabel, Image, Input, Modal, ModalBody, ModalCloseButton, ModalContent, ModalFooter, ModalHeader, ModalOverlay, Textarea, useToast } from '@chakra-ui/react' import { useSnekFinder } from '@jaenjs/snek-finder' import * as React from 'react' import { useForm } from 'react-hook-form' type FormData = Partial<{ image: string title: string description: string }> export interface UpdateModalProps { isOpen: boolean onClose: () => void onUpdate: (data: FormData) => void onDelete: () => void data: FormData } export const UpdateModal = ({ isOpen, onClose, onUpdate, onDelete, data }: UpdateModalProps) => { const toast = useToast() const [defaultValues, setDefaultValues] = React.useState(data) React.useEffect(() => { setDefaultValues(data) reset(data) }, [data]) const { register, reset, handleSubmit, formState: {errors, isSubmitting, isDirty} } = useForm({ defaultValues }) const finder = useSnekFinder({ mode: 'selector', onSelect: ({src, name, description}) => { onUpdate({ image: src, title: name, description: description || '' }) } }) const handleDelete = () => { onDelete() toast({ title: 'Deleted', description: 'The image has been cleared from the field.', status: 'success', duration: 5000, isClosable: true }) onClose() } const onSubmit = (values: FormData) => { onUpdate(values) toast({ title: 'Success', description: 'The image has been updated', status: 'success', duration: 5000, isClosable: true }) onClose() } const handleImageClick = () => { finder.toggleSelector() } return ( {finder.finderElement}
Update Image Title {errors?.title?.message} Description (alt)