0.9.20 • Published 4 years ago

@brainhubeu/react-file-input v0.9.20

Weekly downloads
155
License
MIT
Repository
github
Last release
4 years ago

CircleCI Last commit license PRs Welcome Renovate enabled

Coveralls github Downloads Activity npm Contributors

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:

propNamedescription
classNameCustom className
dropAreaClassNameCustom className for the DropArea
fileInfoClassNameCustom className for the FileInfo
imageEditorClassNameCustom 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.

nametypedescription
valueFile or nullSelected file
imagestring or nullIf file is an image, the image in base64

Props

propNametyperequireddefaultdescription
classNamestringno''Custom className
dropAreaClassNamestringno''Custom className for the DropArea
fileInfoClassNamestringno''Custom className for the FileInfo
imageEditorClassNamestringno''Custom className for the ImageEditor
dragOnDocumentbooleannotrueListen for drag events in the whole document
dropOnDocumentbooleannofalseAllow to drop on document
labelstringyesLabel for the input
metadataComponentReact ComponentnonullCustom component for the metadata. Props: name, size, extension, type
thumbnailComponentReact ComponentnonullCustom component for the image thumbnail. Props: children (<img> node with the thumbnail)
displayImageThumbnailbooleannotrueWhether to generate a thumbnail for image files
cropAspectRationumberno0If cropTool is enabled, the aspect ratio for the selection. 0 means the selection is free
cropToolbooleannofalseWheter to render a crop tool for image files
scaleOptions{width: number, height: number: keepAspectRatio: boolean}nonullScale option for file images. keepAspectRatio refers to if the original aspect ratio should be kept when appliyin scaling
onChangeCallbackfunctionnonullCallback invoked when a file is selected. It is called with the current state of the component
onDragEnterCallbackfunctionnonullCallback invoked when drag enters. It is called with the current state of the component
onDragLeaveCallbackfunctionnonullCallback 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.

0.9.20

4 years ago

0.9.19

4 years ago

0.9.18

4 years ago

0.9.16

4 years ago

0.9.17

4 years ago

0.9.15

4 years ago

0.9.12

4 years ago

0.9.13

4 years ago

0.9.14

4 years ago

0.9.11

4 years ago

0.9.10

4 years ago

0.9.9

4 years ago

0.9.7

4 years ago

0.9.6

4 years ago

0.9.3

4 years ago

0.9.2

4 years ago

0.9.1

4 years ago

0.9.0

4 years ago

0.8.4

4 years ago

0.8.3

4 years ago

0.8.2

4 years ago

0.8.1

4 years ago

0.7.23

4 years ago

0.8.0

4 years ago

0.7.22

4 years ago

0.7.21

4 years ago

0.7.20

4 years ago

0.7.19

4 years ago

0.7.18

4 years ago

0.7.17

4 years ago

0.7.16

4 years ago

0.7.15

4 years ago

0.7.14

4 years ago

0.7.13

4 years ago

0.7.12

4 years ago

0.7.11

4 years ago

0.7.10

4 years ago

0.7.9

4 years ago

0.7.8

4 years ago

0.7.6

4 years ago

0.7.5

4 years ago

0.7.4

5 years ago

0.7.3

5 years ago

0.7.2

5 years ago

0.7.1

5 years ago

0.7.0

6 years ago

0.6.4

6 years ago

0.6.3

6 years ago

0.6.2

6 years ago

0.6.0

6 years ago

0.4.0

6 years ago

0.3.0

6 years ago

0.2.0

6 years ago

0.1.3

6 years ago

0.1.2

6 years ago