Skip to content

Commit 180902a

Browse files
authored
fix(searchbox): use initial input value if provided in the dom (#2342)
Fix #2289
1 parent f822466 commit 180902a

File tree

4 files changed

+35
-6
lines changed

4 files changed

+35
-6
lines changed

dev/app/init-builtin-widgets.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,19 @@ export default () => {
5151
})
5252
);
5353
})
54+
)
55+
.add(
56+
'input with initial value',
57+
wrapWithHits(container => {
58+
container.innerHTML = '<input value="ok"/>';
59+
const input = container.firstChild;
60+
container.appendChild(input);
61+
window.search.addWidget(
62+
instantsearch.widgets.searchBox({
63+
container: input,
64+
})
65+
);
66+
})
5467
);
5568

5669
storiesOf('Stats').add(

dev/app/wrap-with-hits.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export default (initWidget, opts = {}) => container => {
2626
container: '#results-search-box-container',
2727
placeholder: 'Search into our furnitures',
2828
poweredBy: false,
29+
autofocus: false,
2930
})
3031
);
3132

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -680,11 +680,12 @@ describe('searchBox()', () => {
680680
});
681681

682682
it('does not update the input value when focused', () => {
683-
container = document.body.appendChild(document.createElement('input'));
683+
const input = document.createElement('input');
684+
container = document.body.appendChild(input);
684685
container.value = 'initial';
685-
container.focus();
686686
widget = searchBox({ container });
687687
widget.init({ state, helper, onHistoryChange });
688+
input.focus();
688689
widget.render({ helper: { state: { query: 'new value' } } });
689690
expect(container.value).toBe('initial');
690691
});

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,38 @@ const renderer = ({
2929
const INPUT_EVENT = window.addEventListener ? 'input' : 'propertychange';
3030
const input = createInput(containerNode);
3131
const isInputTargeted = input === containerNode;
32+
let queryFromInput = query;
33+
3234
if (isInputTargeted) {
3335
// To replace the node, we need to create an intermediate node
3436
const placeholderNode = document.createElement('div');
3537
input.parentNode.insertBefore(placeholderNode, input);
3638
const parentNode = input.parentNode;
3739
const wrappedInput = wrapInput ? wrapInputFn(input, cssClasses) : input;
3840
parentNode.replaceChild(wrappedInput, placeholderNode);
41+
42+
const initialInputValue = input.value;
43+
44+
// if the input contains a value, we provide it to the state
45+
if (initialInputValue) {
46+
queryFromInput = initialInputValue;
47+
refine(initialInputValue, false);
48+
}
3949
} else {
4050
const wrappedInput = wrapInput ? wrapInputFn(input, cssClasses) : input;
4151
containerNode.appendChild(wrappedInput);
4252
}
53+
4354
if (magnifier) addMagnifier(input, magnifier, templates);
4455
if (reset) addReset(input, reset, templates, clear);
45-
addDefaultAttributesToInput(placeholder, input, query, cssClasses);
56+
57+
addDefaultAttributesToInput(placeholder, input, queryFromInput, cssClasses);
58+
4659
// Optional "powered by Algolia" widget
4760
if (poweredBy) {
4861
addPoweredBy(input, poweredBy, templates);
4962
}
63+
5064
// When the page is coming from BFCache
5165
// (https://developer.mozilla.org/en-US/docs/Working_with_BFCache)
5266
// then we force the input value to be the current query
@@ -57,17 +71,17 @@ const renderer = ({
5771
// - use back button
5872
// - input query is empty (because <input> autocomplete = off)
5973
window.addEventListener('pageshow', () => {
60-
input.value = query;
74+
input.value = queryFromInput;
6175
});
6276

6377
// Update value when query change outside of the input
6478
onHistoryChange(fullState => {
6579
input.value = fullState.query || '';
6680
});
6781

68-
if (autofocus === true || (autofocus === 'auto' && query === '')) {
82+
if (autofocus === true || (autofocus === 'auto' && queryFromInput === '')) {
6983
input.focus();
70-
input.setSelectionRange(query.length, query.length);
84+
input.setSelectionRange(queryFromInput.length, queryFromInput.length);
7185
}
7286

7387
// search on enter

0 commit comments

Comments
 (0)