Skip to content

Commit

Permalink
fix(searchBox): handle external updates of the query
Browse files Browse the repository at this point in the history
fixes #803
  • Loading branch information
vvo committed Mar 2, 2016
1 parent 4bdaa12 commit 6a0af14
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/widgets/search-box/__tests__/search-box-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,16 @@ describe('searchBox()', () => {
expect(container.value).toBe('iphone');
});


it('handles external updates', () => {
container = document.body.appendChild(document.createElement('input'));
container.value = 'initial';
widget = searchBox({container});
widget.init({state, helper, onHistoryChange});
widget.render({helper: {state: {query: 'new value'}}});
expect(container.value).toBe('new value');
});

context('autofocus', () => {
beforeEach(() => {
container = document.body.appendChild(document.createElement('input'));
Expand Down
9 changes: 8 additions & 1 deletion src/widgets/search-box/search-box.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ function searchBox({
},
init: function({state, helper, onHistoryChange}) {
let isInputTargeted = container.tagName === 'INPUT';
let input = this.getInput();
let input = this._input = this.getInput();

// Add all the needed attributes and listeners to the input
this.addDefaultAttributesToInput(input, state.query);
Expand Down Expand Up @@ -193,6 +193,13 @@ function searchBox({
if (autofocus === true || autofocus === 'auto' && helper.state.query === '') {
input.focus();
}
},
render({helper}) {
// updating the query from the outside using the helper
// will fall in this case
if (helper.state.query !== this._input.value) {
this._input.value = helper.state.query;
}
}
};
}
Expand Down

0 comments on commit 6a0af14

Please sign in to comment.