From f4f1acaf0eb79f4297c8cf7a6cdb144fee9468f2 Mon Sep 17 00:00:00 2001 From: Ashley Harrison Date: Mon, 20 Oct 2025 15:12:05 +0100 Subject: [PATCH] convert BigValue to functional component --- eslint-suppressions.json | 5 -- .../src/components/BigValue/BigValue.tsx | 90 +++++++++---------- 2 files changed, 43 insertions(+), 52 deletions(-) diff --git a/eslint-suppressions.json b/eslint-suppressions.json index ae4979a2750..1e60fb62c59 100644 --- a/eslint-suppressions.json +++ b/eslint-suppressions.json @@ -574,11 +574,6 @@ "count": 1 } }, - "packages/grafana-ui/src/components/BigValue/BigValue.tsx": { - "react-prefer-function-component/react-prefer-function-component": { - "count": 1 - } - }, "packages/grafana-ui/src/components/Cascader/Cascader.tsx": { "react-prefer-function-component/react-prefer-function-component": { "count": 1 diff --git a/packages/grafana-ui/src/components/BigValue/BigValue.tsx b/packages/grafana-ui/src/components/BigValue/BigValue.tsx index 6aceeb3660b..08bacf3e2af 100644 --- a/packages/grafana-ui/src/components/BigValue/BigValue.tsx +++ b/packages/grafana-ui/src/components/BigValue/BigValue.tsx @@ -1,6 +1,5 @@ import { cx } from '@emotion/css'; -import { PureComponent } from 'react'; -import * as React from 'react'; +import { memo, type MouseEventHandler } from 'react'; import { DisplayValue, DisplayValueAlignmentFactors, FieldSparkline } from '@grafana/data'; import { PercentChangeColorMode, VizTextDisplayOptions } from '@grafana/schema'; @@ -51,7 +50,7 @@ export interface Props extends Themeable2 { /** Sparkline values for showing a graph under/behind the value */ sparkline?: FieldSparkline; /** onClick handler for the value */ - onClick?: React.MouseEventHandler; + onClick?: MouseEventHandler; /** Custom styling */ className?: string; /** Color mode for coloring the value or the background */ @@ -83,58 +82,55 @@ export interface Props extends Themeable2 { disableWideLayout?: boolean; } -export class BigValue extends PureComponent { - static defaultProps: Partial = { - justifyMode: BigValueJustifyMode.Auto, - }; +export const BigValue = memo((props) => { + const { onClick, className, hasLinks, theme, justifyMode = BigValueJustifyMode.Auto } = props; - render() { - const { onClick, className, hasLinks, theme } = this.props; - const layout = buildLayout(this.props); - const panelStyles = layout.getPanelStyles(); - const valueAndTitleContainerStyles = layout.getValueAndTitleContainerStyles(); - const valueStyles = layout.getValueStyles(); - const titleStyles = layout.getTitleStyles(); - const textValues = layout.textValues; - const percentChange = this.props.value.percentChange; - const percentChangeColorMode = this.props.percentChangeColorMode; - const showPercentChange = percentChange != null && !Number.isNaN(percentChange); + const layout = buildLayout({ ...props, justifyMode }); + const panelStyles = layout.getPanelStyles(); + const valueAndTitleContainerStyles = layout.getValueAndTitleContainerStyles(); + const valueStyles = layout.getValueStyles(); + const titleStyles = layout.getTitleStyles(); + const textValues = layout.textValues; + const percentChange = props.value.percentChange; + const percentChangeColorMode = props.percentChangeColorMode; + const showPercentChange = percentChange != null && !Number.isNaN(percentChange); - // When there is an outer data link this tooltip will override the outer native tooltip - const tooltip = hasLinks ? undefined : textValues.tooltip; - - if (!onClick) { - return ( -
-
- {textValues.title &&
{textValues.title}
} - - {showPercentChange && ( - - )} -
- {layout.renderChart()} -
- ); - } + // When there is an outer data link this tooltip will override the outer native tooltip + const tooltip = hasLinks ? undefined : textValues.tooltip; + if (!onClick) { return ( - + ); } -} + + return ( + + ); +}); + +BigValue.displayName = 'BigValue';