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

react-butterfiles

v1.3.3

Published

A component for building file upload fields of any type.

Downloads

8,343

Readme

🦋 react-butterfiles

Build Status Coverage Status code style: prettier All Contributors PRs Welcome

A small component for building file upload fields of any type, for example a simple file upload button or an image gallery field with drag and drop support and preview of selected images.

Install

npm install --save react-butterfiles

Or if you prefer yarn:

yarn add react-butterfiles

Quick Example:

Create a simple file field which consists of a drop zone and a file upload button (for cases where drag and drop is not convenient). Multiple PDF / JPG files are accepted, but with the following restrictions:

  • 3 files max
  • max 2MB in size per file
  • max 10MB in size for the whole selection
import Files from "react-butterfiles";
<Files
    multiple={true} 
    maxSize="2mb"
    multipleMaxSize="10mb"
    multipleMaxCount={3}
    accept={["application/pdf","image/jpg","image/jpeg"]}
    onSuccess={files => this.setState({ files })}
    onError={errors => this.setState({ errors })}
>
    {({ browseFiles, getDropZoneProps, getLabelProps }) => (
        <>
            <label {...getLabelProps()}>Your files</label>
            <div {...getDropZoneProps({ className: "myDropZone" })}/>
            <button onClick={browseFiles}>Select files...</button>
            <ol>
                {this.state.files.map(file => (
                    <li key={file.name}>{file.name}</li>
                ))}
                {this.state.errors.map(error => (
                    <li key={error.file.name}>
                        {error.file.name} - {error.type}
                    </li>
                ))}
            </ol>
        </>
    )}
</Files>

More examples on https://react-butterfiles.netlify.com.

Props

| Prop | Type | Default | Description | | :------------------ | :-------------------------------------- | :-------- | :-------------------------------------------------------------------------------------------------------------------- | | accept | Array<string> | [] | Defines which file types will be accepted. Example: ["application/pdf"]. | | multiple | boolean | false | Allow multiple file selection by setting this prop to true. | | maxSize |string |"2mb" | Defines maximum file size (bytes lib used behind the scenes). Example:"10mb" | |multipleMaxSize |string |"10mb" | Useful only ifmultipleprop is set totrue. Defines max. file size of all selected files. | | multipleMaxCount |number |null | Useful only ifmultipleprop is set totrue. Defines max. allowed selected files. | | convertToBase64 |boolean |false | Iftrue, selected files will also be converted to baser64 format (useful when working with images / thumbnails). | | onSuccess |(files: Array) => void|undefined| Callback that will get executed once a valid file selection has been made (via browse files dialog or drag and drop). Each file will have a randomidassigned to it. | |onError |(errors: Array) => void |undefined| Callback that will get executed once an invalid file selection has been made. Each error will have a randomid` assigned to it. More info about possible errors below. |

Render (children) prop

Render prop gives you access to three callbacks:

| Prop | Type | Description | | :------------------ | :-------------------------------------- | :---------------------------------- | browseFiles | BrowseFilesParams => void | Once executed, file browser will be shown. Useful for file upload buttons. The callback can also accept custom onSuccess and onError callbacks, that will override the main ones. | | validate | (files: Array<File>) => Array<FileError> | Enables manual validation of files. Eg. after editing the selected image in an image editor. | | getDropZoneProps | (additionalProps: ?Object) => Object | Props that need to be spread to a drop zone. Additional props can be passed, for example className or style. | | getLabelProps | (additionalProps: ?Object) => Object | Props that need to be spread to your file field's label. Additional props can be passed, for example className or style. |

Note that you don't need to have both upload file and drop zone, you can use only one if that's your requirement. For example, to create a simple file field, you would only need to use the browseFiles callback.

Selection error types

While selecting and dropping files, if there are one or more that do not comply with the rules that were set via props, an onError callback will be triggered, with all of the errors passed as the first argument.

Every error in the array will have one of the following error types.

| Type | Description | :------------------ | :------------------- | unsupportedFileType | This can only happen via drag and drop since file browser dialog won't let users choose files of invalid type. | maxSizeExceeded | One or more file sizes are greater than maxSize value. | multipleMaxCountExceeded | User selected more files than allowed (more than multipleMaxCount). | multipleMaxSizeExceeded | User selected one or more files with their total size greater than allowed (more than multipleMaxSize). | multipleNotAllowed | This can only happen via drag and drop since file browser dialog won't let users select two or more files if multiple was not set to true.

Contributors

Thanks goes to these wonderful people (emoji key):

| Adrian Smijulj💻 📖 💡 👀 ⚠️ | readeral💻 📖 | | :---: | :---: |

This project follows the all-contributors specification. Contributions of any kind welcome!