All files / src Collection.jsx

94.74% Statements 36/38
50% Branches 1/2
91.3% Functions 21/23
94.12% Lines 32/34
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                                                            13x               11x 11x           5x 5x       4x 4x 4x               4x       15x       37x       9x 9x 9x         1x       1x 1x 1x 1x 1x 1x       3x     1x 3x 2x       13x 13x 16x 16x 16x 16x         16x          
import * as React from 'react';
import uuid from 'uuid';
import PropTypes from 'prop-types';
 
class Collection extends React.Component {
 
  static propTypes = {
    components: PropTypes.array,
    name: PropTypes.string.isRequired
  }
  static defaultProps = {
    components: [],
    initialValue: []
  }
  _value = []
  _controls = {}
  _controlIDs = []
 
  static contextTypes = {
    onControlMount: PropTypes.func,
    onControlUnmount: PropTypes.func,
    onControlChange: PropTypes.func,
  }
  static childContextTypes = {
    onControlMount: PropTypes.func,
    onControlUnmount: PropTypes.func,
    onControlChange: PropTypes.func,
    onControlChangeID: PropTypes.func,
  }
  getChildContext() {
    return {
      onControlMount: ::this.handleControlMount,
      onControlUnmount: ::this.handleControlUnmount,
      onControlChange: ::this.handleControlChange,
      onControlChangeID: ::this.handleControlChangeID
    };
  }
  handleControlMount(id, control) {
    this._controls[id] = control;
    control.setForm(this._form);
  }
  handleControlUnmount(id) {
    delete this._controls[id];
  }
  handleControlChangeID(prevId, newId, control) {
    delete this._controlIDs[prevId];
    this._controlIDs[newId] = control;
  }
 
  componentDidMount() {
    this._id = uuid.v4();
    this.setValue(this.props.initialValue);
    this.context.onControlMount(this._id, this, this.value);
  }
 
  componentWillUnmount() {
    this.context.onControlUnmount(this._id, this);
  }
 
  setForm(form) {
    this._form = form;
  }
 
  get name() {
    return this.props.name;
  }
 
  get value() {
    return this._value;
  }
 
  setValue(val) {
    this._value = val;
    return new Promise(resolve => {
      this.forceUpdate(() => resolve());
    });
  }
 
  addValue(val) {
    this.setValue([...this.value, val]);
  }
 
  handleControlChange(id, val) {
    let i = this._controlIDs.findIndex(cid => cid == id);
    let value = [...this._value];
    value[i] = val;
    this.setValue(value);
    Eif (this.props.onChange) {
      this.props.onChange(this.value);
    }
  }
  showErrors() {
    Object.entries(this._controls).forEach(c => c[1].showErrors());
  }
  validate() {
    return Object.entries(this._controls).map(
      (c, cIndex) => c[1].validate().map(res => ({ ...res, index: cIndex }))
    ).reduce((a, b) => a.concat(b));
  }
 
  render() {
    this._controlIDs = [];
    return this.value.map((v, i) => {
      let el = this.props.component({value: v}), cid;
      cid = uuid.v4();
      this._controlIDs.push(cid);
      el = React.cloneElement(el, {
        key: i,
        value: this.value[i],
        cid
      });
      return el;
    });
  }
}
 
export default Collection;