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

Template Variables: Use Combobox behind toggle #1011

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
31 changes: 31 additions & 0 deletions packages/scenes/src/variables/components/VariableValueSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
getSelectStyles,
useStyles2,
useTheme2,
Combobox,
} from '@grafana/ui';

import { SceneComponentProps } from '../../core/types';
Expand All @@ -18,6 +19,7 @@ import { selectors } from '@grafana/e2e-selectors';
import { GrafanaTheme2, SelectableValue } from '@grafana/data';
import { css, cx } from '@emotion/css';
import { getOptionSearcher } from './getOptionSearcher';
import { ALL_VARIABLE_TEXT, ALL_VARIABLE_VALUE } from '../constants';
import { sceneGraph } from '../../core/sceneGraph';

const filterNoOp = () => true;
Expand Down Expand Up @@ -256,7 +258,36 @@ const getOptionStyles = (theme: GrafanaTheme2) => ({
}),
});

function VariableValueCombobox({ model }: SceneComponentProps<MultiValueVariable>) {
const { value, key, options, includeAll, isReadOnly, allowCustomValue = true } = model.useState();

let comboboxOptions = options.map((o) => ({ value: o.value.toString(), label: o.label }));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do options always have a label in this context?


if (includeAll) {
comboboxOptions.unshift({ value: ALL_VARIABLE_VALUE, label: ALL_VARIABLE_TEXT });
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are custom All values handled somewhere else?

}

return (
<Combobox
id={'var-' + key}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure where this var- is added for the Select

disabled={isReadOnly}
value={value.toString()}
width="auto"
minWidth={10}
options={comboboxOptions}
createCustomValue={allowCustomValue}
onChange={(newValue) => {
model.changeValueTo(newValue.value, newValue.label);
}}
/>
);
}

export function renderSelectForVariable(model: MultiValueVariable) {
if (model.state.renderWithCombobox && !model.state.isMulti) {
return <VariableValueCombobox model={model} />;
}

if (model.state.isMulti) {
return <VariableValueSelectMulti model={model} />;
} else {
Expand Down
2 changes: 2 additions & 0 deletions packages/scenes/src/variables/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export interface SceneVariableState extends SceneObjectState {
loading?: boolean;
error?: any | null;
description?: string | null;
/* @internal */
renderWithCombobox?: boolean;
}

export interface SceneVariable<TState extends SceneVariableState = SceneVariableState> extends SceneObject<TState> {
Expand Down
Loading