All files EasyV.jsx

100% Statements 14/14
100% Branches 2/2
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              6x 6x 6x 16x 16x 5x           11x       1x     1x 1x       6x 6x       1x       1x            
import PropTypes from 'prop-types';
/* eslint-disable react/forbid-prop-types,import/no-extraneous-dependencies */
import React from 'react';
import * as lib from './helpers';
 
export default class EasyV extends React.Component {
  getChildren = () => {
    const { schema, allState, children } = this.props;
    const names = Object.keys(schema);
    return React.Children.map(children, child => {
      const childName = child.props.name;
      if (names.includes(childName)) {
        return React.cloneElement(child, {
          status: allState[childName].status,
          hint: allState[childName].errorText,
          value: allState[childName].value
        });
      }
      return child;
    });
  };
 
  handleOnChange = e => this.validate(e.target);
 
  validate(target) {
    const { schema, allState, update } = this.props;
    lib.startValidating(target, schema, update, allState);
  }
 
  render() {
    const newChildren = this.getChildren();
    return <section onChange={this.handleOnChange}>{newChildren}</section>;
  }
}
 
EasyV.defaultProps = {
  allState: undefined
};
 
EasyV.propTypes = {
  schema: PropTypes.object.isRequired,
  allState: PropTypes.object,
  update: PropTypes.func.isRequired,
  children: PropTypes.any.isRequired
};