<commit_before>import React from 'react';
import I18nHelper from './../../utils/helpers/i18n';
import Input from './../../utils/decorators/input';
import InputLabel from './../../utils/decorators/input-label';
import InputValidation from './../../utils/decorators/input-validation';

/**
 * A decimal widget.
 *
 * == How to use a Decimal in a component:
 *
 * In your file
 *
 *   import Decimal from 'carbon/lib/components/decimal';
 *
 * To render the Decimal:
 *
 *   <Decimal name="myDecimal" />
 *
 * @class Decimal
 * @constructor
 * @decorators {Input,InputLabel,InputValidation}
 */
const Decimal = Input(InputLabel(InputValidation(
class Decimal extends React.Component {

  /**
   * Stores the document - allows us to override it different contexts, such as
   * when running tests.
   *
   * @property _document
   * @type {document}
   */
  _document = document;

  /**
   * Used within the onClick and onBlur method to
   * check if the current visible input value is
   * highlighted
   *
   * @property highlighted
   * @type {Boolean}
   */
  highlighted = false;

  static propTypes = {
    /**
     * Sets the default value of the decimal field
     *
     * @property defaultValue
     * @type {String}
     * @default '0.00'
     */
    defaultValue: React.PropTypes.string,

    /**
     * Sets the default value alignment
     *
     * @property align
     * @type {String}
     * @default 'right'
     */
    align: React.PropTypes.string,

    /**
     * Sets the pricision of the field
     *
     * @property precision
     * @type {Integer}
     * @default 2
     */
    precision: React.PropTypes.number
  }


  static defaultProps = {
    defaultValue: '0.00',
    align: "right",
    precision: 2
  }

  state = {
    /**
     * The formatted value for display
     *
     * @property visibleValue
     * @type {String}
     */
    visibleValue: I18nHelper.formatDecimal(this.value, this.props.precision)
  }

  /**
   * A lifecycle method to update the visible value with a formatted version,
   * only when the field is not the active element.
   *
   * @method componentWillReceiveProps
   * @param {Object} props The new props passed down to the component
   * @return {void}
   */
  componentWillReceiveProps(props) {
    if (this._document.activeElement != this._input) {
      let value = props.value || props.defaultValue;
      this.setState({ visibleValue: I18nHelper.formatDecimal(value, this.props.precision) });
    }
  }

  /**
   * Callback to update the hidden field on change.
   *
   * @method emitOnChangeCallback
   * @param {String} val The unformatted decimal value
   * @return {void}
   */
  emitOnChangeCallback = (val) => {
    let hiddenField = this.refs.hidden;
    hiddenField.value = val;

    this._handleOnChange({ target: hiddenField });
  }

  /**
   * Checks that visibleValue is valid decimal.
   * This is a post-processor applied after the value has been updated.
   *
   * @method isValidDecimal
   * @param {String} value new prop value
   * @return {Boolean} true if a valid decimal
   */
  isValidDecimal = (value, precision) => {
    let del, regex, sep, format = I18nHelper.format();
    del = format.delimiter;
    sep = format.separator;
    regex = precision > 0 ?
        new RegExp(`^[-]?[0-9]*(?:\${del}?[0-9]?)*\${sep}?[0-9]{0,${precision}}$`) :
        new RegExp(`^[-]?[0-9]*(?:\${del}?[0-9]?)*$`);
    return regex.test(value);
  }

  /**
   * Handles Change to visible field
   *
   * @method handleVisibleInputChange
   * @param {Object} ev event
   * @return {void}
   */
  handleVisibleInputChange = (ev) => {
    if (this.isValidDecimal(ev.target.value, this.props.precision)) {
      this.setState({ visibleValue: ev.target.value });
      this.emitOnChangeCallback(I18nHelper.unformatDecimal(ev.target.value));
    } else {
      // reset the value
      ev.target.value = this.state.visibleValue;
      // reset the selection range
      ev.target.setSelectionRange(this.selectionStart, this.selectionEnd);
    }
  }

  /**
   * Updates visible value on blur
   *
   * @method handleBlur
   * @return {void}<commit_msg>fixed the regular expression bug<commit_after>import React from 'react';
import I18nHelper from './../../utils/helpers/i18n';
import Input from './../../utils/decorators/input';
import InputLabel from './../../utils/decorators/input-label';
import InputValidation from './../../utils/decorators/input-validation';

/**
 * A decimal widget.
 *
 * == How to use a Decimal in a component:
 *
 * In your file
 *
 *   import Decimal from 'carbon/lib/components/decimal';
 *
 * To render the Decimal:
 *
 *   <Decimal name="myDecimal" />
 *
 * @class Decimal
 * @constructor
 * @decorators {Input,InputLabel,InputValidation}
 */
const Decimal = Input(InputLabel(InputValidation(
class Decimal extends React.Component {

  /**
   * Stores the document - allows us to override it different contexts, such as
   * when running tests.
   *
   * @property _document
   * @type {document}
   */
  _document = document;

  /**
   * Used within the onClick and onBlur method to
   * check if the current visible input value is
   * highlighted
   *
   * @property highlighted
   * @type {Boolean}
   */
  highlighted = false;

  static propTypes = {
    /**
     * Sets the default value of the decimal field
     *
     * @property defaultValue
     * @type {String}
     * @default '0.00'
     */
    defaultValue: React.PropTypes.string,

    /**
     * Sets the default value alignment
     *
     * @property align
     * @type {String}
     * @default 'right'
     */
    align: React.PropTypes.string,

    /**
     * Sets the pricision of the field
     *
     * @property precision
     * @type {Integer}
     * @default 2
     */
    precision: React.PropTypes.number
  }


  static defaultProps = {
    defaultValue: '0.00',
    align: "right",
    precision: 2
  }

  state = {
    /**
     * The formatted value for display
     *
     * @property visibleValue
     * @type {String}
     */
    visibleValue: I18nHelper.formatDecimal(this.value, this.props.precision)
  }

  /**
   * A lifecycle method to update the visible value with a formatted version,
   * only when the field is not the active element.
   *
   * @method componentWillReceiveProps
   * @param {Object} props The new props passed down to the component
   * @return {void}
   */
  componentWillReceiveProps(props) {
    if (this._document.activeElement != this._input) {
      let value = props.value || props.defaultValue;
      this.setState({ visibleValue: I18nHelper.formatDecimal(value, this.props.precision) });
    }
  }

  /**
   * Callback to update the hidden field on change.
   *
   * @method emitOnChangeCallback
   * @param {String} val The unformatted decimal value
   * @return {void}
   */
  emitOnChangeCallback = (val) => {
    let hiddenField = this.refs.hidden;
    hiddenField.value = val;

    this._handleOnChange({ target: hiddenField });
  }

  /**
   * Checks that visibleValue is valid decimal.
   * This is a post-processor applied after the value has been updated.
   *
   * @method isValidDecimal
   * @param {String} value new prop value
   * @return {Boolean} true if a valid decimal
   */
  isValidDecimal = (value, precision) => {
    let del, regex, sep, format = I18nHelper.format();
    del = format.delimiter;
    sep = format.separator;
    regex = precision > 0 ?
        new RegExp(`^[-]?[0-9]*(?:${del}?[0-9]?)*${sep}?[0-9]{0,${precision}}$`) :
        new RegExp(`^[-]?[0-9]*(?:${del}?[0-9]?)*$`);
    return regex.test(value);
  }

  /**
   * Handles Change to visible field
   *
   * @method handleVisibleInputChange
   * @param {Object} ev event
   * @return {void}
   */
  handleVisibleInputChange = (ev) => {
    if (this.isValidDecimal(ev.target.value, this.props.precision)) {
      this.setState({ visibleValue: ev.target.value });
      this.emitOnChangeCallback(I18nHelper.unformatDecimal(ev.target.value));
    } else {
      // reset the value
      ev.target.value = this.state.visibleValue;
      // reset the selection range
      ev.target.setSelectionRange(this.selectionStart, this.selectionEnd);
    }
  }

  /**
   * Updates visible value on blur
   *
   * @method handleBlur
   * @return {void}
{'fraction_non_alphanumeric': 0.13191747572815535, 'fraction_numerical': 0.007766990291262136, 'mean_word_length': 2.6988330341113107, 'pattern_counts': {'":': 0, '<': 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': '5903674', 'n_tokens_mistral': 2568, 'n_tokens_neox': 2494, 'n_words': 683}