Skip to content

Commit

Permalink
feat: add existing image and input for custom accepted types
Browse files Browse the repository at this point in the history
  • Loading branch information
magsyg committed Dec 7, 2024
1 parent 10c66bc commit 71553b1
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions frontend/src/Components/InputFile/InputFile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,18 @@ export type InputFileProps = {
label?: ReactNode;
existing_url?: string;
error?: boolean | string;
acceptedTypes?: string;
onSelected?: (file: File) => void;
};

export function InputFile({ fileType = 'any', label, existing_url, error = false, onSelected }: InputFileProps) {
export function InputFile({
fileType = 'any',
acceptedTypes,
label,
existing_url,
error = false,
onSelected,
}: InputFileProps) {
const { t } = useTranslation();
const [selectedFile, setSelectedFile] = useState<File | undefined>(undefined);
const [preview, setPreview] = useState<string | undefined>(undefined);
Expand Down Expand Up @@ -52,14 +60,21 @@ export function InputFile({ fileType = 'any', label, existing_url, error = false
return () => URL.revokeObjectURL(objectUrl);
}, [selectedFile]);

useEffect(() => {
if (existing_url && isFileImage(existing_url)) {
setIsImage(true);
setPreview(existing_url);
}
}, [existing_url]);

function acceptTypes() {
switch (fileType) {
case 'image':
return 'image/*';
case 'pdf':
return 'application/pdf';
if (fileType === 'any' && !acceptedTypes) {
return '*';
}
return '*';
let types = acceptedTypes ? acceptedTypes : '';
if (fileType === 'image') types += ', image/*';
if (fileType === 'pdf') types += ', application/pdf';
return types;
}

const icons: Record<InputFileType, string> = {
Expand Down

0 comments on commit 71553b1

Please sign in to comment.