fmoo / react-typeahead

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

Default option from list of objects? #114

Open ghost opened 9 years ago

ghost commented 9 years ago

If I provide an array of objects to Typeahead, like this:

var options = [{"id": "123", "name": "Hello"}, {"id": "456", "name": "world!"}];

How can I select a default/initial option when the options are an array of objects? If I provide the id as a defaultValue, the Typeahead field will render the id as text, but what I want to appear is the object's name as specified in "displayOption". I can't provide the name as a default value because it's possible that two unique objects with different IDs share the same name, and of course typeahead doesn't know which object I meant.

<Typeahead ref="typeahead"
               options={this.props.items}
               defaultValue={defaultVal.id}
               maxVisible={5}
               displayOption={(option, index) => {
                 return option.name;
               }}
               filterOption={"name"}
               formInputOption={"id"}
               onOptionSelected={this.handleChange} />

This is for a form to edit an existing entity where the user may not want to change their previous selection for this field.

mCzolko commented 9 years ago

did you found solution?