import AddPhotoAlternateIcon from '@mui/icons-material/AddPhotoAlternate'; import { Box, Stack, Typography } from "@mui/material"; import { ImageFormData } from "@/components/Exercises/models/exercise"; import { useFormikContext } from "formik"; import * as React from 'react'; import { useTranslation } from "react-i18next"; export function ImageDropZone() { const [t] = useTranslation(); const { values, setFieldValue } = useFormikContext(); const [isDragOver, setIsDragOver] = React.useState(false); const inputRef = React.useRef(null); const handleFile = (file: File) => { if (!file.type.startsWith('image/')) { return; } setFieldValue('file', file); setFieldValue('url', URL.createObjectURL(file)); }; const handleDrop = (e: React.DragEvent) => { e.preventDefault(); setIsDragOver(false); const file = e.dataTransfer.files?.[0]; if (file) { handleFile(file); } }; return ( { e.preventDefault(); setIsDragOver(true); }} onDragLeave={() => setIsDragOver(false)} onDrop={handleDrop} onClick={() => inputRef.current?.click()} data-testid="image-dropzone" sx={{ position: 'relative', border: '2px dashed', borderColor: isDragOver ? 'primary.main' : 'divider', borderRadius: 1, cursor: 'pointer', aspectRatio: '1', display: 'flex', alignItems: 'center', justifyContent: 'center', overflow: 'hidden', bgcolor: isDragOver ? 'action.hover' : 'transparent', // Visible children must not capture drag events; they would cause flicker. // The hidden file input is excluded so it remains clickable via the input ref. '& > *:not(input)': { pointerEvents: 'none' }, }} > {values.url ? ( Preview ) : ( {t('exercises.dropOrClickImage')} )} { const file = e.target.files?.[0]; if (file) { handleFile(file); } e.target.value = ''; }} /> ); }