/
Search.tsx
54 lines (50 loc) · 1.42 KB
/
Search.tsx
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
'use client'
import classes from '@/components/Search.module.css'
import Settings from '@/components/Settings'
import {useWeatherContext} from '@/components/WeatherProvider'
import {usePlaces} from '@/lib/hooks'
import {Autocomplete} from '@mantine/core'
import {useDebouncedValue} from '@mantine/hooks'
import {IconMapPin} from '@tabler/icons-react'
import {useState} from 'react'
/**
* Search component.
*/
export default function Search() {
const {location, setLocation} = useWeatherContext()
const [searchTerm, setSearchTerm] = useState(location)
const [debounced] = useDebouncedValue(searchTerm, 400)
const {locations} = usePlaces(debounced)
const places =
!!locations && locations.length > 0
? locations
: [
'New York, NY',
'Los Angeles, CA',
'Chicago, IL',
'Houston, TX',
'Phoenix, AZ',
'Philadelphia, PA',
'San Antonio, TX',
'San Diego, CA',
'Dallas, TX',
'San Jose, CA'
]
return (
<>
<Autocomplete
aria-label="Enter the name of your location"
className={classes.searchbar}
data={places}
leftSection={<IconMapPin />}
limit={10}
onChange={setSearchTerm}
onOptionSubmit={(item) => setLocation(item)}
placeholder="Enter the name of your location"
size="lg"
value={searchTerm}
/>
<Settings />
</>
)
}