All files EasyValidator.jsx

100% Statements 15/15
100% Branches 2/2
100% Functions 5/5
100% Lines 15/15
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              6x 6x 6x 6x 16x 16x 5x           11x   6x     1x     1x 1x       6x 6x       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 EasyValidator extends React.Component {
  getChildren = () => {
    const { schema, formStatus, children } = this.props;
    const names = Object.keys(schema);
    const { fields } = formStatus;
    const newChild = React.Children.map(children, child => {
      const childName = child.props.name;
      if (names.includes(childName)) {
        return React.cloneElement(child, {
          status: fields[childName].status,
          hint: fields[childName].errorText,
          value: fields[childName].value
        });
      }
      return child;
    });
    return newChild;
  };
 
  handleOnChange = e => (this.validate(e.target));
 
  validate(target) {
    const { schema, formStatus, update } = this.props;
    lib.startValidating(target, schema, formStatus, update)
  }
 
  render() {
    const newChildren = this.getChildren();
    return <section onChange={this.handleOnChange}>{newChildren}</section>;
  }
}
 
EasyValidator.propTypes = {
  schema: PropTypes.object.isRequired,
  formStatus: PropTypes.object.isRequired,
  update: PropTypes.func.isRequired,
  children: PropTypes.any.isRequired
};