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

[BUG] The dragzone is calling the custom validator on hover with a file #1333

Open
alexandreczg opened this issue Dec 8, 2023 · 2 comments
Labels

Comments

@alexandreczg
Copy link

alexandreczg commented Dec 8, 2023

Describe the bug
When a custom validator is enabled and the user hovers the dropzone with a file dragged, it custom validator gets called with an undefined causing errors in the console.

To Reproduce

  1. Just use the official example from here
  2. Find a file to drag over the dropzone
  3. Observe the errors in the console of your browser
TypeError: Cannot read properties of undefined (reading 'length')
    at nameLengthValidator (test.tsx:6:19)
    at eval (index.js:122:1)
    at Array.every (<anonymous>)
    at allFilesAccepted (index.js:113:1)
    at eval (index.js:578:61)

Expected behavior
The custom validator should not be called until the files are actually dropped in the dropzone.

Screenshots

image

Here's a GIF:

dragzone-error

Desktop (please complete the following information):

  • OS: MacOs & Windows
  • Browser Chrome and Firefox
  • Version Latest
@fecoderchinh
Copy link

fecoderchinh commented Dec 11, 2023

I added ? at file.name?.length.

@alexandreczg
Copy link
Author

@fecoderchinh yes, you can build some guardrails on your own, such as:

const validateInputFile = useCallback(
        (file: File | DataTransferItem) => {
            if (file instanceof DataTransferItem) {
                // This is a on drag item that gets passed as an argument to this function
                // it caused errors below, since it does not have the same properties as File
                return null;
            }
...

But I'd like to understand why is the validate function being called before the files are actually released into the dragzone. If you have an expensive validation function, this would cause some performance issues.

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

No branches or pull requests

2 participants