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