forked from Cambly/syntax
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RichSelectList.tsx
287 lines (276 loc) · 9.3 KB
/
RichSelectList.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
import React, {
type ReactElement,
useMemo,
type SyntheticEvent,
useRef,
useId,
} from "react";
import classNames from "classnames";
import {
ColorBaseDestructive700,
ColorBaseGray800,
} from "@cambly/syntax-design-tokens";
import Typography from "../Typography/Typography";
import useIsHydrated from "../useIsHydrated";
import Popover from "../Popover/Popover";
import {
Label as ReactAriaLabel,
Provider as ReactAriaProvider,
} from "react-aria-components";
import { useControlledState } from "@react-stately/utils";
import { setInteractionModality } from "@react-aria/interactions";
import { DialogContext } from "../Dialog/Dialog";
import styles from "../SelectList/SelectList.module.css";
import RichSelectBox, {
RichSelectBoxContext,
convertSelection,
type RichSelectBoxProps,
} from "./RichSelectBox";
import TapArea from "../TapArea/TapArea";
import { type OverlayHandlerRef } from "../react-aria-utils/Triggerable";
import Box from "../Box/Box";
import RichSelectSection from "./RichSelectSection";
import RichSelectChip from "./RichSelectChip";
import RichSelectRadioButton from "./RichSelectRadioButton";
import { useField } from "react-aria";
const NOOP = () => undefined;
const iconSize = {
sm: 20,
md: 24,
lg: 24,
} as const;
export type RichSelectListProps = Omit<
RichSelectBoxProps,
"accessibilityLabel"
> & {
/** Test id for the select element */
"data-testid"?: string;
/**
* Disables the select dropdown entirely
* @defaultValue false
*/
disabled?: boolean;
/** Callback to be called when select is clicked */
onClick?: (event: SyntheticEvent<HTMLDivElement>) => void;
/** Text shown below select box if there is an input error. */
errorText?: string;
/** Text shown below select box */
helperText?: string;
/** Text shown above select box */
label?: string;
//** */
accessibilityLabel: string;
/**
* Text showing in select box if no option has been chosen.
* There should always have a placeholder unless there is a default option selected
*/
placeholderText?: string;
/** Use to render (override) text shown in the select box */
selectTextValue?: (selectedValues?: "all" | string[]) => string | undefined;
/**
* Size of the select box
*
* @defaultValue "md"
*/
size?: "sm" | "md" | "lg";
};
/**
* [RichSelectList](https://cambly-syntax.vercel.app/?path=/docs/components-richselectlist--docs) is a dropdown menu that allows users to select one or multiple options from a list.
*
* Example Usage:
```
<RichSelectList
label="My Label"
multiple
onChange={() => { ... }}
primaryButtonText="Save"
primaryButtonAccessibilityLabel="Save"
secondaryButtonText="Clear"
secondaryButtonAccessibilityLabel="Clear"
>
<RichSelectList.Section label="Cities">
<RichSelectList.Chip label="San Francisco" value="sf" />
<RichSelectList.Chip label="New York" value="ny" disabled />
<RichSelectList.Chip label="Tulsa" value="tulsa" />
<RichSelectList.Chip label="Chicago" value="chicago" disabled />
</RichSelectList.Section>
</RichSelectList>
```
*/
function RichSelectList(props: RichSelectListProps): ReactElement {
const {
autosave,
children,
"data-testid": dataTestId,
disabled: disabledProp = false,
errorText,
helperText,
label,
accessibilityLabel,
onChange,
onClick = NOOP,
placeholderText,
selectTextValue,
selectedValues: selectedValuesProp,
defaultSelectedValues: defaultSelectedValuesProp,
size = "md",
...richSelectBoxProps
} = props;
const id = useId();
const isHydrated = useIsHydrated();
const disabled = !isHydrated || disabledProp;
// passed to popover, which attached open/close methods
const overlayHandlerRef = useRef<OverlayHandlerRef>({});
const selectedKeysProp = useMemo(
() => convertSelection(selectedValuesProp),
[selectedValuesProp],
);
const defaultSelectedKeys = useMemo(
() => convertSelection(defaultSelectedValuesProp, new Set()),
[defaultSelectedValuesProp],
);
const [selectedKeys, setSelectedKeys] = useControlledState(
selectedKeysProp!, // eslint-disable-line @typescript-eslint/no-non-null-assertion -- there is a bug in the typedef for useControlledState from react-stately. Internally they rely on value (first arg) able to be undefined
defaultSelectedKeys,
(value) => {
const _value = value === "all" ? "all" : [...value].map(String);
onChange(_value);
if (!autosave) overlayHandlerRef.current.close?.();
},
);
const selectedTextValue = useMemo(() => {
if (selectTextValue)
return (
selectTextValue(
selectedKeys === "all" ? "all" : [...selectedKeys].map(String),
) ?? placeholderText
);
if (selectedKeys === "all") return "All selected";
if (selectedKeys.size) return `${selectedKeys.size} selected`;
return placeholderText;
}, [selectTextValue, selectedKeys, placeholderText]);
const fieldRef = useRef<HTMLDivElement>(null);
const { labelProps, fieldProps, descriptionProps, errorMessageProps } =
useField({
label, // this is the label for the select box
description: helperText,
errorMessage: errorText,
});
return (
<ReactAriaProvider
values={[
[RichSelectBoxContext, { autoFocus: true }],
[DialogContext, { padding: autosave ? undefined : 0 }], // this is altering Popover's internal dialog padding to show the sticky save/close buttons. Ideally this could be avoided
]}
>
<div
className={classNames(styles.selectContainer, {
[styles.opacityOverlay]: disabled,
})}
>
<ReactAriaLabel
data-testid={[dataTestId, "label"].filter(Boolean).join("-")}
className={classNames(
styles.selectContainer,
styles.outerTextContainer,
)}
{...labelProps}
onClick={() => {
if (disabled) return;
fieldRef.current?.focus();
setInteractionModality("keyboard"); // Show the focus ring so the user knows where focus went
}}
>
{label && (
<label className={styles.label} htmlFor={id}>
<Box paddingX={1}>
<Typography size={100} color="gray700">
{label}
</Typography>
</Box>
</label>
)}
</ReactAriaLabel>
<Popover
ref={overlayHandlerRef}
disabled={disabled}
accessibilityLabel={accessibilityLabel}
content={
// this Box wrapper is to reapply the padding that was stripped from popover's dialog to show the sticky save/close buttons. Ideally this could be avoided
<Box
padding={autosave ? undefined : 5}
dangerouslySetInlineStyle={
autosave ? undefined : { __style: { paddingBottom: 0 } }
}
>
<RichSelectBox
autosave={autosave}
selectedValues={selectedKeys}
defaultSelectedValues={defaultSelectedKeys}
onChange={(selected) => setSelectedKeys(new Set(selected))}
accessibilityLabel={accessibilityLabel}
{...richSelectBoxProps}
>
{children}
</RichSelectBox>
</Box>
}
>
<TapArea
data-testid={dataTestId}
disabled={disabled}
onClick={onClick}
rounding={size === "lg" ? "lg" : "md"}
{...fieldProps}
ref={fieldRef}
>
<div className={styles.selectWrapper}>
<div
className={classNames(styles.selectBox, styles[size], {
[styles.unselected]:
!errorText && selectedKeys !== "all" && !selectedKeys.size,
[styles.selected]:
!errorText && (selectedKeys === "all" || selectedKeys.size),
[styles.selectError]: errorText,
})}
>
{selectedTextValue}
</div>
<div className={styles.arrowIcon}>
<svg
focusable="false"
aria-hidden="true"
viewBox="0 0 24 24"
width={iconSize[size]}
>
<path
fill={
errorText ? ColorBaseDestructive700 : ColorBaseGray800
}
d="M15.88 9.29 12 13.17 8.12 9.29a.9959.9959 0 0 0-1.41 0c-.39.39-.39 1.02 0 1.41l4.59 4.59c.39.39 1.02.39 1.41 0l4.59-4.59c.39-.39.39-1.02 0-1.41-.39-.38-1.03-.39-1.42 0z"
/>
</svg>
</div>
</div>
</TapArea>
</Popover>
{(helperText || errorText) && (
<div className={styles.outerTextContainer}>
<Typography
size={100}
color={errorText ? "destructive-primary" : "gray700"}
{...(errorText ? errorMessageProps : descriptionProps)}
>
{errorText ? errorText : helperText}
</Typography>
</div>
)}
</div>
</ReactAriaProvider>
);
}
export default Object.assign(RichSelectList, {
Section: RichSelectSection,
Chip: RichSelectChip,
RadioButton: RichSelectRadioButton,
});