Skip to content

Commit

Permalink
[filigran-ui] - (fileInput) Manage allowedTypes with DnD
Browse files Browse the repository at this point in the history
  • Loading branch information
hervyt committed Oct 28, 2024
1 parent 3ccec39 commit 43bf103
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
16 changes: 9 additions & 7 deletions packages/filigran-ui/src/components/clients/file-input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ interface FileInputProps
allowedTypes?: string
value?: unknown
name: string
onChangeFileInput?: (files: FileList) => void
}

interface InputText {
Expand Down Expand Up @@ -70,7 +69,7 @@ const FileInputDropZone: FunctionComponent<{
}

function GenericFileInput(
{texts, allowedTypes, className, onChangeFileInput, ...props}: FileInputProps,
{texts, allowedTypes, className, ...props}: FileInputProps,
ref?: any
) {
const inputRef = useRef<HTMLInputElement | null>(null)
Expand All @@ -83,9 +82,6 @@ function GenericFileInput(
if (props.name) {
form.setValue(props.name, files)
}
if (onChangeFileInput) {
onChangeFileInput(files)
}
form.clearErrors(props.name)
}
const handleDragLeave = (e: React.DragEvent<HTMLDivElement>) => {
Expand All @@ -94,11 +90,17 @@ function GenericFileInput(
}

const handleDrop = (e: React.DragEvent<HTMLDivElement>) => {
// TODO Manage allow type, manage multiple or not
e.preventDefault()
setIsDragActive(false)
const files = e.dataTransfer.files
// const hasExtension = e.dataTransfer.files[0].name.includes('.')
const extension = e.dataTransfer.files[0].name.split('.')[1]

if (allowedTypes && !allowedTypes.includes(extension)) {
form.setError(props.name, {
message: 'Format not accepted',
})
return
}
if (!props.multiple && files.length > 1) {
form.setError(props.name, {
message: 'You can only select one file',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ const ExampleFileInput = () => {
<FormItem>
<FormLabel>File</FormLabel>
<FormControl>
<FileInput {...field} />
<FileInput
{...field}
allowedTypes={'image/png, applicatiion/pdf'}
/>
</FormControl>
<FormMessage />
</FormItem>
Expand All @@ -81,7 +84,10 @@ const ExampleFileInput = () => {
<FormItem>
<FormLabel>File</FormLabel>
<FormControl>
<FileInput {...field} />
<FileInput
{...field}
allowedTypes={'image/png, applicatiion/pdf'}
/>
</FormControl>
<FormMessage />
</FormItem>
Expand Down Expand Up @@ -109,6 +115,7 @@ const ExampleFileInput = () => {
<FormLabel>File</FormLabel>
<FormControl>
<FileInput
allowedTypes={'image/png, applicatiion/pdf'}
multiple
{...field}
/>
Expand Down Expand Up @@ -151,6 +158,7 @@ const ExampleFileInput = () => {
<FormLabel>File</FormLabel>
<FormControl>
<FileInput
allowedTypes={'image/png, applicatiion/pdf'}
multiple
{...field}
/>
Expand Down

0 comments on commit 43bf103

Please sign in to comment.