-
Notifications
You must be signed in to change notification settings - Fork 46
/
Select.tsx
202 lines (187 loc) · 6.38 KB
/
Select.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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import * as React from "react"
import ContextMenu, { ContextMenuProps } from "../ContextMenu/ContextMenu"
import LabelText from "../LabelText/LabelText"
import { useUniqueId } from "../useUniqueId"
import { FilterInput, Combobox, Listbox, DropdownButton, SelectInput, CheckboxContainer } from "./Select.styled"
import { SelectProps, IOption } from "./Select.types"
import {
truncateList,
appendItem,
filterList,
getDisplayValue,
getNewValue,
isOptionSelected,
prependItem,
optionsToContextMenuItems,
getOptionFromItem,
} from "./Select.util"
import Checkbox from "../Checkbox/Checkbox"
// @ts-ignore https://github.com/Swizec/useDimensions/issues/16
import useDimensions from "react-use-dimensions"
export const Select: React.FC<SelectProps> = ({
options,
label,
maxOptions,
value,
naked,
onChange,
customInputSettings,
placeholder,
color,
disabled,
filterable,
id,
customOption,
fullWidth,
tabIndex = 0,
...rest
}) => {
const uniqueId = useUniqueId(id)
const [filter, setFilter] = React.useState("")
const $container = React.useRef<HTMLDivElement>(null)
const $input = React.useMemo(() => React.createRef<HTMLInputElement>(), [])
const isMultiSelect = Array.isArray(value)
React.useEffect(() => {
if (customOption && value === customOption.value && $input.current) {
$input.current.focus()
}
}, [value, customOption])
const appendCustomOption = React.useCallback(
(optionToAppend: IOption) => (options: ContextMenuProps["items"]): ContextMenuProps["items"] => {
// We can't have a multiselect _and_ a custom option.
if (isMultiSelect && process.env.NODE_ENV !== "production") {
console.trace(
"⚠️ Cannot show custom option with a multi-select Select component. Please either choose to have a single value, or remove the `customOption` property from your `Select` component.",
)
return options
}
return appendItem(optionsToContextMenuItems()([optionToAppend])[0])(options)
},
[customOption, value],
)
const filterComponent = React.useMemo(
() => (
<FilterInput
autoFocus
onClick={e => e.stopPropagation()}
fullWidth
placeholder="Filter..."
value={filter}
onChange={setFilter}
/>
),
[filter],
)
/** Get a list of items to feed to the ContextMenu */
const items = React.useMemo(() => {
const initialOptions = options
const filteredOptions = filterList(filter)(initialOptions)
const truncatedOptions = truncateList(maxOptions)(filteredOptions)
const contextMenuItems = optionsToContextMenuItems(option => ({
label: isMultiSelect ? (
<CheckboxContainer>
<Checkbox
condensed
value={isOptionSelected(value)(option)}
label={option.label ? option.label : String(option.value)}
/>
</CheckboxContainer>
) : option.label ? (
option.label
) : (
String(option.value)
),
isActive: isOptionSelected(value)(option),
}))(truncatedOptions)
// Case 1: It's both filterable _and_ has a custom option
if (filterable && customOption !== undefined) {
return appendCustomOption(customOption)(
prependItem({ label: filterComponent, separator: "bottom" })(contextMenuItems),
)
}
// Case 2: It's filterable and no custom option
if (filterable && customOption === undefined) {
return prependItem({ label: filterComponent, separator: "bottom" })(contextMenuItems)
}
// Case 3, It has a custom option but is not filterable
if (!filterable && customOption !== undefined) {
return appendCustomOption(customOption)(contextMenuItems)
}
// Default case, it's just a normal set of items
return contextMenuItems
}, [options, value, filter, maxOptions, filterable, customOption, onChange, isMultiSelect])
const isReadOnly = React.useMemo(() => (customOption ? customOption.value !== value : true), [customOption, value])
const [ref, { width }] = useDimensions()
return (
<ContextMenu
onClick={item => {
if (onChange) {
onChange(getNewValue(value)(item.value), getOptionFromItem(items)(item))
}
if ($container.current) {
$container.current.focus()
}
if ($input.current) {
$input.current.focus()
}
}}
anchored
disabled={disabled}
keepOpenOnItemClick={isMultiSelect}
items={items}
aria-labelledby={`operational-ui__Select-Label-${uniqueId}`}
initialFocusedItemIndex={options.findIndex(option => option.value === value)}
width={width}
{...rest}
>
{isOpen => (
<Listbox
fullWidth={Boolean(fullWidth)}
ref={$container}
aria-labelledby={`operational-ui__Select-Label-${uniqueId}`}
aria-disabled={Boolean(disabled)}
disabled={Boolean(disabled)}
aria-expanded={isOpen}
id={`operational-ui__Select-${uniqueId}`}
color={color}
>
{label && <LabelText id={`operational-ui__Select-Label-${uniqueId}`}>{label}</LabelText>}
<Combobox
isOpen={isOpen}
hasCustomOption={customOption ? customOption.value === value : false}
naked={Boolean(naked)}
ref={ref}
>
<SelectInput
inputRef={$input}
fullWidth={fullWidth}
tabIndex={customOption && customOption.value === value ? 0 : -1}
disabled={disabled}
placeholder={placeholder}
readOnly={isReadOnly}
value={getDisplayValue(value)(options)}
id={`operational-ui__Select-Input-${uniqueId}`}
onClick={e => {
if (customOption && value === customOption.value) {
e.stopPropagation()
}
}}
onChange={newValue => {
if (onChange) {
onChange(newValue, customOption)
}
}}
onBlur={() => {
if (!isReadOnly && customInputSettings && customInputSettings.onBlur) {
customInputSettings.onBlur(getDisplayValue(value)(options))
}
}}
/>
<DropdownButton naked={Boolean(naked)} isOpen={isOpen} />
</Combobox>
</Listbox>
)}
</ContextMenu>
)
}
export default Select