Skip to content

Commit d8fa6b2

Browse files
adamviktoramattnolting
authored andcommitted
feat(MultiTypeaheadSelect): add initialInputValue prop (patternfly#11791)
1 parent d2541de commit d8fa6b2

File tree

3 files changed

+41
-7
lines changed

3 files changed

+41
-7
lines changed

packages/react-templates/src/components/Select/MultiTypeaheadSelect.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ export interface MultiTypeaheadSelectProps extends Omit<SelectProps, 'toggle' |
5353
toggleProps?: MenuToggleProps;
5454
/** Additional props passed to each label of the selected option. */
5555
labelProps?: LabelProps;
56+
/** Initial value of the typeahead text input. */
57+
initialInputValue?: string;
5658
}
5759

5860
export const MultiTypeaheadSelectBase: React.FunctionComponent<MultiTypeaheadSelectProps> = ({
@@ -68,13 +70,14 @@ export const MultiTypeaheadSelectBase: React.FunctionComponent<MultiTypeaheadSel
6870
toggleWidth,
6971
toggleProps,
7072
labelProps,
73+
initialInputValue,
7174
...props
7275
}: MultiTypeaheadSelectProps) => {
7376
const [isOpen, setIsOpen] = useState(false);
7477
const [selected, setSelected] = useState<(string | number)[]>(
7578
(initialOptions?.filter((o) => o.selected) ?? []).map((o) => o.value)
7679
);
77-
const [inputValue, setInputValue] = useState<string>();
80+
const [inputValue, setInputValue] = useState<string>(initialInputValue);
7881
const [selectOptions, setSelectOptions] = useState<MultiTypeaheadSelectOption[]>(initialOptions);
7982
const [focusedItemIndex, setFocusedItemIndex] = useState<number | null>(null);
8083
const [activeItemId, setActiveItemId] = useState<string | null>(null);
@@ -107,9 +110,6 @@ export const MultiTypeaheadSelectBase: React.FunctionComponent<MultiTypeaheadSel
107110
}
108111
];
109112
}
110-
111-
// Open the menu when the input value changes and the new value is not empty
112-
openMenu();
113113
}
114114

115115
setSelectOptions(newSelectOptions);
@@ -173,6 +173,10 @@ export const MultiTypeaheadSelectBase: React.FunctionComponent<MultiTypeaheadSel
173173
setInputValue(value);
174174
onInputChange && onInputChange(value);
175175

176+
if (value && !isOpen) {
177+
openMenu();
178+
}
179+
176180
resetActiveAndFocusedItem();
177181
};
178182

packages/react-templates/src/components/Select/__tests__/MultiTypeaheadSelect.test.tsx

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { render, screen, waitForElementToBeRemoved } from '@testing-library/react';
1+
import { render, screen, waitForElementToBeRemoved, within } from '@testing-library/react';
22
import userEvent from '@testing-library/user-event';
33
import { MultiTypeaheadSelect } from '../MultiTypeaheadSelect';
44
import styles from '@patternfly/react-styles/css/components/Menu/menu';
@@ -413,6 +413,36 @@ describe('MultiTypeaheadSelect', () => {
413413
expect(onInputKeyDownMock).toHaveBeenCalledTimes(1);
414414
});
415415

416+
it('text input is empty by default', async () => {
417+
render(<MultiTypeaheadSelect initialOptions={[]} />);
418+
419+
const input = screen.getByRole('combobox');
420+
expect(input).toHaveValue('');
421+
});
422+
423+
it('initialInputValue prop sets the default text input value', async () => {
424+
const initialOptions = [
425+
{ content: 'Option 1', value: 'option1' },
426+
{ content: 'Option 2', value: 'option2' },
427+
{ content: 'Option 3', value: 'option3' }
428+
];
429+
430+
const user = userEvent.setup();
431+
432+
render(<MultiTypeaheadSelect initialInputValue={'Option 1'} initialOptions={initialOptions} />);
433+
434+
const input = screen.getByRole('combobox');
435+
expect(input).toHaveValue('Option 1');
436+
437+
await user.click(input);
438+
439+
const menu = screen.getByRole('listbox');
440+
const options = within(menu).getAllByRole('option');
441+
442+
expect(options).toHaveLength(1);
443+
expect(options[0]).toHaveTextContent('Option 1');
444+
});
445+
416446
it('Matches snapshot', async () => {
417447
const initialOptions = [
418448
{ content: 'Option 1', value: 'option1' },

packages/react-templates/src/components/Select/__tests__/__snapshots__/MultiTypeaheadSelect.test.tsx.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ exports[`MultiTypeaheadSelect Matches snapshot 1`] = `
3434
<button
3535
aria-label="Clear input value"
3636
class="pf-v6-c-button pf-m-plain"
37-
data-ouia-component-id="OUIA-Generated-Button-plain-21"
37+
data-ouia-component-id="OUIA-Generated-Button-plain-23"
3838
data-ouia-component-type="PF6/Button"
3939
data-ouia-safe="true"
4040
type="button"
@@ -63,7 +63,7 @@ exports[`MultiTypeaheadSelect Matches snapshot 1`] = `
6363
aria-expanded="true"
6464
aria-label="Multi select Typeahead menu toggle"
6565
class="pf-v6-c-menu-toggle__button"
66-
data-ouia-component-id="OUIA-Generated-MenuToggle-typeahead-18"
66+
data-ouia-component-id="OUIA-Generated-MenuToggle-typeahead-20"
6767
data-ouia-component-type="PF6/MenuToggle"
6868
data-ouia-safe="true"
6969
tabindex="-1"

0 commit comments

Comments
 (0)