@patternfly/react-templates 6.0.0-alpha.4 → 6.0.0-alpha.40

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.
Files changed (93) hide show
  1. package/CHANGELOG.md +150 -0
  2. package/README.md +4 -17
  3. package/components/package.json +1 -1
  4. package/dist/dynamic/components/Dropdown/package.json +1 -0
  5. package/dist/dynamic/components/Select/package.json +1 -1
  6. package/dist/dynamic-modules.json +15 -0
  7. package/dist/esm/components/Dropdown/SimpleDropdown.d.ts +48 -0
  8. package/dist/esm/components/Dropdown/SimpleDropdown.d.ts.map +1 -0
  9. package/dist/esm/components/Dropdown/SimpleDropdown.js +33 -0
  10. package/dist/esm/components/Dropdown/SimpleDropdown.js.map +1 -0
  11. package/dist/esm/components/Dropdown/index.d.ts +2 -0
  12. package/dist/esm/components/Dropdown/index.d.ts.map +1 -0
  13. package/dist/esm/components/Dropdown/index.js +2 -0
  14. package/dist/esm/components/Dropdown/index.js.map +1 -0
  15. package/dist/esm/components/Select/CheckboxSelect.d.ts +7 -3
  16. package/dist/esm/components/Select/CheckboxSelect.d.ts.map +1 -1
  17. package/dist/esm/components/Select/CheckboxSelect.js +9 -6
  18. package/dist/esm/components/Select/CheckboxSelect.js.map +1 -1
  19. package/dist/esm/components/Select/{SelectSimple.d.ts → SimpleSelect.d.ts} +10 -7
  20. package/dist/esm/components/Select/SimpleSelect.d.ts.map +1 -0
  21. package/dist/esm/components/Select/{SelectSimple.js → SimpleSelect.js} +13 -9
  22. package/dist/esm/components/Select/SimpleSelect.js.map +1 -0
  23. package/dist/esm/components/Select/TypeaheadSelect.d.ts +33 -0
  24. package/dist/esm/components/Select/TypeaheadSelect.d.ts.map +1 -0
  25. package/dist/esm/components/Select/TypeaheadSelect.js +180 -0
  26. package/dist/esm/components/Select/TypeaheadSelect.js.map +1 -0
  27. package/dist/esm/components/Select/index.d.ts +2 -1
  28. package/dist/esm/components/Select/index.d.ts.map +1 -1
  29. package/dist/esm/components/Select/index.js +2 -1
  30. package/dist/esm/components/Select/index.js.map +1 -1
  31. package/dist/esm/components/index.d.ts +1 -0
  32. package/dist/esm/components/index.d.ts.map +1 -1
  33. package/dist/esm/components/index.js +1 -0
  34. package/dist/esm/components/index.js.map +1 -1
  35. package/dist/js/components/Dropdown/SimpleDropdown.d.ts +48 -0
  36. package/dist/js/components/Dropdown/SimpleDropdown.d.ts.map +1 -0
  37. package/dist/js/components/Dropdown/SimpleDropdown.js +36 -0
  38. package/dist/js/components/Dropdown/SimpleDropdown.js.map +1 -0
  39. package/dist/js/components/Dropdown/index.d.ts +2 -0
  40. package/dist/js/components/Dropdown/index.d.ts.map +1 -0
  41. package/dist/js/components/Dropdown/index.js +5 -0
  42. package/dist/js/components/Dropdown/index.js.map +1 -0
  43. package/dist/js/components/Select/CheckboxSelect.d.ts +7 -3
  44. package/dist/js/components/Select/CheckboxSelect.d.ts.map +1 -1
  45. package/dist/js/components/Select/CheckboxSelect.js +9 -6
  46. package/dist/js/components/Select/CheckboxSelect.js.map +1 -1
  47. package/dist/js/components/Select/{SelectSimple.d.ts → SimpleSelect.d.ts} +10 -7
  48. package/dist/js/components/Select/SimpleSelect.d.ts.map +1 -0
  49. package/dist/js/components/Select/{SelectSimple.js → SimpleSelect.js} +14 -10
  50. package/dist/js/components/Select/SimpleSelect.js.map +1 -0
  51. package/dist/js/components/Select/TypeaheadSelect.d.ts +33 -0
  52. package/dist/js/components/Select/TypeaheadSelect.d.ts.map +1 -0
  53. package/dist/js/components/Select/TypeaheadSelect.js +184 -0
  54. package/dist/js/components/Select/TypeaheadSelect.js.map +1 -0
  55. package/dist/js/components/Select/index.d.ts +2 -1
  56. package/dist/js/components/Select/index.d.ts.map +1 -1
  57. package/dist/js/components/Select/index.js +2 -1
  58. package/dist/js/components/Select/index.js.map +1 -1
  59. package/dist/js/components/index.d.ts +1 -0
  60. package/dist/js/components/index.d.ts.map +1 -1
  61. package/dist/js/components/index.js +1 -0
  62. package/dist/js/components/index.js.map +1 -1
  63. package/dist/umd/assets/output-lwv4P_-M.css +23421 -0
  64. package/dist/umd/react-templates.min.js +1 -1
  65. package/package.json +7 -11
  66. package/rollup.config.mjs +8 -0
  67. package/src/components/Dropdown/SimpleDropdown.tsx +139 -0
  68. package/src/components/Dropdown/__tests__/SimpleDropdown.test.tsx +266 -0
  69. package/src/components/Dropdown/__tests__/__snapshots__/SimpleDropdown.test.tsx.snap +81 -0
  70. package/src/components/Dropdown/examples/DropdownTemplates.md +24 -0
  71. package/src/components/Dropdown/examples/SimpleDropdownExample.tsx +53 -0
  72. package/src/components/Dropdown/index.ts +1 -0
  73. package/src/components/Select/CheckboxSelect.tsx +18 -7
  74. package/src/components/Select/{SelectSimple.tsx → SimpleSelect.tsx} +27 -14
  75. package/src/components/Select/TypeaheadSelect.tsx +329 -0
  76. package/src/components/Select/{CheckboxSelect.test.tsx → __tests__/CheckboxSelect.test.tsx} +29 -1
  77. package/src/components/Select/{CheckboxSelectSnapshots.test.tsx → __tests__/CheckboxSelectSnapshots.test.tsx} +1 -1
  78. package/src/components/Select/__tests__/SimpleSelect.test.tsx +241 -0
  79. package/src/components/Select/__tests__/TypeaheadSelect.test.tsx +408 -0
  80. package/src/components/Select/__tests__/__snapshots__/CheckboxSelectSnapshots.test.tsx.snap +83 -0
  81. package/src/components/Select/__tests__/__snapshots__/SimpleSelect.test.tsx.snap +83 -0
  82. package/src/components/Select/__tests__/__snapshots__/TypeaheadSelect.test.tsx.snap +97 -0
  83. package/src/components/Select/examples/CheckboxSelectDemo.tsx +1 -1
  84. package/src/components/Select/examples/SelectTemplates.md +11 -4
  85. package/src/components/Select/examples/{SelectSimpleDemo.tsx → SimpleSelectDemo.tsx} +6 -6
  86. package/src/components/Select/examples/TypeaheadSelectDemo.tsx +21 -0
  87. package/src/components/Select/index.ts +2 -1
  88. package/src/components/index.ts +1 -0
  89. package/dist/esm/components/Select/SelectSimple.d.ts.map +0 -1
  90. package/dist/esm/components/Select/SelectSimple.js.map +0 -1
  91. package/dist/js/components/Select/SelectSimple.d.ts.map +0 -1
  92. package/dist/js/components/Select/SelectSimple.js.map +0 -1
  93. package/src/components/Select/__snapshots__/CheckboxSelectSnapshots.test.tsx.snap +0 -212
@@ -0,0 +1,329 @@
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
+ /** Placeholder text for the select input. */
40
+ placeholder?: string;
41
+ /** Message to display when no options match the filter. */
42
+ noOptionsFoundMessage?: string | ((filter: string) => string);
43
+ /** Flag indicating the select should be disabled. */
44
+ isDisabled?: boolean;
45
+ /** Width of the toggle. */
46
+ toggleWidth?: string;
47
+ /** Additional props passed to the toggle. */
48
+ toggleProps?: MenuToggleProps;
49
+ }
50
+
51
+ export const TypeaheadSelectBase: React.FunctionComponent<TypeaheadSelectProps> = ({
52
+ innerRef,
53
+ initialOptions,
54
+ onSelect,
55
+ onToggle,
56
+ onInputChange,
57
+ placeholder = 'Select an option',
58
+ noOptionsFoundMessage = (filter) => `No results found for "${filter}"`,
59
+ isDisabled,
60
+ toggleWidth,
61
+ toggleProps,
62
+ ...props
63
+ }: TypeaheadSelectProps) => {
64
+ const [isOpen, setIsOpen] = React.useState(false);
65
+ const [selected, setSelected] = React.useState<string>('');
66
+ const [inputValue, setInputValue] = React.useState<string>('');
67
+ const [filterValue, setFilterValue] = React.useState<string>('');
68
+ const [selectOptions, setSelectOptions] = React.useState<TypeaheadSelectOption[]>(initialOptions);
69
+ const [focusedItemIndex, setFocusedItemIndex] = React.useState<number | null>(null);
70
+ const [activeItemId, setActiveItemId] = React.useState<string | null>(null);
71
+ const textInputRef = React.useRef<HTMLInputElement>();
72
+
73
+ const NO_RESULTS = 'no results';
74
+
75
+ React.useEffect(() => {
76
+ let newSelectOptions: TypeaheadSelectOption[] = initialOptions;
77
+
78
+ // Filter menu items based on the text input value when one exists
79
+ if (filterValue) {
80
+ newSelectOptions = initialOptions.filter((option) =>
81
+ String(option.content).toLowerCase().includes(filterValue.toLowerCase())
82
+ );
83
+
84
+ // When no options are found after filtering, display 'No results found'
85
+ if (!newSelectOptions.length) {
86
+ newSelectOptions = [
87
+ {
88
+ isAriaDisabled: true,
89
+ content:
90
+ typeof noOptionsFoundMessage === 'string' ? noOptionsFoundMessage : noOptionsFoundMessage(filterValue),
91
+ value: NO_RESULTS
92
+ }
93
+ ];
94
+ }
95
+
96
+ // Open the menu when the input value changes and the new value is not empty
97
+ if (!isOpen) {
98
+ openMenu();
99
+ }
100
+ }
101
+
102
+ setSelectOptions(newSelectOptions);
103
+ }, [filterValue, initialOptions]);
104
+
105
+ const setActiveAndFocusedItem = (itemIndex: number) => {
106
+ setFocusedItemIndex(itemIndex);
107
+ const focusedItem = selectOptions[itemIndex];
108
+ setActiveItemId(focusedItem.value as string);
109
+ };
110
+
111
+ const resetActiveAndFocusedItem = () => {
112
+ setFocusedItemIndex(null);
113
+ setActiveItemId(null);
114
+ };
115
+
116
+ const openMenu = () => {
117
+ onToggle && onToggle(true);
118
+ setIsOpen(true);
119
+ };
120
+
121
+ const closeMenu = () => {
122
+ onToggle && onToggle(false);
123
+ setIsOpen(false);
124
+ resetActiveAndFocusedItem();
125
+ };
126
+
127
+ const onInputClick = () => {
128
+ if (!isOpen) {
129
+ openMenu();
130
+ } else if (!inputValue) {
131
+ closeMenu();
132
+ }
133
+ };
134
+
135
+ const selectOption = (
136
+ _event: React.MouseEvent<Element, MouseEvent> | React.KeyboardEvent<HTMLInputElement> | undefined,
137
+ option: TypeaheadSelectOption
138
+ ) => {
139
+ onSelect && onSelect(_event, option.value);
140
+
141
+ setInputValue(String(option.content));
142
+ setFilterValue('');
143
+ setSelected(String(option.value));
144
+
145
+ closeMenu();
146
+ };
147
+
148
+ const _onSelect = (_event: React.MouseEvent<Element, MouseEvent> | undefined, value: string | number | undefined) => {
149
+ if (value && value !== NO_RESULTS) {
150
+ const optionToSelect = selectOptions.find((option) => option.value === value);
151
+ selectOption(_event, optionToSelect);
152
+ }
153
+ };
154
+
155
+ const onTextInputChange = (_event: React.FormEvent<HTMLInputElement>, value: string) => {
156
+ setInputValue(value);
157
+ onInputChange && onInputChange(value);
158
+ setFilterValue(value);
159
+
160
+ resetActiveAndFocusedItem();
161
+
162
+ if (value !== selected) {
163
+ setSelected('');
164
+ }
165
+ };
166
+
167
+ const handleMenuArrowKeys = (key: string) => {
168
+ let indexToFocus = 0;
169
+
170
+ if (!isOpen) {
171
+ openMenu();
172
+ }
173
+
174
+ if (selectOptions.every((option) => option.isDisabled)) {
175
+ return;
176
+ }
177
+
178
+ if (key === 'ArrowUp') {
179
+ // When no index is set or at the first index, focus to the last, otherwise decrement focus index
180
+ if (focusedItemIndex === null || focusedItemIndex === 0) {
181
+ indexToFocus = selectOptions.length - 1;
182
+ } else {
183
+ indexToFocus = focusedItemIndex - 1;
184
+ }
185
+
186
+ // Skip disabled options
187
+ while (selectOptions[indexToFocus].isDisabled) {
188
+ indexToFocus--;
189
+ if (indexToFocus === -1) {
190
+ indexToFocus = selectOptions.length - 1;
191
+ }
192
+ }
193
+ }
194
+
195
+ if (key === 'ArrowDown') {
196
+ // When no index is set or at the last index, focus to the first, otherwise increment focus index
197
+ if (focusedItemIndex === null || focusedItemIndex === selectOptions.length - 1) {
198
+ indexToFocus = 0;
199
+ } else {
200
+ indexToFocus = focusedItemIndex + 1;
201
+ }
202
+
203
+ // Skip disabled options
204
+ while (selectOptions[indexToFocus].isDisabled) {
205
+ indexToFocus++;
206
+ if (indexToFocus === selectOptions.length) {
207
+ indexToFocus = 0;
208
+ }
209
+ }
210
+ }
211
+
212
+ setActiveAndFocusedItem(indexToFocus);
213
+ };
214
+
215
+ const onInputKeyDown = (event: React.KeyboardEvent<HTMLInputElement>) => {
216
+ const focusedItem = focusedItemIndex !== null ? selectOptions[focusedItemIndex] : null;
217
+
218
+ switch (event.key) {
219
+ case 'Enter':
220
+ if (isOpen && focusedItem && focusedItem.value !== NO_RESULTS && !focusedItem.isAriaDisabled) {
221
+ selectOption(event, focusedItem);
222
+ }
223
+
224
+ if (!isOpen) {
225
+ onToggle && onToggle(true);
226
+ setIsOpen(true);
227
+ }
228
+
229
+ break;
230
+ case 'ArrowUp':
231
+ case 'ArrowDown':
232
+ event.preventDefault();
233
+ handleMenuArrowKeys(event.key);
234
+ break;
235
+ }
236
+ };
237
+
238
+ const onToggleClick = () => {
239
+ onToggle && onToggle(!isOpen);
240
+ setIsOpen(!isOpen);
241
+ textInputRef?.current?.focus();
242
+ };
243
+
244
+ const onClearButtonClick = () => {
245
+ setSelected('');
246
+ setInputValue('');
247
+ onInputChange && onInputChange('');
248
+ setFilterValue('');
249
+ resetActiveAndFocusedItem();
250
+ textInputRef?.current?.focus();
251
+ };
252
+
253
+ const toggle = (toggleRef: React.Ref<MenuToggleElement>) => (
254
+ <MenuToggle
255
+ ref={toggleRef}
256
+ variant="typeahead"
257
+ aria-label="Typeahead menu toggle"
258
+ onClick={onToggleClick}
259
+ isExpanded={isOpen}
260
+ isDisabled={isDisabled}
261
+ isFullWidth
262
+ style={
263
+ {
264
+ width: toggleWidth
265
+ } as React.CSSProperties
266
+ }
267
+ {...toggleProps}
268
+ >
269
+ <TextInputGroup isPlain>
270
+ <TextInputGroupMain
271
+ value={inputValue}
272
+ onClick={onInputClick}
273
+ onChange={onTextInputChange}
274
+ onKeyDown={onInputKeyDown}
275
+ autoComplete="off"
276
+ innerRef={textInputRef}
277
+ placeholder={placeholder}
278
+ {...(activeItemId && { 'aria-activedescendant': activeItemId })}
279
+ role="combobox"
280
+ isExpanded={isOpen}
281
+ aria-controls="select-typeahead-listbox"
282
+ />
283
+
284
+ <TextInputGroupUtilities {...(!inputValue ? { style: { display: 'none' } } : {})}>
285
+ <Button
286
+ variant="plain"
287
+ onClick={onClearButtonClick}
288
+ aria-label="Clear input value"
289
+ icon={<TimesIcon aria-hidden />}
290
+ />
291
+ </TextInputGroupUtilities>
292
+ </TextInputGroup>
293
+ </MenuToggle>
294
+ );
295
+
296
+ return (
297
+ <Select
298
+ isOpen={isOpen}
299
+ selected={selected}
300
+ onSelect={_onSelect}
301
+ onOpenChange={(isOpen) => {
302
+ !isOpen && closeMenu();
303
+ }}
304
+ toggle={toggle}
305
+ shouldFocusFirstItemOnOpen={false}
306
+ ref={innerRef}
307
+ {...props}
308
+ >
309
+ <SelectList>
310
+ {selectOptions.map((option, index) => {
311
+ const { content, value, ...props } = option;
312
+
313
+ return (
314
+ <SelectOption key={value} value={value} isFocused={focusedItemIndex === index} {...props}>
315
+ {content}
316
+ </SelectOption>
317
+ );
318
+ })}
319
+ </SelectList>
320
+ </Select>
321
+ );
322
+ };
323
+ TypeaheadSelectBase.displayName = 'TypeaheadSelectBase';
324
+
325
+ export const TypeaheadSelect = React.forwardRef((props: TypeaheadSelectProps, ref: React.Ref<any>) => (
326
+ <TypeaheadSelectBase {...props} innerRef={ref} />
327
+ ));
328
+
329
+ 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 './CheckboxSelect';
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 './CheckboxSelect';
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')
@@ -0,0 +1,241 @@
1
+ import * as React from 'react';
2
+ import { render, screen, waitForElementToBeRemoved } from '@testing-library/react';
3
+ import userEvent from '@testing-library/user-event';
4
+ import { SimpleSelect } from '../SimpleSelect';
5
+
6
+ test('renders simple select with options', async () => {
7
+ const initialOptions = [
8
+ { content: 'Option 1', value: 'option1' },
9
+ { content: 'Option 2', value: 'option2' },
10
+ { content: 'Option 3', value: 'option3' }
11
+ ];
12
+
13
+ const user = userEvent.setup();
14
+
15
+ render(<SimpleSelect initialOptions={initialOptions} />);
16
+
17
+ const toggle = screen.getByRole('button', { name: 'Select a value' });
18
+
19
+ await user.click(toggle);
20
+
21
+ const option1 = screen.getByText('Option 1');
22
+ const option2 = screen.getByText('Option 2');
23
+ const option3 = screen.getByText('Option 3');
24
+
25
+ expect(option1).toBeInTheDocument();
26
+ expect(option2).toBeInTheDocument();
27
+ expect(option3).toBeInTheDocument();
28
+ });
29
+
30
+ test('selects options when clicked', async () => {
31
+ const initialOptions = [
32
+ { content: 'Option 1', value: 'option1' },
33
+ { content: 'Option 2', value: 'option2' },
34
+ { content: 'Option 3', value: 'option3' }
35
+ ];
36
+
37
+ const user = userEvent.setup();
38
+
39
+ render(<SimpleSelect initialOptions={initialOptions} />);
40
+
41
+ const toggle = screen.getByRole('button', { name: 'Select a value' });
42
+
43
+ await user.click(toggle);
44
+
45
+ const option1 = screen.getByText('Option 1');
46
+ await user.click(option1);
47
+
48
+ expect(option1).toBeInTheDocument();
49
+ });
50
+
51
+ test('calls the onSelect callback with the selected value when an option is selected', async () => {
52
+ const initialOptions = [
53
+ { content: 'Option 1', value: 'option1' },
54
+ { content: 'Option 2', value: 'option2' },
55
+ { content: 'Option 3', value: 'option3' }
56
+ ];
57
+
58
+ const user = userEvent.setup();
59
+ const onSelectMock = jest.fn();
60
+
61
+ render(<SimpleSelect initialOptions={initialOptions} onSelect={onSelectMock} />);
62
+
63
+ const toggle = screen.getByRole('button', { name: 'Select a value' });
64
+
65
+ await user.click(toggle);
66
+
67
+ expect(onSelectMock).not.toHaveBeenCalled();
68
+
69
+ const option1 = screen.getByText('Option 1');
70
+
71
+ await user.click(option1);
72
+
73
+ expect(onSelectMock).toHaveBeenCalledTimes(1);
74
+ expect(onSelectMock).toHaveBeenCalledWith(expect.anything(), 'option1');
75
+ });
76
+
77
+ test('toggles the select menu when the toggle button is clicked', async () => {
78
+ const initialOptions = [
79
+ { content: 'Option 1', value: 'option1' },
80
+ { content: 'Option 2', value: 'option2' },
81
+ { content: 'Option 3', value: 'option3' }
82
+ ];
83
+
84
+ const user = userEvent.setup();
85
+
86
+ render(<SimpleSelect initialOptions={initialOptions} />);
87
+
88
+ const toggleButton = screen.getByRole('button', { name: 'Select a value' });
89
+
90
+ await user.click(toggleButton);
91
+
92
+ expect(screen.getByRole('listbox')).toBeInTheDocument();
93
+
94
+ await user.click(toggleButton);
95
+
96
+ await waitForElementToBeRemoved(() => screen.queryByRole('listbox'));
97
+
98
+ expect(screen.queryByRole('listbox')).not.toBeInTheDocument();
99
+ });
100
+
101
+ test('displays custom toggle content', async () => {
102
+ const initialOptions = [
103
+ { content: 'Option 1', value: 'option1' },
104
+ { content: 'Option 2', value: 'option2' },
105
+ { content: 'Option 3', value: 'option3' }
106
+ ];
107
+
108
+ render(<SimpleSelect initialOptions={initialOptions} toggleContent="Custom Toggle" />);
109
+
110
+ const toggleButton = screen.getByRole('button', { name: 'Custom Toggle' });
111
+
112
+ expect(toggleButton).toBeInTheDocument();
113
+ });
114
+
115
+ test('Passes toggleWidth', () => {
116
+ const initialOptions = [
117
+ { content: 'Option 1', value: 'option1' },
118
+ { content: 'Option 2', value: 'option2' },
119
+ { content: 'Option 3', value: 'option3' }
120
+ ];
121
+
122
+ render(<SimpleSelect initialOptions={initialOptions} toggleContent="Custom Toggle" toggleWidth="500px" />);
123
+
124
+ const toggleButton = screen.getByRole('button', { name: 'Custom Toggle' });
125
+ expect(toggleButton).toHaveAttribute('style', 'width: 500px;');
126
+ });
127
+
128
+ test('Passes additional toggleProps', () => {
129
+ const initialOptions = [
130
+ { content: 'Option 1', value: 'option1' },
131
+ { content: 'Option 2', value: 'option2' },
132
+ { content: 'Option 3', value: 'option3' }
133
+ ];
134
+
135
+ render(<SimpleSelect initialOptions={initialOptions} toggleContent="Custom Toggle" toggleProps={{ id: 'toggle' }} />);
136
+
137
+ const toggleButton = screen.getByRole('button', { name: 'Custom Toggle' });
138
+ expect(toggleButton).toHaveAttribute('id', 'toggle');
139
+ });
140
+
141
+ test('calls the onToggle callback when the select opens or closes', async () => {
142
+ const initialOptions = [
143
+ { content: 'Option 1', value: 'option1' },
144
+ { content: 'Option 2', value: 'option2' },
145
+ { content: 'Option 3', value: 'option3' }
146
+ ];
147
+
148
+ const user = userEvent.setup();
149
+ const onToggleMock = jest.fn();
150
+
151
+ render(<SimpleSelect initialOptions={initialOptions} onToggle={onToggleMock} />);
152
+
153
+ const toggle = screen.getByRole('button', { name: 'Select a value' });
154
+
155
+ await user.click(toggle);
156
+
157
+ expect(onToggleMock).toHaveBeenCalledTimes(1);
158
+ expect(onToggleMock).toHaveBeenCalledWith(true);
159
+
160
+ await user.click(toggle);
161
+
162
+ expect(onToggleMock).toHaveBeenCalledTimes(2);
163
+ expect(onToggleMock).toHaveBeenCalledWith(false);
164
+ });
165
+
166
+ test('does not call the onToggle callback when the toggle is not clicked', async () => {
167
+ const initialOptions = [
168
+ { content: 'Option 1', value: 'option1' },
169
+ { content: 'Option 2', value: 'option2' },
170
+ { content: 'Option 3', value: 'option3' }
171
+ ];
172
+
173
+ const onToggleMock = jest.fn();
174
+
175
+ render(<SimpleSelect initialOptions={initialOptions} onToggle={onToggleMock} />);
176
+
177
+ expect(onToggleMock).not.toHaveBeenCalled();
178
+ });
179
+
180
+ test('disables the select when isDisabled prop is true', async () => {
181
+ const initialOptions = [
182
+ { content: 'Option 1', value: 'option1' },
183
+ { content: 'Option 2', value: 'option2' },
184
+ { content: 'Option 3', value: 'option3' }
185
+ ];
186
+
187
+ const user = userEvent.setup();
188
+
189
+ render(<SimpleSelect initialOptions={initialOptions} isDisabled={true} />);
190
+
191
+ const toggleButton = screen.getByRole('button', { name: 'Select a value' });
192
+
193
+ expect(toggleButton).toBeDisabled();
194
+
195
+ await user.click(toggleButton);
196
+
197
+ expect(screen.queryByRole('menu')).not.toBeInTheDocument();
198
+ });
199
+
200
+ test('passes other SelectOption props to the SelectOption component', async () => {
201
+ const initialOptions = [{ content: 'Option 1', value: 'option1', isDisabled: true }];
202
+
203
+ const user = userEvent.setup();
204
+
205
+ render(<SimpleSelect initialOptions={initialOptions} />);
206
+
207
+ const toggle = screen.getByRole('button', { name: 'Select a value' });
208
+
209
+ await user.click(toggle);
210
+
211
+ const option1 = screen.getByRole('option', { name: 'Option 1' });
212
+
213
+ expect(option1).toBeDisabled();
214
+ });
215
+
216
+ jest.mock('@patternfly/react-core/dist/js/helpers/GenerateId/GenerateId', () => ({
217
+ GenerateId: ({ children }) => children('generated-id')
218
+ }));
219
+
220
+ test('checkbox select with no props snapshot', () => {
221
+ const { asFragment } = render(<SimpleSelect />);
222
+
223
+ expect(asFragment()).toMatchSnapshot();
224
+ });
225
+
226
+ test('Matches snapshot', async () => {
227
+ const initialOptions = [
228
+ { content: 'Option 1', value: 'option1' },
229
+ { content: 'Option 2', value: 'option2' },
230
+ { content: 'Option 3', value: 'option3' }
231
+ ];
232
+
233
+ const user = userEvent.setup();
234
+
235
+ const { asFragment } = render(<SimpleSelect initialOptions={initialOptions} toggleContent="Select" />);
236
+
237
+ const toggle = screen.getByRole('button', { name: 'Select' });
238
+ await user.click(toggle);
239
+
240
+ expect(asFragment()).toMatchSnapshot();
241
+ });