<commit_before>'use strict';
var React   = require('react')
  , CustomPropTypes = require('./util/propTypes')
  , localizers  = require('./util/configuration').locale;

var format = props => props.format || localizers.number.formats.default

module.exports = React.createClass({

  displayName: 'NumberPickerInput',

  propTypes: {
    value:        React.PropTypes.number,

    format:       CustomPropTypes.numberFormat,
    parse:        React.PropTypes.func.isRequired,
    culture:      React.PropTypes.string,

    min:          React.PropTypes.number,

    onChange:     React.PropTypes.func.isRequired,
    onKeyDown:    React.PropTypes.func
  },

  getDefaultProps(){
    return {
      value: null,
      editing: false,
      parse: (number, culture) => localizers.number.parse(number, culture)
    }
  },

  getDefaultState(props){
    var value = props.editing
          ? props.value
          : formatNumber(props.value, format(props), props.culture)

    if ( value == null || isNaN(props.value) )
      value = ''

    return {
      stringValue: '' + value
    }
  },

  getInitialState() {
    return this.getDefaultState(this.props)
  },

  componentWillReceiveProps(nextProps) {
    this.setState(
      this.getDefaultState(nextProps))
  },

  render(){
    var value = this.state.stringValue;

    return (
      <input {...this.props}
        type='text'
        className='rw-input'
        onChange={this._change}
        onBlur={this._finish}
        aria-disabled={this.props.disabled}
        aria-readonly={this.props.readOnly}
        disabled={this.props.disabled}
        readOnly={this.props.readOnly}
        value={value}/>
    )
  },

  _change(e){
    var val = e.target.value
      , number = this.props.parse(e.target.value, this.props.culture)
      , valid = this.isValid(number);

    if( val == null || val.trim() === '' )
      return this.props.onChange(null)

    if( valid && number !== this.props.value && !this.isAtDelimiter(number, val))
      return this.props.onChange(number)

    //console.log(val !== 0 && !val)
    if ( !isNaN(number) || this.isAtDelimiter(number, val))
      this.current(e.target.value)
  },

  _finish() {
    var str = this.state.stringValue
      , number = this.props.parse(str, this.props.culture);

    // if number is below the min
    // we need to flush low values and decimal stops, onBlur means i'm done inputing
    if(!isNaN(number) && (number < this.props.min || this.isAtDelimiter(number, str)) ) {
      this.props.onChange(number)
    }
  },

  isAtDelimiter(num, str){
    var next;

    if ( str.length <= 1) return false<commit_msg>Trigger onChange in NumberPicker on '-'<commit_after>'use strict';
var React   = require('react')
  , CustomPropTypes = require('./util/propTypes')
  , localizers  = require('./util/configuration').locale;

var format = props => props.format || localizers.number.formats.default

module.exports = React.createClass({

  displayName: 'NumberPickerInput',

  propTypes: {
    value:        React.PropTypes.number,

    format:       CustomPropTypes.numberFormat,
    parse:        React.PropTypes.func.isRequired,
    culture:      React.PropTypes.string,

    min:          React.PropTypes.number,

    onChange:     React.PropTypes.func.isRequired,
    onKeyDown:    React.PropTypes.func
  },

  getDefaultProps(){
    return {
      value: null,
      editing: false,
      parse: (number, culture) => localizers.number.parse(number, culture)
    }
  },

  getDefaultState(props){
    var value = props.editing
          ? props.value
          : formatNumber(props.value, format(props), props.culture)

    if ( value == null || isNaN(props.value) )
      value = ''

    return {
      stringValue: '' + value
    }
  },

  getInitialState() {
    return this.getDefaultState(this.props)
  },

  componentWillReceiveProps(nextProps) {
    this.setState(
      this.getDefaultState(nextProps))
  },

  render(){
    var value = this.state.stringValue;

    return (
      <input {...this.props}
        type='text'
        className='rw-input'
        onChange={this._change}
        onBlur={this._finish}
        aria-disabled={this.props.disabled}
        aria-readonly={this.props.readOnly}
        disabled={this.props.disabled}
        readOnly={this.props.readOnly}
        value={value}/>
    )
  },

  _change(e){
    var val = e.target.value
      , number = this.props.parse(e.target.value, this.props.culture)
      , valid = this.isValid(number);

    if( val == null || val.trim() === '' || val.trim() === '-')
      return this.props.onChange(null)

    if( valid && number !== this.props.value && !this.isAtDelimiter(number, val))
      return this.props.onChange(number)

    //console.log(val !== 0 && !val)
    if ( !isNaN(number) || this.isAtDelimiter(number, val))
      this.current(e.target.value)
  },

  _finish() {
    var str = this.state.stringValue
      , number = this.props.parse(str, this.props.culture);

    // if number is below the min
    // we need to flush low values and decimal stops, onBlur means i'm done inputing
    if(!isNaN(number) && (number < this.props.min || this.isAtDelimiter(number, str)) ) {
      this.props.onChange(number)
    }
  },

  isAtDelimiter(num, str){
    var next;

    if ( str.length <= 1) return false
{'fraction_non_alphanumeric': 0.14105223458419763, 'fraction_numerical': 0.0007542900245144258, 'mean_word_length': 2.622950819672131, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '3175809', 'n_tokens_mistral': 1681, 'n_tokens_neox': 1657, 'n_words': 315}