Skip to content

Commit

Permalink
move current location button inside input and allow immediate focus o…
Browse files Browse the repository at this point in the history
…f first input
  • Loading branch information
karussell committed Nov 30, 2024
1 parent 4718098 commit 8aa4dee
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 65 deletions.
11 changes: 11 additions & 0 deletions src/sidebar/search/AddressInput.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@
scale: 0.7;
}

.btnCurrentLocation {
padding: 0 7px 0 5px;
color: grey;
width: 32px;
}

.btnCurrentLocation > svg {
height: 100%;
width: 100%;
}

.btnClose {
display: none;
}
Expand Down
29 changes: 17 additions & 12 deletions src/sidebar/search/AddressInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,20 @@ import Autocomplete, {
AutocompleteItem,
GeocodingItem,
POIQueryItem,
SelectCurrentLocationItem,
} from '@/sidebar/search/AddressInputAutocomplete'

import ArrowBack from './arrow_back.svg'
import Cross from '@/sidebar/times-solid-thin.svg'
import CurrentLocationIcon from './current-location.svg'
import styles from './AddressInput.module.css'
import Api, { ApiImpl, getApi } from '@/api/Api'
import Api, { getApi } from '@/api/Api'
import { tr } from '@/translation/Translation'
import { coordinateToText, hitToItem, nominatimHitToItem, textToCoordinate } from '@/Converters'
import { useMediaQuery } from 'react-responsive'
import PopUp from '@/sidebar/search/PopUp'
import PlainButton from '@/PlainButton'
import { onCurrentLocationSelected } from '@/map/MapComponent'
import { toLonLat, transformExtent } from 'ol/proj'
import { calcDist } from '@/distUtils'
import { Map } from 'ol'
import { AddressParseResult } from '@/pois/AddressParseResult'
import { getMap } from '@/map/map'
Expand Down Expand Up @@ -79,11 +78,6 @@ export default function AddressInput(props: AddressInputProps) {

// if item is selected we need to clear the autocompletion list
useEffect(() => setAutocompleteItems([]), [props.point])
// if no items but input is selected show current location item
useEffect(() => {
if (hasFocus && text.length == 0 && autocompleteItems.length === 0)
setAutocompleteItems([new SelectCurrentLocationItem()])
}, [autocompleteItems, hasFocus])

function hideSuggestions() {
geocoder.cancel()
Expand Down Expand Up @@ -207,6 +201,7 @@ export default function AddressInput(props: AddressInputProps) {
style={props.moveStartIndex == props.index ? { borderWidth: '2px', margin: '-1px' } : {}}
className={styles.input}
type="text"
autoFocus={props.index == 0}
ref={searchInput}
autoComplete="off"
onChange={e => {
Expand All @@ -223,6 +218,7 @@ export default function AddressInput(props: AddressInputProps) {
if (origAutocompleteItems.length > 0) setAutocompleteItems(origAutocompleteItems)
}}
onBlur={() => {
setHasFocus(false)
if (!isSmallScreen) hideSuggestions() // see #398
}}
value={text}
Expand All @@ -234,7 +230,8 @@ export default function AddressInput(props: AddressInputProps) {
<PlainButton
style={text.length == 0 ? { display: 'none' } : {}}
className={styles.btnInputClear}
onClick={() => {
onMouseDown={(e) => {
e.preventDefault() // do not lose focus and close mobile-input view when clicked
setText('')
props.onChange('')
searchInput.current!.focus()
Expand All @@ -243,6 +240,17 @@ export default function AddressInput(props: AddressInputProps) {
<Cross />
</PlainButton>

<PlainButton
style={text.length == 0 && hasFocus ? {} : { display: 'none' }}
className={styles.btnCurrentLocation}
onMouseDown={(e) => {
// here it is desired to close mobile-input view when clicked
onCurrentLocationSelected(props.onAddressSelected)
}}
>
<CurrentLocationIcon />
</PlainButton>

{autocompleteItems.length > 0 && (
<ResponsiveAutocomplete
inputRef={searchInputContainer.current!}
Expand All @@ -257,9 +265,6 @@ export default function AddressInput(props: AddressInputProps) {
if (item instanceof GeocodingItem) {
hideSuggestions()
props.onAddressSelected(item.toText(), item.point)
} else if (item instanceof SelectCurrentLocationItem) {
hideSuggestions()
onCurrentLocationSelected(props.onAddressSelected)
} else if (item instanceof POIQueryItem) {
hideSuggestions()
handlePoiSearch(poiSearch, item.result, props.map)
Expand Down
17 changes: 0 additions & 17 deletions src/sidebar/search/AddressInputAutocomplete.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,6 @@
background-color: #c6c6c6;
}

.currentLocationEntry {
display: flex;
flex-direction: row;
align-items: center;
gap: 0.5rem;
margin: 0.5rem 0.5rem;
}

.currentLocationIcon {
width: 1.2rem;
}

.currentLocationIcon > svg {
height: 100%;
width: 100%;
}

.poiEntry {
padding: 0.5em 0;
display: flex;
Expand Down
27 changes: 0 additions & 27 deletions src/sidebar/search/AddressInputAutocomplete.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import styles from './AddressInputAutocomplete.module.css'
import CurrentLocationIcon from './current-location.svg'
import { tr } from '@/translation/Translation'
import { Bbox } from '@/api/graphhopper'
import { AddressParseResult } from '@/pois/AddressParseResult'

Expand All @@ -24,8 +22,6 @@ export class GeocodingItem implements AutocompleteItem {
}
}

export class SelectCurrentLocationItem implements AutocompleteItem {}

export class POIQueryItem implements AutocompleteItem {
result: AddressParseResult

Expand Down Expand Up @@ -55,8 +51,6 @@ export default function Autocomplete({ items, highlightedItem, onSelect }: Autoc
function mapToComponent(item: AutocompleteItem, isHighlighted: boolean, onSelect: (hit: AutocompleteItem) => void) {
if (item instanceof GeocodingItem)
return <GeocodingEntry item={item} isHighlighted={isHighlighted} onSelect={onSelect} />
else if (item instanceof SelectCurrentLocationItem)
return <SelectCurrentLocation item={item} isHighlighted={isHighlighted} onSelect={onSelect} />
else if (item instanceof POIQueryItem)
return <POIQueryEntry item={item} isHighlighted={isHighlighted} onSelect={onSelect} />
else throw Error('Unsupported item type: ' + typeof item)
Expand All @@ -82,27 +76,6 @@ export function POIQueryEntry({
)
}

export function SelectCurrentLocation({
item,
isHighlighted,
onSelect,
}: {
item: SelectCurrentLocationItem
isHighlighted: boolean
onSelect: (item: SelectCurrentLocationItem) => void
}) {
return (
<AutocompleteEntry isHighlighted={isHighlighted} onSelect={() => onSelect(item)}>
<div className={styles.currentLocationEntry}>
<div className={styles.currentLocationIcon}>
<CurrentLocationIcon />
</div>
<span className={styles.mainText}>{tr('current_location')}</span>
</div>
</AutocompleteEntry>
)
}

function GeocodingEntry({
item,
isHighlighted,
Expand Down
9 changes: 0 additions & 9 deletions src/sidebar/search/Search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,6 @@ const SearchBox = ({
}) => {
const point = points[index]

// With this ref and tabIndex=-1 we ensure that the first 'TAB' gives the focus the first input but the marker won't be included in the TAB sequence, #194
const myMarkerRef = useRef<HTMLDivElement>(null)

useEffect(() => {
if (index == 0) myMarkerRef.current?.focus()
}, [])

function onClickOrDrop() {
onDropPreviewSelect(-1)
const newIndex = moveStartIndex < index ? index + 1 : index
Expand All @@ -113,8 +106,6 @@ const SearchBox = ({
<>
{(moveStartIndex < 0 || moveStartIndex == index) && (
<div
ref={myMarkerRef}
tabIndex={-1}
title={tr('drag_to_reorder')}
className={styles.markerContainer}
draggable
Expand Down

0 comments on commit 8aa4dee

Please sign in to comment.