grafana/public/app/features/scenes/variables/components/VariableValueSelectors.tsx
Torkel Ödegaard 8c585a4ebf
Scene: Variables interpolation formats and multi value handling (#58591)
* Component that can cache and extract variable dependencies

* Component that can cache and extract variable dependencies

* Updates

* Refactoring

* Lots of refactoring and iterations of supporting both re-rendering and query re-execution

* Updated SceneCanvasText

* Updated name of file

* Updated

* Refactoring a bit

* Added back getName

* Added comment

* minor fix

* Minor fix

* Merge fixes

* Scene variable interpolation progress

* Merge fixes

* Added all format registeries

* Progress on multi value support

* Progress on multi value support

* Updates

* Progress on scoped vars

* Fixed circular dependency

* Updates

* Some review fixes

* Updated comment

* Added forceRender function

* Add back fail on console log

* Update public/app/features/scenes/variables/interpolation/sceneInterpolator.test.ts

* Moving functions from SceneObjectBase

* fixing tests

* Fixed e2e

Co-authored-by: Dominik Prokop <dominik.prokop@grafana.com>
2022-11-16 11:36:30 +01:00

75 lines
2.0 KiB
TypeScript

import React from 'react';
import { VariableHide } from '@grafana/data';
import { selectors } from '@grafana/e2e-selectors';
import { Tooltip } from '@grafana/ui';
import { SceneObjectBase } from '../../core/SceneObjectBase';
import { sceneGraph } from '../../core/sceneGraph';
import { SceneComponentProps, SceneObject, SceneObjectStatePlain } from '../../core/types';
import { SceneVariableState } from '../types';
export class VariableValueSelectors extends SceneObjectBase<SceneObjectStatePlain> {
public static Component = VariableValueSelectorsRenderer;
}
function VariableValueSelectorsRenderer({ model }: SceneComponentProps<VariableValueSelectors>) {
const variables = sceneGraph.getVariables(model)!.useState();
return (
<>
{variables.variables.map((variable) => (
<VariableValueSelectWrapper key={variable.state.key} variable={variable} />
))}
</>
);
}
function VariableValueSelectWrapper({ variable }: { variable: SceneObject<SceneVariableState> }) {
const state = variable.useState();
if (state.hide === VariableHide.hideVariable) {
return null;
}
return (
<div className="gf-form">
<VariableLabel state={state} />
<variable.Component model={variable} />
</div>
);
}
function VariableLabel({ state }: { state: SceneVariableState }) {
if (state.hide === VariableHide.hideLabel) {
return null;
}
const elementId = `var-${state.key}`;
const labelOrName = state.label ?? state.name;
if (state.description) {
return (
<Tooltip content={state.description} placement={'bottom'}>
<label
className="gf-form-label gf-form-label--variable"
data-testid={selectors.pages.Dashboard.SubMenu.submenuItemLabels(labelOrName)}
htmlFor={elementId}
>
{labelOrName}
</label>
</Tooltip>
);
}
return (
<label
className="gf-form-label gf-form-label--variable"
data-testid={selectors.pages.Dashboard.SubMenu.submenuItemLabels(labelOrName)}
htmlFor={elementId}
>
{labelOrName}
</label>
);
}