From 5242d446936c5767312e3c5eafe11a8e2ab8075a Mon Sep 17 00:00:00 2001 From: Victor Marin <36818606+mdvictor@users.noreply.github.com> Date: Tue, 29 Mar 2022 13:05:30 +0300 Subject: [PATCH] Table panel: Show datalinks for cell display modes JSON View and Gauge derivates (#46020) * Show datalinks on Table panel JSON View and Gauge derivates * Use DataLinksContextMenu on BarGaugeCell * Change import path * PR modifications * Re-add cell container style --- .../src/components/Table/BarGaugeCell.tsx | 49 +++++++++++++++++-- .../src/components/Table/JSONViewCell.tsx | 20 +++++++- 2 files changed, 63 insertions(+), 6 deletions(-) diff --git a/packages/grafana-ui/src/components/Table/BarGaugeCell.tsx b/packages/grafana-ui/src/components/Table/BarGaugeCell.tsx index d008fc8fe27..1d68b923893 100644 --- a/packages/grafana-ui/src/components/Table/BarGaugeCell.tsx +++ b/packages/grafana-ui/src/components/Table/BarGaugeCell.tsx @@ -1,7 +1,9 @@ import React, { FC } from 'react'; -import { ThresholdsConfig, ThresholdsMode, VizOrientation, getFieldConfigWithMinMax } from '@grafana/data'; +import { ThresholdsConfig, ThresholdsMode, VizOrientation, getFieldConfigWithMinMax, LinkModel } from '@grafana/data'; import { BarGauge, BarGaugeDisplayMode } from '../BarGauge/BarGauge'; import { TableCellProps, TableCellDisplayMode } from './types'; +import { DataLinksContextMenu, DataLinksContextMenuApi } from '../DataLinks/DataLinksContextMenu'; +import { isFunction } from 'lodash'; const defaultScale: ThresholdsConfig = { mode: ThresholdsMode.Absolute, @@ -18,7 +20,7 @@ const defaultScale: ThresholdsConfig = { }; export const BarGaugeCell: FC = (props) => { - const { field, innerWidth, tableStyles, cell, cellProps } = props; + const { field, innerWidth, tableStyles, cell, cellProps, row } = props; let config = getFieldConfigWithMinMax(field, false); if (!config.thresholds) { @@ -37,8 +39,20 @@ export const BarGaugeCell: FC = (props) => { barGaugeMode = BarGaugeDisplayMode.Basic; } - return ( -
+ const getLinks = () => { + if (!isFunction(field.getLinks)) { + return [] as LinkModel[]; + } + + return field.getLinks({ valueRowIndex: row.index }); + }; + + const hasLinks = !!getLinks().length; + + const renderComponent = (menuProps: DataLinksContextMenuApi) => { + const { openMenu, targetClassName } = menuProps; + + return ( = (props) => { value={displayValue} orientation={VizOrientation.Horizontal} theme={tableStyles.theme} + onClick={openMenu} + className={targetClassName} itemSpacing={1} lcdCellWidth={8} displayMode={barGaugeMode} /> + ); + }; + + return ( +
+ {hasLinks && ( + + {(api) => renderComponent(api)} + + )} + {!hasLinks && ( + + )}
); }; diff --git a/packages/grafana-ui/src/components/Table/JSONViewCell.tsx b/packages/grafana-ui/src/components/Table/JSONViewCell.tsx index 57fda2b325c..3a9904f026b 100644 --- a/packages/grafana-ui/src/components/Table/JSONViewCell.tsx +++ b/packages/grafana-ui/src/components/Table/JSONViewCell.tsx @@ -3,9 +3,10 @@ import { css, cx } from '@emotion/css'; import { isString } from 'lodash'; import { TableCellProps, TableFieldOptions } from './types'; import { CellActions } from './CellActions'; +import { getCellLinks } from '../../utils'; export function JSONViewCell(props: TableCellProps): JSX.Element { - const { cell, tableStyles, cellProps, field } = props; + const { cell, tableStyles, cellProps, field, row } = props; const inspectEnabled = Boolean((field.config.custom as TableFieldOptions)?.inspect); const txt = css` cursor: pointer; @@ -23,9 +24,24 @@ export function JSONViewCell(props: TableCellProps): JSX.Element { displayValue = JSON.stringify(value, null, ' '); } + const { link, onClick } = getCellLinks(field, row); + return (
-
{displayValue}
+
+ {!link &&
{value}
} + {link && ( + + {displayValue} + + )} +
{inspectEnabled && }
);