convert BigValue to functional component
This commit is contained in:
@@ -574,11 +574,6 @@
|
|||||||
"count": 1
|
"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": {
|
"packages/grafana-ui/src/components/Cascader/Cascader.tsx": {
|
||||||
"react-prefer-function-component/react-prefer-function-component": {
|
"react-prefer-function-component/react-prefer-function-component": {
|
||||||
"count": 1
|
"count": 1
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
import { cx } from '@emotion/css';
|
import { cx } from '@emotion/css';
|
||||||
import { PureComponent } from 'react';
|
import { memo, type MouseEventHandler } from 'react';
|
||||||
import * as React from 'react';
|
|
||||||
|
|
||||||
import { DisplayValue, DisplayValueAlignmentFactors, FieldSparkline } from '@grafana/data';
|
import { DisplayValue, DisplayValueAlignmentFactors, FieldSparkline } from '@grafana/data';
|
||||||
import { PercentChangeColorMode, VizTextDisplayOptions } from '@grafana/schema';
|
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 values for showing a graph under/behind the value */
|
||||||
sparkline?: FieldSparkline;
|
sparkline?: FieldSparkline;
|
||||||
/** onClick handler for the value */
|
/** onClick handler for the value */
|
||||||
onClick?: React.MouseEventHandler<HTMLElement>;
|
onClick?: MouseEventHandler<HTMLElement>;
|
||||||
/** Custom styling */
|
/** Custom styling */
|
||||||
className?: string;
|
className?: string;
|
||||||
/** Color mode for coloring the value or the background */
|
/** Color mode for coloring the value or the background */
|
||||||
@@ -83,58 +82,55 @@ export interface Props extends Themeable2 {
|
|||||||
disableWideLayout?: boolean;
|
disableWideLayout?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class BigValue extends PureComponent<Props> {
|
export const BigValue = memo<Props>((props) => {
|
||||||
static defaultProps: Partial<Props> = {
|
const { onClick, className, hasLinks, theme, justifyMode = BigValueJustifyMode.Auto } = props;
|
||||||
justifyMode: BigValueJustifyMode.Auto,
|
|
||||||
};
|
|
||||||
|
|
||||||
render() {
|
const layout = buildLayout({ ...props, justifyMode });
|
||||||
const { onClick, className, hasLinks, theme } = this.props;
|
const panelStyles = layout.getPanelStyles();
|
||||||
const layout = buildLayout(this.props);
|
const valueAndTitleContainerStyles = layout.getValueAndTitleContainerStyles();
|
||||||
const panelStyles = layout.getPanelStyles();
|
const valueStyles = layout.getValueStyles();
|
||||||
const valueAndTitleContainerStyles = layout.getValueAndTitleContainerStyles();
|
const titleStyles = layout.getTitleStyles();
|
||||||
const valueStyles = layout.getValueStyles();
|
const textValues = layout.textValues;
|
||||||
const titleStyles = layout.getTitleStyles();
|
const percentChange = props.value.percentChange;
|
||||||
const textValues = layout.textValues;
|
const percentChangeColorMode = props.percentChangeColorMode;
|
||||||
const percentChange = this.props.value.percentChange;
|
const showPercentChange = percentChange != null && !Number.isNaN(percentChange);
|
||||||
const percentChangeColorMode = this.props.percentChangeColorMode;
|
|
||||||
const showPercentChange = percentChange != null && !Number.isNaN(percentChange);
|
|
||||||
|
|
||||||
// When there is an outer data link this tooltip will override the outer native tooltip
|
// When there is an outer data link this tooltip will override the outer native tooltip
|
||||||
const tooltip = hasLinks ? undefined : textValues.tooltip;
|
const tooltip = hasLinks ? undefined : textValues.tooltip;
|
||||||
|
|
||||||
if (!onClick) {
|
|
||||||
return (
|
|
||||||
<div className={className} style={panelStyles} title={tooltip}>
|
|
||||||
<div style={valueAndTitleContainerStyles}>
|
|
||||||
{textValues.title && <div style={titleStyles}>{textValues.title}</div>}
|
|
||||||
<FormattedValueDisplay value={textValues} style={valueStyles} />
|
|
||||||
{showPercentChange && (
|
|
||||||
<PercentChange
|
|
||||||
percentChange={percentChange}
|
|
||||||
styles={layout.getPercentChangeStyles(percentChange, percentChangeColorMode, valueStyles)}
|
|
||||||
/>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
{layout.renderChart()}
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
if (!onClick) {
|
||||||
return (
|
return (
|
||||||
<button
|
<div className={className} style={panelStyles} title={tooltip}>
|
||||||
type="button"
|
|
||||||
className={cx(clearButtonStyles(theme), className)}
|
|
||||||
style={panelStyles}
|
|
||||||
onClick={onClick}
|
|
||||||
title={tooltip}
|
|
||||||
>
|
|
||||||
<div style={valueAndTitleContainerStyles}>
|
<div style={valueAndTitleContainerStyles}>
|
||||||
{textValues.title && <div style={titleStyles}>{textValues.title}</div>}
|
{textValues.title && <div style={titleStyles}>{textValues.title}</div>}
|
||||||
<FormattedValueDisplay value={textValues} style={valueStyles} />
|
<FormattedValueDisplay value={textValues} style={valueStyles} />
|
||||||
|
{showPercentChange && (
|
||||||
|
<PercentChange
|
||||||
|
percentChange={percentChange}
|
||||||
|
styles={layout.getPercentChangeStyles(percentChange, percentChangeColorMode, valueStyles)}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
{layout.renderChart()}
|
{layout.renderChart()}
|
||||||
</button>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
return (
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
className={cx(clearButtonStyles(theme), className)}
|
||||||
|
style={panelStyles}
|
||||||
|
onClick={onClick}
|
||||||
|
title={tooltip}
|
||||||
|
>
|
||||||
|
<div style={valueAndTitleContainerStyles}>
|
||||||
|
{textValues.title && <div style={titleStyles}>{textValues.title}</div>}
|
||||||
|
<FormattedValueDisplay value={textValues} style={valueStyles} />
|
||||||
|
</div>
|
||||||
|
{layout.renderChart()}
|
||||||
|
</button>
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
BigValue.displayName = 'BigValue';
|
||||||
|
|||||||
Reference in New Issue
Block a user