<commit_before>import React from "react";
import PropTypes from 'prop-types';
import messages from '../utils/messages';
import utils from '../utils/utils';
import cx from "classnames";

const errorCodes = {
  "MISSING_REQUIRED": 0
}

class Form extends React.PureComponent {
  constructor(props) {
    super(props);

    let childState = [];

    React.Children.map(props.children, (child, i) => {

      let state = {
                    index: i,
                    value: child.props.value || '',
                    isValid: true,
                    onChange: child.props.onChange || null
                  };

      childState.push(state)

      return child
    });

    this.state = {
      "childState": childState
    };
  }

  validate = () => {
    let isValid = true;
  	const data = React.Children.map(this.props.children, (child, i) => {

      if(typeof child.props.required !== 'undefined') {
        if(this.state.childState[i].value === '') {
          const state = this.state.childState;
          state[i].isValid = false;
          this.setState({state});
          isValid = false;
          return;
        }
      }

      /* Skip children with no name prop. */
      if(!child.props.name) {
        return;
      }

      let childData = {
                        "name": child.props.name,
                        "value": this.state.childState[i].value
                      };

      return childData;
    });

    if(isValid) {
      return data;
    } else {
      return null;
    }
  }

  transformData = (data) => {
    let sumbitData = {};
    for(let i = 0; i < data.length; i++) {
      let key = data[i].name;
      let value = data[i].value;

      sumbitData[key] = value;

    }

    return sumbitData;
  }

  submitHandler = (e) => {
  	/* Prevent form submission if action prop is not set. */
  	if(!this.props.action) {
  	  e.preventDefault();
  	}

  	/* Validate children components before submiting. */
  	let data = this.validate();
    if(!data) {
      if(typeof this.props.onError !== 'undefined') {
        this.props.onError(errorCodes.MISSING_REQUIRED, messages.missingRequired);
      }
      return;
    }

    /* Tranform data array from validate() to an object using name
     * as the key and the child's value as the object value. */
    let sumbitData = this.transformData(data);

  	/* Check if onSubmit was set. Call onSubmit if
  	 * it was passed throw a error if not set. */
  	if(this.props.onSubmit) {
  		this.props.onSubmit(e, sumbitData);
  	} else {
      throw messages.onSubmitAction;
  	}
  }

  /* Fires whenever a child input is changed. */<commit_msg>Don't let form submit when action is set, and the form is invalid.<commit_after>import React from "react";
import PropTypes from 'prop-types';
import messages from '../utils/messages';
import utils from '../utils/utils';
import cx from "classnames";

const errorCodes = {
  "MISSING_REQUIRED": 0
}

class Form extends React.PureComponent {
  constructor(props) {
    super(props);

    let childState = [];

    React.Children.map(props.children, (child, i) => {

      let state = {
                    index: i,
                    value: child.props.value || '',
                    isValid: true,
                    onChange: child.props.onChange || null
                  };

      childState.push(state)

      return child
    });

    this.state = {
      "childState": childState
    };
  }

  validate = () => {
    let isValid = true;
  	const data = React.Children.map(this.props.children, (child, i) => {

      if(typeof child.props.required !== 'undefined') {
        if(this.state.childState[i].value === '') {
          const state = this.state.childState;
          state[i].isValid = false;
          this.setState({state});
          isValid = false;
          return;
        }
      }

      /* Skip children with no name prop. */
      if(!child.props.name) {
        return;
      }

      let childData = {
                        "name": child.props.name,
                        "value": this.state.childState[i].value
                      };

      return childData;
    });

    if(isValid) {
      return data;
    } else {
      return null;
    }
  }

  transformData = (data) => {
    let sumbitData = {};
    for(let i = 0; i < data.length; i++) {
      let key = data[i].name;
      let value = data[i].value;

      sumbitData[key] = value;

    }

    return sumbitData;
  }

  submitHandler = (e) => {
  	/* Prevent form submission if action prop is not set. */
  	if(!this.props.action) {
  	  e.preventDefault();
  	}

  	/* Validate children components before submiting. */
  	let data = this.validate();
    if(!data) {
      /* Prevent form submission when action is set and the
       * form is not valid. */
      if(this.props.action) {
        e.preventDefault();
      }

      if(typeof this.props.onError !== 'undefined') {
        this.props.onError(errorCodes.MISSING_REQUIRED, messages.missingRequired);
      }
      return;
    }

    /* Tranform data array from validate() to an object using name
     * as the key and the child's value as the object value. */
    let sumbitData = this.transformData(data);

  	/* Check if onSubmit was set. Call onSubmit if
  	 * it was passed throw a error if not set. */
  	if(this.props.onSubmit) {
  		this.props.onSubmit(e, sumbitData);
  	} else {
      throw messages.onSubmitAction;
  	}
  }

  /* Fires whenever a child input is changed. */
{'fraction_non_alphanumeric': 0.12662159693038552, 'fraction_numerical': 0.0007308605883427736, 'mean_word_length': 1.9086078639744952, 'pattern_counts': {'":': 8, '<': 5, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3301811', 'n_tokens_mistral': 1673, 'n_tokens_neox': 1626, 'n_words': 423}