<commit_before>import React, { PropTypes } from 'react';
import './ReactTextField.css';

export default class ReactTextField extends React.Component {

  constructor(props) {
    super(props);
    this.state = {
      successMessage: null,

      errorMessage: null,

      isValid: false,
    };
    this.onChange = this.onChange.bind(this);
  }

  onChange(e) {
    const value = e.target.value;
    this.validate(value);
    // Execute callback function.
    if (this.props.onChange) {
      this.props.onChange(this.state.isValid);
    }
  }

  // TODO: Refactoring

  validate(value) {
    let errorMessage = null;
    let isValid = true;

    for (const err of this.props.validators) {
      if (!err.validator) {
        continue;
      }
      if (!err.validator(value)) {
        errorMessage = err.message;
        isValid = false;
        break;
      }
    }
    this.setState({ isValid, errorMessage });
  }

  render() {
    return (
      <div className="ReactTextField--container">
        <input type={this.props.type} className="ReactTextField--input" onChange={this.onChange} />
        { this.state.isValid ?
          <span className="ReactTextField-message ReactTextField--success">
            {this.props.successMessage}
          </span>
          :
          <span className="ReactTextField-message ReactTextField--error">
            {this.state.errorMessage}
          </span>
        }
      </div>
    );
  }
}

ReactTextField.propTypes = {

  type: PropTypes.oneOf([
    'text',
    'password',
    'email',
    'tel',
    'url',
  ]),

  // Success message when validator passed.
  successMessage: PropTypes.string,

  // One of Error object includes validater and error message.
  validators: PropTypes.arrayOf(React.PropTypes.shape({
    message: PropTypes.string.isRequired,
    validator: PropTypes.func.isRequired,
  })),

  onChange: React.PropTypes.func,

};

ReactTextField.defaultProps = {
  type: 'text',
  validators: [],
};
<commit_msg>Update component. Add validateOnBlur, onBlur, onFocus props.<commit_after>import React, { PropTypes } from 'react';
import './ReactTextField.css';

export default class ReactTextField extends React.Component {

  constructor(props) {
    super(props);
    this.state = {
      successMessage: null,

      errorMessage: null,

      isValid: false,
    };
    this.onChange = this.onChange.bind(this);
    this.onBlur = this.onBlur.bind(this);
    this.onFocus = this.onFocus.bind(this);
  }

  onChange(e) {
    if (!this.props.validateOnBlur) {
      const value = e.target.value;
      this.validate(value);
    }
    // Execute callback function.
    if (this.props.onChange) {
      this.props.onChange(this.state.isValid);
    }
  }

  onBlur(e) {
    const value = e.target.value;
    if (this.props.validateOnBlur) {
      this.validate(value);
    }

    if (this.props.onBlur) {
      this.props.onBlur(e);
    }
  }

  onFocus(e) {
    if (this.props.onFocus) {
      this.prop.onFocus(e);
    }
  }

  // TODO: Refactoring

  validate(value) {
    let errorMessage = null;
    let isValid = true;

    for (const err of this.props.validators) {
      if (!err.validator) {
        continue;
      }
      if (!err.validator(value)) {
        errorMessage = err.message;
        isValid = false;
        break;
      }
    }
    this.setState({ isValid, errorMessage });
  }

  render() {
    return (
      <div className="ReactTextField--container">
        <input
          type={this.props.type}
          className="ReactTextField--input"
          onChange={this.onChange}
          onBlur={this.onBlur}
          onFocus={this.onFocus}
          placeholder={this.props.placeholder}
        />
        { this.state.isValid ?
          <span className="ReactTextField-message ReactTextField--success">
            {this.props.successMessage}
          </span>
          :
          <span className="ReactTextField-message ReactTextField--error">
            {this.state.errorMessage}
          </span>
        }
      </div>
    );
  }
}

ReactTextField.propTypes = {

  type: PropTypes.oneOf([
    'text',
    'password',
    'email',
    'tel',
    'url',
  ]),

  placeholder: PropTypes.string,

  // Success message when validator passed.
  successMessage: PropTypes.string,

  // One of Error object includes validater and error message.
  validators: PropTypes.arrayOf(React.PropTypes.shape({
    message: PropTypes.string.isRequired,
    validator: PropTypes.func.isRequired,
  })),

  onChange: PropTypes.func,
  onBlur: PropTypes.func,
  onFocus: PropTypes.func,

  validateOnBlur: PropTypes.bool,

};

ReactTextField.defaultProps = {
  type: 'text',
  placeholder: '',
  validators: [],
  validateOnBlur: false,
};

{'fraction_non_alphanumeric': 0.13024142312579415, 'fraction_numerical': 0.0, 'mean_word_length': 2.3880918220946916, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, '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': '6416887', 'n_tokens_mistral': 1460, 'n_tokens_neox': 1475, 'n_words': 276}