Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add {validator} for custom validation
- Loading branch information
1 parent
5a4ae93
commit ebe2130
Showing
5 changed files
with
137 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
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 me rejected. | ||
|
||
```jsx harmony | ||
import React from 'react'; | ||
import {useDropzone} from 'react-dropzone'; | ||
|
||
const maxLength = 20; | ||
|
||
function nameLengthValidator(file) { | ||
if (file.name.length > maxLength) { | ||
return { | ||
code: "name-too-large", | ||
message: `Name is larger than ${maxLength} characters` | ||
}; | ||
} | ||
|
||
return null | ||
} | ||
|
||
function CustomValidation(props) { | ||
const { | ||
acceptedFiles, | ||
fileRejections, | ||
getRootProps, | ||
getInputProps | ||
} = useDropzone({ | ||
validator: nameLengthValidator | ||
}); | ||
|
||
const acceptedFileItems = acceptedFiles.map(file => ( | ||
<li key={file.path}> | ||
{file.path} - {file.size} bytes | ||
</li> | ||
)); | ||
|
||
const fileRejectionItems = fileRejections.map(({ file, errors }) => ( | ||
<li key={file.path}> | ||
{file.path} - {file.size} bytes | ||
<ul> | ||
{errors.map(e => ( | ||
<li key={e.code}>{e.message}</li> | ||
))} | ||
</ul> | ||
</li> | ||
)); | ||
|
||
return ( | ||
<section className="container"> | ||
<div {...getRootProps({ className: 'dropzone' })}> | ||
<input {...getInputProps()} /> | ||
<p>Drag 'n' drop some files here, or click to select files</p> | ||
<em>(Only files with name less than 20 characters will be accepted)</em> | ||
</div> | ||
<aside> | ||
<h4>Accepted files</h4> | ||
<ul>{acceptedFileItems}</ul> | ||
<h4>Rejected files</h4> | ||
<ul>{fileRejectionItems}</ul> | ||
</aside> | ||
</section> | ||
); | ||
} | ||
|
||
<CustomValidation /> | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters