All files / src EasyV.jsx

100% Statements 23/23
83.33% Branches 10/12
100% Functions 6/6
100% Lines 22/22
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                1x 1x 1x       25x   18x     7x 7x 6x   1x       6x 6x               26x 25x 25x 6x     19x 19x   19x 19x             7x               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';
import is from 'is_js';
 
export default class EasyV extends React.Component {
  handleOnChange = e => {
    e.preventDefault();
    const { schema, allState, update } = this.props;
    lib.startValidating(e.target, schema, update, allState);
  };
 
  isRegisteredComponent = (child, childName) => {
    if (is.string(child.type)) {
      // Skip HTML element
      Eif (is.lowerCase(child.type[0])) return false;
    }
 
    const names = Object.keys(this.props.schema);
    if (React.isValidElement(child) && names.includes(childName)) {
      return true;
    }
    return false;
  };
 
  cloneElement = (child, childName) => {
    const childProp = this.props.allState[childName];
    return React.cloneElement(child, {
      status: childProp.status,
      errorText: childProp.errorText,
      value: childProp.value
    });
  };
 
  recursiveCloneChildren = children => {
    return React.Children.map(children, child => {
      const childName = child.props.name;      
      if (this.isRegisteredComponent(child, childName)) {
        return this.cloneElement(child, childName);
      }
      
      const childProps = {};
      Eif (child.props) {
        // String has no Prop
        childProps.children = this.recursiveCloneChildren(child.props.children);
        return React.cloneElement(child, childProps);          
      }
      // return child;
    });
  };
 
  render() {
    return (
      <section onChange={this.handleOnChange}>
        {this.recursiveCloneChildren(this.props.children)}
      </section>
    );
  }
}
 
EasyV.defaultProps = {
  allState: undefined
};
 
EasyV.propTypes = {
  schema: PropTypes.object.isRequired,
  allState: PropTypes.object,
  update: PropTypes.func.isRequired,
  children: PropTypes.any.isRequired
};