forked from Cambly/syntax
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SelectList.tsx
189 lines (185 loc) · 5.14 KB
/
SelectList.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
import React, {
type ReactElement,
type ReactNode,
useId,
useState,
} from "react";
import Box from "../Box/Box";
import classNames from "classnames";
import {
ColorBaseDestructive700,
ColorBaseGray800,
} from "@cambly/syntax-design-tokens";
import Typography from "../Typography/Typography";
import styles from "./SelectList.module.css";
import focusStyles from "../Focus.module.css";
import SelectOption from "./SelectOption";
import useFocusVisible from "../useFocusVisible";
import useIsHydrated from "../useIsHydrated";
import { useTheme } from "../ThemeProvider/ThemeProvider";
const iconSize = {
sm: 20,
md: 24,
lg: 24,
} as const;
/**
* [SelectList](https://cambly-syntax.vercel.app/?path=/docs/components-selectlist--docs) is a dropdown menu that allows users to select one option from a list.
*/
export default function SelectList({
children,
"data-testid": dataTestId,
disabled: disabledProp = false,
errorText,
helperText,
id,
label,
onChange,
onClick,
placeholderText,
selectedValue = "",
size = "md",
}: {
/**
* One or more SelectList.Option components.
*/
children: ReactNode;
/**
* Test id for the select element
*/
"data-testid"?: string;
/**
* true if the select dropdown is disabled
* @defaultValue false
*/
disabled?: boolean;
/**
* Callback to be called when select is clicked
*/
onClick?: React.MouseEventHandler<HTMLSelectElement>;
/**
* Text shown below select box if there is an input error.
*/
errorText?: string;
/**
* Text shown below select box
*/
helperText?: string;
/**
* Id of the select element
*/
id?: string;
/**
* Text shown above select box
*/
label: string;
/**
* The callback to be called when an option is selected
*/
onChange: React.ChangeEventHandler<HTMLSelectElement>;
/**
* Text showing in select box if no option has been chosen.
* We should always have a placeholder unless there is a default option selected
*/
placeholderText?: string;
/**
* Value of the currently selected option
*/
selectedValue?: string;
/**
* Size of the select box
* * `sm`: 32px
* * `md`: 40px
* * `lg`: 48px
*
* @defaultValue "md"
*/
size?: "sm" | "md" | "lg";
}): ReactElement {
const reactId = useId();
const isHydrated = useIsHydrated();
const disabled = !isHydrated || disabledProp;
const { themeName } = useTheme();
const selectId = id ?? reactId;
const { isFocusVisible } = useFocusVisible();
const [isFocused, setIsFocused] = useState(false);
return (
<div
className={classNames(styles.selectContainer, {
[styles.opacityOverlay]: disabled,
[styles.selectContainerCambio]: themeName === "cambio",
})}
>
{label && (
<label htmlFor={selectId}>
<Box paddingX={1}>
<Typography size={100} color="gray700">
{label}
</Typography>
</Box>
</label>
)}
<div className={styles.selectWrapper}>
<select
id={selectId}
data-testid={dataTestId}
disabled={disabled}
className={classNames(
styles.selectBox,
themeName === "classic"
? styles.selectBoxClassic
: styles.selectBoxCambio,
themeName === "classic" && styles[size],
{
[styles.unselected]: !selectedValue && !errorText,
[styles.selected]: selectedValue && !errorText,
[themeName === "classic"
? styles.selectError
: styles.selectErrorCambio]: errorText,
[focusStyles.accessibilityOutlineFocus]:
isFocused && isFocusVisible, // for focus keyboard
[styles.selectMouseFocusStyling]: isFocused && !isFocusVisible, // for focus mouse
},
)}
onChange={onChange}
onClick={onClick}
value={
placeholderText && !selectedValue ? placeholderText : selectedValue
}
onFocus={() => setIsFocused(true)}
onBlur={() => setIsFocused(false)}
>
{placeholderText && (
<option disabled value={placeholderText}>
{placeholderText}
</option>
)}
{children}
</select>
<div className={styles.arrowIcon}>
<svg
focusable="false"
aria-hidden="true"
viewBox="0 0 24 24"
width={themeName === "classic" ? iconSize[size] : 24}
>
<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>
{(helperText || errorText) && (
<Box paddingX={1}>
<Typography
size={100}
color={errorText ? "destructive-primary" : "gray700"}
>
{errorText ? errorText : helperText}
</Typography>
</Box>
)}
</div>
);
}
SelectList.Option = SelectOption;