fmoo / react-typeahead

Pure react-based typeahead and typeahead-tokenizer
ISC License
677 stars 230 forks source link

defaultValue not selecting the value, dropdown box remains open #151

Open benrlodge opened 8 years ago

benrlodge commented 8 years ago

When I use defaultValue if there is a fuzzy match, it does not select the matching value, but instead shows the matching options (as if you typed in the value). I may be able to look into submitting a PR if this isn't already being worked on.

benrlodge commented 8 years ago

actually, i'm seeing the drop-down remain open even if I have a single match. Not sure if this is by design, but I would prefer, or at least have the option, to have the default value pre-selected.

benrlodge commented 8 years ago

@fmoo not sure if it's the best approach, but I have something working where I check if this.props.defaultValue exists as well as a hasRendered flag (could be a better word for this...) which will be false on first run, then flip it true when the user first starts to interact with the input box again at _onTextEntryUpdated.

   // added:
  _renderIncrementalSearchResults: function() {
     // A default value was passed in
    if (this.props.defaultValue && !this.state.hasRendered) {
      return "";
    }
  _onTextEntryUpdated: function() {
    var value = this.refs.entry.getDOMNode().value;
    this.setState({visible: this.getOptionsForValue(value, this.props.options),
                   selection: null,
                   selectionIndex: 0,
                   hasRendered: true, // <-- added
                   entryValue: value});
  }
ragebiswas commented 8 years ago

+1 for this. @benrlodge is there any way without modifying the source code? :(

benrlodge commented 8 years ago

'fraid not from what I can tell. I'm working off a forked version in the mean time.

benrlodge commented 8 years ago

This could be a separate issue - but related - I'm also realizing it would be helpful to have some kind of currentValue option that can be accessed at componentWillReceiveProps, as I'm trying to implement routing with default values that would be pre-selected based on the routers params.

my bad. didnt see props.value

nosilleg commented 8 years ago

@benrlodge I'm coming to this from #176

What's the use case here? Is this something that Tokenizer defaultSelected doesn't solve? Are you only using Typeahead and not Tokenizer?

benrlodge commented 8 years ago

i'm only using Typeahead @nosilleg

As fmoo mentioned, it's the same issue noted here https://github.com/fmoo/react-typeahead/issues/21 (i just didn't discover that when posting this issue)