import { DragEventHandler, RefObject, } from 'react'; import { Box, BoxProps, Center, Flex, HStack, Image, } from '@chakra-ui/react'; import { MdHighlightOff, MdUploadFile, } from 'react-icons/md'; import { FormGroup } from '@/components/form/FormGroup'; import { UseFormidableReturn } from '@/components/form/Formidable'; import { DataUrlAccess } from '@/utils/data-url-access'; export type DragNdropProps = { onFilesSelected?: (file: File[]) => void; onUriSelected?: (uri: string) => void; width?: string; height?: string; }; export const DragNdrop = ({ onFilesSelected = () => { }, onUriSelected = () => { }, width = '100px', height = '100px', }: DragNdropProps) => { const handleFileChange = (event) => { const selectedFiles = event.target.files; if (selectedFiles && selectedFiles.length > 0) { const newFiles: File[] = Array.from(selectedFiles); onFilesSelected(newFiles); } }; const handleDrop = (eventInput: any) => { const event = eventInput as DragEvent; event.preventDefault(); const droppedFiles = event.dataTransfer?.files; console.log('drop ...' + droppedFiles?.length); if (droppedFiles && droppedFiles?.length > 0) { const newFiles: File[] = Array.from(droppedFiles); onFilesSelected(newFiles); } else { console.log(`drop types: ${event.dataTransfer?.types}`); const listUri = event.dataTransfer?.getData('text/uri-list'); console.log(`listUri: ${listUri}`); if (!listUri) { return; } onUriSelected(listUri); } }; return ( event.preventDefault()} > ); }; export type CenterIconProps = BoxProps & { children: any; sizeIcon?: string; }; export const CenterIcon = ({ children, sizeIcon = '15px', ...rest }: CenterIconProps) => { return ( {children} ); }; export type FormCoversProps = { form: UseFormidableReturn; variableName: string; ref?: RefObject; label?: string; isRequired?: boolean; onFilesSelected?: (files: File[]) => void; onUriSelected?: (uri: string) => void; onRemove?: (index: number) => void; }; export const FormCovers = ({ form, variableName, ref, onFilesSelected = () => { }, onUriSelected = () => { }, onRemove = () => { }, ...rest }: FormCoversProps) => { const urls = DataUrlAccess.getListThumbnailUrl(form.values[variableName]) ?? []; return ( form.restoreValue({ [variableName]: true })} {...rest} > {urls.map((data, index) => ( onRemove && onRemove(index)} > ))} ); };