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 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
44 changes: 44 additions & 0 deletions benchmark.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
var jsdom = require("jsdom");
global.document = jsdom.jsdom('<!doctype html><html><body></body></html>');
global.window = document.defaultView;
global.navigator = window.navigator;


var Benchmark = require('benchmark');
var React = require('react');
var TestUtils = require("react/addons").addons.TestUtils;
global.assert = require('chai').assert;

// A super simple DOM ready for React to render into

var ReactTypeahead = require('./lib/react-typeahead').Typeahead;
var TypeaheadOption = require("./lib/typeahead/option");

global.simulateTextInput = function(component, value) {
var node = component.refs.entry;
node.value = value;
TestUtils.Simulate.change(node);
return TestUtils.scryRenderedComponentsWithType(component, TypeaheadOption);
};

var props = {
options: [
"a",
"ab",
"dfe",
],
};
global.component = TestUtils.renderIntoDocument(React.createElement(ReactTypeahead, props));

console.log("running benchmark");

var suite = new Benchmark.Suite();
suite.add("Filtering options", function() {
var results = simulateTextInput(component, "a");
assert.equal(results.length, 2);
simulateTextInput(component, "");
}).on('complete', function() {
console.log(this["0"].stats.mean);
}).run({async: false});

console.log("done");
7 changes: 5 additions & 2 deletions 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 Expand Up @@ -40,12 +40,14 @@
},
"main": "lib/react-typeahead.js",
"devDependencies": {
"benchmark": "^1.0.0",
"browserify": "^8.0.2",
"chai": "^1.9.1",
"es5-shim": "^4.0.1",
"gulp": "^3.8.7",
"gulp-mocha-phantomjs": "^0.8.1",
"gulp-react": "^3.0.1",
"jsdom": "^7.2.1",
"literalify": "^0.4.0",
"lodash": "^2.4.1",
"mocha": "^1.21.4",
Expand All @@ -61,7 +63,8 @@
"build": "browserify ./src/react-typeahead.js -t reactify -t literalify -x react -s ReactTypeahead -o ./dist/react-typeahead.js",
"watchify": "watchify ./src/react-typeahead.js -t reactify -t literalify -x react -s ReactTypeahead -o ./dist/react-typeahead.js",
"lib": "gulp build",
"prepublish": "npm run lib"
"prepublish": "npm run lib",
"benchmark": "node ./benchmark.js"
},
"literalify": {
"react": "window.React || require('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;
},

getVisibleOptions: function() {
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.getVisibleOptions().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.getVisibleOptions()}
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.getVisibleOptions()[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.getVisibleOptions().length > 0 ? this.getVisibleOptions()[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.getVisibleOptions().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.getVisibleOptions().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);
});
});
});