forked from opendatahub-io/odh-dashboard
-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathSimpleSelect.tsx
More file actions
231 lines (217 loc) · 6.98 KB
/
SimpleSelect.tsx
File metadata and controls
231 lines (217 loc) · 6.98 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
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
import * as React from 'react';
import {
MenuToggle,
// eslint-disable-next-line no-restricted-imports
Select,
SelectList,
SelectOption,
SelectGroup,
Divider,
MenuToggleProps,
FormHelperText,
HelperText,
HelperTextItem,
Skeleton,
} from '@patternfly/react-core';
import TruncatedText from '#~/components/TruncatedText';
import './SimpleSelect.scss';
export type SimpleSelectOption = {
key: string;
label: string;
description?: React.ReactNode;
dropdownLabel?: React.ReactNode;
isPlaceholder?: boolean;
isDisabled?: boolean;
// Use aria-disabled (instead of disabled) when we still need hover/focus behavior
// (e.g., tooltips on "disabled" options).
isAriaDisabled?: boolean;
isFavorited?: boolean;
dataTestId?: string;
optionKey?: string; // Used to differentiate the only option with the same key to trigger the one-option hook in the component
};
export type SimpleGroupSelectOption = {
key: string;
label: string;
options: SimpleSelectOption[];
};
type SimpleSelectProps = {
options?: SimpleSelectOption[];
groupedOptions?: SimpleGroupSelectOption[];
value?: string;
toggleLabel?: React.ReactNode;
placeholder?: string;
onChange: (key: string, isPlaceholder: boolean) => void;
isFullWidth?: boolean;
toggleProps?: MenuToggleProps;
isDisabled?: boolean;
icon?: React.ReactNode;
dataTestId?: string;
previewDescription?: boolean;
isSkeleton?: boolean;
} & Omit<
React.ComponentProps<typeof Select>,
'isOpen' | 'toggle' | 'dropdownItems' | 'onChange' | 'selected'
>;
const SimpleSelect: React.FC<SimpleSelectProps> = ({
isDisabled,
onChange,
options,
groupedOptions,
placeholder = 'Select...',
value,
toggleLabel,
isFullWidth,
icon,
dataTestId,
toggleProps,
previewDescription = true,
popperProps,
isSkeleton,
...props
}) => {
const [open, setOpen] = React.useState(false);
const groupedOptionsFlat = React.useMemo(
() =>
groupedOptions?.reduce<SimpleSelectOption[]>((acc, group) => [...acc, ...group.options], []),
[groupedOptions],
);
const findOptionForKey = (key: string) =>
options?.find((option) => option.key === key) || groupedOptionsFlat?.find((o) => o.key === key);
const selectedOption = value ? findOptionForKey(value) : undefined;
const selectedLabel = selectedOption?.label ?? placeholder;
const totalOptions = React.useMemo(
() => [...(options || []), ...(groupedOptionsFlat || [])],
[options, groupedOptionsFlat],
);
const singleOptionKey =
totalOptions.length === 1 ? totalOptions[0].optionKey || totalOptions[0].key : null;
// If there is only one option, call the onChange function
React.useEffect(() => {
if (singleOptionKey && !isSkeleton) {
onChange(totalOptions[0].key, false);
}
// We don't want the callback function to be a dependency
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [singleOptionKey, isSkeleton]);
if (isSkeleton) {
return <Skeleton style={{ minWidth: 100 }} />;
}
return (
<>
<Select
{...props}
isOpen={open}
selected={value || toggleLabel}
onSelect={(e, selectValue) => {
const key = String(selectValue);
const option = findOptionForKey(key);
if (option?.isAriaDisabled) {
return;
}
onChange(key, !!selectValue && (option?.isPlaceholder ?? false));
setOpen(false);
}}
onOpenChange={setOpen}
toggle={(toggleRef) => (
<MenuToggle
ref={toggleRef}
data-testid={dataTestId}
aria-label="Options menu"
onClick={() => setOpen(!open)}
icon={icon}
isExpanded={open}
isDisabled={totalOptions.length <= 1 || isDisabled}
isFullWidth={isFullWidth}
{...toggleProps}
>
{/* Plain text: MenuToggle's .pf-v6-c-menu-toggle__text handles truncation. Using Truncate
caused Safari flex layout bugs (labels clipped when space was available). */}
{(() => {
const displayedLabel = toggleLabel ?? selectedLabel;
return (
<span title={typeof displayedLabel === 'string' ? displayedLabel : undefined}>
{displayedLabel}
</span>
);
})()}
</MenuToggle>
)}
shouldFocusToggleOnSelect
popperProps={{ maxWidth: 'trigger', ...popperProps }}
>
{groupedOptions?.map((group, index) => (
<React.Fragment key={group.key}>
{index > 0 ? <Divider /> : null}
<SelectGroup label={group.label}>
<SelectList>
{group.options.map(
({
key,
label,
dropdownLabel,
description,
isFavorited,
isDisabled: optionDisabled,
isAriaDisabled: optionAriaDisabled,
dataTestId: optionDataTestId,
}) => (
<SelectOption
key={key}
value={key}
description={<TruncatedText maxLines={2} content={description} />}
isDisabled={optionDisabled}
isAriaDisabled={optionAriaDisabled}
isFavorited={isFavorited}
data-testid={optionDataTestId || key}
>
{dropdownLabel || label}
</SelectOption>
),
)}
</SelectList>
</SelectGroup>
</React.Fragment>
)) ?? null}
{options?.length ? (
<SelectList>
{groupedOptions?.length ? <Divider /> : null}
{options.map(
({
key,
label,
dropdownLabel,
description,
isFavorited,
isDisabled: optionDisabled,
isAriaDisabled: optionAriaDisabled,
dataTestId: optionDataTestId,
}) => (
<SelectOption
key={key}
value={key}
description={<TruncatedText maxLines={2} content={description} />}
isFavorited={isFavorited}
isDisabled={optionDisabled}
isAriaDisabled={optionAriaDisabled}
data-testid={optionDataTestId || key}
>
{dropdownLabel || label}
</SelectOption>
),
)}
</SelectList>
) : null}
</Select>
{previewDescription && selectedOption?.description ? (
<FormHelperText>
<HelperText>
<HelperTextItem>
<TruncatedText maxLines={2} content={selectedOption.description} />
</HelperTextItem>
</HelperText>
</FormHelperText>
) : null}
</>
);
};
export default SimpleSelect;