-
Notifications
You must be signed in to change notification settings - Fork 34
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
Filetype validation #49
Comments
Hi @Lehj14 class FileTypeValidator implements Validator {
validateBeforeParsing(_config: UseFilePickerConfig, plainFiles: File[]): Promise<void> {
for (const file of plainFiles) {
// you can check the file either by MIME type or the extension, which can be extracted from file.name
if (file.type !== 'text/plain') {
// You can reject a promise with any object you want, it's going to be passed to errors prop
return Promise.reject({
fileTypeError: 'Only plain text files are allowed',
});
}
}
return Promise.resolve();
}
validateAfterParsing(
config: UseFilePickerConfig,
file: FileWithPath,
reader: FileReader
): Promise<void> {
return Promise.resolve();
}
}
function App() {
const [open, { plainFiles, errors }] = useFilePicker({
// don't forget to pass this new validator to the useFilePicker hook
validators: [new FileTypeValidator()],
});
return (
<div className="App">
<div className="card">
<button onClick={() => open()}>open picker</button>
</div>
{plainFiles.map((file) => (
<div>{file.name}</div>
))}
{errors.map((err) => (
<div>{JSON.stringify(err, null, 2)}</div>
))}
</div>
);
} Is that what you need? |
hi @MrKampla , can i do this on plain react? or it this a typescript? |
You can do it on plain reactjs. Just make sure to remove types from the validator. Besides that we advice to use typescript as it's industry standard |
@Lehj14 Sure, you can just create a plain object that has const fileTypeValidator = {
validateBeforeParsing(_config, plainFiles) {
// code
},
validateAfterParsing(config, file, reader) {
// code
}
}; Then in the hook: const [open, { plainFiles, errors }] = useFilePicker({
validators: [fileTypeValidator],
}); As @Jaaneek said, we strongly encourage using typescript and that's why we show every example using TS. |
The question has been answered, I'm closing this issue. |
It would be more in line with HTML A feature of default fileType validation that's based on the accept prop would be very helpful. |
In the latest release, v2.1.1 we've added |
Hi. Would you be able to add filetype validation? So it shows error when the filetype selected is not on the accept criteria?
The text was updated successfully, but these errors were encountered: