react-dropzone

  • Version 19.1.1
  • Published
  • 287 kB
  • 2 dependencies
  • MIT license

Install

npm i react-dropzone
yarn add react-dropzone
pnpm add react-dropzone

Overview

Simple HTML5 drag-drop zone with React.js

Index

Variables

variable Dropzone

const Dropzone: React.ForwardRefExoticComponent<any>;
  • Convenience wrapper component for the useDropzone hook

    <Dropzone>
    {({getRootProps, getInputProps}) => (
    <div {...getRootProps()}>
    <input {...getInputProps()} />
    <p>Drag 'n' drop some files here, or click to select files</p>
    </div>
    )}
    </Dropzone>

Functions

function useDropzone

useDropzone: (props?: DropzoneOptions) => DropzoneState;
  • A React hook that creates a drag 'n' drop area.

    function MyDropzone(props) {
    const {getRootProps, getInputProps} = useDropzone({
    onDrop: acceptedFiles => {
    // do something with the File objects, e.g. upload to some server
    }
    });
    return (
    <div {...getRootProps()}>
    <input {...getInputProps()} />
    <p>Drag and drop some files here, or click to select files</p>
    </div>
    )
    }

Interfaces

interface Accept

interface Accept {}
  • A map of accepted MIME types to file extensions, as passed to the accept prop.

index signature

[key: string]: readonly string[];

    interface DropzoneInputProps

    interface DropzoneInputProps extends React.InputHTMLAttributes<HTMLInputElement> {}

      property refKey

      refKey?: string;

        interface DropzoneProps

        interface DropzoneProps extends DropzoneOptions {}

          property children

          children?: (state: DropzoneState) => React.ReactElement;

            interface DropzoneRef

            interface DropzoneRef {}

              property open

              open: () => void;

                interface DropzoneRootProps

                interface DropzoneRootProps extends React.HTMLAttributes<HTMLElement> {}

                  property refKey

                  refKey?: string;

                    index signature

                    [key: string]: any;

                      interface FileError

                      interface FileError {}
                      • A file rejection error.

                      property code

                      code: ErrorCode | string;

                        property message

                        message: string;

                          interface FileRejection

                          interface FileRejection {}

                            property errors

                            errors: readonly FileError[];

                              property file

                              file: FileWithPath;

                                Enums

                                enum ErrorCode

                                enum ErrorCode {
                                FileInvalidType = 'file-invalid-type',
                                FileTooLarge = 'file-too-large',
                                FileTooSmall = 'file-too-small',
                                TooManyFiles = 'too-many-files',
                                }

                                  member FileInvalidType

                                  FileInvalidType = 'file-invalid-type'

                                    member FileTooLarge

                                    FileTooLarge = 'file-too-large'

                                      member FileTooSmall

                                      FileTooSmall = 'file-too-small'

                                        member TooManyFiles

                                        TooManyFiles = 'too-many-files'

                                          Type Aliases

                                          type DropEvent

                                          type DropEvent =
                                          | React.DragEvent<HTMLElement>
                                          | React.ChangeEvent<HTMLInputElement>
                                          | DragEvent
                                          | Event;

                                            type DropzoneOptions

                                            type DropzoneOptions = Pick<React.HTMLProps<HTMLElement>, SharedProps> & {
                                            accept?: Accept;
                                            minSize?: number;
                                            maxSize?: number;
                                            maxFiles?: number;
                                            preventDropOnDocument?: boolean;
                                            noClick?: boolean;
                                            noKeyboard?: boolean;
                                            noDrag?: boolean;
                                            noDragEventsBubbling?: boolean;
                                            disabled?: boolean;
                                            onDrop?: <T extends File>(
                                            acceptedFiles: T[],
                                            fileRejections: FileRejection[],
                                            event: DropEvent
                                            ) => void;
                                            onDropAccepted?: <T extends File>(files: T[], event: DropEvent) => void;
                                            onDropRejected?: (fileRejections: FileRejection[], event: DropEvent) => void;
                                            getFilesFromEvent?: (
                                            event: DropEvent | Array<FileSystemFileHandle>
                                            ) => Promise<Array<File | DataTransferItem>>;
                                            onFileDialogCancel?: () => void;
                                            onFileDialogOpen?: () => void;
                                            onError?: (err: Error) => void;
                                            /**
                                            * Custom validation, run once per file on drop/selection. Return `null` to accept the file, or a
                                            * {@link FileError} (or array of them) to reject it. May be `async` (return a `Promise`) to support
                                            * checks that can't run synchronously - e.g. reading image dimensions, inspecting file contents,
                                            * or calling an external service. While an async validator is pending, {@link DropzoneState.isProcessing}
                                            * is `true`, and `onDrop`/`onDropAccepted`/`onDropRejected` fire only once it settles. If the
                                            * validator throws or rejects, `onError` is called and the drop is discarded.
                                            *
                                            * Note: the validator never runs during a drag (a `DataTransferItem` has no name/size), so a
                                            * validator-configured dropzone is `isDragUnknown` until drop.
                                            */
                                            validator?: <T extends File>(
                                            file: T
                                            ) => ValidatorResult | Promise<ValidatorResult>;
                                            /**
                                            * Override the message of any rejection error (built-in or custom). Called once per error;
                                            * receives the error and the file it belongs to and returns the message to use. Return
                                            * `error.message` for codes you don't want to change. Useful for localizing error messages.
                                            */
                                            getErrorMessage?: (error: FileError, file: File) => string;
                                            useFsAccessApi?: boolean;
                                            autoFocus?: boolean;
                                            };

                                              type DropzoneState

                                              type DropzoneState = DropzoneRef & {
                                              isFocused: boolean;
                                              isDragActive: boolean;
                                              isDragAccept: boolean;
                                              isDragReject: boolean;
                                              isDragUnknown: boolean;
                                              isDragGlobal: boolean;
                                              isFileDialogActive: boolean;
                                              /**
                                              * `true` while a drop/selection is being processed asynchronously - i.e. while `getFilesFromEvent`
                                              * reads the files and/or an async {@link DropzoneOptions.validator} runs. Spans the whole pipeline,
                                              * from when files start being read until validation settles. When both are synchronous (the default
                                              * `getFilesFromEvent` with no/async-free validator) the work resolves within a microtask, so it's
                                              * only observable for genuinely async work. Use it to show a spinner or disable UI while processing.
                                              */
                                              isProcessing: boolean;
                                              acceptedFiles: readonly FileWithPath[];
                                              fileRejections: readonly FileRejection[];
                                              rootRef: React.RefObject<HTMLElement>;
                                              inputRef: React.RefObject<HTMLInputElement>;
                                              getRootProps: <T extends DropzoneRootProps>(props?: T) => T;
                                              getInputProps: <T extends DropzoneInputProps>(props?: T) => T;
                                              };

                                                type ValidatorResult

                                                type ValidatorResult = FileError | readonly FileError[] | null;
                                                • What a custom validator returns: a single error, a list of errors, or null when the file passes. A validator may return the result directly (synchronous) or wrapped in a Promise (asynchronous, e.g. reading image dimensions or calling an external service).

                                                Package Files (1)

                                                Dependencies (2)

                                                Dev Dependencies (25)

                                                Peer Dependencies (2)

                                                Badge

                                                To add a badge like this onejsDocs.io badgeto your package's README, use the codes available below.

                                                You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/react-dropzone.

                                                • Markdown
                                                  [![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/react-dropzone)
                                                • HTML
                                                  <a href="https://www.jsdocs.io/package/react-dropzone"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>