Skip to content

Commit 7f7844a

Browse files
committed
use onMouseDown for preventDefault but not for the action as this feels unnatural; fix bug when 'ESC' is pressed
1 parent f1da724 commit 7f7844a

File tree

1 file changed

+12
-16
lines changed

1 file changed

+12
-16
lines changed

src/sidebar/search/AddressInput.tsx

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,8 @@ export default function AddressInput(props: AddressInputProps) {
100100
(event: React.KeyboardEvent<HTMLInputElement>) => {
101101
const inputElement = event.target as HTMLInputElement
102102
if (event.key === 'Escape') {
103-
// onBlur is deactivated for mobile so force:
104-
setHasFocus(false)
105103
setText(origText)
106-
hideSuggestions()
104+
searchInput.current!.blur()
107105
return
108106
}
109107

@@ -157,9 +155,6 @@ export default function AddressInput(props: AddressInputProps) {
157155
props.onAddressSelected(item.toText(), item.point)
158156
}
159157
}
160-
// onBlur is deactivated for mobile so force:
161-
setHasFocus(false)
162-
hideSuggestions()
163158
break
164159
}
165160
},
@@ -193,10 +188,7 @@ export default function AddressInput(props: AddressInputProps) {
193188
>
194189
<PlainButton
195190
className={styles.btnClose}
196-
onClick={() => {
197-
setHasFocus(false)
198-
hideSuggestions()
199-
}}
191+
onClick={() => searchInput.current!.blur()}
200192
>
201193
<ArrowBack />
202194
</PlainButton>
@@ -233,9 +225,10 @@ export default function AddressInput(props: AddressInputProps) {
233225
<PlainButton
234226
style={text.length == 0 ? { display: 'none' } : {}}
235227
className={styles.btnInputClear}
236-
// no onClick because otherwise focus would be lost before button receives click
237-
onMouseDown={(e) => {
238-
e.preventDefault() // do not lose focus and close mobile-input view when clicked
228+
onMouseDown={(e) =>
229+
e.preventDefault() // prevents that input->onBlur is called when clicking the button
230+
}
231+
onClick={(e) => {
239232
setText('')
240233
props.onChange('')
241234
}}
@@ -246,10 +239,13 @@ export default function AddressInput(props: AddressInputProps) {
246239
<PlainButton
247240
style={text.length == 0 && hasFocus ? {} : { display: 'none' }}
248241
className={styles.btnCurrentLocation}
249-
// no onClick because otherwise focus would be lost before button receives click
250-
onMouseDown={(e) => {
251-
// here it is desired to close mobile-input view when clicked -> no "e.preventDefault()"
242+
onMouseDown={(e) =>
243+
e.preventDefault() // prevents that input->onBlur is called when clicking the button
244+
}
245+
onClick={() => {
252246
onCurrentLocationSelected(props.onAddressSelected)
247+
// but when clicked => close mobile-input view
248+
searchInput.current!.blur()
253249
}}
254250
>
255251
<CurrentLocationIcon />

0 commit comments

Comments
 (0)