@patternfly/react-templates 1.1.0-prerelease.3 → 1.1.0-prerelease.31
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 +132 -0
- package/README.md +2 -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 +14 -3
- 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 +35 -0
- package/dist/esm/components/Select/TypeaheadSelect.d.ts.map +1 -0
- package/dist/esm/components/Select/TypeaheadSelect.js +194 -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 +35 -0
- package/dist/js/components/Select/TypeaheadSelect.d.ts.map +1 -0
- package/dist/js/components/Select/TypeaheadSelect.js +198 -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-DV5sTbNI.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/SimpleDropdown.tsx +139 -0
- package/src/components/Dropdown/__tests__/SimpleDropdown.test.tsx +266 -0
- package/src/components/Dropdown/__tests__/__snapshots__/SimpleDropdown.test.tsx.snap +153 -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 +342 -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/{__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 +17 -4
- package/src/components/Select/examples/SimpleSelectDemo.tsx +36 -0
- package/src/components/Select/examples/TypeaheadSelectDemo.tsx +35 -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/examples/SelectSimpleDemo.tsx +0 -26
|
@@ -0,0 +1,342 @@
|
|
|
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'> {
|
|
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>,
|
|
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
|
+
/** Message to display when no options match the filter. */
|
|
44
|
+
noOptionsFoundMessage?: string | ((filter: string) => string);
|
|
45
|
+
/** Flag indicating the select should be disabled. */
|
|
46
|
+
isDisabled?: boolean;
|
|
47
|
+
/** Width of the toggle. */
|
|
48
|
+
toggleWidth?: string;
|
|
49
|
+
/** Additional props passed to the toggle. */
|
|
50
|
+
toggleProps?: MenuToggleProps;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export const TypeaheadSelectBase: React.FunctionComponent<TypeaheadSelectProps> = ({
|
|
54
|
+
innerRef,
|
|
55
|
+
initialOptions,
|
|
56
|
+
onSelect,
|
|
57
|
+
onToggle,
|
|
58
|
+
onInputChange,
|
|
59
|
+
onClearSelection,
|
|
60
|
+
placeholder = 'Select an option',
|
|
61
|
+
noOptionsFoundMessage = (filter) => `No results found for "${filter}"`,
|
|
62
|
+
isDisabled,
|
|
63
|
+
toggleWidth,
|
|
64
|
+
toggleProps,
|
|
65
|
+
...props
|
|
66
|
+
}: TypeaheadSelectProps) => {
|
|
67
|
+
const [isOpen, setIsOpen] = React.useState(false);
|
|
68
|
+
const [selected, setSelected] = React.useState<string>(String(initialOptions.find((o) => o.selected)?.content ?? ''));
|
|
69
|
+
const [inputValue, setInputValue] = React.useState<string>(
|
|
70
|
+
String(initialOptions.find((o) => o.selected)?.content ?? '')
|
|
71
|
+
);
|
|
72
|
+
const [filterValue, setFilterValue] = React.useState<string>('');
|
|
73
|
+
const [selectOptions, setSelectOptions] = React.useState<TypeaheadSelectOption[]>(initialOptions);
|
|
74
|
+
const [focusedItemIndex, setFocusedItemIndex] = React.useState<number | null>(null);
|
|
75
|
+
const [activeItemId, setActiveItemId] = React.useState<string | null>(null);
|
|
76
|
+
const textInputRef = React.useRef<HTMLInputElement>();
|
|
77
|
+
|
|
78
|
+
const NO_RESULTS = 'no results';
|
|
79
|
+
|
|
80
|
+
React.useEffect(() => {
|
|
81
|
+
let newSelectOptions: TypeaheadSelectOption[] = initialOptions;
|
|
82
|
+
|
|
83
|
+
// Filter menu items based on the text input value when one exists
|
|
84
|
+
if (filterValue) {
|
|
85
|
+
newSelectOptions = initialOptions.filter((option) =>
|
|
86
|
+
String(option.content).toLowerCase().includes(filterValue.toLowerCase())
|
|
87
|
+
);
|
|
88
|
+
|
|
89
|
+
// When no options are found after filtering, display 'No results found'
|
|
90
|
+
if (!newSelectOptions.length) {
|
|
91
|
+
newSelectOptions = [
|
|
92
|
+
{
|
|
93
|
+
isAriaDisabled: true,
|
|
94
|
+
content:
|
|
95
|
+
typeof noOptionsFoundMessage === 'string' ? noOptionsFoundMessage : noOptionsFoundMessage(filterValue),
|
|
96
|
+
value: NO_RESULTS
|
|
97
|
+
}
|
|
98
|
+
];
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// Open the menu when the input value changes and the new value is not empty
|
|
102
|
+
if (!isOpen) {
|
|
103
|
+
openMenu();
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
setSelectOptions(newSelectOptions);
|
|
108
|
+
}, [filterValue, initialOptions]);
|
|
109
|
+
|
|
110
|
+
React.useEffect(() => {
|
|
111
|
+
const selectedOption = initialOptions.find((o) => o.selected);
|
|
112
|
+
if (selectedOption?.value !== selected) {
|
|
113
|
+
setInputValue(String(selectedOption?.content ?? ''));
|
|
114
|
+
setSelected(String(selectedOption?.content ?? ''));
|
|
115
|
+
}
|
|
116
|
+
// Do not update when selected changes, only if the given options have changed
|
|
117
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
118
|
+
}, [initialOptions]);
|
|
119
|
+
|
|
120
|
+
const setActiveAndFocusedItem = (itemIndex: number) => {
|
|
121
|
+
setFocusedItemIndex(itemIndex);
|
|
122
|
+
const focusedItem = selectOptions[itemIndex];
|
|
123
|
+
setActiveItemId(focusedItem.value as string);
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
const resetActiveAndFocusedItem = () => {
|
|
127
|
+
setFocusedItemIndex(null);
|
|
128
|
+
setActiveItemId(null);
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
const openMenu = () => {
|
|
132
|
+
onToggle && onToggle(true);
|
|
133
|
+
setIsOpen(true);
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
const closeMenu = () => {
|
|
137
|
+
onToggle && onToggle(false);
|
|
138
|
+
setIsOpen(false);
|
|
139
|
+
resetActiveAndFocusedItem();
|
|
140
|
+
const option = initialOptions.find((o) => o.value === selected);
|
|
141
|
+
if (option) {
|
|
142
|
+
setInputValue(String(option.content));
|
|
143
|
+
}
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
const onInputClick = () => {
|
|
147
|
+
if (!isOpen) {
|
|
148
|
+
openMenu();
|
|
149
|
+
} else if (!inputValue) {
|
|
150
|
+
closeMenu();
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
const selectOption = (
|
|
155
|
+
_event: React.MouseEvent<Element, MouseEvent> | React.KeyboardEvent<HTMLInputElement> | undefined,
|
|
156
|
+
option: TypeaheadSelectOption
|
|
157
|
+
) => {
|
|
158
|
+
onSelect && onSelect(_event, option.value);
|
|
159
|
+
|
|
160
|
+
setInputValue(String(option.content));
|
|
161
|
+
setFilterValue('');
|
|
162
|
+
setSelected(String(option.value));
|
|
163
|
+
|
|
164
|
+
closeMenu();
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
const _onSelect = (_event: React.MouseEvent<Element, MouseEvent> | undefined, value: string | number | undefined) => {
|
|
168
|
+
if (value && value !== NO_RESULTS) {
|
|
169
|
+
const optionToSelect = selectOptions.find((option) => option.value === value);
|
|
170
|
+
selectOption(_event, optionToSelect);
|
|
171
|
+
}
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
const onTextInputChange = (_event: React.FormEvent<HTMLInputElement>, value: string) => {
|
|
175
|
+
setInputValue(value);
|
|
176
|
+
onInputChange && onInputChange(value);
|
|
177
|
+
setFilterValue(value);
|
|
178
|
+
|
|
179
|
+
resetActiveAndFocusedItem();
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
const handleMenuArrowKeys = (key: string) => {
|
|
183
|
+
let indexToFocus = 0;
|
|
184
|
+
|
|
185
|
+
if (!isOpen) {
|
|
186
|
+
openMenu();
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
if (selectOptions.every((option) => option.isDisabled)) {
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
if (key === 'ArrowUp') {
|
|
194
|
+
// When no index is set or at the first index, focus to the last, otherwise decrement focus index
|
|
195
|
+
if (focusedItemIndex === null || focusedItemIndex === 0) {
|
|
196
|
+
indexToFocus = selectOptions.length - 1;
|
|
197
|
+
} else {
|
|
198
|
+
indexToFocus = focusedItemIndex - 1;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// Skip disabled options
|
|
202
|
+
while (selectOptions[indexToFocus].isDisabled) {
|
|
203
|
+
indexToFocus--;
|
|
204
|
+
if (indexToFocus === -1) {
|
|
205
|
+
indexToFocus = selectOptions.length - 1;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
if (key === 'ArrowDown') {
|
|
211
|
+
// When no index is set or at the last index, focus to the first, otherwise increment focus index
|
|
212
|
+
if (focusedItemIndex === null || focusedItemIndex === selectOptions.length - 1) {
|
|
213
|
+
indexToFocus = 0;
|
|
214
|
+
} else {
|
|
215
|
+
indexToFocus = focusedItemIndex + 1;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
// Skip disabled options
|
|
219
|
+
while (selectOptions[indexToFocus].isDisabled) {
|
|
220
|
+
indexToFocus++;
|
|
221
|
+
if (indexToFocus === selectOptions.length) {
|
|
222
|
+
indexToFocus = 0;
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
setActiveAndFocusedItem(indexToFocus);
|
|
228
|
+
};
|
|
229
|
+
|
|
230
|
+
const onInputKeyDown = (event: React.KeyboardEvent<HTMLInputElement>) => {
|
|
231
|
+
const focusedItem = focusedItemIndex !== null ? selectOptions[focusedItemIndex] : null;
|
|
232
|
+
|
|
233
|
+
switch (event.key) {
|
|
234
|
+
case 'Enter':
|
|
235
|
+
if (isOpen && focusedItem && focusedItem.value !== NO_RESULTS && !focusedItem.isAriaDisabled) {
|
|
236
|
+
selectOption(event, focusedItem);
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
if (!isOpen) {
|
|
240
|
+
onToggle && onToggle(true);
|
|
241
|
+
setIsOpen(true);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
break;
|
|
245
|
+
case 'ArrowUp':
|
|
246
|
+
case 'ArrowDown':
|
|
247
|
+
event.preventDefault();
|
|
248
|
+
handleMenuArrowKeys(event.key);
|
|
249
|
+
break;
|
|
250
|
+
}
|
|
251
|
+
};
|
|
252
|
+
|
|
253
|
+
const onToggleClick = () => {
|
|
254
|
+
onToggle && onToggle(!isOpen);
|
|
255
|
+
setIsOpen(!isOpen);
|
|
256
|
+
textInputRef?.current?.focus();
|
|
257
|
+
};
|
|
258
|
+
|
|
259
|
+
const onClearButtonClick = () => {
|
|
260
|
+
setSelected('');
|
|
261
|
+
setInputValue('');
|
|
262
|
+
onInputChange && onInputChange('');
|
|
263
|
+
setFilterValue('');
|
|
264
|
+
resetActiveAndFocusedItem();
|
|
265
|
+
textInputRef?.current?.focus();
|
|
266
|
+
onClearSelection && onClearSelection();
|
|
267
|
+
};
|
|
268
|
+
|
|
269
|
+
const toggle = (toggleRef: React.Ref<MenuToggleElement>) => (
|
|
270
|
+
<MenuToggle
|
|
271
|
+
ref={toggleRef}
|
|
272
|
+
variant="typeahead"
|
|
273
|
+
aria-label="Typeahead menu toggle"
|
|
274
|
+
onClick={onToggleClick}
|
|
275
|
+
isExpanded={isOpen}
|
|
276
|
+
isDisabled={isDisabled}
|
|
277
|
+
isFullWidth
|
|
278
|
+
style={
|
|
279
|
+
{
|
|
280
|
+
width: toggleWidth
|
|
281
|
+
} as React.CSSProperties
|
|
282
|
+
}
|
|
283
|
+
{...toggleProps}
|
|
284
|
+
>
|
|
285
|
+
<TextInputGroup isPlain>
|
|
286
|
+
<TextInputGroupMain
|
|
287
|
+
value={inputValue}
|
|
288
|
+
onClick={onInputClick}
|
|
289
|
+
onChange={onTextInputChange}
|
|
290
|
+
onKeyDown={onInputKeyDown}
|
|
291
|
+
autoComplete="off"
|
|
292
|
+
innerRef={textInputRef}
|
|
293
|
+
placeholder={placeholder}
|
|
294
|
+
{...(activeItemId && { 'aria-activedescendant': activeItemId })}
|
|
295
|
+
role="combobox"
|
|
296
|
+
isExpanded={isOpen}
|
|
297
|
+
aria-controls="select-typeahead-listbox"
|
|
298
|
+
/>
|
|
299
|
+
|
|
300
|
+
<TextInputGroupUtilities {...(!inputValue ? { style: { display: 'none' } } : {})}>
|
|
301
|
+
<Button variant="plain" onClick={onClearButtonClick} aria-label="Clear input value">
|
|
302
|
+
<TimesIcon aria-hidden />
|
|
303
|
+
</Button>
|
|
304
|
+
</TextInputGroupUtilities>
|
|
305
|
+
</TextInputGroup>
|
|
306
|
+
</MenuToggle>
|
|
307
|
+
);
|
|
308
|
+
|
|
309
|
+
return (
|
|
310
|
+
<Select
|
|
311
|
+
isOpen={isOpen}
|
|
312
|
+
selected={selected}
|
|
313
|
+
onSelect={_onSelect}
|
|
314
|
+
onOpenChange={(isOpen) => {
|
|
315
|
+
!isOpen && closeMenu();
|
|
316
|
+
}}
|
|
317
|
+
toggle={toggle}
|
|
318
|
+
shouldFocusFirstItemOnOpen={false}
|
|
319
|
+
ref={innerRef}
|
|
320
|
+
{...props}
|
|
321
|
+
>
|
|
322
|
+
<SelectList>
|
|
323
|
+
{selectOptions.map((option, index) => {
|
|
324
|
+
const { content, value, ...props } = option;
|
|
325
|
+
|
|
326
|
+
return (
|
|
327
|
+
<SelectOption key={value} value={value} isFocused={focusedItemIndex === index} {...props}>
|
|
328
|
+
{content}
|
|
329
|
+
</SelectOption>
|
|
330
|
+
);
|
|
331
|
+
})}
|
|
332
|
+
</SelectList>
|
|
333
|
+
</Select>
|
|
334
|
+
);
|
|
335
|
+
};
|
|
336
|
+
TypeaheadSelectBase.displayName = 'TypeaheadSelectBase';
|
|
337
|
+
|
|
338
|
+
export const TypeaheadSelect = React.forwardRef((props: TypeaheadSelectProps, ref: React.Ref<any>) => (
|
|
339
|
+
<TypeaheadSelectBase {...props} innerRef={ref} />
|
|
340
|
+
));
|
|
341
|
+
|
|
342
|
+
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')
|