# All files / components/FileLoader FileLoader.js

0% Statements 0/7
0% Branches 0/4
0% Functions 0/3
0% Lines 0/7

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94

     import React from 'react';
    import PropTypes from 'prop-types';
    import Dropzone from 'react-dropzone';
    import Icon from '../Icon';
    import FileLoaderDropArea from './styles/FileLoaderDropArea';
    import FileLoaderPreview from './styles/FileLoaderPreview';

    export default class FileLoader extends React.Component {
      static propTypes = {
        /**
         * A FileList object to use as the value of the input.
         * Using undefined makes this an 'uncontrolled' input
         */
        value: PropTypes.instanceOf(FileList),
        /**
         * Handler for the onChange event on the input.
         */
        onChange: PropTypes.func,
        /**
         * Marks that this input is required for form submission.
         */
        required: PropTypes.bool,
        /**
         * Marks that the user cannot change this input.
         */
        disabled: PropTypes.bool,
        /**
         * Adds a class name to the input element.
         */
        className: PropTypes.string,
        /**
         * Adds an id to the input element.
         */
        id: PropTypes.string,
        /**
         * A component to render the drop area
         */
        DropArea: PropTypes.func,
        /**
         * A component to render the preview container
         */
        Preview: PropTypes.func,
      };

      static defaultProps = {
        required: false,
        disabled: false,
        className: null,
        id: null,
        DropArea: FileLoaderDropArea,
        Preview: FileLoaderPreview,
      };

      renderFiles = () => {
        const { value, DropArea, Preview } = this.props;
        if (value && value[0]) {
          return (
            <DropArea>
              <Icon name="fileFilled" />
              <Preview active={true}>{value[0].name}</Preview>
            </DropArea>
          );
        } else {
          return (
            <DropArea>
              <Icon name="file" />
              <Preview>Drop a file here, or click to browse.</Preview>
            </DropArea>
          );
        }
      };

      render() {
        const { onChange, value, required, disabled, className, id } = this.props;

        return (
          <Dropzone
            multiple={false}
            onDrop={files => onChange(files)}
            style={{ width: '100%' }}
            activeStyle={{ color: '#00bcec' }}
            rejectStyle={{}}
            value={value}
            required={required}
            disabled={disabled}
            className={className}
            id={id}
          >
            {this.renderFiles()}
          </Dropzone>
        );
      }
    }