From 6d61196e55b7550dae8fa8c69698e604f80adcdf Mon Sep 17 00:00:00 2001 From: Sergej-Vlasov <37613182+Sergej-Vlasov@users.noreply.github.com> Date: Thu, 13 Mar 2025 14:37:16 +0000 Subject: [PATCH] ConditionalRendering: Restore variable conditional rendering logic (#102115) restore variable conditional rendering logic --- .../ConditionalRenderingVariable.tsx | 38 ++++++++++--------- 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/public/app/features/dashboard-scene/conditional-rendering/ConditionalRenderingVariable.tsx b/public/app/features/dashboard-scene/conditional-rendering/ConditionalRenderingVariable.tsx index a9b58843a2f..7a2b15d296b 100644 --- a/public/app/features/dashboard-scene/conditional-rendering/ConditionalRenderingVariable.tsx +++ b/public/app/features/dashboard-scene/conditional-rendering/ConditionalRenderingVariable.tsx @@ -1,7 +1,8 @@ import { css } from '@emotion/css'; import { ReactNode, useMemo } from 'react'; -import { SceneComponentProps, sceneGraph } from '@grafana/scenes'; +import { GrafanaTheme2 } from '@grafana/data'; +import { SceneComponentProps, sceneGraph, VariableDependencyConfig } from '@grafana/scenes'; import { ConditionalRenderingVariableKind } from '@grafana/schema/dist/esm/schema/dashboard/v2alpha0'; import { Combobox, ComboboxOption, Field, Input, Stack, useStyles2 } from '@grafana/ui'; import { t } from 'app/core/internationalization'; @@ -16,17 +17,26 @@ export type VariableConditionValue = { value: string; }; -interface ConditionalRenderingVariableState extends ConditionalRenderingBaseState {} +type ConditionalRenderingVariableState = ConditionalRenderingBaseState; export class ConditionalRenderingVariable extends ConditionalRenderingBase { public get title(): string { return t('dashboard.conditional-rendering.variable.label', 'Variable'); } + protected _variableDependency = new VariableDependencyConfig(this, { + onAnyVariableChanged: (v) => { + if (v.state.name === this.state.value.name) { + this.getConditionalLogicRoot().notifyChange(); + } + }, + }); + public evaluate(): boolean { if (!this.state.value.name) { return true; } + const variable = sceneGraph.getVariables(this).state.variables.find((v) => v.state.name === this.state.value.name); // name is defined but no variable found - return false @@ -35,13 +45,7 @@ export class ConditionalRenderingVariable extends ConditionalRenderingBase ({ +const getStyles = (theme: GrafanaTheme2) => ({ variableNameSelect: css({ flexGrow: 1, }), operatorSelect: css({ - width: '6rem', + width: theme.spacing(12), }), });