<commit_before>/** @jsx React.DOM */

var React = require('react');
var _ = require('lodash');

var lastKeyDown;

module.exports = React.createClass({
  getInitialState: function() {
    var initialValue = this.props.value 
      ? this.props.value[this.props.valueLabel] : '';

    var fauxBest = {};
    fauxBest[this.props.valueLabel] = '';
    return { 
      bestMatch: fauxBest,
      value: initialValue
    }
  },

  setOption: function(option) {
    var bestMatch = this.getBestMatch(option[this.props.valueLabel]);
    if(bestMatch)
      this.setState({ 
        value: bestMatch[this.props.valueLabel], 
        bestMatch: bestMatch
      });
  },

  shouldComponentUpdate: function(nextProps, nextState) {
    return nextProps.value != nextState.bestMatch.id;
  },

  componentDidUpdate: function(nextProps, nextState) {
    var isMatched = _.find(this.props.options, function(option) {
      return option[this.props.valueLabel] == this.state.value;
    }, this);
    if(isMatched && typeof this.props.onSelect === 'function')
      this.props.onSelect(this.state.bestMatch);
  },

  handleBlur: function() {
    var DOMVal = this.getDOMNode().value;
    this.setState({ value: DOMVal });
  },

  handleKeyDown: function(e) {
    var node = this.refs.field.getDOMNode();
    var value = node.value;
    if(e.keyCode === 8) {
      lastKeyDown = 8;
      var best = {};
      best[this.props.valueLabel] =  this.state.value
      this.setState({ bestMatch: best });
      return;
    }
    if(e.keyCode === 13) {
      node.blur();
      e.preventDefault();
    }

  },

  handleChange: function(e) {
    var node = this.refs.field.getDOMNode();
    var value = node.value;
    value = value.slice(0, node.selectionStart);

    var bestMatch = {};
    bestMatch[this.props.valueLabel] = value;
    if(lastKeyDown === 8)
      lastKeyDown = undefined;
    else 
      bestMatch = this.getBestMatch(value);

    this.setState(
      { value: value, bestMatch: bestMatch },
      function() {
        this.getDOMNode().setSelectionRange(value.length, bestMatch[this.props.valueLabel].length);
        if(typeof this.props.onChange == 'function')
          this.props.onChange(value);
      }
    );
  },

  getBestMatch: function(value) {
    var bestMatch = {};
    bestMatch[this.props.valueLabel] = value;
    
    return _.find(this.props.options, function(option) { 
      if(option[this.props.valueLabel])
        return option[this.props.valueLabel].lastIndexOf(value, 0) === 0;
    }, this) || bestMatch;
  },

  render: function() {
    return <input ref='field'
          onBlur={this.handleBlur}
          type="text"
          className="form-control" id="newOrderAdvertiser" 
          onKeyDown={this.handleKeyDown}
          onChange={this.handleChange} value={this.state.bestMatch.advertiserName} />
  }

});
<commit_msg>make autocomplete less ridgid. still needs work<commit_after>/** @jsx React.DOM */

var React = require('react');
var _ = require('lodash');

var lastKeyDown;

module.exports = React.createClass({
  getInitialState: function() {
    var initialValue = this.props.value 
      ? this.props.value[this.props.valueLabel] : '';

    var fauxBest = {};
    fauxBest[this.props.valueLabel] = '';
    return { 
      bestMatch: fauxBest,
      value: initialValue
    }
  },

  setOption: function(option) {
    var bestMatch = this.getBestMatch(option[this.props.valueLabel]);
    if(bestMatch)
      this.setState({ 
        value: bestMatch[this.props.valueLabel], 
        bestMatch: bestMatch
      });
  },

  shouldComponentUpdate: function(nextProps, nextState) {
    return nextProps.value != nextState.bestMatch.id || this.state != nextState;
  },

  componentDidUpdate: function(nextProps, nextState) {
    var isMatched = _.find(this.props.options, function(option) {
      return option[this.props.valueLabel] == this.state.value;
    }, this);
    if(isMatched && typeof this.props.onSelect === 'function')
      this.props.onSelect(this.state.bestMatch);
  },

  handleBlur: function() {
    var DOMVal = this.getDOMNode().value;
    this.setState({ value: DOMVal });
  },

  handleKeyDown: function(e) {
    var node = this.refs.field.getDOMNode();
    var value = node.value;
    if(e.keyCode === 8) {
      lastKeyDown = 8;
      var best = {};
      best[this.props.valueLabel] =  this.state.value
      this.setState({ bestMatch: best });
      return;
    }
    if(e.keyCode === 13) {
      node.blur();
      e.preventDefault();
    }

  },

  handleChange: function(e) {
    var node = this.refs.field.getDOMNode();
    var value = node.value;
    value = value.slice(0, node.selectionStart);

    var bestMatch = {};
    bestMatch[this.props.valueLabel] = value;
    if(lastKeyDown === 8)
      lastKeyDown = undefined;
    else 
      bestMatch = this.getBestMatch(value);

    this.setState(
      { value: value, bestMatch: bestMatch },
      function() {
        this.getDOMNode().setSelectionRange(value.length, bestMatch[this.props.valueLabel].length);
        if(typeof this.props.onChange == 'function')
          this.props.onChange(value);
      }
    );
  },

  getBestMatch: function(value) {
    var bestMatch = {};
    bestMatch[this.props.valueLabel] = value;
    
    return _.find(this.props.options, function(option) { 
      if(option[this.props.valueLabel])
        return option[this.props.valueLabel].lastIndexOf(value, 0) === 0;
    }, this) || bestMatch;
  },

  render: function() {
    return <input ref='field'
          onBlur={this.handleBlur}
          type="text"
          className="form-control" id="newOrderAdvertiser" 
          onKeyDown={this.handleKeyDown}
          onChange={this.handleChange} value={this.state.bestMatch.advertiserName} />
  }

});

{'fraction_non_alphanumeric': 0.14387489139878368, 'fraction_numerical': 0.002780191138140747, 'mean_word_length': 2.855324849296718, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '497358', 'n_tokens_mistral': 1859, 'n_tokens_neox': 1808, 'n_words': 314}