Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

onClick isn't triggered for touch devices #364

Merged
merged 3 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 2 additions & 9 deletions src/sidebar/search/AddressInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ export default function AddressInput(props: AddressInputProps) {

// keep track of focus and toggle fullscreen display on small screens
const [hasFocus, setHasFocus] = useState(false)
const [pointerDownOnSuggestion, setPointerDownOnSuggestion] = useState(false)
const isSmallScreen = useMediaQuery({ query: '(max-width: 44rem)' })

// container for geocoding results which gets set by the geocoder class and set to empty if the underlying query point gets changed from outside
Expand Down Expand Up @@ -175,12 +174,10 @@ export default function AddressInput(props: AddressInputProps) {
props.clearDragDrop()
}}
onBlur={() => {
// Suppress onBlur if there was a click on a suggested item.
// Otherwise, the item would be removed before (hideSuggestions) its onclick handler can be called.
if (isSmallScreen || pointerDownOnSuggestion) return
// Suppress onBlur if we are on the small screen
if (isSmallScreen) return
setHasFocus(false)
hideSuggestions()
setPointerDownOnSuggestion(false)
}}
value={text}
placeholder={tr(
Expand All @@ -191,9 +188,6 @@ export default function AddressInput(props: AddressInputProps) {
<PlainButton
style={text.length == 0 ? { display: 'none' } : {}}
className={styles.btnInputClear}
onMouseDown={() => setPointerDownOnSuggestion(true)}
onMouseLeave={() => setPointerDownOnSuggestion(false)}
onMouseUp={() => setPointerDownOnSuggestion(false)}
onClick={() => {
setText('')
props.onChange('')
Expand All @@ -212,7 +206,6 @@ export default function AddressInput(props: AddressInputProps) {
<Autocomplete
items={autocompleteItems}
highlightedItem={autocompleteItems[highlightedResult]}
setPointerDown={setPointerDownOnSuggestion}
onSelect={item => {
setHasFocus(false)
if (item instanceof GeocodingItem) {
Expand Down
24 changes: 16 additions & 8 deletions src/sidebar/search/AddressInputAutocomplete.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,11 @@ export interface AutocompleteProps {
items: AutocompleteItem[]
highlightedItem: AutocompleteItem
onSelect: (hit: AutocompleteItem) => void
setPointerDown: (b: boolean) => void
}

export default function Autocomplete({ items, highlightedItem, onSelect, setPointerDown }: AutocompleteProps) {
export default function Autocomplete({ items, highlightedItem, onSelect }: AutocompleteProps) {
return (
<ul
onPointerDown={() => setPointerDown(true)}
onPointerUp={() => setPointerDown(false)}
onPointerLeave={() => setPointerDown(false)}
>
<ul>
{items.map((item, i) => (
<li key={i} className={styles.autocompleteItem}>
{mapToComponent(item, highlightedItem === item, onSelect)}
Expand Down Expand Up @@ -136,7 +131,20 @@ function AutocompleteEntry({
}) {
const className = isHighlighted ? styles.selectableItem + ' ' + styles.highlightedItem : styles.selectableItem
return (
<button className={className} onClick={() => onSelect()}>
<button
className={className}
// using click events for mouse interaction and touch end to select an entry.
onClick={() => onSelect()}
onTouchEnd={e => {
e.preventDefault()
onSelect()
}}

// prevent blur event for our input (seems to be only required for mouse)
onMouseDown={e => {
e.preventDefault()
}}
>
{children}
</button>
)
Expand Down