All files / src Form.jsx

97.56% Statements 40/41
86.67% Branches 13/15
90.91% Functions 10/11
97.56% Lines 40/41
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                                            39x                 13x 13x 2x 2x   13x 2x       33x 33x 33x     2x 2x     2x           3x 3x 3x 6x 6x 1x     3x 2x 1x     1x 1x         73x 73x 95x   73x     1x 2x       3x 3x 3x 7x 14x 6x 6x       3x 3x     39x                  
import * as React from 'react';
import PropTypes from 'prop-types';
 
 
class Form extends React.Component {
  _controls = {};
  _values = {};
  _errors = {};
  static propTypes = {
    onSubmit: PropTypes.func,
    onSubmitFailed: PropTypes.func,
    onChange: PropTypes.func,
  }
  static childContextTypes = {
    onControlMount: PropTypes.func,
    onControlUnmount: PropTypes.func,
    onControlChange: PropTypes.func,
 
    onErrorMount: PropTypes.func,
    onErrorUnmount: PropTypes.func,
  }
  getChildContext() {
    return {
      onControlMount: ::this.handleControlMount,
      onControlUnmount: ::this.handleControlUnmount,
      onControlChange: ::this.handleControlChange,
      onErrorMount: ::this.handleErrorMount,
      onErrorUnmount: ::this.handleErrorUnmount,
    };
  }
  handleControlChange(id, val) {
    this._values[id] = val;
    if (this.props.onChange) {
      let component = this._controls[id];
      this.props.onChange({[component.name]: val}, this.value);
    }
    for (let [id, err] of Object.entries(this._errors)) {
      err.onFormChanged(this.value);
    }
  }
  handleControlMount(id, component, initialValue) {
    this._controls[id] = component;
    this._values[id] = initialValue;
    component.setForm(this);
  }
  handleControlUnmount(id) {
    delete this._controls[id];
    delete this._values[id];
  }
  handleErrorMount(id, component) {
    this._errors[id] = component;
  }
  handleErrorUnmount(id) {
    delete this._errors[id];
  }
  handleSubmit(e) {
    e.preventDefault();
    let err = false;
    for (let [id, control] of Object.entries(this._controls)) {
      control.showErrors();
      if (!err && control.validate().length) {
        err = true;
      }
    }
    if (!err) {
      if (this.props.onSubmit) {
        this.props.onSubmit(this.value, e);
      }
    } else {
      Eif (this.props.onSubmitFailed) {
        this.props.onSubmitFailed(this.value, e);
      }
    }
  }
  get value() {
    let data = {};
    for (let [id, component] of Object.entries(this._controls)) {
      data[component.name] = this._values[id];
    }
    return data;
  }
  reset() {
    for (let [id, control] of Object.entries(this._controls)) {
      control.reset();
    }
  }
  setValue(data={}) {
    let promises = [],
        newValues = {};
    for (let [name, value] of Object.entries(data)) {
      for (let [id, control] of Object.entries(this._controls)) {
        if (control.name == name) {
          newValues[id] = value;
          promises.push(control.setValue(value));
        }
      }
    }
    this._values = newValues;
    return Promise.all(promises);
  }
  render() {
    return (
      <form onSubmit={::this.handleSubmit}>
        {this.props.children}
      </form>
    );
  }
}
 
export default Form;