-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathDropdown.js
More file actions
183 lines (175 loc) · 4.92 KB
/
Dropdown.js
File metadata and controls
183 lines (175 loc) · 4.92 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import React, { useEffect, useRef, useState } from 'react'
import { array, func, string } from 'prop-types'
import { Box, Text, Input, InputGroup, InputRightElement, Flex, InputLeftElement } from '@chakra-ui/react'
import { ChevronDownIcon, ChevronUpIcon, SearchIcon } from '@chakra-ui/icons'
export function Dropdown({ label, labelDirection, options, placeholder, onChange, searchValue, onSearch, ...props }) {
const [open, setOpen] = useState(false)
const inputRef = useRef(null)
const optionRefs = []
useEffect(() => {
document.addEventListener('click', close)
return () => document.removeEventListener('click', close)
}, [])
function close(e) {
setOpen(e && e.target === inputRef.current)
}
const setOptRef = (element) => {
if (element !== null) {
optionRefs.push(element)
}
}
function handleInputOnKeyDown(e) {
switch (e.key) {
case 'Enter':
setOpen(!open)
break
case 'Escape':
setOpen(false)
break
case 'ArrowDown':
e.preventDefault()
optionRefs.length > 0 && optionRefs[0].focus()
break
case 'ArrowUp':
e.preventDefault()
optionRefs.length > 0 && optionRefs[optionRefs.length - 1].focus()
break
default:
}
}
function handleOptionOnKeyDown(e, option, index) {
switch (e.key) {
case 'Enter':
onChange(option)
onSearch('')
setOpen(false)
inputRef.current.focus()
break
case 'Escape':
setOpen(false)
inputRef.current.focus()
break
case 'ArrowUp':
e.preventDefault()
if (index === 0) inputRef.current.focus()
else optionRefs[index - 1].focus()
break
case 'ArrowDown':
e.preventDefault()
if (index + 1 >= optionRefs.length) inputRef.current.focus()
else optionRefs[index + 1].focus()
break
default:
}
}
return (
<Box
// Parent box
{...props}
w={{ base: '100%', md: '75%' }}
position="relative"
color="#333"
cursor="default"
aria-pressed={open}
aria-expanded={open}
>
<Box
// Box containing label and input
lineHeight={1.5}
fontSize="1rem"
boxSizing="border-box"
cursor="default"
outline="none"
transition="all 200ms ease"
width="100%"
_focus={{
border: '2px solid blue',
color: '#222',
}}
>
<label>
<Flex flexDirection={{ base: 'column', md: labelDirection }} align="center">
<Text fontWeight="bold" fontSize="2xl" mr={{ base: '0', md: '4' }}>
{label}
</Text>
<InputGroup>
<InputLeftElement>
<SearchIcon boxSize="icons.md" />
</InputLeftElement>
<Input
borderColor="black"
borderWidth="1px"
ref={inputRef}
type="text"
placeholder={placeholder}
value={searchValue}
onChange={(e) => {
onSearch(e.target.value)
}}
onClick={close}
onKeyDown={handleInputOnKeyDown}
/>
<InputRightElement>
<InputRightElement>
{open ? <ChevronUpIcon boxSize="icons.md" /> : <ChevronDownIcon boxSize="icons.md" />}
</InputRightElement>
</InputRightElement>
</InputGroup>
</Flex>
</label>
</Box>
<Box
// Box containing list of options
display={open ? 'block' : 'none'}
bgColor="#fff"
border="1px solid #ccc"
boxShadow="0 1px 0 rgba(0, 0, 0, 0.06)"
boxSizing="border-box"
marginTop="2px"
maxH="200px"
overflowY="auto"
position="absolute"
top="100%"
width="100%"
zIndex={1000}
>
{options.map((option, idx) => (
<Box
// Box containing individual options
tabIndex={0}
key={option.value.id}
boxSizing="border-box"
color="rgba(51, 51, 51, 0.8)"
cursor="pointer"
display="block"
padding="8px 10px"
_hover={{ bgColor: '#f2f9fc', color: '#333' }}
_focus={{
bgColor: '#f2f9fc',
border: '2px solid blue',
color: '#222',
}}
onClick={() => {
onChange(option)
onSearch('')
setOpen(false)
}}
onKeyDown={(e) => handleOptionOnKeyDown(e, option, idx)}
ref={setOptRef}
>
{option.label}
</Box>
))}
</Box>
</Box>
)
}
Dropdown.propTypes = {
label: string,
labelDirection: string,
options: array,
placeholder: string,
onChange: func,
searchValue: string,
onSearch: func,
}