react-typeahead icon indicating copy to clipboard operation
react-typeahead copied to clipboard

Reset or change Typeahead Value.

Open ritesh8467 opened this issue 10 years ago • 6 comments

Is there any callback where i can change the Input value of Typeahead?

Case: These is a search bar and a reset button. I want to reset the value of search bar on clicking reset button. So how can i approach this case.

ritesh8467 avatar Nov 24 '15 16:11 ritesh8467

:+1:

lipenco avatar Nov 30 '15 17:11 lipenco

You can force the state of the Typeahead:

this.refs.myInput.setState({
  entryValue: '',
  selection: null,
  selectionIndex: null,
  visible: []
});

lgiraudel avatar Dec 04 '15 23:12 lgiraudel

:+1:

cganas avatar Dec 15 '15 14:12 cganas

nice. Would be worth mentioning in the readme

barbalex avatar Dec 18 '15 21:12 barbalex

I went with replacing this method with a one-line change so that it definitely always pays attention to value when value is reset:

    typeahead.componentWillReceiveProps = function(nextProps) {
      this.setState({
        // Added this line.
        entryValue: nextProps.value,
        visible: this.getOptionsForValue(this.state.entryValue, nextProps.options)
      });
    };

You can do this with refs in the component including the typeahead. Somewhere in render():

<Typeahead
  // .. other props here ..
  value={ ... whatever value is tracked by here ... }

  ref={
    // Need to obtain a ref to this because it isn't behaving
    // completely correctly. We adjust things in
    // componentDidMount.
    (ref) => this._typeahead = ref
  }
/>

Then in componentDidMount():

componentDidMount () {
    this._typeahead.componentWillReceiveProps = function(nextProps) {
      this.setState({
        entryValue: nextProps.value,
        visible: this.getOptionsForValue(this.state.entryValue, nextProps.options)
      });
    };
  };

reason-bv avatar Dec 26 '15 17:12 reason-bv

Note that in the source code it says that entryValue should change name in the future and your code will explode :bomb:

birge avatar Feb 17 '16 00:02 birge