Skip to content

Commit 8e8d457

Browse files
committed
fix: leave results open during typing
previously the check if results matched the input string happened during rendering, which meant that when results were shown and a new key was pressed they disappeared. this commit moves the check to the _setting_ of results so the results stay open.
1 parent 40c538b commit 8e8d457

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

src/autocomplete/autocomplete.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ export default ({
2424
environment = window
2525
}) => {
2626
const [results, setResults] = useState(emptyResults)
27-
const [searchTerm, setSearchTerm] = useState('')
2827
const inputRef = useRef()
2928

3029
// Geocode Earth Autocomplete Client
@@ -37,7 +36,7 @@ export default ({
3736
if (!text) return
3837

3938
autocomplete(text).then(({ features, discard }) => {
40-
if (discard) {
39+
if (discard || inputRef.current.value !== text) {
4140
return
4241
}
4342

@@ -53,7 +52,9 @@ export default ({
5352

5453
const onInputValueChange = ({ type, inputValue }) => {
5554
const term = inputValue.trim()
56-
setSearchTerm(term)
55+
if (term === '') {
56+
setResults(emptyResults)
57+
}
5758

5859
// call user-supplied onChange callback
5960
if (typeof userOnChange === 'function') {
@@ -102,7 +103,7 @@ export default ({
102103
onSelectedItemChange: onSelectItem
103104
})
104105

105-
const showResults = isOpen && searchTerm === results.text && results.features.length > 0
106+
const showResults = isOpen && results.features.length > 0
106107

107108
return <>
108109
<style>{css}</style>

0 commit comments

Comments
 (0)