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

@vinimiranda/react-file-input

v1.0.0

Published

Simple unstyled file input for React

Downloads

2

Readme

Simple unstyled file input for React.

Installation yarn add @vinimiranda/react-input-file


Props

accepts string | string[]

The accept property for the HTML input. Can be any of the pre-defined types or a custom string/array.

default: undefined (all file types)

isMultiple boolean

Whether or not to allow multiple file entries.

default: false

onUpload function

The callback function that will be executed after the files were uploaded. It receives the files as the first parameter and helpers functions as the second.

default: undefined

parseBase64 boolean

Whether or not to parse the uploaded files to base64. It happens asynchronously before the onUpload callback using the getFileBase64 function, which is also exported by the lib if you wish to use it.

default: false

inputProps HTMLProps<HTMLInput>

The HTML input props. These will not overwrite any of the default input settings used by this lib.

default: undefined

children function | ReactNode

The children will be rendered after the HTML input. It can be a simple ReactNode, or a custom function, which receives the uploaded files and the helpers such as the examples bellow.

default: undefined

Examples

Here are some really simple usage examples, but I bet you can get more creative then this.

Basic

A really basic usage that will probably attend most of the cases.

import { FileInput } from '@magrathea42/react-file-input'

export default function App() {
  return (
    <FileInput onUpload={(files) => console.log(files)}>
      {({ requestFiles }) => (
        <button type='button' onClick={requestFiles}>
          Upload files
        </button>
      )}
    </FileInput>
  )
}

Children function

More of the data provided for the children function.

import { FileInput } from '@magrathea42/react-file-input'

export default function App() {
  return (
    <FileInput isMultiple>
      {({ files, requestFiles, clearFiles }) => (
        <>
          <button type='button' onClick={requestFiles}>
            Upload files
          </button>

          {files.length > 0 && (
            <>
              <button type='button' onClick={clearFiles}>
                Remove files
              </button>

              <div>
                Your files:
                <ul>
                  {files.map((file) => (
                    <li key={file.id}>{file.data.name}</li>
                  ))}
                </ul>
              </div>
            </>
          )}
        </>
      )}
    </FileInput>
  )
}

Previewing

Simple image preview.

import { FileInput } from '@magrathea42/react-file-input'

export default function App() {
  return (
    <FileInput accepts={['.jpg', '.jpeg', '.png']} parseBase64>
      {({ files, requestFiles }) => {
        const imageBase64 = files[0]?.base64 as string

        return (
          <div>
            <button type='button' onClick={requestFiles}>
              Upload profile picture
            </button>

            <br />

            {imageBase64 && <img width='400' src={imageBase64} alt='' />}
          </div>
        )
      }}
    </FileInput>
  )
}

Hooks

Using with React hooks.

import { File, FileInput, FileInputRef } from '@magrathea42/react-file-input'
import { useEffect, useRef, useState } from 'react'

export default function App() {
  const fileInputRef = useRef<FileInputRef>(null)

  const [files, setFiles] = useState<File[]>([])

  useEffect(() => {
    console.log(files)
  }, [files])

  return (
    <div>
      <FileInput
        ref={fileInputRef}
        accepts='image/*'
        isMultiple
        onUpload={(files, helpers) => {
          setFiles((prev) => [...prev, ...files])
          helpers.clearFiles()
        }}
      />

      <button type='button' onClick={() => fileInputRef.current?.requestFiles()}>
        Upload videos
      </button>
    </div>
  )
}

Any suggestions and tips will always be welcome! Vini Miranda