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): Correctly show the newly selected item on selection #10792

Merged
merged 1 commit into from
Jul 23, 2024
Merged
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
21 changes: 15 additions & 6 deletions packages/react-templates/src/components/Select/TypeaheadSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ export interface TypeaheadSelectProps extends Omit<SelectProps, 'toggle'> {
onClearSelection?: () => void;
/** Placeholder text for the select input. */
placeholder?: string;
/** Message to display when no options are available. */
noOptionsAvailableMessage?: string;
/** Message to display when no options match the filter. */
noOptionsFoundMessage?: string | ((filter: string) => string);
/** Flag indicating the select should be disabled. */
Expand All @@ -58,6 +60,7 @@ export const TypeaheadSelectBase: React.FunctionComponent<TypeaheadSelectProps>
onInputChange,
onClearSelection,
placeholder = 'Select an option',
noOptionsAvailableMessage = 'No options are available',
noOptionsFoundMessage = (filter) => `No results found for "${filter}"`,
isDisabled,
toggleWidth,
Expand Down Expand Up @@ -104,17 +107,23 @@ export const TypeaheadSelectBase: React.FunctionComponent<TypeaheadSelectProps>
}
}

// When no options are available, display 'No options available'
if (!newSelectOptions.length) {
newSelectOptions = [
{
isAriaDisabled: true,
content: noOptionsAvailableMessage,
value: NO_RESULTS
}
];
}

setSelectOptions(newSelectOptions);
}, [filterValue, initialOptions]);

React.useEffect(() => {
const selectedOption = initialOptions.find((o) => o.selected);
if (selectedOption?.value !== selected) {
setInputValue(String(selectedOption?.content ?? ''));
setSelected(String(selectedOption?.content ?? ''));
}
// Do not update when selected changes, only if the given options have changed
// eslint-disable-next-line react-hooks/exhaustive-deps
setInputValue(String(selectedOption?.content ?? ''));
}, [initialOptions]);

const setActiveAndFocusedItem = (itemIndex: number) => {
Expand Down
Loading