A file input (dropzone) management component for React
npm install react-filesReact Files
=======================
A minimal, zero dependency, file input (dropzone) component for React.
If upgrading from version 1 or 2, see the Upgrading to Version 3 section below.
Install using npm or yarn. Requires React 16.8+.
``bash`
npm install react-files --save
`js
import React from 'react'
import Files from 'react-files'
const FileDropzone = () => {
const handleChange = (files) => {
console.log(files)
}
const handleError = (error, file) => {
console.log('error code ' + error.code + ': ' + error.message)
}
return (
Upgrading to Version 3
Most of the changes made to version 3 are internal, but there are some notable and breaking changes:
1. The most significant change is that
react-files no longer manages state internally to track files that have been uploaded to a file list. This can be achieved quite simply however - please refer to the ListWithUpload example.
2. dropActiveClassName prop has been renamed to dragActiveClassName.
2. Removed unnecessary parent/wrapper div element. No more default values for className or dragActiveClassName props.
3. Ability to pass in a render prop with a prop that indicates whether a drag is in progress. See the RenderProps example.
4. Ability to pass in attributes to underlying inputFor a full list of changes, please checkout the v3.0.0 release changelog or the corresponding pull request.
Props
onChange(files) - FunctionPerform work on files added when submit is clicked.
---
onError(error, file) - Function
- error.code - Number
- error.message - StringPerform work or notify the user when an error occurs.
Error codes are:
1. Invalid file type
2. File too large
3. File too small
4. Maximum file count reached
---
accepts - Array of StringControl what types of generic/specific MIME types or file extensions can be dropped/added.
> See full list of MIME types here: http://www.iana.org/assignments/media-types/media-types.xhtml
Example:
`js
accepts={['image/', 'video/mp4', 'audio/', '.pdf']}
`---
multiple - BooleanDefault:
trueAllow multiple files
---
clickable - BooleanDefault:
trueDropzone is clickable to open file browser. Disable for dropping only.
---
maxFiles - NumberDefault:
InfinityMaximum number of files allowed
---
maxFileSize - NumberDefault:
InfinityMaximum file size allowed (in bytes)
---
minFileSize - NumberDefault:
0Minimum file size allowed (in bytes)
---
dragActiveClassName - StringClass added to the Files component when user is actively hovering over the dropzone with files selected.
---
inputProps - ObjectDefault:
{}Inject properties directly into the underlying HTML
file input. Useful for setting required or overriding the style attributes.---
Examples
To run the examples locally, clone and install peer dependencies (React 16.8+)
`
git clone https://github.com/mother/react-files
npm install
npm i react react-dom
`Then run the examples server:
`
npm run examples
``Then visit http://localhost:8080/
MIT. Copyright (c) Mother Co. 2023