forked from OurHike/OurHike
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearch.tsx
More file actions
82 lines (74 loc) · 2.67 KB
/
Copy pathSearch.tsx
File metadata and controls
82 lines (74 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Search (WIREFRAMES.md Interactions, and `7c` for the empty state).
//
// Local only. There is no network path in this component or in
// lib/searchPoi.ts, and that is the design rather than an unfinished edge:
// search that needs signal fails exactly where someone needs it.
//
// The empty state is the part worth care. "No results" is a dead end;
// "that may exist just outside what you downloaded" tells someone what to do
// next. The component never suggests going online, because going online is
// not a thing that would help here.
import { useState } from 'react'
import { searchPois, type SearchablePoi } from '../lib/searchPoi'
import { typeLabel } from './legendLabels'
export interface SearchProps {
open: boolean
pois: SearchablePoi[]
onSelect: (poi: SearchablePoi) => void
onClose: () => void
}
export function Search({ open, pois, onSelect, onClose }: SearchProps) {
const [query, setQuery] = useState('')
if (!open) return null
const results = searchPois(query, pois)
const searched = query.trim() !== ''
return (
<div className="search">
<div className="search__bar">
<input
type="search"
className="search__input"
// The header has been taken over specifically so someone can type
// straight away.
autoFocus
value={query}
placeholder="Search shelters, water, towns"
aria-label="Search the downloaded map"
onChange={(event) => setQuery(event.target.value)}
/>
<button type="button" className="search__close" onClick={onClose}>
Cancel
</button>
</div>
{searched && results.length === 0 && (
<p className="search__empty">
Nothing here by that name. It may exist outside the part of the trail you
downloaded.
</p>
)}
{results.length > 0 && (
<ul className="search__results">
{results.map((poi) => (
<li key={poi.id} className="search__result">
<button
type="button"
className="search__result-button"
onClick={() => onSelect(poi)}
>
<span className="search__result-name">{poi.name}</span>
<span className="search__result-meta">
{poi.mile === undefined
? typeLabel(poi.type)
: `${typeLabel(poi.type)} · mi ${poi.mile.toLocaleString('en-US', {
minimumFractionDigits: 1,
maximumFractionDigits: 1,
})}`}
</span>
</button>
</li>
))}
</ul>
)}
</div>
)
}