Skip to content

Commit 642236c

Browse files
fix(TypeaheadSelect): Update input value only when appropriate (patternfly#10826)
1 parent 2b462a9 commit 642236c

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ export interface TypeaheadSelectOption extends Omit<SelectOptionProps, 'content'
2222
value: string | number;
2323
}
2424

25-
export interface TypeaheadSelectProps extends Omit<SelectProps, 'toggle'> {
25+
export interface TypeaheadSelectProps extends Omit<SelectProps, 'toggle' | 'onSelect'> {
2626
/** @hide Forwarded ref */
2727
innerRef?: React.Ref<any>;
2828
/** Initial options of the select. */
2929
initialOptions: TypeaheadSelectOption[];
3030
/** Callback triggered on selection. */
3131
onSelect?: (
32-
_event: React.MouseEvent<Element, MouseEvent> | React.KeyboardEvent<HTMLInputElement>,
32+
_event: React.MouseEvent<Element, MouseEvent> | React.KeyboardEvent<HTMLInputElement> | undefined,
3333
selection: string | number
3434
) => void;
3535
/** Callback triggered when the select opens or closes. */
@@ -154,14 +154,19 @@ export const TypeaheadSelectBase: React.FunctionComponent<TypeaheadSelectProps>
154154
]);
155155

156156
React.useEffect(() => {
157+
// If the selected option changed and the current input value is the previously selected item, update the displayed value.
157158
const selectedOption = initialOptions.find((o) => o.selected);
158-
setInputValue(String(selectedOption?.content ?? ''));
159+
if (inputValue === selected && selectedOption?.value !== selected) {
160+
setInputValue(String(selectedOption?.content ?? ''));
161+
}
162+
// Only update when options change
163+
// eslint-disable-next-line react-hooks/exhaustive-deps
159164
}, [initialOptions]);
160165

161166
const setActiveAndFocusedItem = (itemIndex: number) => {
162167
setFocusedItemIndex(itemIndex);
163168
const focusedItem = selectOptions[itemIndex];
164-
setActiveItemId(focusedItem.value as string);
169+
setActiveItemId(String(focusedItem.value));
165170
};
166171

167172
const resetActiveAndFocusedItem = () => {
@@ -291,7 +296,7 @@ export const TypeaheadSelectBase: React.FunctionComponent<TypeaheadSelectProps>
291296
const onToggleClick = () => {
292297
onToggle && onToggle(!isOpen);
293298
setIsOpen(!isOpen);
294-
textInputRef?.current?.focus();
299+
textInputRef.current?.focus();
295300
};
296301

297302
const onClearButtonClick = () => {
@@ -300,7 +305,7 @@ export const TypeaheadSelectBase: React.FunctionComponent<TypeaheadSelectProps>
300305
onInputChange && onInputChange('');
301306
setFilterValue('');
302307
resetActiveAndFocusedItem();
303-
textInputRef?.current?.focus();
308+
textInputRef.current?.focus();
304309
onClearSelection && onClearSelection();
305310
};
306311

packages/react-templates/src/components/Select/examples/TypeaheadSelectDemo.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export const SelectTypeaheadDemo: React.FunctionComponent = () => {
3535
noOptionsFoundMessage={(filter) => `No state was found for "${filter}"`}
3636
onClearSelection={() => setSelected(undefined)}
3737
onSelect={(_ev, selection) => {
38-
if (!options.find((o) => o.content === selection)) {
38+
if (!options.find((o) => o.value === selection)) {
3939
setOptions([...options, { content: String(selection), value: String(selection) }]);
4040
}
4141
setSelected(String(selection));

0 commit comments

Comments
 (0)