Skip to content

Commit

Permalink
Merge pull request algolia#949 from algolia/fix/searchBox/do-not-upda…
Browse files Browse the repository at this point in the history
…te-when-focused

fix(searchBox): do not update the input when focused
  • Loading branch information
Vincent Voyer committed Mar 22, 2016
2 parents e83db4a + 61cf9be commit 94969f3
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
1 change: 0 additions & 1 deletion dev/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -349,4 +349,3 @@ search.once('render', function() {
});

search.start();

12 changes: 11 additions & 1 deletion src/widgets/search-box/__tests__/search-box-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -357,10 +357,21 @@ describe('searchBox()', () => {
container.value = 'initial';
widget = searchBox({container});
widget.init({state, helper, onHistoryChange});
container.blur();
widget.render({helper: {state: {query: 'new value'}}});
expect(container.value).toBe('new value');
});

it('does not update the input value when focused', () => {
container = document.body.appendChild(document.createElement('input'));
container.value = 'initial';
container.focus();
widget = searchBox({container});
widget.init({state, helper, onHistoryChange});
widget.render({helper: {state: {query: 'new value'}}});
expect(container.value).toBe('initial');
});

context('autofocus', () => {
beforeEach(() => {
container = document.body.appendChild(document.createElement('input'));
Expand Down Expand Up @@ -440,4 +451,3 @@ describe('searchBox()', () => {
});
});
});

5 changes: 5 additions & 0 deletions src/widgets/search-box/search-box.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,11 @@ function searchBox({
render({helper}) {
// updating the query from the outside using the helper
// will fall in this case
// If the input is focused, we do not update it.
if (document.activeElement === this._input) {
return;
}

if (helper.state.query !== this._input.value) {
this._input.value = helper.state.query;
}
Expand Down

0 comments on commit 94969f3

Please sign in to comment.