Skip to content

Commit 61cf9be

Browse files
author
vvo
committed
fix(searchBox): do not update the input when focused
When we have an external update: - search.helper.setQuery - debounce option used on searchBox (resulting in an "external" update: search happens after some time) Then if the input is focused (user typing), we should not try to update it. It will always result in a bad behavior. Thus, it means we do not handle use cases like "When the user is focused, I want to programmatically update is query". If you want to do that, you would have to blur the input, update, focus. But that's an edge case and bad UX I believe. fixes #944
1 parent e83db4a commit 61cf9be

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

dev/app.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,4 +349,3 @@ search.once('render', function() {
349349
});
350350

351351
search.start();
352-

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,10 +357,21 @@ describe('searchBox()', () => {
357357
container.value = 'initial';
358358
widget = searchBox({container});
359359
widget.init({state, helper, onHistoryChange});
360+
container.blur();
360361
widget.render({helper: {state: {query: 'new value'}}});
361362
expect(container.value).toBe('new value');
362363
});
363364

365+
it('does not update the input value when focused', () => {
366+
container = document.body.appendChild(document.createElement('input'));
367+
container.value = 'initial';
368+
container.focus();
369+
widget = searchBox({container});
370+
widget.init({state, helper, onHistoryChange});
371+
widget.render({helper: {state: {query: 'new value'}}});
372+
expect(container.value).toBe('initial');
373+
});
374+
364375
context('autofocus', () => {
365376
beforeEach(() => {
366377
container = document.body.appendChild(document.createElement('input'));
@@ -440,4 +451,3 @@ describe('searchBox()', () => {
440451
});
441452
});
442453
});
443-

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,11 @@ function searchBox({
197197
render({helper}) {
198198
// updating the query from the outside using the helper
199199
// will fall in this case
200+
// If the input is focused, we do not update it.
201+
if (document.activeElement === this._input) {
202+
return;
203+
}
204+
200205
if (helper.state.query !== this._input.value) {
201206
this._input.value = helper.state.query;
202207
}

0 commit comments

Comments
 (0)