GithubHelp home page GithubHelp logo

mike1808 / react-dropzone Goto Github PK

View Code? Open in Web Editor NEW

This project forked from react-dropzone/react-dropzone

0.0 2.0 0.0 2.04 MB

Simple HTML5 drag-drop zone with React.js.

Home Page: https://react-dropzone.js.org/

License: MIT License

JavaScript 87.78% TypeScript 12.22%

react-dropzone's Introduction

react-dropzone logo

react-dropzone

npm Build Status codecov OpenCollective OpenCollective

Simple HTML5-compliant drag'n'drop zone for files built with React.js.

Documentation and examples: https://react-dropzone.js.org Source code: https://github.com/react-dropzone/react-dropzone/


Looking for maintainers: react-dropzone#479


Installation

Install it from npm and include it in your React build process (using Webpack, Browserify, etc).

npm install --save react-dropzone

or:

yarn add react-dropzone

Usage

Import Dropzone in your React component:

import Dropzone from 'react-dropzone'

and specify the onDrop method that accepts two arguments. The first argument represents the accepted files and the second argument the rejected files.

function onDrop(acceptedFiles, rejectedFiles) {
  // do stuff with files...
}

Files accepted or rejected based on accept prop. This must be a valid MIME type according to input element specification.

Please note that onDrop method will always be called regardless if dropped file was accepted or rejected. The onDropAccepted method will be called if all dropped files were accepted and the onDropRejected method will be called if any of the dropped files was rejected.

Using react-dropzone is similar to using a file form field, but instead of getting the files property from the field, you listen to the onDrop callback to handle the files. Simple explanation here: http://abandon.ie/notebook/simple-file-uploads-using-jquery-ajax

Specifying the onDrop method, provides you with an array of Files which you can then send to a server. For example, with SuperAgent as a http/ajax library:

onDrop: acceptedFiles => {
    const req = request.post('/upload');
    acceptedFiles.forEach(file => {
        req.attach(file.name, file);
    });
    req.end(callback);
}

Warning: On most recent browsers versions, the files given by onDrop won't have properties path or fullPath, see this SO question and this issue. If you want to access file content you have to use the FileReader API.

onDrop: acceptedFiles => {
    acceptedFiles.forEach(file => {
        const reader = new FileReader();
        reader.onload = () => {
            const fileAsBinaryString = reader.result;
            // do whatever you want with the file content
        };
        reader.onabort = () => console.log('file reading was aborted');
        reader.onerror = () => console.log('file reading has failed');

        reader.readAsBinaryString(file);
    });
}

PropTypes

See https://react-dropzone.netlify.com/#proptypes

Testing

Important: react-dropzone makes its drag'n'drop callbacks asynchronous to enable promise based getDataTransfer functions. In order to properly test this, you may want to utilize a helper function to run all promises like this:

const flushPromises = () => new Promise(resolve => setImmediate(resolve));

Example with enzyme 3:

it('tests drag state', async () => {
  const flushPromises = () => new Promise(resolve => setImmediate(resolve));
  const DummyChildComponent = () => null
  const dropzone = mount(
    <Dropzone>{props => <DummyChildComponent {...props} />}</Dropzone>
  )
  dropzone.simulate('dragEnter', {
    dataTransfer: { files: files.concat(images) }
  })
  await flushPromises(dropzone)
  dropzone.update()

  const child = dropzone.find(DummyChildComponent)
  expect(child).toHaveProp('isDragActive', true)
  expect(child).toHaveProp('isDragAccept', false)
  expect(child).toHaveProp('isDragReject', true)
})

Remember to update your mounted component before asserting any props. A complete example for this can be found in react-dropzones own test suite.

Support

Backers

Support us with a monthly donation and help us continue our activities. [Become a backer]

Sponsors

Become a sponsor and get your logo on our README on Github with a link to your site. [Become a sponsor]

License

MIT

react-dropzone's People

Contributors

akkie avatar anuragces avatar appleboy avatar chrisbuttery avatar dimitarchristoff avatar donavon avatar frankwallis avatar glennreyes avatar greenkeeper[bot] avatar idolize avatar indraneel avatar jonathanhudak avatar maffoobristol avatar mwolson avatar nfantone avatar nuc avatar okonet avatar paramaggarwal avatar robmclarty avatar rolandjitsu avatar rxmarbles avatar saravieira avatar subtirelumihail avatar tikotzky avatar tmarshall avatar tobilen avatar toshipon avatar vlindhol avatar voronianski avatar wmartins avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.