diff --git a/public/locales/en/translation.json b/public/locales/en/translation.json index 98b0d5a..ca480b9 100644 --- a/public/locales/en/translation.json +++ b/public/locales/en/translation.json @@ -260,6 +260,10 @@ "loading": "Loading... This may take a moment.", "result": "Result" }, + "toolMultipleImageInput": { + "inputTitle": "Images Input", + "noFilesSelected": "No files selected" + }, "userTypes": { "developers": "Developers", "generalUsers": "General users" diff --git a/src/components/input/ToolMultipleImageInput.tsx b/src/components/input/ToolMultipleImageInput.tsx new file mode 100644 index 0000000..fe5e2f1 --- /dev/null +++ b/src/components/input/ToolMultipleImageInput.tsx @@ -0,0 +1,169 @@ +import React, { useRef } from 'react'; +import { Box } from '@mui/material'; +import Typography from '@mui/material/Typography'; +import InputHeader from '../InputHeader'; +import InputFooter from './InputFooter'; +import ImageIcon from '@mui/icons-material/Image'; +import { useTranslation } from 'react-i18next'; + +interface MultiImageInputComponentProps { + accept: string[]; + title?: string; + type: 'image'; + value: MultiImageInput[]; + onChange: (files: MultiImageInput[]) => void; +} + +export interface MultiImageInput { + file: File; + order: number; + preview?: string; +} + +export default function ToolMultiImageInput({ + value, + onChange, + accept, + title, + type +}: MultiImageInputComponentProps) { + const { t } = useTranslation(); + const fileInputRef = useRef(null); + + const handleFileChange = (event: React.ChangeEvent) => { + const files = event.target.files; + if (files) { + const newFiles: MultiImageInput[] = Array.from(files).map((file) => ({ + file, + order: value.length, + preview: URL.createObjectURL(file) + })); + onChange([...value, ...newFiles]); + } + }; + + const handleImportClick = () => { + fileInputRef.current?.click(); + }; + + function handleClear() { + onChange([]); + } + + function fileNameTruncate(fileName: string) { + const maxLength = 10; + if (fileName.length > maxLength) { + return fileName.slice(0, maxLength) + '...'; + } + return fileName; + } + + return ( + + + + + {value?.length ? ( + value.map((file, index) => ( + + {file.preview ? ( + + ) : ( + + )} + + + {fileNameTruncate(file.file.name)} + + { + const updatedFiles = value.filter((_, i) => i !== index); + onChange(updatedFiles); + }} + > + ✖ + + + + )) + ) : ( + + {t('toolMultipleImageInput.noFilesSelected')} + + )} + + + + + + + ); +}