Gathering detailed insights and metrics for use-file-picker
Gathering detailed insights and metrics for use-file-picker
Gathering detailed insights and metrics for use-file-picker
Gathering detailed insights and metrics for use-file-picker
vanilla-picker
A simple, easy to use vanilla JS color picker with alpha selection.
react-datepicker
A simple and reusable datepicker component for React
react-date-picker
A date picker for your React app.
react-native-image-crop-picker
Select single or multiple images, with cropping option
npm install use-file-picker
93.7
Supply Chain
98
Quality
80.5
Maintenance
100
Vulnerability
100
License
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
281 Stars
137 Commits
34 Forks
4 Watching
21 Branches
14 Contributors
Updated on 22 Nov 2024
TypeScript (98.26%)
JavaScript (1.19%)
HTML (0.55%)
Cumulative downloads
Total Downloads
Last day
-7.2%
4,510
Compared to previous day
Last week
-8.5%
21,949
Compared to previous week
Last month
5.1%
103,427
Compared to previous month
Last year
42.9%
1,076,605
Compared to previous year
1
1
26
🏠 Homepage
npm i use-file-picker
or
yarn add use-file-picker
1import { useFilePicker } from 'use-file-picker'; 2import React from 'react'; 3 4export default function App() { 5 const { openFilePicker, filesContent, loading } = useFilePicker({ 6 accept: '.txt', 7 }); 8 9 if (loading) { 10 return <div>Loading...</div>; 11 } 12 13 return ( 14 <div> 15 <button onClick={() => openFilePicker()}>Select files</button> 16 <br /> 17 {filesContent.map((file, index) => ( 18 <div key={index}> 19 <h2>{file.name}</h2> 20 <div key={index}>{file.content}</div> 21 <br /> 22 </div> 23 ))} 24 </div> 25 ); 26}
1import { useFilePicker } from 'use-file-picker'; 2import { 3 FileAmountLimitValidator, 4 FileTypeValidator, 5 FileSizeValidator, 6 ImageDimensionsValidator, 7} from 'use-file-picker/validators'; 8 9export default function App() { 10 const { openFilePicker, filesContent, loading, errors } = useFilePicker({ 11 readAs: 'DataURL', 12 accept: 'image/*', 13 multiple: true, 14 validators: [ 15 new FileAmountLimitValidator({ max: 1 }), 16 new FileTypeValidator(['jpg', 'png']), 17 new FileSizeValidator({ maxFileSize: 50 * 1024 * 1024 /* 50 MB */ }), 18 new ImageDimensionsValidator({ 19 maxHeight: 900, // in pixels 20 maxWidth: 1600, 21 minHeight: 600, 22 minWidth: 768, 23 }), 24 ], 25 }); 26 27 if (loading) { 28 return <div>Loading...</div>; 29 } 30 31 if (errors.length) { 32 return <div>Error...</div>; 33 } 34 35 return ( 36 <div> 37 <button onClick={() => openFilePicker()}>Select files </button> 38 <br /> 39 {filesContent.map((file, index) => ( 40 <div key={index}> 41 <h2>{file.name}</h2> 42 <img alt={file.name} src={file.content}></img> 43 <br /> 44 </div> 45 ))} 46 </div> 47 ); 48}
1import { useFilePicker } from 'use-file-picker'; 2 3export default function App() { 4 const { openFilePicker, filesContent, loading, errors } = useFilePicker({ 5 readAs: 'DataURL', 6 accept: 'image/*', 7 multiple: true, 8 onFilesSelected: ({ plainFiles, filesContent, errors }) => { 9 // this callback is always called, even if there are errors 10 console.log('onFilesSelected', plainFiles, filesContent, errors); 11 }, 12 onFilesRejected: ({ errors }) => { 13 // this callback is called when there were validation errors 14 console.log('onFilesRejected', errors); 15 }, 16 onFilesSuccessfullySelected: ({ plainFiles, filesContent }) => { 17 // this callback is called when there were no validation errors 18 console.log('onFilesSuccessfullySelected', plainFiles, filesContent); 19 }, 20 }); 21 22 if (loading) { 23 return <div>Loading...</div>; 24 } 25 26 if (errors.length) { 27 return <div>Error...</div>; 28 } 29 30 return ( 31 <div> 32 <button onClick={() => openFilePicker()}>Select files </button> 33 <br /> 34 {filesContent.map((file, index) => ( 35 <div key={index}> 36 <h2>{file.name}</h2> 37 <img alt={file.name} src={file.content}></img> 38 <br /> 39 </div> 40 ))} 41 </div> 42 ); 43}
1import { useFilePicker } from 'use-file-picker'; 2import React from 'react'; 3 4export default function App() { 5 const { openFilePicker, filesContent, loading, errors, plainFiles, clear } = useFilePicker({ 6 multiple: true, 7 readAs: 'DataURL', // availible formats: "Text" | "BinaryString" | "ArrayBuffer" | "DataURL" 8 // accept: '.ics,.pdf', 9 accept: ['.json', '.pdf'], 10 validators: [new FileAmountLimitValidator({ min: 2, max: 3 })], 11 // readFilesContent: false, // ignores file content 12 }); 13 14 if (errors.length) { 15 return ( 16 <div> 17 <button onClick={() => openFilePicker()}>Something went wrong, retry! </button> 18 {errors.map(err => ( 19 <div> 20 {err.name}: {err.reason} 21 /* e.g. "name":"FileAmountLimitError", "reason":"MAX_AMOUNT_OF_FILES_EXCEEDED" */ 22 </div> 23 ))} 24 </div> 25 ); 26 } 27 28 if (loading) { 29 return <div>Loading...</div>; 30 } 31 32 return ( 33 <div> 34 <button onClick={() => openFilePicker()}>Select file </button> 35 <button onClick={() => clear()}>Clear</button> 36 <br /> 37 Number of selected files: 38 {plainFiles.length} 39 <br /> 40 {/* If readAs is set to DataURL, You can display an image */} 41 {!!filesContent.length && <img src={filesContent[0].content} />} 42 <br /> 43 {plainFiles.map(file => ( 44 <div key={file.name}>{file.name}</div> 45 ))} 46 </div> 47 ); 48}
You can hook your logic into callbacks that will be fired at specific events during the lifetime of the hook. useFilePicker accepts these callbacks:
These are described in more detail in the Props section.
1import { useFilePicker } from 'use-file-picker'; 2 3export default function App() { 4 const { openFilePicker, filesContent, loading, errors, plainFiles, clear } = useFilePicker({ 5 multiple: true, 6 readAs: 'DataURL', 7 accept: ['.json', '.pdf'], 8 onFilesSelected: ({ plainFiles, filesContent, errors }) => { 9 // this callback is always called, even if there are errors 10 console.log('onFilesSelected', plainFiles, filesContent, errors); 11 }, 12 onFilesRejected: ({ errors }) => { 13 // this callback is called when there were validation errors 14 console.log('onFilesRejected', errors); 15 }, 16 onFilesSuccessfullySelected: ({ plainFiles, filesContent }) => { 17 // this callback is called when there were no validation errors 18 console.log('onFilesSuccessfullySelected', plainFiles, filesContent); 19 }, 20 onClear: () => { 21 // this callback is called when the selection is cleared 22 console.log('onClear'); 23 }, 24 }); 25}
useImperativePicker
hook also accepts the callbacks listed above. Additionally, it accepts the onFileRemoved
callback, which is called when a file is removed from the list of selected files.
1import { useImperativeFilePicker } from 'use-file-picker'; 2 3export default function App() { 4 const { openFilePicker, filesContent, loading, errors, plainFiles, clear } = useImperativeFilePicker({ 5 onFileRemoved: (removedFile, removedIndex) => { 6 // this callback is called when a file is removed from the list of selected files 7 console.log('onFileRemoved', removedFile, removedIndex); 8 }, 9 }); 10}
If you want to keep the previously selected files and remove them from the selection, you can use a separate hook called useImperativeFilePicker
that is also exported in this package. For files removal, you can use removeFileByIndex
or removeFileByReference
functions.
1import React from 'react'; 2import { useImperativeFilePicker } from 'use-file-picker'; 3 4const Imperative = () => { 5 const { openFilePicker, filesContent, loading, errors, plainFiles, clear, removeFileByIndex, removeFileByReference } = 6 useImperativeFilePicker({ 7 multiple: true, 8 readAs: 'Text', 9 readFilesContent: true, 10 }); 11 12 if (errors.length) { 13 return ( 14 <div> 15 <button onClick={() => openFilePicker()}>Something went wrong, retry! </button> 16 <div style={{ display: 'flex', flexDirection: 'column' }}> 17 {console.log(errors)} 18 {errors.map(err => ( 19 <div> 20 {err.name}: {err.reason} 21 /* e.g. "name":"FileAmountLimitError", "reason":"MAX_AMOUNT_OF_FILES_EXCEEDED" */ 22 </div> 23 ))} 24 </div> 25 </div> 26 ); 27 } 28 29 if (loading) { 30 return <div>Loading...</div>; 31 } 32 33 return ( 34 <div> 35 <button onClick={async () => openFilePicker()}>Select file</button> 36 <button onClick={() => clear()}>Clear</button> 37 <br /> 38 Amount of selected files: 39 {plainFiles.length} 40 <br /> 41 Amount of filesContent: 42 {filesContent.length} 43 <br /> 44 {plainFiles.map((file, i) => ( 45 <div> 46 <div style={{ display: 'flex', alignItems: 'center' }} key={file.name}> 47 <div>{file.name}</div> 48 <button style={{ marginLeft: 24 }} onClick={() => removeFileByReference(file)}> 49 Remove by reference 50 </button> 51 <button style={{ marginLeft: 24 }} onClick={() => removeFileByIndex(i)}> 52 Remove by index 53 </button> 54 </div> 55 <div>{filesContent[i]?.content}</div> 56 </div> 57 ))} 58 </div> 59 ); 60};
Prop name | Description | Default value | Example values |
---|---|---|---|
multiple | Allow user to pick multiple files at once | true | true, false |
accept | Set type of files that user can choose from the list | "*" | [".png", ".txt"], "image/*", ".txt" |
readAs | Set a return type of filesContent | "Text" | "DataURL", "Text", "BinaryString", "ArrayBuffer" |
readFilesContent | Ignores files content and omits reading process if set to false | true | true, false |
validators | Add validation logic. You can use some of the built-in validators like FileAmountLimitValidator or create your own custom validation logic | [] | [MyValidator, MySecondValidator] |
initializeWithCustomParameters | allows for customization of the input element that is created by the file picker. It accepts a function that takes in the input element as a parameter and can be used to set any desired attributes or styles on the element. | n/a | (input) => input.setAttribute("disabled", "") |
onFilesSelected | A callback function that is called when files are successfully selected. The function is passed an array of objects with information about each successfully selected file | n/a | (data) => console.log(data) |
onFilesSuccessfullySelected | A callback function that is called when files are successfully selected. The function is passed an array of objects with information about each successfully selected file | n/a | (data) => console.log(data) |
onFilesRejected | A callback function that is called when files are rejected due to validation errors or other issues. The function is passed an array of objects with information about each rejected file | n/a | (data) => console.log(data) |
onClear | A callback function that is called when the selection is cleared. | n/a | () => console.log('selection cleared') |
Name | Description |
---|---|
openFilePicker | Opens file selector |
clear | Clears all files and errors |
filesContent | Get files array of type FileContent |
plainFiles | Get array of the File objects |
loading | True if the reading files is in progress, otherwise False |
errors | Get errors array of type FileError if any appears |
useFilePicker has some built-in validators that can be used out of the box. These are:
useFilePicker allows for injection of custom validation logic. Validation is divided into two steps:
1interface Validator { 2 validateBeforeParsing(config: UseFilePickerConfig, plainFiles: File[]): Promise<void>; 3 validateAfterParsing(config: UseFilePickerConfig, file: FileWithPath, reader: FileReader): Promise<void>; 4}
Validators must return Promise object - resolved promise means that file passed validation, rejected promise means that file did not pass.
Since version 2.0, validators also have optional callback handlers that will be run when an important event occurs during the selection process. These are:
1 /** 2 * lifecycle method called after user selection (regardless of validation result) 3 */ 4 onFilesSelected( 5 _data: SelectedFilesOrErrors<ExtractContentTypeFromConfig<ConfigType>, CustomErrors> 6 ): Promise<void> | void {} 7 /** 8 * lifecycle method called after successful validation 9 */ 10 onFilesSuccessfullySelected(_data: SelectedFiles<ExtractContentTypeFromConfig<ConfigType>>): Promise<void> | void {} 11 /** 12 * lifecycle method called after failed validation 13 */ 14 onFilesRejected(_data: FileErrors<CustomErrors>): Promise<void> | void {} 15 /** 16 * lifecycle method called after the selection is cleared 17 */ 18 onClear(): Promise<void> | void {} 19 20 /** 21 * This method is called when file is removed from the list of selected files. 22 * Invoked only by the useImperativeFilePicker hook 23 * @param _removedFile removed file 24 * @param _removedIndex index of removed file 25 */ 26 onFileRemoved(_removedFile: File, _removedIndex: number): Promise<void> | void {}
1/** 2 * validateBeforeParsing allows the user to select only an even number of files 3 * validateAfterParsing allows the user to select only files that have not been modified in the last 24 hours 4 */ 5class CustomValidator extends Validator { 6 async validateBeforeParsing(config: ConfigType, plainFiles: File) { 7 return new Promise<void>((res, rej) => (plainFiles.length % 2 === 0 ? res() : rej({ oddNumberOfFiles: true }))); 8 } 9 async validateAfterParsing(config: ConfigType, file: FileWithPath, reader: FileReader) { 10 return new Promise<void>((res, rej) => 11 file.lastModified < new Date().getTime() - 24 * 60 * 60 * 1000 12 ? res() 13 : rej({ fileRecentlyModified: true, lastModified: file.lastModified }) 14 ); 15 } 16 onFilesSuccessfullySelected(data: SelectedFiles<ExtractContentTypeFromConfig<ConfigType>>) { 17 console.log(data); 18 } 19}
👤 Milosz Jankiewicz
👤 Kamil Planer
👤 Adam Dobrzeniewski
Contributions, issues and feature requests are welcome!
Feel free to check the issues page.
Give a ⭐️ if this project helped you!
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 14/18 approved changesets -- score normalized to 7
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
security policy file not detected
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
46 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More