npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

rnw-dropzone

v0.0.0-development

Published

Forked from react-dropzone to support react-native-web

Downloads

3

Readme

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/

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 React from 'react'
import classNames from 'classnames'
import Dropzone from 'react-dropzone'

class MyDropzone extends React.Component {
   onDrop = (acceptedFiles, rejectedFiles) => {
     // Do something with files
   }

   render() {
    return (
      <Dropzone onDrop={this.onDrop}>
        {({getRootProps, getInputProps, isDragActive}) => {
          return (
            <div
              {...getRootProps()}
              className={classNames('dropzone', {'dropzone--isActive': isDragActive})}
            >
              <input {...getInputProps()} />
              {
                isDragActive ?
                  <p>Drop files here...</p> :
                  <p>Try dropping some files here, or click to select files to upload.</p>
              }
            </div>
          )
        }}
      </Dropzone>
    );
  }
}

Render Prop Function

The render property function is what you use to render whatever you want to based on the state of Dropzone:

<Dropzone>
  {({getRootProps}) => <div {...getRootProps()} />}
</Dropzone>

Prop Getters

See https://react-dropzone.netlify.com/#proptypes {children} for more info.

These functions are used to apply props to the elements that you render.

This gives you maximum flexibility to render what, when, and wherever you like. You call these on the element in question (for example: <div {...getRootProps()} />).

You should pass all your props to that function rather than applying them on the element yourself to avoid your props being overridden (or overriding the props returned). E.g.

<div
  {...getRootProps({
    onClick: evt => console.log(event)
  })}
/>

State

See https://react-dropzone.netlify.com/#proptypes {children} for more info.

Custom refKey

Both getRootProps and getInputProps accept custom refKey (defaulted to ref) as one of the attributes passed down in the parameter.

const StyledDropArea = styled.div`
// Some styling here
`
const Example = () => (
  <Dropzone>
   {({ getRootProps, getInputProps }) => (
      <StyledDropArea {...getRootProps({ refKey: 'innerRef' })}>
        <input {...getInputProps()} />
        <p>Drop some files here</p>
      </StyledDropArea>
    )}
  </Dropzone>
);

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