0.1.0 • Published 7 years ago

react-enable-select-file v0.1.0

Weekly downloads
3
License
MIT
Repository
github
Last release
7 years ago

react-enable-select-file

A Higher-Order Component that Enable Normal Component to Select File.

Requirment

  • ES6/7

Install

npm install -S react-enable-select-file

Usage

import React from 'react';

import enableSelectFile from 'react-enable-select-file';

const Button = ({ children }) => (
  <button>{children}</button>
);
const Div = ({ children }) => (
  <div>{children}</div>
);

const SelectFileButton = enableSelectFile(Button);
const SelectFileDiv = enableSelectFile(Div);

class Demo extends React.Component {
  state = {
    file: null,
  }

  selectFile = (file) => this.setState({ file })

  click = event => console.log(event)

  render() {
    const { file } = this.state;
    return (
      <div>
        <SelectFileButton
          onSelect={this.selectFile}
          onClick={this.click}
        >Click to Select File</SelectFileButton>
        <SelectFileDiv
          onSelect={this.selectFile}
          onClick={this.click}
        >Click to Select File</SelectFileDiv>
        <div>{file ? file.name : 'null'}</div>
      </div>
    );
  }
}

export default Demo;

Properties

NameTypeDefaultDescription
acceptStringIt is same with \<input>'s accept that make you can limit the file type.
multipleBooleanfalseIt is same with \<input>'s multiple that make you can select more than one file.
onSelectFunctionn( multiple ? File : File )() => {}After selecting files, onSelect will be triggered.
Other properties are applied to the origin element.

Other

react-enable-select-file open file selector trigger by onClick, if you apply onClick to the origin element, onClick will be triggered after onSelect.

0.1.0

7 years ago