Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(TypeaheadSelect): Update input value only when appropriate #10826

Merged
merged 1 commit into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions packages/react-templates/src/components/Select/TypeaheadSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ export interface TypeaheadSelectOption extends Omit<SelectOptionProps, 'content'
value: string | number;
}

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

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

const setActiveAndFocusedItem = (itemIndex: number) => {
setFocusedItemIndex(itemIndex);
const focusedItem = selectOptions[itemIndex];
setActiveItemId(focusedItem.value as string);
setActiveItemId(String(focusedItem.value));
};

const resetActiveAndFocusedItem = () => {
Expand Down Expand Up @@ -291,7 +296,7 @@ export const TypeaheadSelectBase: React.FunctionComponent<TypeaheadSelectProps>
const onToggleClick = () => {
onToggle && onToggle(!isOpen);
setIsOpen(!isOpen);
textInputRef?.current?.focus();
textInputRef.current?.focus();
};

const onClearButtonClick = () => {
Expand All @@ -300,7 +305,7 @@ export const TypeaheadSelectBase: React.FunctionComponent<TypeaheadSelectProps>
onInputChange && onInputChange('');
setFilterValue('');
resetActiveAndFocusedItem();
textInputRef?.current?.focus();
textInputRef.current?.focus();
onClearSelection && onClearSelection();
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const SelectTypeaheadDemo: React.FunctionComponent = () => {
noOptionsFoundMessage={(filter) => `No state was found for "${filter}"`}
onClearSelection={() => setSelected(undefined)}
onSelect={(_ev, selection) => {
if (!options.find((o) => o.content === selection)) {
if (!options.find((o) => o.value === selection)) {
setOptions([...options, { content: String(selection), value: String(selection) }]);
}
setSelected(String(selection));
Expand Down
Loading