Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File validator: "no selected files" case #64

Open
goodwin64 opened this issue Sep 9, 2019 · 2 comments
Open

File validator: "no selected files" case #64

goodwin64 opened this issue Sep 9, 2019 · 2 comments

Comments

@goodwin64
Copy link

ezgif com-video-to-gif

So basically file validation runs with empty files list producing "is not a file" error.

Any piece of advice would be highly appreciated.

Field:

<Field
  component={FieldUploadFile}
  type="file"
  name="attachments"
  label={<FormattedMessage id={messageIds['entity.ticket.field.attachments']} />}
  validate={[file({ allowBlank: true, maxFiles: 10, maxSize: '1 MB' })]}
/>

FieldUploadFile:

export function FieldUploadFileBase(props: IProps) {
  const [attachments, setAttachments] = React.useState<File[]>([]);

  const handleFileDrop = React.useCallback(
    (item: any, monitor: DropTargetMonitor) => {
      if (monitor) {
        const files = monitor.getItem().files;
        setAttachments(attachments.concat(files));
      }
    },
    [attachments],
  );

  const handleFileSelect = React.useCallback((e: React.ChangeEvent<HTMLInputElement>) => {
    if (e.target.files) {
      const selectedFiles = Array.from(e.target.files);
      setAttachments(attachments.concat(selectedFiles));
    }
  }, [attachments]);

  const onRemoveFile: AttachmentProps['onRemoveFile'] = (fileToRemove) => {
    setAttachments(attachments.filter((attachment) => attachment !== fileToRemove));
  };

  React.useEffect(() => {
    props.input.onChange(attachments);
  }, [attachments]);

  return (
    <FormControl fullWidth>
      <Box marginBottom={3}>
        <AttachmentTargetBox onDrop={handleFileDrop} handleFileSelect={handleFileSelect} />
      </Box>
      <AttachmentsList files={attachments} onRemoveFile={onRemoveFile} />
      <p>{props.meta.error}</p>
    </FormControl>
  );
}
@goodwin64
Copy link
Author

Nor

<Field
  validate={[file({ allowBlank: true })]}
/>

neither

<Field
  validate={[file({ minFiles: 0 })]}
/>

helps

@goodwin64
Copy link
Author

Found a workaround:

props.input.onChange(attachments.length > 0 ? attachments : undefined);

but IMO lib has to handle "empty files list" case as well

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant