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

When props value changes, if it has only single option - populate the input with proper string #133

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions src/typeahead/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,20 +163,26 @@ var Typeahead = React.createClass({
return this.state.visible[index];
},

_onOptionSelected: function(option, event) {
var nEntry = this.refs.entry.getDOMNode();
nEntry.focus();

_setOptionValue: function(option) {
var displayOption = this._generateOptionToStringFor(this.props.displayOption);
var optionString = displayOption(option, 0);

var formInputOption = this._generateOptionToStringFor(this.props.formInputOption || displayOption);
var formInputOptionString = formInputOption(option);

nEntry.value = optionString;
this.setState({visible: this.getOptionsForValue(optionString, this.props.options),
selection: formInputOptionString,
entryValue: optionString});

this.refs.entry.getDOMNode().value = optionString;
},

_onOptionSelected: function(option, event) {
var nEntry = this.refs.entry.getDOMNode();
nEntry.focus();

this._setOptionValue(option);

return this.props.onOptionSelected(option, event);
},

Expand Down Expand Up @@ -281,9 +287,22 @@ var Typeahead = React.createClass({
},

componentWillReceiveProps: function(nextProps) {
var options = null;

this.setState({
visible: this.getOptionsForValue(this.state.entryValue, nextProps.options)
});

// The value prop is changed
if (nextProps.value !== this.props.value) {
options = this.getOptionsForValue(nextProps.value, nextProps.options);

// if we have only one option matching, we can choose it
if (options.length === 1) {
this._setOptionValue(options[0]);
}
}

},

render: function() {
Expand Down