Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reset or change Typeahead Value. #143

Open
ritesh8467 opened this issue Nov 24, 2015 · 6 comments
Open

Reset or change Typeahead Value. #143

ritesh8467 opened this issue Nov 24, 2015 · 6 comments

Comments

@ritesh8467
Copy link

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.

@lipenco
Copy link

lipenco commented Nov 30, 2015

👍

@lgiraudel
Copy link

You can force the state of the Typeahead:

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

@cganas
Copy link

cganas commented Dec 15, 2015

👍

@barbalex
Copy link

nice. Would be worth mentioning in the readme

@reason-bv
Copy link

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)
      });
    };
  };

@birge
Copy link

birge commented Feb 17, 2016

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants