Skip to content

Commit 15cbfae

Browse files
committed
feat(MultiTypeaheadSelect): add initialInputValue prop
1 parent 31a4c28 commit 15cbfae

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ export interface MultiTypeaheadSelectProps extends Omit<SelectProps, 'toggle' |
5151
toggleWidth?: string;
5252
/** Additional props passed to the toggle. */
5353
toggleProps?: MenuToggleProps;
54+
/** Initial value of the typeahead text input. */
55+
initialInputValue?: string;
5456
}
5557

5658
export const MultiTypeaheadSelectBase: React.FunctionComponent<MultiTypeaheadSelectProps> = ({
@@ -65,13 +67,14 @@ export const MultiTypeaheadSelectBase: React.FunctionComponent<MultiTypeaheadSel
6567
isDisabled,
6668
toggleWidth,
6769
toggleProps,
70+
initialInputValue,
6871
...props
6972
}: MultiTypeaheadSelectProps) => {
7073
const [isOpen, setIsOpen] = useState(false);
7174
const [selected, setSelected] = useState<(string | number)[]>(
7275
(initialOptions?.filter((o) => o.selected) ?? []).map((o) => o.value)
7376
);
74-
const [inputValue, setInputValue] = useState<string>();
77+
const [inputValue, setInputValue] = useState<string>(initialInputValue);
7578
const [selectOptions, setSelectOptions] = useState<MultiTypeaheadSelectOption[]>(initialOptions);
7679
const [focusedItemIndex, setFocusedItemIndex] = useState<number | null>(null);
7780
const [activeItemId, setActiveItemId] = useState<string | null>(null);
@@ -104,9 +107,6 @@ export const MultiTypeaheadSelectBase: React.FunctionComponent<MultiTypeaheadSel
104107
}
105108
];
106109
}
107-
108-
// Open the menu when the input value changes and the new value is not empty
109-
openMenu();
110110
}
111111

112112
setSelectOptions(newSelectOptions);
@@ -130,7 +130,6 @@ export const MultiTypeaheadSelectBase: React.FunctionComponent<MultiTypeaheadSel
130130
onToggle && onToggle(false);
131131
setIsOpen(false);
132132
resetActiveAndFocusedItem();
133-
setInputValue('');
134133
};
135134

136135
const onInputClick = () => {
@@ -170,6 +169,10 @@ export const MultiTypeaheadSelectBase: React.FunctionComponent<MultiTypeaheadSel
170169
setInputValue(value);
171170
onInputChange && onInputChange(value);
172171

172+
if (value && !isOpen) {
173+
openMenu();
174+
}
175+
173176
resetActiveAndFocusedItem();
174177
};
175178

0 commit comments

Comments
 (0)