React file input component for complete control over styling and abstraction from file reading.

Overview

react-file-reader-input

React file input component for complete control over styling and abstraction from file reading.

<FileReaderInput as={dataFormat} onChange={handler} {...props}/>

  • as (string): what format the FileReader should read the file as (i.e., buffer, binary, url, text). Defaults to url.
  • children (element): if children is passed into FileReaderInput, then the component will hide the native file input and instead display children. Whenever the custom children are clicked, the component will trigger the native file input prompt. This allows complete control over styling an display.
  • onChange (function): callback function(event, results). Results will be an array of arrays, the size of which depending on how many files were selected. Each result will be an array of two items:
    • progressEvent: result[0] is a ProgressEvent object. You can retrieve the raw results at progressEvent.target.result among other things.
    • file: result[1] is a File object. You can retrieve the file name at file.name among other things.

All other props on FileReaderInput will be passed down to the native file input.

Usage

import React from 'react';
import FileReaderInput from 'react-file-reader-input';


class MyComponent extends React.Component {
  handleChange = (e, results) => {
    results.forEach(result => {
      const [e, file] = result;
      this.props.dispatch(uploadFile(e.target.result));
      console.log(`Successfully uploaded ${file.name}!`);
    });
  }
  render() {
    return (
      <form>
        <label htmlFor="my-file-input">Upload a File:</label>
        <FileReaderInput as="binary" id="my-file-input"
                         onChange={this.handleChange}>
          <button>Select a file!</button>
        </FileReaderInput>
      </form>
    );
  }
}
Comments
  • Choosing the same file doesn't trigger onChange

    Choosing the same file doesn't trigger onChange

    This is only from initial testing, but wondering if anyone else can reproduce. Here is my scenario using similar markup as the readme:

    1. Click "Select a file!" button and select house.jpg. This triggers onChange, everything is fine.
    2. Click "Select a file!" button again and select house.jpg again. This does not trigger onChange. I would expect re-rendering would solve this issue but it doesn't.

    This is a really weird issue so again I'm just wondering if this is an actual problem with the component itself.

    opened by sunnysingh 14
  • Changed children property from empty string to undefined

    Changed children property from empty string to undefined

    Currently library returns Warning: input is a void element tag and must not have children or use props.dangerouslySetInnerHTML. Check the render method of FileInput. when using React 0.14 and specifying own children.

    opened by JSila 3
  • Modernize the tooling

    Modernize the tooling

    ⚠️ Based on https://github.com/ngokevin/react-file-reader-input/pull/18, please review/merge it before.

    In this commit:

    • upgrade to Babel 6 (replacing "--stage 0" by the proper "transform" plugins).
    • upgrade to Karma 2
    • upgrade to Mocha 5

    The resulting build file (lib/index.js) is very similar to the previous one.

    opened by maximerety 2
  • Not compatible with react 16

    Not compatible with react 16

    Hey, first of all thanks for the project. I got the following issue after I upgrade React to v16.0.0. npm WARN react-file-reader-inpu[email protected] requires a peer of react@^15.0.0 but none was installed. Any update regarding a new version that supports React 16?

    opened by dingdingbai 2
  • Warning: Unknown prop 'as'

    Warning: Unknown prop 'as'

    Here is the usage:

    <FileReaderInput as="binary" id="my-file-input" accept=".png,.gif,.jpg,.jpeg" onChange={this.onImageChanged}>

    Here is the warning:

    warning.js?8a56:44Warning: Unknown prop `as` on <input> tag. Remove this prop from the element. For details, see https://fb.me/react-unknown-prop
        in input (created by FileInput)
        in div (created by FileInput)
        in FileInput (created by HomePage)```
    
    opened by vitalybe 2
  • Get error message when init

    Get error message when init

    Hi,

    I got the error message when init.

    Uncaught Error: Invariant Violation: addComponentAsRefTo(...): Only a ReactOwner can have refs. You might be adding a ref to a component that was not created inside a component's render method, or you have multiple copies of React loaded (details: https://fb.me/react-refs-must-have-owner).

    opened by cipherchien 2
  • Fixed ref invariant error

    Fixed ref invariant error

    This fixed #2 for me. I also added a check for window to be typeof 'object' in order to support server rendering. Hopefully this can be of help, thanks for an awesome component!

    opened by nickell 1
  • Is it correct tag name?

    Is it correct tag name?

    I'm not sure that the tag name is correct in usage.

    ...... <**FileReaderInput** as="binary" id="my-file-input" onChange={this.handleChange}> <button>Select a file!</button> </FileReaderInput>

    FileReaderInput should be FileInput, right?

    opened by cipherchien 1
  • Add flow type system and remove prop-types dependency

    Add flow type system and remove prop-types dependency

    No rocket science here, but switching to flow allows to remove the prop-types dependency which is a small win.

    This is a proposal, feedback by anyone interested (or not) is welcome!

    opened by maximerety 0
  • React 16

    React 16

    Fixing: https://github.com/ngokevin/react-file-reader-input/issues/17

    React 16 is the version used in dev now, and the preferred version at runtime, although React 15 and React 16 are both accepted at runtime (see peerDependencies)..

    I also removed a dependency on "karma" which was already present (and better placed) in the devDependencies.

    opened by maximerety 0
  • removed reference to React.PropTypes (deprecated). added dependency for 'prop-types' and updated all prop-type references.

    removed reference to React.PropTypes (deprecated). added dependency for 'prop-types' and updated all prop-type references.

    As of react 15.5.0, referencing React.PropTypes is now deprecated in favor of the prop-types library. The src has been updated to remove deprecation warnings.

    Thanks in advance, Gavin.

    opened by gavingregory 0
  • Is this file input compatible with IE11?

    Is this file input compatible with IE11?

    Hi, I'm trying to use this component with IE11, but I'm getting this error while file is uploaded: It works on Chrome.

    image

    EDIT: Here they say that FileReader.readAsBinaryString has been deprecated. https://stackoverflow.com/a/47049347/1210819

    opened by kkorus 1
  • [Enhancement] add an option to pass a custom css class

    [Enhancement] add an option to pass a custom css class

    Hi! i'd like to be able to customize the css class of the rendered component.

    a simple prop to add a className would let the users control the styling of each File reader input separately.

    opened by Javier-Rotelli 0
Owner
Kevin Ngo
Building @learncoupling to help couples learn each other's languages. Prev @aframevr @supermedium (YC W18)
Kevin Ngo
We have updated our application over to using hooks and functional components to replace any class components we had before.

Crwn hooks We have updated our application over to using hooks and functional components to replace any class components we had before. How to fork an

Shazil Sattar 2 Feb 10, 2022
Auth model created by Using nodeJs for backend & reactJs for frontend with the help of TailwindCss in styling

The Universal Auth System Using The MERN Stack Including Mysql --> The project is divded to two separte projects 1- The Client side -> containing the

m.bebars 1 Aug 22, 2022
Code examples for the blog post titled The Complete Guide to Full Stack Solana Development with React, Anchor, Rust, and Phantom

The Complete Guide to Full Stack Solana Development with React, Anchor, Rust, and Phantom Code examples to go with the blog post available here Prereq

Nader Dabit 436 Dec 31, 2022
A complete application from scratch using Node.js, React, and Tailwind

A complete application from scratch using Node.js, React, and Tailwind

Fernando Bade 1 May 6, 2022
Complete token authentication system for react + redux that supports isomorphic rendering.

Simple, secure authentication for react + redux TL;DR - View the Live Demo You can see a complete working example here. The code for the demo is here.

Lynn Dylan Hurley 2.1k Dec 27, 2022
The Complete Intro to React, as taught by Brian Holt on Frontend Masters

Please click here to head to the course website. Issues and Pull Requests Please file issues and open pull requests here! Thank you! For issues with p

Brian Holt 307 Jan 2, 2023
Minimum complete reproducible example of the `defaultSource` prop issue

There's an issue when adding a defaultSource prop to the Image component from the react-native library on iOS causes an error. Replication steps Insta

Jon Samp 0 Mar 1, 2022
React Three Fiber FPS control for mobile and desktop

React Three Fiber FPS control for mobile and desktop

Antony Budianto 21 Jul 13, 2022
Custom layers control for react-leaflet with typescript

Custom layers control for react-leaflet with typescript. https://github.com/PaulLeCam/react-leaflet/issues/706 Credits https://github.com/PaulLeCam fo

Robert 5 Feb 15, 2022
A Currency Converter Expense Control Portfolio With React

TRYBE WALLET Habilidades Neste projeto, verificamos se voce é capaz de: Criar um store Redux em aplicações React Criar reducers no Redux em aplicações

Luana Moneró 1 Dec 24, 2021
A demo project on how to control smart lights Philips Hue with Web Bluetooth

Basic smart light controller A demo project on how to control smart lights (Philips Hue) with Web Bluetooth. Created with React, TypeScript and Chakra

Sivert Schou Olsen 5 Dec 4, 2022
rfxTree: a small prototype/weekend project to control addressable RGB christmas tree lights

rfxTree is a small prototype/weekend project to control addressable RGB Christma

null 1 Mar 31, 2022
React component to format numbers in an input or as a text.

react-number-format React component to format number in an input or as a text Features Prefix, suffix and thousand separator. Custom format pattern. M

Sudhanshu Yadav 3.4k Jan 6, 2023
React japanese auto input kana

React Auto Kana Japanese React Kana Input Component Demo Live Demo Installation npm install -S react-auto-kana Usage Example var React = require("rea

S. Suzuki 4 Feb 17, 2021
Keyborg is a library that tracks the state of current keyboard input on a web page through focus events.

Keyborg ⌨️ ?? Keyborg is a library that tracks the state of current keyboard input on a web page through focus events. It does not do anything invasiv

Microsoft 8 Nov 1, 2022
📫 Graceful upload file(s) solution.

TSDX User Guide Congrats! You just saved yourself hours of work by bootstrapping this project with TSDX. Let’s get you oriented with what’s here and h

tuanzi 3 Dec 9, 2021
Koma Human 2 Jun 25, 2022
🖱 A resizable and draggable component for React.

A resizable and draggable component for React. Table of Contents Screenshot Live Demo Storybook CodeSandbox Install Usage Props Instance API updateSiz

bokuweb 3.1k Jan 2, 2023