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

Fix issue 128 where the autocomplete was one step behind the input #146

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-typeahead",
"version": "1.1.5",
"version": "1.1.6",
"description": "React-based typeahead and typeahead-tokenizer",
"keywords": [
"react",
Expand Down
31 changes: 12 additions & 19 deletions src/typeahead/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,6 @@ var Typeahead = React.createClass({

getInitialState: function() {
return {
// The currently visible set of options
visible: this.getOptionsForValue(this.props.defaultValue, this.props.options),

// This should be called something else, "entryValue"
entryValue: this.props.value || this.props.defaultValue,

Expand All @@ -105,6 +102,10 @@ var Typeahead = React.createClass({
return result;
},

visibleOptions: function(){
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be getVisibleOptions() for consistency with other accessors ?

Also, whitespace between () and {

return this.getOptionsForValue(this.state.entryValue, this.props.options);
},

setEntryText: function(value) {
this.refs.entry.value = value;
this._onTextEntryUpdated();
Expand All @@ -117,7 +118,7 @@ var Typeahead = React.createClass({
_hasCustomValue: function() {
if (this.props.allowCustomValues > 0 &&
this.state.entryValue.length >= this.props.allowCustomValues &&
this.state.visible.indexOf(this.state.entryValue) < 0) {
this.visibleOptions().indexOf(this.state.entryValue) < 0) {
return true;
}
return false;
Expand All @@ -143,7 +144,7 @@ var Typeahead = React.createClass({

return (
<this.props.customListComponent
ref="sel" options={this.state.visible}
ref="sel" options={this.visibleOptions()}
onOptionSelected={this._onOptionSelected}
customValue={this._getCustomValue()}
customClasses={this.props.customClasses}
Expand All @@ -162,7 +163,7 @@ var Typeahead = React.createClass({
index--;
}
}
return this.state.visible[index];
return this.visibleOptions()[index];
},

_onOptionSelected: function(option, event) {
Expand All @@ -176,16 +177,14 @@ var Typeahead = React.createClass({
var formInputOptionString = formInputOption(option);

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

_onTextEntryUpdated: function() {
var value = this.refs.entry.value;
this.setState({visible: this.getOptionsForValue(value, this.props.options),
selection: null,
this.setState({selection: null,
entryValue: value});
},

Expand All @@ -206,7 +205,7 @@ var Typeahead = React.createClass({
_onTab: function(event) {
var selection = this.getSelection();
var option = selection ?
selection : (this.state.visible.length > 0 ? this.state.visible[0] : null);
selection : (this.visibleOptions().length > 0 ? this.visibleOptions()[0] : null);

if (option === null && this._hasCustomValue()) {
option = this._getCustomValue();
Expand Down Expand Up @@ -234,7 +233,7 @@ var Typeahead = React.createClass({
return;
}
var newIndex = this.state.selectionIndex === null ? (delta == 1 ? 0 : delta) : this.state.selectionIndex + delta;
var length = this.state.visible.length;
var length = this.visibleOptions().length;
if (this._hasCustomValue()) {
length += 1;
}
Expand Down Expand Up @@ -283,12 +282,6 @@ var Typeahead = React.createClass({
event.preventDefault();
},

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

render: function() {
var inputClasses = {};
inputClasses[this.props.customClasses.input] = !!this.props.customClasses.input;
Expand Down Expand Up @@ -368,7 +361,7 @@ var Typeahead = React.createClass({
},

_hasHint: function() {
return this.state.visible.length > 0 || this._hasCustomValue();
return this.visibleOptions().length > 0 || this._hasCustomValue();
}
});

Expand Down
46 changes: 46 additions & 0 deletions test/typeahead-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -564,4 +564,50 @@ describe('Typeahead Component', function() {
});
})
});

context("issue-128", function(){
var WrappingComponent = React.createClass({
getInitialState: function(){
return {
value: "",
options: [
{value: "a"},
{value: "ab"},
{value: "abc"},
{value: "other"}
]
};
},
onChange: function(event) {
var newState = {
value: event.target.value,
options: this.state.options
};
this.setState(newState);
},
render: function() {
return (<Typeahead
filterOption="value"
displayOption="value"
options={this.state.options}
onChange={this.onChange}
/>);
}
});

beforeEach(function() {
this.wrappingComponent = TestUtils.renderIntoDocument(
<WrappingComponent />
);
this.component = TestUtils.findRenderedComponentWithType(
this.wrappingComponent,
Typeahead
);
});

it("should not autocomplete one step behind the input", function() {
var results = simulateTextInput(this.component, "a");
assert.equal(results.length, 3);
});
});
});