Skip to content

Commit 6a0af14

Browse files
author
vvo
committed
fix(searchBox): handle external updates of the query
fixes #803
1 parent 4bdaa12 commit 6a0af14

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/widgets/search-box/__tests__/search-box-test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,16 @@ describe('searchBox()', () => {
351351
expect(container.value).toBe('iphone');
352352
});
353353

354+
355+
it('handles external updates', () => {
356+
container = document.body.appendChild(document.createElement('input'));
357+
container.value = 'initial';
358+
widget = searchBox({container});
359+
widget.init({state, helper, onHistoryChange});
360+
widget.render({helper: {state: {query: 'new value'}}});
361+
expect(container.value).toBe('new value');
362+
});
363+
354364
context('autofocus', () => {
355365
beforeEach(() => {
356366
container = document.body.appendChild(document.createElement('input'));

src/widgets/search-box/search-box.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ function searchBox({
135135
},
136136
init: function({state, helper, onHistoryChange}) {
137137
let isInputTargeted = container.tagName === 'INPUT';
138-
let input = this.getInput();
138+
let input = this._input = this.getInput();
139139

140140
// Add all the needed attributes and listeners to the input
141141
this.addDefaultAttributesToInput(input, state.query);
@@ -193,6 +193,13 @@ function searchBox({
193193
if (autofocus === true || autofocus === 'auto' && helper.state.query === '') {
194194
input.focus();
195195
}
196+
},
197+
render({helper}) {
198+
// updating the query from the outside using the helper
199+
// will fall in this case
200+
if (helper.state.query !== this._input.value) {
201+
this._input.value = helper.state.query;
202+
}
196203
}
197204
};
198205
}

0 commit comments

Comments
 (0)