Skip to content

Commit 8aa4dee

Browse files
committed
move current location button inside input and allow immediate focus of first input
1 parent 4718098 commit 8aa4dee

File tree

5 files changed

+28
-65
lines changed

5 files changed

+28
-65
lines changed

src/sidebar/search/AddressInput.module.css

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,17 @@
55
scale: 0.7;
66
}
77

8+
.btnCurrentLocation {
9+
padding: 0 7px 0 5px;
10+
color: grey;
11+
width: 32px;
12+
}
13+
14+
.btnCurrentLocation > svg {
15+
height: 100%;
16+
width: 100%;
17+
}
18+
819
.btnClose {
920
display: none;
1021
}

src/sidebar/search/AddressInput.tsx

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,20 @@ import Autocomplete, {
55
AutocompleteItem,
66
GeocodingItem,
77
POIQueryItem,
8-
SelectCurrentLocationItem,
98
} from '@/sidebar/search/AddressInputAutocomplete'
109

1110
import ArrowBack from './arrow_back.svg'
1211
import Cross from '@/sidebar/times-solid-thin.svg'
12+
import CurrentLocationIcon from './current-location.svg'
1313
import styles from './AddressInput.module.css'
14-
import Api, { ApiImpl, getApi } from '@/api/Api'
14+
import Api, { getApi } from '@/api/Api'
1515
import { tr } from '@/translation/Translation'
1616
import { coordinateToText, hitToItem, nominatimHitToItem, textToCoordinate } from '@/Converters'
1717
import { useMediaQuery } from 'react-responsive'
1818
import PopUp from '@/sidebar/search/PopUp'
1919
import PlainButton from '@/PlainButton'
2020
import { onCurrentLocationSelected } from '@/map/MapComponent'
2121
import { toLonLat, transformExtent } from 'ol/proj'
22-
import { calcDist } from '@/distUtils'
2322
import { Map } from 'ol'
2423
import { AddressParseResult } from '@/pois/AddressParseResult'
2524
import { getMap } from '@/map/map'
@@ -79,11 +78,6 @@ export default function AddressInput(props: AddressInputProps) {
7978

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

8882
function hideSuggestions() {
8983
geocoder.cancel()
@@ -207,6 +201,7 @@ export default function AddressInput(props: AddressInputProps) {
207201
style={props.moveStartIndex == props.index ? { borderWidth: '2px', margin: '-1px' } : {}}
208202
className={styles.input}
209203
type="text"
204+
autoFocus={props.index == 0}
210205
ref={searchInput}
211206
autoComplete="off"
212207
onChange={e => {
@@ -223,6 +218,7 @@ export default function AddressInput(props: AddressInputProps) {
223218
if (origAutocompleteItems.length > 0) setAutocompleteItems(origAutocompleteItems)
224219
}}
225220
onBlur={() => {
221+
setHasFocus(false)
226222
if (!isSmallScreen) hideSuggestions() // see #398
227223
}}
228224
value={text}
@@ -234,7 +230,8 @@ export default function AddressInput(props: AddressInputProps) {
234230
<PlainButton
235231
style={text.length == 0 ? { display: 'none' } : {}}
236232
className={styles.btnInputClear}
237-
onClick={() => {
233+
onMouseDown={(e) => {
234+
e.preventDefault() // do not lose focus and close mobile-input view when clicked
238235
setText('')
239236
props.onChange('')
240237
searchInput.current!.focus()
@@ -243,6 +240,17 @@ export default function AddressInput(props: AddressInputProps) {
243240
<Cross />
244241
</PlainButton>
245242

243+
<PlainButton
244+
style={text.length == 0 && hasFocus ? {} : { display: 'none' }}
245+
className={styles.btnCurrentLocation}
246+
onMouseDown={(e) => {
247+
// here it is desired to close mobile-input view when clicked
248+
onCurrentLocationSelected(props.onAddressSelected)
249+
}}
250+
>
251+
<CurrentLocationIcon />
252+
</PlainButton>
253+
246254
{autocompleteItems.length > 0 && (
247255
<ResponsiveAutocomplete
248256
inputRef={searchInputContainer.current!}
@@ -257,9 +265,6 @@ export default function AddressInput(props: AddressInputProps) {
257265
if (item instanceof GeocodingItem) {
258266
hideSuggestions()
259267
props.onAddressSelected(item.toText(), item.point)
260-
} else if (item instanceof SelectCurrentLocationItem) {
261-
hideSuggestions()
262-
onCurrentLocationSelected(props.onAddressSelected)
263268
} else if (item instanceof POIQueryItem) {
264269
hideSuggestions()
265270
handlePoiSearch(poiSearch, item.result, props.map)

src/sidebar/search/AddressInputAutocomplete.module.css

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,6 @@
3030
background-color: #c6c6c6;
3131
}
3232

33-
.currentLocationEntry {
34-
display: flex;
35-
flex-direction: row;
36-
align-items: center;
37-
gap: 0.5rem;
38-
margin: 0.5rem 0.5rem;
39-
}
40-
41-
.currentLocationIcon {
42-
width: 1.2rem;
43-
}
44-
45-
.currentLocationIcon > svg {
46-
height: 100%;
47-
width: 100%;
48-
}
49-
5033
.poiEntry {
5134
padding: 0.5em 0;
5235
display: flex;

src/sidebar/search/AddressInputAutocomplete.tsx

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
import styles from './AddressInputAutocomplete.module.css'
2-
import CurrentLocationIcon from './current-location.svg'
3-
import { tr } from '@/translation/Translation'
42
import { Bbox } from '@/api/graphhopper'
53
import { AddressParseResult } from '@/pois/AddressParseResult'
64

@@ -24,8 +22,6 @@ export class GeocodingItem implements AutocompleteItem {
2422
}
2523
}
2624

27-
export class SelectCurrentLocationItem implements AutocompleteItem {}
28-
2925
export class POIQueryItem implements AutocompleteItem {
3026
result: AddressParseResult
3127

@@ -55,8 +51,6 @@ export default function Autocomplete({ items, highlightedItem, onSelect }: Autoc
5551
function mapToComponent(item: AutocompleteItem, isHighlighted: boolean, onSelect: (hit: AutocompleteItem) => void) {
5652
if (item instanceof GeocodingItem)
5753
return <GeocodingEntry item={item} isHighlighted={isHighlighted} onSelect={onSelect} />
58-
else if (item instanceof SelectCurrentLocationItem)
59-
return <SelectCurrentLocation item={item} isHighlighted={isHighlighted} onSelect={onSelect} />
6054
else if (item instanceof POIQueryItem)
6155
return <POIQueryEntry item={item} isHighlighted={isHighlighted} onSelect={onSelect} />
6256
else throw Error('Unsupported item type: ' + typeof item)
@@ -82,27 +76,6 @@ export function POIQueryEntry({
8276
)
8377
}
8478

85-
export function SelectCurrentLocation({
86-
item,
87-
isHighlighted,
88-
onSelect,
89-
}: {
90-
item: SelectCurrentLocationItem
91-
isHighlighted: boolean
92-
onSelect: (item: SelectCurrentLocationItem) => void
93-
}) {
94-
return (
95-
<AutocompleteEntry isHighlighted={isHighlighted} onSelect={() => onSelect(item)}>
96-
<div className={styles.currentLocationEntry}>
97-
<div className={styles.currentLocationIcon}>
98-
<CurrentLocationIcon />
99-
</div>
100-
<span className={styles.mainText}>{tr('current_location')}</span>
101-
</div>
102-
</AutocompleteEntry>
103-
)
104-
}
105-
10679
function GeocodingEntry({
10780
item,
10881
isHighlighted,

src/sidebar/search/Search.tsx

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,6 @@ const SearchBox = ({
9292
}) => {
9393
const point = points[index]
9494

95-
// 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
96-
const myMarkerRef = useRef<HTMLDivElement>(null)
97-
98-
useEffect(() => {
99-
if (index == 0) myMarkerRef.current?.focus()
100-
}, [])
101-
10295
function onClickOrDrop() {
10396
onDropPreviewSelect(-1)
10497
const newIndex = moveStartIndex < index ? index + 1 : index
@@ -113,8 +106,6 @@ const SearchBox = ({
113106
<>
114107
{(moveStartIndex < 0 || moveStartIndex == index) && (
115108
<div
116-
ref={myMarkerRef}
117-
tabIndex={-1}
118109
title={tr('drag_to_reorder')}
119110
className={styles.markerContainer}
120111
draggable

0 commit comments

Comments
 (0)