Dropzone
The default <input type="file"/>
is just a quite boring button. That's why we present the <Dropzone/>
component as an special input
enhanced by the ability to allow users to drag and drop files there or choose files from a file dialog and also to validate an upload them.
The widget is useful for handling one
File
or a list of Files
in one or more of these scenarios:- The file(s) must be chosen from a File Dialog or must be dragged and dropped into the widget
- The file(s) must be validated or not. If validation is required, it can be done by taking into account a predefined set of allowed Common MIME Types; specifiying the max file size (in bytes) and/or the max amount of files.
- The file(s) must be uploaded somewhere in the internet.
It's meant to be an improved version of the react-dropzone and dropzone packages.
Basic Dropzone
In this demo we set the
<Dropzone/>
with the minimum props that allows you to get your task done fast. These props are onChange
and value
.<Dropzone onChange={updateFiles} value={files}> {files.map((file) => ( <FileMosaic key={file.id} {...file} onDelete={removeFile} info/> ))}</Dropzone>
Validation
In this demo you can see how
<Dropzone/>
component covers the following features when it comes to validating files.- Accepting specific file types.
- Accepting an specific number of files.
- Accepting files with an specific size (in bytes).
Max file size: 28.00 KB, Files 0/2,
<Dropzone onChange={updateFiles} value={files} accept="image/*" maxFileSize={28 * 1024} maxFiles={2} //cleanFiles actionButtons={{ position: "bottom", cleanButton: {} }}> {files.map((file) => ( <FileMosaic key={file.id} {...file} onDelete={removeFile} info/> ))}</Dropzone>
Custom validation
You can also override the Dropzone validation operation by giving a custom validation function that must fit the following signature:
validator?: (f: File) => ValidateFileResponse
. Custom validator can work together with accept
, maxFileSize
and maxFiles
props.Max file size: 28.00 KB, Files 0/2,
Uploading
For uploading , the prop
uploadConfig
must be given. This prop recieves an object in which you can specify the HTTP method, the url, the headers and also extra data to be uploaded. The type definition for the prop mentioned can be found here.Max file size: 28.00 MB, Files 0/2,
<Dropzone onChange={updateFiles} value={files} accept={"image/*"} maxFileSize={28 * 1024*1024} maxFiles={2} actionButtons={{ position: "bottom", uploadButton: {}, abortButton: {} }} uploadConfig={{ url: "https://www.myawsomeserver.com/upload", method: "POST", headers: { Authorization: "bearer HTIBI/IBYG/&GU&/GV%&G/&IC%&V/Ibi76bfh8g67gg68g67i6g7G&58768&/(&/(FR&G/&H%&/", }, cleanOnUpload: true, }} onUploadFinish={handleFinishUpload} fakeUpload> {files.map((file) => ( <FileMosaic key={file.id} {...file} onDelete={removeFile} info preview/> ))}</Dropzone>
Dropzone header config
You can use the
headerConfig
prop to define what will be displayed in the header.- Dropzone with the headerConfig.customHeaderprop defined will display this prop replacing the entire default header.
true
.<Dropzone onChange={updateFiles} value={files} headerConfig={{ customHeader: ( <div style={{ display: "flex", justifyContent: "flex-end" }}> <button style={{ backgroundColor: "teal", color: "white" }} onClick={removeAllFiles} > delete files </button> </div> ), }}> {files.map((file) => ( <FileMosaic key={file.id} {...file} onDelete={removeFile} info/> ))}</Dropzone>
Styling Dropzone
You can use change the look and feel of the
Dropzone
component- Dropzone with the colorprop defined will use this color for border, drop layer, font color and ripple.
- Dropzone with the minHeightprop defined will use this value to define the minimum height of the component.
- Dropzone with the backgroundprop defined will use this value for the background. You can set nice gradients or even a background image.
<Dropzone color="#6200EE">{/** files */}</Dropzone><Dropzone minHeight="120px" header={false} footer={false}> {/** files */}</Dropzone><Dropzone background="radial-gradient(circle at 18.7% 37.8%, rgb(250, 250, 250) 0%, rgb(225, 234, 238) 90%);"> {/** files */}</Dropzone>
Label
You can specify a fixed label for
<Dropzone/>
component to display when there isn't any files.<Dropzone style={{ width: "300px" }}>{/**Files */}</Dropzone><Dropzone style={{ width: "300px" }} label={"Files ui ❤️"}> {/**Files */}</Dropzone>
Ripple
According to Material Design, the Ripple component provides a radial action in the form of a visual ripple expanding outward from the user's touch. Ripple is a visual form of feedback for touch events providing users a clear signal that an element is being touched. In this component, a ripple is displayed after 2 user actions:
- When user clicks or touches the component.
- Just after user dropped files.
disableRipple
prop to true.ripple enabled
ripple disabled
<Dropzone style={{ width: "300px" }}>{"ripple enabled"}</Dropzone><Dropzone style={{ width: "300px" }} disableRipple> {"ripple disabled"}</Dropzone>
Disabled Dropzone
According to Material Design a disabled state communicates when a component or element isn’t interactive, and should be deemphasized in a UI.
disabled
prop to true.Dropzone is disabled
<Dropzone style={{ width: "300px" }}>{/**Files */}</Dropzone><Dropzone style={{ width: "300px" }} disabled> {"Dropzone is disabled"} {/**Files */}</Dropzone>
Clickable Dropzone
Dropzone with the
clickable
prop set to false will not open the file dialog to select files when user clicks the component.<Dropzone style={{ width: "300px" }}>{/**Files */}</Dropzone><Dropzone style={{ width: "300px" }} clickable={false}> {/**Files */}</Dropzone>
Drop Layer
Dropzone with the
In this demo try to drag and drop files in both dropzones to see the difference.
dropOnLayer
prop set to false will not perform the drop operation in a layer that covers the complete component container.In this demo try to drag and drop files in both dropzones to see the difference.
<Dropzone style={{ width: "300px" }}>{/**Files */}</Dropzone><Dropzone style={{ width: "300px" }} dropOnLayer={false}> {/**Files */}</Dropzone>
Add or replace files
There are 2 different behaviours when user selects or drops new files:
- Dropzone with the
behaviour
set to"add"will add the new files to the current array of ExtFiles. - Dropzone with the
behaviour
set to"replace"will replace the current array of ExtFiles with the new ones.
<Dropzone style={{ width: "min(100% , 420px)" }} onChange={updateFilesAdd} value={filesAdd} label="Dropzone with behaviour=add" behaviour={"add"}> {filesAdd.length > 0 && filesAdd.map((file) => ( <FileMosaic key={file.id} {...file} onDelete={removeFileAdd} info preview /> ))}</Dropzone><Dropzone style={{ width: "min(100% , 420px)" }} onChange={updateFilesReplace} value={filesReplace} label="Dropzone with behaviour=replace" behaviour={"replace"}> {filesReplace.length > 0 && filesReplace.map((file) => ( <FileMosaic key={file.id} {...file} onDelete={removeFileReplace} info preview /> ))}</Dropzone>
Localization
<Dropzone/>
component uses the localization
prop in the validation and the upload process for setting status and messages according to the language that this prop refers to.The localization demo for this component can be found in the localization page.
API
See the documentation below for a complete reference to all of the props and classes available to the components mentioned here.