All files / upload index.js

97.92% Statements 47/48
95.83% Branches 23/24
100% Functions 2/2
100% Lines 42/42
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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168  1x 1x 1x 1x                                                                 39x 39x   39x   7x   6x 2x     4x 1x     3x     3x       7x   3x 3x           2x     1x       1x 1x             30x 30x 30x           19x 19x 10x       9x           1x 1x     1x 1x         1x       1x       19x 19x       2x 2x     63x 63x           63x                                             109x              
 
import React from 'react'
import classNames from 'classnames'
import accept from 'attr-accept'
import Button from '../button/'
 
export default class Upload extends React.Component {
 
  static propTypes = {
    accept: React.PropTypes.string,
    disabled: React.PropTypes.bool,
    onChange: React.PropTypes.func.isRequired,
    buttonText: React.PropTypes.string,
    textBefore: React.PropTypes.string,
    textAfter: React.PropTypes.string
  }
 
  static defaultProps = {
    accept: '*',
    disabled: false,
    buttonText: 'Select File',
    textBefore: '',
    textAfter: 'or drag file in this area'
  }
 
  state = {
    isOverWindow: false,
    isOverDropzone: false
  }
 
  // counter is an internal variable which is updated during drag enter and drag leave events.
  // the problem is that the browser fires drag leave and drag enter events
  // when dragging over a child element of window.
  counter = 0
 
  // prevent drag and drop of multiple files
  // this only works in chrome since Firefox does not allow access to files while dragging
  isInvalid (event) {
    const edt = event.dataTransfer
    // event.dataTransfer is empty during tests. no need to check every time in the following if ...
    if (!edt) return false
    // check for disabled
    if (this.props.disabled) return true
    // check firefox
    if (edt.mozItemCount && edt.mozItemCount !== 1) {
      return true
    }
    // check chrome
    if (edt.items && edt.items.length !== 1) {
      return true
    }
    // check mime types. only works in chrome. files is a FileList (no real array)
    const unaccepted = edt.files && Array.prototype.slice.call(edt.files).every((file) =>
      !accept(file, this.props.accept)
    )
    return unaccepted
  }
 
  onDragEnter = (event) => {
    if (this.isInvalid(event)) return
    // prevent event to allow drop
    event.preventDefault()
    this.setState({
      isOverDropzone: true
    })
  }
 
  onDragOver = (event) => {
    if (this.isInvalid(event)) return
    // If you want to allow a drop, you must prevent the default handling by cancelling the event
    // https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Drag_operations#Specifying_Drop_Targets
    event.preventDefault()
  }
 
  onDragLeave = (event) => {
    event.preventDefault()
    this.setState({
      isOverDropzone: false
    })
  }
 
  onDocumentDragEnter = (event) => {
    // prevent drag & drop of multiple items
    Iif (this.isInvalid(event)) return
    ++this.counter
    this.setState({
      isOverWindow: true
    })
  }
 
  onDocumentDragLeave = (event) => {
    event.preventDefault()
    if (--this.counter > 0) {
      return
    }
    // update state only when really leaving the document
    // not when leaving document children
    this.setState({
      isOverWindow: false
    })
  }
 
  onChange = (event) => {
    event.preventDefault()
    this.counter = 0
    // event.target.files is from standard file upload dialog
    // event.dataTransfer.files is from drap and drop api
    let files = event.target.files || event.dataTransfer.files
    this.setState({
      isOverWindow: false,
      isOverDropzone: false
    })
    // call callback to send files to parent
    this.props.onChange(files)
  }
 
  onClick = (event) => {
    this.fileInput.click()
  }
 
  componentDidMount () {
    document.addEventListener('dragenter', this.onDocumentDragEnter)
    document.addEventListener('dragleave', this.onDocumentDragLeave)
  }
 
  componentWillUnmount () {
    document.removeEventListener('dragenter', this.onDocumentDragEnter)
    document.removeEventListener('dragleave', this.onDocumentDragLeave)
  }
 
  render () {
    const klass = classNames('Upload', {
      'is-disabled': this.props.disabled,
      'is-over-window': this.state.isOverWindow,
      'is-over-dropzone': this.state.isOverDropzone
    })
 
    return (
      <div
        className={klass}
        onDragEnter={this.onDragEnter}
        onDragOver={this.onDragOver}
        onDragLeave={this.onDragLeave}
        onDrop={this.onChange}
      >
        <div className='Upload-textBefore'>
          {this.props.textBefore}
        </div>
        <Button raised onClick={this.onClick} disabled={this.props.disabled}>
          {this.props.buttonText}
        </Button>
        <div className='Upload-textAfter'>
          {this.props.textAfter}
        </div>
        <input
          accept={this.props.accept}
          className='Upload-fileInput'
          disabled={this.props.disabled}
          type='file'
          onChange={this.onChange}
          ref={(el) => (this.fileInput = el)}
        />
      </div>
    )
  }
 
}