@patternfly/react-templates 6.0.0-alpha.9 → 6.0.0-prerelease.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +204 -0
- package/README.md +4 -17
- package/components/package.json +1 -1
- package/dist/dynamic/components/Dropdown/package.json +1 -0
- package/dist/dynamic/components/Select/package.json +1 -1
- package/dist/dynamic-modules.json +19 -0
- package/dist/esm/components/Dropdown/SimpleDropdown.d.ts +48 -0
- package/dist/esm/components/Dropdown/SimpleDropdown.d.ts.map +1 -0
- package/dist/esm/components/Dropdown/SimpleDropdown.js +33 -0
- package/dist/esm/components/Dropdown/SimpleDropdown.js.map +1 -0
- package/dist/esm/components/Dropdown/index.d.ts +2 -0
- package/dist/esm/components/Dropdown/index.d.ts.map +1 -0
- package/dist/esm/components/Dropdown/index.js +2 -0
- package/dist/esm/components/Dropdown/index.js.map +1 -0
- package/dist/esm/components/Select/CheckboxSelect.d.ts +7 -3
- package/dist/esm/components/Select/CheckboxSelect.d.ts.map +1 -1
- package/dist/esm/components/Select/CheckboxSelect.js +14 -6
- package/dist/esm/components/Select/CheckboxSelect.js.map +1 -1
- package/dist/esm/components/Select/MultiTypeaheadSelect.d.ts +33 -0
- package/dist/esm/components/Select/MultiTypeaheadSelect.d.ts.map +1 -0
- package/dist/esm/components/Select/MultiTypeaheadSelect.js +186 -0
- package/dist/esm/components/Select/MultiTypeaheadSelect.js.map +1 -0
- package/dist/esm/components/Select/{SelectSimple.d.ts → SimpleSelect.d.ts} +12 -7
- package/dist/esm/components/Select/SimpleSelect.d.ts.map +1 -0
- package/dist/esm/components/Select/SimpleSelect.js +39 -0
- package/dist/esm/components/Select/SimpleSelect.js.map +1 -0
- package/dist/esm/components/Select/TypeaheadSelect.d.ts +43 -0
- package/dist/esm/components/Select/TypeaheadSelect.d.ts.map +1 -0
- package/dist/esm/components/Select/TypeaheadSelect.js +219 -0
- package/dist/esm/components/Select/TypeaheadSelect.js.map +1 -0
- package/dist/esm/components/Select/index.d.ts +3 -1
- package/dist/esm/components/Select/index.d.ts.map +1 -1
- package/dist/esm/components/Select/index.js +3 -1
- package/dist/esm/components/Select/index.js.map +1 -1
- package/dist/esm/components/index.d.ts +1 -0
- package/dist/esm/components/index.d.ts.map +1 -1
- package/dist/esm/components/index.js +1 -0
- package/dist/esm/components/index.js.map +1 -1
- package/dist/js/components/Dropdown/SimpleDropdown.d.ts +48 -0
- package/dist/js/components/Dropdown/SimpleDropdown.d.ts.map +1 -0
- package/dist/js/components/Dropdown/SimpleDropdown.js +36 -0
- package/dist/js/components/Dropdown/SimpleDropdown.js.map +1 -0
- package/dist/js/components/Dropdown/index.d.ts +2 -0
- package/dist/js/components/Dropdown/index.d.ts.map +1 -0
- package/dist/js/components/Dropdown/index.js +5 -0
- package/dist/js/components/Dropdown/index.js.map +1 -0
- package/dist/js/components/Select/CheckboxSelect.d.ts +7 -3
- package/dist/js/components/Select/CheckboxSelect.d.ts.map +1 -1
- package/dist/js/components/Select/CheckboxSelect.js +14 -6
- package/dist/js/components/Select/CheckboxSelect.js.map +1 -1
- package/dist/js/components/Select/MultiTypeaheadSelect.d.ts +33 -0
- package/dist/js/components/Select/MultiTypeaheadSelect.d.ts.map +1 -0
- package/dist/js/components/Select/MultiTypeaheadSelect.js +190 -0
- package/dist/js/components/Select/MultiTypeaheadSelect.js.map +1 -0
- package/dist/js/components/Select/{SelectSimple.d.ts → SimpleSelect.d.ts} +12 -7
- package/dist/js/components/Select/SimpleSelect.d.ts.map +1 -0
- package/dist/js/components/Select/SimpleSelect.js +42 -0
- package/dist/js/components/Select/SimpleSelect.js.map +1 -0
- package/dist/js/components/Select/TypeaheadSelect.d.ts +43 -0
- package/dist/js/components/Select/TypeaheadSelect.d.ts.map +1 -0
- package/dist/js/components/Select/TypeaheadSelect.js +223 -0
- package/dist/js/components/Select/TypeaheadSelect.js.map +1 -0
- package/dist/js/components/Select/index.d.ts +3 -1
- package/dist/js/components/Select/index.d.ts.map +1 -1
- package/dist/js/components/Select/index.js +3 -1
- package/dist/js/components/Select/index.js.map +1 -1
- package/dist/js/components/index.d.ts +1 -0
- package/dist/js/components/index.d.ts.map +1 -1
- package/dist/js/components/index.js +1 -0
- package/dist/js/components/index.js.map +1 -1
- package/dist/umd/assets/output-Drxynxu2.css +23380 -0
- package/dist/umd/react-templates.min.js +1 -1
- package/package.json +10 -14
- package/rollup.config.mjs +8 -0
- package/src/components/Dropdown/SimpleDropdown.tsx +139 -0
- package/src/components/Dropdown/__tests__/SimpleDropdown.test.tsx +266 -0
- package/src/components/Dropdown/__tests__/__snapshots__/SimpleDropdown.test.tsx.snap +81 -0
- package/src/components/Dropdown/examples/DropdownTemplates.md +24 -0
- package/src/components/Dropdown/examples/SimpleDropdownExample.tsx +53 -0
- package/src/components/Dropdown/index.ts +1 -0
- package/src/components/Select/CheckboxSelect.tsx +23 -7
- package/src/components/Select/MultiTypeaheadSelect.tsx +348 -0
- package/src/components/Select/{SelectSimple.tsx → SimpleSelect.tsx} +39 -18
- package/src/components/Select/TypeaheadSelect.tsx +388 -0
- package/src/components/Select/{CheckboxSelect.test.tsx → __tests__/CheckboxSelect.test.tsx} +29 -1
- package/src/components/Select/{CheckboxSelectSnapshots.test.tsx → __tests__/CheckboxSelectSnapshots.test.tsx} +1 -1
- package/src/components/Select/__tests__/MultiTypeaheadSelect.test.tsx +414 -0
- package/src/components/Select/__tests__/SimpleSelect.test.tsx +241 -0
- package/src/components/Select/__tests__/TypeaheadSelect.test.tsx +408 -0
- package/src/components/Select/__tests__/__snapshots__/CheckboxSelectSnapshots.test.tsx.snap +83 -0
- package/src/components/Select/__tests__/__snapshots__/MultiTypeaheadSelect.test.tsx.snap +97 -0
- package/src/components/Select/__tests__/__snapshots__/SimpleSelect.test.tsx.snap +83 -0
- package/src/components/Select/__tests__/__snapshots__/TypeaheadSelect.test.tsx.snap +97 -0
- package/src/components/Select/examples/CheckboxSelectDemo.tsx +25 -8
- package/src/components/Select/examples/MultiTypeaheadSelectDemo.tsx +35 -0
- package/src/components/Select/examples/SelectTemplates.md +17 -4
- package/src/components/Select/examples/SimpleSelectDemo.tsx +36 -0
- package/src/components/Select/examples/TypeaheadSelectDemo.tsx +65 -0
- package/src/components/Select/index.ts +3 -1
- package/src/components/index.ts +1 -0
- package/dist/esm/components/Select/SelectSimple.d.ts.map +0 -1
- package/dist/esm/components/Select/SelectSimple.js +0 -31
- package/dist/esm/components/Select/SelectSimple.js.map +0 -1
- package/dist/js/components/Select/SelectSimple.d.ts.map +0 -1
- package/dist/js/components/Select/SelectSimple.js +0 -34
- package/dist/js/components/Select/SelectSimple.js.map +0 -1
- package/rollup.config.js +0 -7
- package/src/components/Select/__snapshots__/CheckboxSelectSnapshots.test.tsx.snap +0 -212
- package/src/components/Select/examples/SelectSimpleDemo.tsx +0 -26
|
@@ -0,0 +1,388 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import {
|
|
3
|
+
Select,
|
|
4
|
+
SelectOption,
|
|
5
|
+
SelectList,
|
|
6
|
+
SelectOptionProps,
|
|
7
|
+
MenuToggle,
|
|
8
|
+
MenuToggleElement,
|
|
9
|
+
TextInputGroup,
|
|
10
|
+
TextInputGroupMain,
|
|
11
|
+
TextInputGroupUtilities,
|
|
12
|
+
Button,
|
|
13
|
+
MenuToggleProps,
|
|
14
|
+
SelectProps
|
|
15
|
+
} from '@patternfly/react-core';
|
|
16
|
+
import TimesIcon from '@patternfly/react-icons/dist/esm/icons/times-icon';
|
|
17
|
+
|
|
18
|
+
export interface TypeaheadSelectOption extends Omit<SelectOptionProps, 'content'> {
|
|
19
|
+
/** Content of the select option. */
|
|
20
|
+
content: string | number;
|
|
21
|
+
/** Value of the select option. */
|
|
22
|
+
value: string | number;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface TypeaheadSelectProps extends Omit<SelectProps, 'toggle' | 'onSelect'> {
|
|
26
|
+
/** @hide Forwarded ref */
|
|
27
|
+
innerRef?: React.Ref<any>;
|
|
28
|
+
/** Initial options of the select. */
|
|
29
|
+
initialOptions: TypeaheadSelectOption[];
|
|
30
|
+
/** Callback triggered on selection. */
|
|
31
|
+
onSelect?: (
|
|
32
|
+
_event: React.MouseEvent<Element, MouseEvent> | React.KeyboardEvent<HTMLInputElement> | undefined,
|
|
33
|
+
selection: string | number
|
|
34
|
+
) => void;
|
|
35
|
+
/** Callback triggered when the select opens or closes. */
|
|
36
|
+
onToggle?: (nextIsOpen: boolean) => void;
|
|
37
|
+
/** Callback triggered when the text in the input field changes. */
|
|
38
|
+
onInputChange?: (newValue: string) => void;
|
|
39
|
+
/** Callback triggered when the clear button is selected */
|
|
40
|
+
onClearSelection?: () => void;
|
|
41
|
+
/** Placeholder text for the select input. */
|
|
42
|
+
placeholder?: string;
|
|
43
|
+
/** Flag to indicate if the typeahead select allows new items */
|
|
44
|
+
isCreatable?: boolean;
|
|
45
|
+
/** Flag to indicate if create option should be at top of typeahead */
|
|
46
|
+
isCreateOptionOnTop?: boolean;
|
|
47
|
+
/** Message to display to create a new option */
|
|
48
|
+
createOptionMessage?: string | ((newValue: string) => string);
|
|
49
|
+
/** Message to display when no options are available. */
|
|
50
|
+
noOptionsAvailableMessage?: string;
|
|
51
|
+
/** Message to display when no options match the filter. */
|
|
52
|
+
noOptionsFoundMessage?: string | ((filter: string) => string);
|
|
53
|
+
/** Flag indicating the select should be disabled. */
|
|
54
|
+
isDisabled?: boolean;
|
|
55
|
+
/** Width of the toggle. */
|
|
56
|
+
toggleWidth?: string;
|
|
57
|
+
/** Additional props passed to the toggle. */
|
|
58
|
+
toggleProps?: MenuToggleProps;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const defaultNoOptionsFoundMessage = (filter: string) => `No results found for "${filter}"`;
|
|
62
|
+
const defaultCreateOptionMessage = (newValue: string) => `Create "${newValue}"`;
|
|
63
|
+
|
|
64
|
+
export const TypeaheadSelectBase: React.FunctionComponent<TypeaheadSelectProps> = ({
|
|
65
|
+
innerRef,
|
|
66
|
+
initialOptions,
|
|
67
|
+
onSelect,
|
|
68
|
+
onToggle,
|
|
69
|
+
onInputChange,
|
|
70
|
+
onClearSelection,
|
|
71
|
+
placeholder = 'Select an option',
|
|
72
|
+
noOptionsAvailableMessage = 'No options are available',
|
|
73
|
+
noOptionsFoundMessage = defaultNoOptionsFoundMessage,
|
|
74
|
+
isCreatable = false,
|
|
75
|
+
isCreateOptionOnTop = false,
|
|
76
|
+
createOptionMessage = defaultCreateOptionMessage,
|
|
77
|
+
isDisabled,
|
|
78
|
+
toggleWidth,
|
|
79
|
+
toggleProps,
|
|
80
|
+
...props
|
|
81
|
+
}: TypeaheadSelectProps) => {
|
|
82
|
+
const [isOpen, setIsOpen] = React.useState(false);
|
|
83
|
+
const [selected, setSelected] = React.useState<string>(String(initialOptions.find((o) => o.selected)?.content ?? ''));
|
|
84
|
+
const [inputValue, setInputValue] = React.useState<string>(
|
|
85
|
+
String(initialOptions.find((o) => o.selected)?.content ?? '')
|
|
86
|
+
);
|
|
87
|
+
const [filterValue, setFilterValue] = React.useState<string>('');
|
|
88
|
+
const [selectOptions, setSelectOptions] = React.useState<TypeaheadSelectOption[]>(initialOptions);
|
|
89
|
+
const [focusedItemIndex, setFocusedItemIndex] = React.useState<number | null>(null);
|
|
90
|
+
const [activeItemId, setActiveItemId] = React.useState<string | null>(null);
|
|
91
|
+
const textInputRef = React.useRef<HTMLInputElement>();
|
|
92
|
+
|
|
93
|
+
const NO_RESULTS = 'no results';
|
|
94
|
+
|
|
95
|
+
React.useEffect(() => {
|
|
96
|
+
let newSelectOptions: TypeaheadSelectOption[] = initialOptions;
|
|
97
|
+
|
|
98
|
+
// Filter menu items based on the text input value when one exists
|
|
99
|
+
if (filterValue) {
|
|
100
|
+
newSelectOptions = initialOptions.filter((option) =>
|
|
101
|
+
String(option.content).toLowerCase().includes(filterValue.toLowerCase())
|
|
102
|
+
);
|
|
103
|
+
|
|
104
|
+
if (
|
|
105
|
+
isCreatable &&
|
|
106
|
+
filterValue &&
|
|
107
|
+
!initialOptions.find((o) => String(o.content).toLowerCase() === filterValue.toLowerCase())
|
|
108
|
+
) {
|
|
109
|
+
const createOption = {
|
|
110
|
+
content: typeof createOptionMessage === 'string' ? createOptionMessage : createOptionMessage(filterValue),
|
|
111
|
+
value: filterValue
|
|
112
|
+
};
|
|
113
|
+
newSelectOptions = isCreateOptionOnTop
|
|
114
|
+
? [createOption, ...newSelectOptions]
|
|
115
|
+
: [...newSelectOptions, createOption];
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// When no options are found after filtering, display 'No results found'
|
|
119
|
+
if (!newSelectOptions.length) {
|
|
120
|
+
newSelectOptions = [
|
|
121
|
+
{
|
|
122
|
+
isAriaDisabled: true,
|
|
123
|
+
content:
|
|
124
|
+
typeof noOptionsFoundMessage === 'string' ? noOptionsFoundMessage : noOptionsFoundMessage(filterValue),
|
|
125
|
+
value: NO_RESULTS
|
|
126
|
+
}
|
|
127
|
+
];
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// Open the menu when the input value changes and the new value is not empty
|
|
131
|
+
openMenu();
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// When no options are available, display 'No options available'
|
|
135
|
+
if (!newSelectOptions.length) {
|
|
136
|
+
newSelectOptions = [
|
|
137
|
+
{
|
|
138
|
+
isAriaDisabled: true,
|
|
139
|
+
content: noOptionsAvailableMessage,
|
|
140
|
+
value: NO_RESULTS
|
|
141
|
+
}
|
|
142
|
+
];
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
setSelectOptions(newSelectOptions);
|
|
146
|
+
}, [
|
|
147
|
+
filterValue,
|
|
148
|
+
initialOptions,
|
|
149
|
+
noOptionsFoundMessage,
|
|
150
|
+
isCreatable,
|
|
151
|
+
isCreateOptionOnTop,
|
|
152
|
+
createOptionMessage,
|
|
153
|
+
noOptionsAvailableMessage
|
|
154
|
+
]);
|
|
155
|
+
|
|
156
|
+
React.useEffect(() => {
|
|
157
|
+
// If the selected option changed and the current input value is the previously selected item, update the displayed value.
|
|
158
|
+
const selectedOption = initialOptions.find((o) => o.selected);
|
|
159
|
+
if (inputValue === selected && selectedOption?.value !== selected) {
|
|
160
|
+
setInputValue(String(selectedOption?.content ?? ''));
|
|
161
|
+
}
|
|
162
|
+
// Only update when options change
|
|
163
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
164
|
+
}, [initialOptions]);
|
|
165
|
+
|
|
166
|
+
const setActiveAndFocusedItem = (itemIndex: number) => {
|
|
167
|
+
setFocusedItemIndex(itemIndex);
|
|
168
|
+
const focusedItem = selectOptions[itemIndex];
|
|
169
|
+
setActiveItemId(String(focusedItem.value));
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
const resetActiveAndFocusedItem = () => {
|
|
173
|
+
setFocusedItemIndex(null);
|
|
174
|
+
setActiveItemId(null);
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
const openMenu = () => {
|
|
178
|
+
if (!isOpen) {
|
|
179
|
+
onToggle && onToggle(true);
|
|
180
|
+
setIsOpen(true);
|
|
181
|
+
}
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
const closeMenu = () => {
|
|
185
|
+
onToggle && onToggle(false);
|
|
186
|
+
setIsOpen(false);
|
|
187
|
+
resetActiveAndFocusedItem();
|
|
188
|
+
const option = initialOptions.find((o) => o.value === selected);
|
|
189
|
+
if (option) {
|
|
190
|
+
setInputValue(String(option.content));
|
|
191
|
+
}
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
const onInputClick = () => {
|
|
195
|
+
if (!isOpen) {
|
|
196
|
+
openMenu();
|
|
197
|
+
} else if (!inputValue) {
|
|
198
|
+
closeMenu();
|
|
199
|
+
}
|
|
200
|
+
};
|
|
201
|
+
|
|
202
|
+
const selectOption = (
|
|
203
|
+
_event: React.MouseEvent<Element, MouseEvent> | React.KeyboardEvent<HTMLInputElement> | undefined,
|
|
204
|
+
option: TypeaheadSelectOption
|
|
205
|
+
) => {
|
|
206
|
+
onSelect && onSelect(_event, option.value);
|
|
207
|
+
|
|
208
|
+
setInputValue(String(option.content));
|
|
209
|
+
setFilterValue('');
|
|
210
|
+
setSelected(String(option.value));
|
|
211
|
+
|
|
212
|
+
closeMenu();
|
|
213
|
+
};
|
|
214
|
+
|
|
215
|
+
const _onSelect = (_event: React.MouseEvent<Element, MouseEvent> | undefined, value: string | number | undefined) => {
|
|
216
|
+
if (value && value !== NO_RESULTS) {
|
|
217
|
+
const optionToSelect = selectOptions.find((option) => option.value === value);
|
|
218
|
+
selectOption(_event, optionToSelect);
|
|
219
|
+
}
|
|
220
|
+
};
|
|
221
|
+
|
|
222
|
+
const onTextInputChange = (_event: React.FormEvent<HTMLInputElement>, value: string) => {
|
|
223
|
+
setInputValue(value);
|
|
224
|
+
onInputChange && onInputChange(value);
|
|
225
|
+
setFilterValue(value);
|
|
226
|
+
|
|
227
|
+
resetActiveAndFocusedItem();
|
|
228
|
+
};
|
|
229
|
+
|
|
230
|
+
const handleMenuArrowKeys = (key: string) => {
|
|
231
|
+
let indexToFocus = 0;
|
|
232
|
+
|
|
233
|
+
openMenu();
|
|
234
|
+
|
|
235
|
+
if (selectOptions.every((option) => option.isDisabled)) {
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
if (key === 'ArrowUp') {
|
|
240
|
+
// When no index is set or at the first index, focus to the last, otherwise decrement focus index
|
|
241
|
+
if (focusedItemIndex === null || focusedItemIndex === 0) {
|
|
242
|
+
indexToFocus = selectOptions.length - 1;
|
|
243
|
+
} else {
|
|
244
|
+
indexToFocus = focusedItemIndex - 1;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
// Skip disabled options
|
|
248
|
+
while (selectOptions[indexToFocus].isDisabled) {
|
|
249
|
+
indexToFocus--;
|
|
250
|
+
if (indexToFocus === -1) {
|
|
251
|
+
indexToFocus = selectOptions.length - 1;
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
if (key === 'ArrowDown') {
|
|
257
|
+
// When no index is set or at the last index, focus to the first, otherwise increment focus index
|
|
258
|
+
if (focusedItemIndex === null || focusedItemIndex === selectOptions.length - 1) {
|
|
259
|
+
indexToFocus = 0;
|
|
260
|
+
} else {
|
|
261
|
+
indexToFocus = focusedItemIndex + 1;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
// Skip disabled options
|
|
265
|
+
while (selectOptions[indexToFocus].isDisabled) {
|
|
266
|
+
indexToFocus++;
|
|
267
|
+
if (indexToFocus === selectOptions.length) {
|
|
268
|
+
indexToFocus = 0;
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
setActiveAndFocusedItem(indexToFocus);
|
|
274
|
+
};
|
|
275
|
+
|
|
276
|
+
const onInputKeyDown = (event: React.KeyboardEvent<HTMLInputElement>) => {
|
|
277
|
+
const focusedItem = focusedItemIndex !== null ? selectOptions[focusedItemIndex] : null;
|
|
278
|
+
|
|
279
|
+
switch (event.key) {
|
|
280
|
+
case 'Enter':
|
|
281
|
+
if (isOpen && focusedItem && focusedItem.value !== NO_RESULTS && !focusedItem.isAriaDisabled) {
|
|
282
|
+
selectOption(event, focusedItem);
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
openMenu();
|
|
286
|
+
|
|
287
|
+
break;
|
|
288
|
+
case 'ArrowUp':
|
|
289
|
+
case 'ArrowDown':
|
|
290
|
+
event.preventDefault();
|
|
291
|
+
handleMenuArrowKeys(event.key);
|
|
292
|
+
break;
|
|
293
|
+
}
|
|
294
|
+
};
|
|
295
|
+
|
|
296
|
+
const onToggleClick = () => {
|
|
297
|
+
onToggle && onToggle(!isOpen);
|
|
298
|
+
setIsOpen(!isOpen);
|
|
299
|
+
textInputRef.current?.focus();
|
|
300
|
+
};
|
|
301
|
+
|
|
302
|
+
const onClearButtonClick = () => {
|
|
303
|
+
setSelected('');
|
|
304
|
+
setInputValue('');
|
|
305
|
+
onInputChange && onInputChange('');
|
|
306
|
+
setFilterValue('');
|
|
307
|
+
resetActiveAndFocusedItem();
|
|
308
|
+
textInputRef.current?.focus();
|
|
309
|
+
onClearSelection && onClearSelection();
|
|
310
|
+
};
|
|
311
|
+
|
|
312
|
+
const toggle = (toggleRef: React.Ref<MenuToggleElement>) => (
|
|
313
|
+
<MenuToggle
|
|
314
|
+
ref={toggleRef}
|
|
315
|
+
variant="typeahead"
|
|
316
|
+
aria-label="Typeahead menu toggle"
|
|
317
|
+
onClick={onToggleClick}
|
|
318
|
+
isExpanded={isOpen}
|
|
319
|
+
isDisabled={isDisabled}
|
|
320
|
+
isFullWidth
|
|
321
|
+
style={
|
|
322
|
+
{
|
|
323
|
+
width: toggleWidth
|
|
324
|
+
} as React.CSSProperties
|
|
325
|
+
}
|
|
326
|
+
{...toggleProps}
|
|
327
|
+
>
|
|
328
|
+
<TextInputGroup isPlain>
|
|
329
|
+
<TextInputGroupMain
|
|
330
|
+
value={inputValue}
|
|
331
|
+
onClick={onInputClick}
|
|
332
|
+
onChange={onTextInputChange}
|
|
333
|
+
onKeyDown={onInputKeyDown}
|
|
334
|
+
autoComplete="off"
|
|
335
|
+
innerRef={textInputRef}
|
|
336
|
+
placeholder={placeholder}
|
|
337
|
+
{...(activeItemId && { 'aria-activedescendant': activeItemId })}
|
|
338
|
+
role="combobox"
|
|
339
|
+
isExpanded={isOpen}
|
|
340
|
+
aria-controls="select-typeahead-listbox"
|
|
341
|
+
/>
|
|
342
|
+
|
|
343
|
+
<TextInputGroupUtilities {...(!inputValue ? { style: { display: 'none' } } : {})}>
|
|
344
|
+
<Button
|
|
345
|
+
variant="plain"
|
|
346
|
+
onClick={onClearButtonClick}
|
|
347
|
+
aria-label="Clear input value"
|
|
348
|
+
icon={<TimesIcon aria-hidden />}
|
|
349
|
+
/>
|
|
350
|
+
</TextInputGroupUtilities>
|
|
351
|
+
</TextInputGroup>
|
|
352
|
+
</MenuToggle>
|
|
353
|
+
);
|
|
354
|
+
|
|
355
|
+
return (
|
|
356
|
+
<Select
|
|
357
|
+
isOpen={isOpen}
|
|
358
|
+
selected={selected}
|
|
359
|
+
onSelect={_onSelect}
|
|
360
|
+
onOpenChange={(isOpen) => {
|
|
361
|
+
!isOpen && closeMenu();
|
|
362
|
+
}}
|
|
363
|
+
toggle={toggle}
|
|
364
|
+
shouldFocusFirstItemOnOpen={false}
|
|
365
|
+
ref={innerRef}
|
|
366
|
+
{...props}
|
|
367
|
+
>
|
|
368
|
+
<SelectList>
|
|
369
|
+
{selectOptions.map((option, index) => {
|
|
370
|
+
const { content, value, ...props } = option;
|
|
371
|
+
|
|
372
|
+
return (
|
|
373
|
+
<SelectOption key={value} value={value} isFocused={focusedItemIndex === index} {...props}>
|
|
374
|
+
{content}
|
|
375
|
+
</SelectOption>
|
|
376
|
+
);
|
|
377
|
+
})}
|
|
378
|
+
</SelectList>
|
|
379
|
+
</Select>
|
|
380
|
+
);
|
|
381
|
+
};
|
|
382
|
+
TypeaheadSelectBase.displayName = 'TypeaheadSelectBase';
|
|
383
|
+
|
|
384
|
+
export const TypeaheadSelect = React.forwardRef((props: TypeaheadSelectProps, ref: React.Ref<any>) => (
|
|
385
|
+
<TypeaheadSelectBase {...props} innerRef={ref} />
|
|
386
|
+
));
|
|
387
|
+
|
|
388
|
+
TypeaheadSelect.displayName = 'TypeaheadSelect';
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import * as React from 'react';
|
|
2
2
|
import { render, screen, waitForElementToBeRemoved } from '@testing-library/react';
|
|
3
3
|
import userEvent from '@testing-library/user-event';
|
|
4
|
-
import { CheckboxSelect } from '
|
|
4
|
+
import { CheckboxSelect } from '../CheckboxSelect';
|
|
5
5
|
import styles from '@patternfly/react-styles/css/components/Badge/badge';
|
|
6
6
|
|
|
7
7
|
test('renders checkbox select with options', async () => {
|
|
@@ -156,6 +156,34 @@ test('displays custom toggle content', async () => {
|
|
|
156
156
|
expect(toggleButton).toBeInTheDocument();
|
|
157
157
|
});
|
|
158
158
|
|
|
159
|
+
test('Passes toggleWidth', () => {
|
|
160
|
+
const initialOptions = [
|
|
161
|
+
{ content: 'Option 1', value: 'option1' },
|
|
162
|
+
{ content: 'Option 2', value: 'option2' },
|
|
163
|
+
{ content: 'Option 3', value: 'option3' }
|
|
164
|
+
];
|
|
165
|
+
|
|
166
|
+
render(<CheckboxSelect initialOptions={initialOptions} toggleContent="Custom Toggle" toggleWidth="500px" />);
|
|
167
|
+
|
|
168
|
+
const toggleButton = screen.getByRole('button', { name: 'Custom Toggle' });
|
|
169
|
+
expect(toggleButton).toHaveAttribute('style', 'width: 500px;');
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
test('Passes additional toggleProps', () => {
|
|
173
|
+
const initialOptions = [
|
|
174
|
+
{ content: 'Option 1', value: 'option1' },
|
|
175
|
+
{ content: 'Option 2', value: 'option2' },
|
|
176
|
+
{ content: 'Option 3', value: 'option3' }
|
|
177
|
+
];
|
|
178
|
+
|
|
179
|
+
render(
|
|
180
|
+
<CheckboxSelect initialOptions={initialOptions} toggleContent="Custom Toggle" toggleProps={{ id: 'toggle' }} />
|
|
181
|
+
);
|
|
182
|
+
|
|
183
|
+
const toggleButton = screen.getByRole('button', { name: 'Custom Toggle' });
|
|
184
|
+
expect(toggleButton).toHaveAttribute('id', 'toggle');
|
|
185
|
+
});
|
|
186
|
+
|
|
159
187
|
test('calls the onToggle callback when the select opens or closes', async () => {
|
|
160
188
|
const initialOptions = [
|
|
161
189
|
{ content: 'Option 1', value: 'option1' },
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import * as React from 'react';
|
|
2
2
|
import { render, screen } from '@testing-library/react';
|
|
3
3
|
import userEvent from '@testing-library/user-event';
|
|
4
|
-
import { CheckboxSelect } from '
|
|
4
|
+
import { CheckboxSelect } from '../CheckboxSelect';
|
|
5
5
|
|
|
6
6
|
jest.mock('@patternfly/react-core/dist/js/helpers/GenerateId/GenerateId', () => ({
|
|
7
7
|
GenerateId: ({ children }) => children('generated-id')
|