Commit 5eca4db9 authored by tom's avatar tom

multi file upload

parent bc69a003
...@@ -19,7 +19,7 @@ interface Props { ...@@ -19,7 +19,7 @@ interface Props {
} }
const ContractVerificationFieldSources = ({ accept, multiple, title, className, hint }: Props) => { const ContractVerificationFieldSources = ({ accept, multiple, title, className, hint }: Props) => {
const { setValue, getValues, control, formState } = useFormContext<FormFields>(); const { setValue, getValues, control, formState, clearErrors } = useFormContext<FormFields>();
const error = 'sources' in formState.errors ? formState.errors.sources : undefined; const error = 'sources' in formState.errors ? formState.errors.sources : undefined;
...@@ -31,8 +31,9 @@ const ContractVerificationFieldSources = ({ accept, multiple, title, className, ...@@ -31,8 +31,9 @@ const ContractVerificationFieldSources = ({ accept, multiple, title, className,
const value = getValues('sources').slice(); const value = getValues('sources').slice();
value.splice(index, 1); value.splice(index, 1);
setValue('sources', value); setValue('sources', value);
clearErrors('sources');
}, [ getValues, setValue ]); }, [ getValues, clearErrors, setValue ]);
const renderFiles = React.useCallback((files: Array<File>) => { const renderFiles = React.useCallback((files: Array<File>) => {
return ( return (
...@@ -54,7 +55,12 @@ const ContractVerificationFieldSources = ({ accept, multiple, title, className, ...@@ -54,7 +55,12 @@ const ContractVerificationFieldSources = ({ accept, multiple, title, className,
const renderControl = React.useCallback(({ field }: {field: ControllerRenderProps<FormFields, 'sources'>}) => ( const renderControl = React.useCallback(({ field }: {field: ControllerRenderProps<FormFields, 'sources'>}) => (
<> <>
<FileInput<FormFields, 'sources'> accept={ accept } multiple={ multiple } field={ field }> <FileInput<FormFields, 'sources'> accept={ accept } multiple={ multiple } field={ field }>
<Button variant="outline" size="sm" display={ field.value && field.value.length > 0 ? 'none' : 'block' }> <Button
variant="outline"
size="sm"
display={ field.value && field.value.length > 0 && !multiple ? 'none' : 'block' }
mb={ field.value && field.value.length > 0 ? 2 : 0 }
>
Upload file{ multiple ? 's' : '' } Upload file{ multiple ? 's' : '' }
</Button> </Button>
</FileInput> </FileInput>
......
...@@ -20,7 +20,7 @@ const FileInput = <Values extends FieldValues, Names extends Path<Values>>({ chi ...@@ -20,7 +20,7 @@ const FileInput = <Values extends FieldValues, Names extends Path<Values>>({ chi
} }
const files = Array.from(fileList); const files = Array.from(fileList);
field.onChange(files); field.onChange([ ...(field.value || []), ...files ]);
field.onBlur(); field.onBlur();
}, [ field ]); }, [ field ]);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment