All files / src FileInput.js

92.31% Statements 12/13
75% Branches 3/4
100% Functions 3/3
92.31% Lines 12/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                                        1x   1x   1x     1x       5x 5x   5x       5x                                                               2x   2x   2x                   1x  
import React, {Component} from 'react'
import PropTypes from 'prop-types'
import classNames from 'classnames'
import InputGroup from './InputGroup'
import Button from './Button'
import InputLabel from './InputLabel'
import InputErrors from './InputErrors'
import InputHelpText from './InputHelpText'
 
export default class FileInput extends Component {
  static propTypes = {
    button: PropTypes.string,
    error: PropTypes.oneOfType([PropTypes.bool, PropTypes.string]),
    placeholder: PropTypes.string,
    onChange: PropTypes.func,
    className: PropTypes.string,
  }
 
  static defaultProps = {
    button: 'Choose File',
    placeholder: 'No file chosen',
  }
 
  state = {
    filePath: null,
  }
 
  onChange = (e, ...rest) => {
    const filePath = e.currentTarget.value.substring('C:\\fakepath\\'.length)
 
    this.setState({filePath})
 
    Iif (this.props.onChange) {
      return this.props.onChange(e, ...rest)
    }
    return null
  }
 
  render() {
    const {button, className, error, placeholder, ...props} = this.props
    const {filePath} = this.state
 
    const inputGroupClassName = classNames(className, 'rev-FileInput', {
      'is-invalid': !!error,
    })
 
    return (
      <InputGroup className={inputGroupClassName}>
        <InputGroup.Field>
          <InputGroup.Label className="rev-FileInput-label">
            {filePath || placeholder}
          </InputGroup.Label>
        </InputGroup.Field>
        <InputGroup.Button>
          <Button tag="label" className="rev-FileInput-button">
            {button}
            <input
              type="file"
              className="ShowForSR"
              onChange={this.onChange}
              {...props}
            />
          </Button>
        </InputGroup.Button>
      </InputGroup>
    )
  }
}
 
class FileInputStack extends Component {
  static propTypes = {
    error: PropTypes.oneOfType([PropTypes.bool, PropTypes.string]),
    help: PropTypes.string,
    label: PropTypes.string,
    className: PropTypes.string,
  }
 
  render() {
    const {error, help, className, label, ...props} = this.props
 
    const labelClassName = classNames(className, 'rev-FileInputStack')
 
    return (
      <InputLabel className={labelClassName} error={error}>
        {label}
        <FileInput {...props} error={error} />
        <InputHelpText>{help}</InputHelpText>
        <InputErrors>{error}</InputErrors>
      </InputLabel>
    )
  }
}
FileInput.Stack = FileInputStack