-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathSelect.tsx
More file actions
176 lines (173 loc) · 6.79 KB
/
Copy pathSelect.tsx
File metadata and controls
176 lines (173 loc) · 6.79 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
import React from 'react';
import {
Select as MuiSelect,
SelectProps as MuiSelectProps,
MenuItem,
SelectChangeEvent,
ListItemIcon,
ListItemText,
} from '@mui/material';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { faCheck } from '@fortawesome/free-solid-svg-icons';
import { useTheme } from '@mui/material/styles';
import { colors, elevations } from '..';
import { isDarkColor } from 'utils';
type SelectProps = {
options: Array<{ value: string | number; label: string }>;
value: string | number;
onChange: (event: SelectChangeEvent<unknown>) => void;
id: string;
renderValue: (value: unknown) => React.ReactNode;
displayEmpty?: boolean;
inputProps?: Record<string, unknown>;
MenuProps?: Record<string, unknown>;
color?: 'default' | 'danger' | 'warning' | 'success' | string;
};
/**
* A customizable select component that allows users to choose from a list of options.
* It supports custom rendering of selected values, and can display a checkmark next to the selected option.
* The component is styled to match the application's theme and supports various colors.
* @param {SelectProps} props - The properties for the select component.
* @param {Array<{ value: string | number; label: string }>} props.options - The list of options to display in the select dropdown.
* @param {string | number} props.value - The currently selected value.
* @param {function} props.onChange - The function to call when the selected value changes.
* @param {string} props.id - The unique identifier for the select component.
* @param {function} props.renderValue - A function to render the selected value.
* @param {boolean} [props.displayEmpty=false] - If true, allows the select to display an empty value.
* @param {Record<string, unknown>} [props.inputProps={}] - Additional properties to pass to the input element.
* @param {Record<string, unknown>} [props.MenuProps={}] - Additional properties for the dropdown menu.
* @param {string} [props.color='default'] - The color of the select component, can be 'default', 'danger', 'warning', 'success', or any custom color.
* @returns {JSX.Element} A styled select component that allows users to choose from a list of options.
* @example
* <Select
* options={[
* { value: 'option1', label: 'Option 1' },
* { value: 'option2', label: 'Option 2' },
* ]}
* value="option1"
* onChange={(event) => console.log(event.target.value)}
* id="my-select"
* renderValue={(value) => `Selected: ${value}`}
* displayEmpty
*
*/
export const Select: React.FC<SelectProps & Omit<MuiSelectProps, 'value' | 'onChange' | 'renderValue'>> = ({
options,
value,
onChange,
id,
renderValue,
displayEmpty = false,
inputProps = {},
MenuProps = {},
color = 'default',
...selectProps
}) => {
const theme = useTheme();
const customColor = colors.button[color as keyof typeof colors.button]?.shade ?? color;
const bgColor = customColor;
const darkBgColor = `color-mix(in srgb, ${bgColor}, black 20%)`;
const textColors = isDarkColor(bgColor, 0.4) ? colors.type.inverted : colors.type.regular;
return (
<MuiSelect
value={value}
onChange={onChange}
id={id}
renderValue={renderValue}
displayEmpty={displayEmpty}
inputProps={{
...inputProps,
'aria-label': inputProps['aria-label'] ?? '',
style: { padding: 0, height: 48 },
}}
sx={{
mr: 1,
mb: 1.5,
p: 1,
height: 48,
borderRadius: '2em',
backgroundColor: bgColor,
color: textColors.primary,
boxShadow: 3,
'&:hover': {
backgroundColor: darkBgColor,
boxShadow: elevations.hover,
},
'&.Mui-focused': {
backgroundColor: darkBgColor,
outline: 'none',
boxShadow: `0 0 0 2px ${colors.focus.regular.inner}, 0 0 0 4px ${colors.focus.regular.outer}`,
},
'&:focus-visible': {
backgroundColor: darkBgColor,
outline: 'none',
boxShadow: `0 0 0 2px ${colors.focus.regular.inner}, 0 0 0 4px ${colors.focus.regular.outer}`,
},
'&:active': {
backgroundColor: darkBgColor,
},
'& svg': {
color: 'white',
},
}}
MenuProps={{
anchorOrigin: {
vertical: 'bottom',
horizontal: 'left',
},
transformOrigin: {
vertical: 'top',
horizontal: 'left',
},
sx: {
backgroundColor: 'transparent',
boxShadow: 3,
mt: 1,
ml: -1,
'& .MuiPaper-root': {
borderRadius: '8px',
boxShadow: 3,
overflow: 'hidden',
border: '1px solid #D9D9D9',
},
'& ul': {
p: 0,
'& li': {
fontSize: '16px',
height: 48,
'&:hover': {
backgroundColor: '#ECEAE8',
},
'&.Mui-selected': {
backgroundColor: '#D8EAFD',
},
'& span': {
color: 'text.primary',
},
'&.Mui-selected span': {
fontWeight: 'bold',
color: theme.palette.primary.main,
},
'&:not(:first-of-type)': {
borderTop: '1px solid #D9D9D9',
},
},
},
},
...MenuProps,
}}
{...selectProps}
>
{options.map((option) => (
<MenuItem key={option.value} value={option.value}>
{value === option.value && (
<ListItemIcon>
<FontAwesomeIcon icon={faCheck} style={{ fontSize: '20px' }} />
</ListItemIcon>
)}
<ListItemText primary={option.label} />
</MenuItem>
))}
</MuiSelect>
);
};