@brainhubeu/react-file-input
⚛️  A file Input, width drag'n'drop and image editor for React
Last updated 9 days ago by brainhubeu-ci .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install @brainhubeu/react-file-input 
SYNC missed versions from official npm registry.

react-file-input

CircleCI Last commit license PRs Welcome Renovate enabled

Coveralls github Downloads Activity npm Contributors

A File Input, width drag'n'drop and image editor.

Why?

An image is worth thousand words

Screenshot

Installation

npm i @brainhubeu/react-file-input

Or if you prefer

yarn add @brainhubeu/react-file-input

Usage

The basic usage is very simple, the only important prop you have to pass is onChangeCallback:

import React from 'react';
import FileInput from '@brainhubeu/react-file-input';
import doSomethingWithMyFile from '../utils/doThings';

const MyFileUploader = () => (
  <div>
    <FileInput
      label='Awesome Uploader'
      onChangeCallback={doSomethingWithMyFile}
    />
  </div>
);

export const MyFileUploader;

And your set and ready to do something with your file.

See the reference below for more advanced usages.

Usage with Redux Form

Probably you are not crazy enough to handle your forms like in 1999. Chances are that you are using redux-form. If so, it's your lucky day, because you can use our FileInput with redux-forms. Here's a basic example:

// MyFileInput.js
import React, { PureComponent } from 'react';
import FileInput from '@brainhubeu/react-file-input';

export default class MyFileInput extends PureComponent {
  constructor(props) {
    super(props);

    this.onChange = this.onChange.bind(this)
  }
  onChange({ value }) {
    const { input } = this.props;

    input.onChange(value);
  }
  render() {
    const { input, label } = this.props;

    return (
      <FileInput
        label='Awesome Uploader'
        onChangeCallback={this.onChange}
        onDragEnterCallback={input.onFocus}
        onDragLeaveCallback={input.onBlur}
      />
    );
  }
}
// MyGreatForm.js
import MyFileInput from './MyFileInput'

...

<Field name="myField" component={MyFileInput}/>

And that's it. Prepare some coffee and enjoy.

Styles

The Component comes with custom css. You must import them using our component (if not things will look wrong). Of course you could also override them ;)

// index.js
import React from 'react';
import { render } from 'react-dom';

import App from './App';

import '@brainhubeu/react-file-input/dist/react-file-input.css';

render(
  <App />,
  document.getElementById('app')
);

Or if you prefer from your stylesheet directly:

@import "~@brainhubeu/react-file-input/dist/react-file-input.css";

// very good css here...

Customize styles

You can customize the styles of the different components passing classnames as a prop:

propName description
className Custom className
dropAreaClassName Custom className for the DropArea
fileInfoClassName Custom className for the FileInfo
imageEditorClassName Custom className for the ImageEditor

Those classnames are for the top main components. But if you want to override child styles you can writing some css. Here's how you can do so:

.myCustomClassName {
  .brainhub-file-input__label {
    font-size: 10px;
    color: #ccc;
  }
}

Take a look on the styles and selectors here: https://github.com/brainhubeu/react-file-input/tree/master/src/styles

Reference

FileInput

State

The internal state of the FileInput is important to you, because the callbacks will be call with it as argument.

name type description
value File or null Selected file
image string or null If file is an image, the image in base64

Props

propName type required default description
className string no '' Custom className
dropAreaClassName string no '' Custom className for the DropArea
fileInfoClassName string no '' Custom className for the FileInfo
imageEditorClassName string no '' Custom className for the ImageEditor
dragOnDocument boolean no true Listen for drag events in the whole document
dropOnDocument boolean no false Allow to drop on document
label string yes Label for the input
metadataComponent React Component no null Custom component for the metadata. Props: name, size, extension, type
thumbnailComponent React Component no null Custom component for the image thumbnail. Props: children (<img> node with the thumbnail)
displayImageThumbnail boolean no true Whether to generate a thumbnail for image files
cropAspectRatio number no 0 If cropTool is enabled, the aspect ratio for the selection. 0 means the selection is free
cropTool boolean no false Wheter to render a crop tool for image files
scaleOptions {width: number, height: number: keepAspectRatio: boolean} no null Scale option for file images. keepAspectRatio refers to if the original aspect ratio should be kept when appliyin scaling
onChangeCallback function no null Callback invoked when a file is selected. It is called with the current state of the component
onDragEnterCallback function no null Callback invoked when drag enters. It is called with the current state of the component
onDragLeaveCallback function no null Callback invoked when drag leaves. It is called with the current state of the component

Example

To run the example, type:

cd docs-www

and

npm run develop
// or
yarn develop

if you want to run a local version of react-file-input, please use:

npm run develop:local
// or
yarn develop:local

And go to http://localhost:8000

License

react-file-input is copyright © 2018-2020 Brainhub It is free software, and may be redistributed under the terms specified in the license.

About

react-file-input is maintained by the Brainhub development team. It is funded by Brainhub and the names and logos for Brainhub are trademarks of Brainhub Sp. z o.o.. You can check other open-source projects supported/developed by our teammates here.

Brainhub

We love open-source JavaScript software! See our other projects or hire us to build your next web, desktop and mobile application with JavaScript.

Current Tags

  • 0.8.1                                ...           latest (9 days ago)

34 Versions

  • 0.8.1                                ...           9 days ago
  • 0.8.0                                ...           13 days ago
  • 0.7.23                                ...           13 days ago
  • 0.7.22                                ...           14 days ago
  • 0.7.21                                ...           14 days ago
  • 0.7.20                                ...           a month ago
  • 0.7.19                                ...           a month ago
  • 0.7.18                                ...           a month ago
  • 0.7.17                                ...           a month ago
  • 0.7.16                                ...           3 months ago
  • 0.7.15                                ...           3 months ago
  • 0.7.14                                ...           4 months ago
  • 0.7.13                                ...           4 months ago
  • 0.7.12                                ...           4 months ago
  • 0.7.11                                ...           4 months ago
  • 0.7.10                                ...           4 months ago
  • 0.7.9                                ...           4 months ago
  • 0.7.8                                ...           4 months ago
  • 0.7.6                                ...           4 months ago
  • 0.7.5                                ...           4 months ago
  • 0.7.4                                ...           8 months ago
  • 0.7.3                                ...           10 months ago
  • 0.7.2                                ...           a year ago
  • 0.7.1                                ...           2 years ago
  • 0.7.0                                ...           2 years ago
  • 0.6.4                                ...           2 years ago
  • 0.6.3                                ...           2 years ago
  • 0.6.2                                ...           2 years ago
  • 0.6.0                                ...           2 years ago
  • 0.4.0                                ...           2 years ago
  • 0.3.0                                ...           2 years ago
  • 0.2.0                                ...           2 years ago
  • 0.1.3                                ...           2 years ago
  • 0.1.2                                ...           2 years ago

Copyright 2014 - 2016 © taobao.org |