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

Select: Fixes issue preserving search term (input) when selecting a value #87013

Merged
merged 3 commits into from May 2, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 7 additions & 2 deletions packages/grafana-ui/src/components/Select/SelectBase.tsx
Expand Up @@ -240,8 +240,13 @@ export function SelectBase<T, Rest = {}>({
onBlur,
onChange: onChangeWithEmpty,
onInputChange: (val: string, actionMeta: InputActionMeta) => {
setHasInputValue(!!val);
onInputChange?.(val, actionMeta);
const newValue = onInputChange?.(val, actionMeta) ?? val;
const newHasValue = !!newValue;
if (newHasValue !== hasInputValue) {
setHasInputValue(newHasValue);
}

return newValue;
},
onKeyDown,
onMenuClose: onCloseMenu,
Expand Down
5 changes: 4 additions & 1 deletion packages/grafana-ui/src/components/Select/SelectMenu.tsx
Expand Up @@ -36,6 +36,8 @@ SelectMenu.displayName = 'SelectMenu';
const VIRTUAL_LIST_ITEM_HEIGHT = 37;
const VIRTUAL_LIST_WIDTH_ESTIMATE_MULTIPLIER = 8;
const VIRTUAL_LIST_PADDING = 8;
// Some list items have icons or checkboxes so we need some extra width
const VIRTUAL_LIST_WIDTH_EXTRA = 36;

// A virtualized version of the SelectMenu, descriptions for SelectableValue options not supported since those are of a variable height.
//
Expand All @@ -58,7 +60,8 @@ export const VirtualizedSelectMenu = ({ children, maxHeight, options, getValue }
}

const longestOption = max(options.map((option) => option.label?.length)) ?? 0;
const widthEstimate = longestOption * VIRTUAL_LIST_WIDTH_ESTIMATE_MULTIPLIER + VIRTUAL_LIST_PADDING * 2;
const widthEstimate =
longestOption * VIRTUAL_LIST_WIDTH_ESTIMATE_MULTIPLIER + VIRTUAL_LIST_PADDING * 2 + VIRTUAL_LIST_WIDTH_EXTRA;
const heightEstimate = Math.min(options.length * VIRTUAL_LIST_ITEM_HEIGHT, maxHeight);

// Try to scroll to keep current value in the middle
Expand Down