Skip to content

Commit

Permalink
fix(searchBox): stop updating query at eachkeystroke with searchOnEnt…
Browse files Browse the repository at this point in the history
…erKeyPressOnly

fixes #875
  • Loading branch information
vvo committed Feb 29, 2016
1 parent 86ef50a commit 28dc4d2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
10 changes: 8 additions & 2 deletions src/widgets/search-box/__tests__/search-box-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ describe('searchBox()', () => {

it('does not performs (will be handle by keyup event)', () => {
simulateInputEvent();
expect(helper.setQuery.calledOnce).toBe(true);
expect(helper.setQuery.calledOnce).toBe(false);
expect(helper.search.called).toBe(false);
});
});
Expand Down Expand Up @@ -289,14 +289,20 @@ describe('searchBox()', () => {
widget.getInput = sinon.stub().returns(input);
});

it('sets the query on keyup if <ENTER>', () => {
simulateKeyUpEvent({keyCode: 13});
expect(helper.setQuery.calledOnce).toBe(true);
});

it('performs the search on keyup if <ENTER>', () => {
simulateKeyUpEvent({keyCode: 13});
expect(helper.search.calledOnce).toBe(true);
});

it('doesn\'t perform the search on keyup if not <ENTER>', () => {
simulateKeyUpEvent({});
expect(helper.search.calledOnce).toBe(false);
expect(helper.setQuery.called).toBe(false);
expect(helper.search.called).toBe(false);
});
});
});
Expand Down
22 changes: 11 additions & 11 deletions src/widgets/search-box/search-box.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,21 +135,21 @@ function searchBox({
// Add all the needed attributes and listeners to the input
this.addDefaultAttributesToInput(input, state.query);

// always set the query on every change
addListener(input, INPUT_EVENT, setQuery);

// handle IE8 weirdness where BACKSPACE key will not trigger an input change..
// can be removed as soon as we remove support for it
if (INPUT_EVENT === 'propertychange' || window.attachEvent) {
addListener(input, 'keyup', ifKey(KEY_SUPPRESS, setQuery));
addListener(input, 'keyup', ifKey(KEY_SUPPRESS, search));
}

// only search on enter
// only update query and search on enter
if (searchOnEnterKeyPressOnly) {
addListener(input, 'keyup', ifKey(KEY_ENTER, setQuery));
addListener(input, 'keyup', ifKey(KEY_ENTER, search));
} else {
// always set the query and search on every keystrokes
addListener(input, INPUT_EVENT, setQuery);
addListener(input, INPUT_EVENT, search);

// handle IE8 weirdness where BACKSPACE key will not trigger an input change..
// can be removed as soon as we remove support for it
if (INPUT_EVENT === 'propertychange' || window.attachEvent) {
addListener(input, 'keyup', ifKey(KEY_SUPPRESS, setQuery));
addListener(input, 'keyup', ifKey(KEY_SUPPRESS, search));
}
}

function setQuery(e) {
Expand Down

0 comments on commit 28dc4d2

Please sign in to comment.