All files / src Error.jsx

100% Statements 14/14
87.5% Branches 7/8
100% Functions 5/5
100% Lines 14/14
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                                                  2x 2x       2x 2x 1x   2x 1x 1x     1x         1x       2x       6x 2x           4x            
import * as React from 'react';
import PropTypes from 'prop-types';
import uuid from 'uuid';
 
class Error extends React.Component {
  static propTypes = {
    onSubmit: PropTypes.func,
    onSubmitFailed: PropTypes.func,
    onChange: PropTypes.func,
    validator: PropTypes.func,
  }
  static defaultProps = {
    hideOnChange: true,
  }
 
  static contextTypes = {
    onErrorMount: PropTypes.func,
    onErrorUnmount: PropTypes.func,
  }
 
  state = {
    show: false
  }
 
  componentDidMount() {
    this._id = uuid.v4();
    this.context.onErrorMount(this._id, this);
  }
 
  onFormChanged(data) {
    let isValid = true;
    if (this.props.validator) {
      isValid = this.props.validator(data);
    }
    if (isValid) {
      Eif (this.props.hideOnChange) {
        this.hide();
      }
    } else {
      this.show();
    }
  }
 
  hide() {
    this.setState({show: false});
  }
 
  show() {
    this.setState({show: true});
  }
 
  render() {
    if (this.state.show) {
      return (
        <div className='form-error'>
          {this.props.children}
        </div>
      );
    } else {
      return null;
    }
  }
}
 
export default Error;