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

[@mantine/dropzone] Add the getFilesFromEvent and validator props #3053

Merged
merged 3 commits into from Dec 2, 2022

Conversation

mpinedami
Copy link
Contributor

Description

Add getFilesFromEvent and validator props to the Dropzone component, both are methods that React Dropzone supports

getFilesFromEvent prop

The Dropzone component a getFilesFromEvent prop that enhances the handling of dropped file system objects and allows more flexible use of them e.g. passing a function that accepts drop event of a folder and resolves it to an array of files adds plug-in functionality of folders drag-and-drop.

Though, note that the provided getFilesFromEvent fn must return a Promise with a list of File objects (or DataTransferItem of {kind: 'file'} when data cannot be accessed). Otherwise, the results will be discarded and none of the drag events callbacks will be invoked.

In case you need to extend the File with some additional properties, you should use Object.defineProperty() so that the result will still pass through our filter:

validator prop

By providing validator prop you can specify custom validation for files.

The value must be a function that accepts File object and returns null if file should be accepted or error object/array of error objects if file should be rejected.

@mpinedami mpinedami marked this pull request as ready for review November 24, 2022 19:24
src/mantine-dropzone/src/Dropzone.tsx Outdated Show resolved Hide resolved
src/mantine-dropzone/src/Dropzone.tsx Show resolved Hide resolved
@rtivital rtivital merged commit 4b90fe3 into mantinedev:master Dec 2, 2022
@rtivital
Copy link
Member

rtivital commented Dec 2, 2022

Thanks!

@porkopek
Copy link

porkopek commented Dec 4, 2022

After upgrading to 5.9.1, the Dropzone component throws this exception

getFilesFromEvent is not a function

so it seems that providing a value to the getFilesFromEvent is mandatory.
Before this, dropzone worked without this prop

@porkopek
Copy link

porkopek commented Dec 4, 2022

As a workaround, I made it work with

<Dropzone

   getFilesFromEvent={e => {
          return Promise.resolve([...(e.target as EventTarget & HTMLInputElement)?.files]);
        }}
  ...
/>

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

Successfully merging this pull request may close these issues.

None yet

3 participants