Skip to content
This repository was archived by the owner on Jul 28, 2021. It is now read-only.

Commit 0a8d585

Browse files
committed
feat(search): fixing issue with focus remaining on search results when search input should be edited
1 parent 1b69cd9 commit 0a8d585

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

lib/components/search.jsx

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ const libnpm = require('libnpm')
1313
const FOCUS_SEARCH = 'FOCUS_SEARCH'
1414
const FOCUS_RESULTS = 'FOCUS_RESULTS'
1515

16+
// @todo Re-typing search after matches have been found installs whatever is highlighted
17+
// @todo Make sure non-focused matches list looks identical, pretty sure it doesn't currently
18+
1619
class Search extends Component {
1720
constructor (props) {
1821
super(props)
@@ -129,17 +132,26 @@ class Search extends Component {
129132
selectedPackage
130133
} = this.state
131134

132-
// Handle up/down arrow press
135+
// If up/down arrows are pressed, make sure we're focused on search results
133136

134-
if (['up', 'down'].includes(key.name) && focusedOn === FOCUS_SEARCH && matches && matches.length) {
135-
this.setState({ focusedOn: FOCUS_RESULTS })
137+
if (['up', 'down'].includes(key.name)) {
138+
if (focusedOn !== FOCUS_RESULTS && matches && matches.length) {
139+
return this.setState({ focusedOn: FOCUS_RESULTS })
140+
}
136141
return
137142
}
138143

139-
// Handle enter press
144+
// If enter is pressed and we're focused on search results, install
140145

141146
if (key.name === 'enter' && focusedOn === FOCUS_RESULTS && selectedPackage) {
142-
this.install(selectedPackage)
147+
return this.install(selectedPackage)
148+
}
149+
150+
// If non up/down/enter keys were pressed and we're not focused on search
151+
// input, focus on search input
152+
153+
if (focusedOn !== FOCUS_SEARCH) {
154+
return this.setState({ focusedOn: FOCUS_SEARCH })
143155
}
144156
}
145157

@@ -223,7 +235,7 @@ const PackageSelector = ({ isFocused, matches, onSelect }) => {
223235
itemComponent={PackageItem}
224236
onSelect={({ value }) => onSelect(value)} />
225237
} else if (items && items.length) {
226-
return items.map(({ value }) => <div><PackageItem value={value} /></div>)
238+
return items.map(({ value }) => <div>{ ' ' }<PackageItem value={value} /></div>)
227239
}
228240
}
229241

0 commit comments

Comments
 (0)