Skip to content

Commit 9125f0d

Browse files
Table: Add support for ActionsCell (#94648)
1 parent 0d70dbe commit 9125f0d

File tree

5 files changed

+64
-14
lines changed

5 files changed

+64
-14
lines changed

packages/grafana-schema/src/common/common.gen.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,7 @@ export interface Labels {}
690690
* modes are deprecated in favor of new cell subOptions
691691
*/
692692
export enum TableCellDisplayMode {
693+
Actions = 'actions',
693694
Auto = 'auto',
694695
BasicGauge = 'basic',
695696
ColorBackground = 'color-background',
@@ -784,6 +785,13 @@ export interface TableDataLinksCellOptions {
784785
type: TableCellDisplayMode.DataLinks;
785786
}
786787

788+
/**
789+
* Show actions in the cell
790+
*/
791+
export interface TableActionsCellOptions {
792+
type: TableCellDisplayMode.Actions;
793+
}
794+
787795
/**
788796
* Gauge cell options
789797
*/
@@ -825,7 +833,7 @@ export enum TableCellHeight {
825833
* Table cell options. Each cell has a display mode
826834
* and other potential options for that display.
827835
*/
828-
export type TableCellOptions = (TableAutoCellOptions | TableSparklineCellOptions | TableBarGaugeCellOptions | TableColoredBackgroundCellOptions | TableColorTextCellOptions | TableImageCellOptions | TableDataLinksCellOptions | TableJsonViewCellOptions);
836+
export type TableCellOptions = (TableAutoCellOptions | TableSparklineCellOptions | TableBarGaugeCellOptions | TableColoredBackgroundCellOptions | TableColorTextCellOptions | TableImageCellOptions | TableDataLinksCellOptions | TableActionsCellOptions | TableJsonViewCellOptions);
829837

830838
/**
831839
* Use UTC/GMT timezone

packages/grafana-schema/src/common/table.cue

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ package common
44
// in the table such as colored text, JSON, gauge, etc.
55
// The color-background-solid, gradient-gauge, and lcd-gauge
66
// modes are deprecated in favor of new cell subOptions
7-
TableCellDisplayMode: "auto" | "color-text" | "color-background" | "color-background-solid" | "gradient-gauge" | "lcd-gauge" | "json-view" | "basic" | "image" | "gauge" | "sparkline" | "data-links" | "custom" @cuetsy(kind="enum",memberNames="Auto|ColorText|ColorBackground|ColorBackgroundSolid|GradientGauge|LcdGauge|JSONView|BasicGauge|Image|Gauge|Sparkline|DataLinks|Custom")
7+
TableCellDisplayMode: "auto" | "color-text" | "color-background" | "color-background-solid" | "gradient-gauge" | "lcd-gauge" | "json-view" | "basic" | "image" | "gauge" | "sparkline" | "data-links" | "custom" | "actions" @cuetsy(kind="enum",memberNames="Auto|ColorText|ColorBackground|ColorBackgroundSolid|GradientGauge|LcdGauge|JSONView|BasicGauge|Image|Gauge|Sparkline|DataLinks|Custom|Actions")
88

99
// Display mode to the "Colored Background" display
1010
// mode for table cells. Either displays a solid color (basic mode)
@@ -57,6 +57,11 @@ TableDataLinksCellOptions: {
5757
type: TableCellDisplayMode & "data-links"
5858
} @cuetsy(kind="interface")
5959

60+
// Show actions in the cell
61+
TableActionsCellOptions: {
62+
type: TableCellDisplayMode & "actions"
63+
} @cuetsy(kind="interface")
64+
6065
// Gauge cell options
6166
TableBarGaugeCellOptions: {
6267
type: TableCellDisplayMode & "gauge"
@@ -84,7 +89,7 @@ TableCellHeight: "sm" | "md" | "lg" | "auto" @cuetsy(kind="enum")
8489

8590
// Table cell options. Each cell has a display mode
8691
// and other potential options for that display.
87-
TableCellOptions: TableAutoCellOptions | TableSparklineCellOptions | TableBarGaugeCellOptions | TableColoredBackgroundCellOptions | TableColorTextCellOptions | TableImageCellOptions | TableDataLinksCellOptions | TableJsonViewCellOptions @cuetsy(kind="type")
92+
TableCellOptions: TableAutoCellOptions | TableSparklineCellOptions | TableBarGaugeCellOptions | TableColoredBackgroundCellOptions | TableColorTextCellOptions | TableImageCellOptions | TableDataLinksCellOptions | TableActionsCellOptions | TableJsonViewCellOptions @cuetsy(kind="type")
8893

8994
// Field options for each field within a table (e.g 10, "The String", 64.20, etc.)
9095
// Generally defines alignment, filtering capabilties, display options, etc.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { css, cx } from '@emotion/css';
2+
3+
import { GrafanaTheme2 } from '@grafana/data';
4+
5+
import { useStyles2 } from '../../themes';
6+
import { ActionButton } from '../Actions/ActionButton';
7+
8+
import { TableCellProps } from './types';
9+
10+
export const ActionsCell = (props: TableCellProps) => {
11+
const { cellProps, tableStyles, actions } = props;
12+
13+
const styles = useStyles2(getStyles);
14+
15+
return (
16+
<div {...cellProps} className={cx(tableStyles.cellContainerText, styles.buttonsGap)}>
17+
{actions && actions.map((action, i) => <ActionButton key={i} action={action} variant="secondary" />)}
18+
</div>
19+
);
20+
};
21+
22+
const getStyles = (theme: GrafanaTheme2) => ({
23+
buttonsGap: css({
24+
gap: 6,
25+
}),
26+
});

packages/grafana-ui/src/components/Table/utils.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
import { Property } from 'csstype';
22
import { clone, sampleSize } from 'lodash';
33
import memoize from 'micro-memoize';
4-
import { Row, HeaderGroup } from 'react-table';
4+
import { HeaderGroup, Row } from 'react-table';
55
import tinycolor from 'tinycolor2';
66

77
import {
88
DataFrame,
9+
DisplayValue,
10+
DisplayValueAlignmentFactors,
911
Field,
12+
fieldReducers,
1013
FieldType,
1114
formattedValueToString,
12-
getFieldDisplayName,
13-
SelectableValue,
14-
fieldReducers,
1515
getDisplayProcessor,
16-
reduceField,
16+
getFieldDisplayName,
1717
GrafanaTheme2,
1818
isDataFrame,
1919
isDataFrameWithValue,
2020
isTimeSeriesFrame,
21-
DisplayValueAlignmentFactors,
22-
DisplayValue,
21+
reduceField,
22+
SelectableValue,
2323
} from '@grafana/data';
2424
import {
2525
BarGaugeDisplayMode,
@@ -30,6 +30,7 @@ import {
3030

3131
import { getTextColorForAlphaBackground } from '../../utils';
3232

33+
import { ActionsCell } from './ActionsCell';
3334
import { BarGaugeCell } from './BarGaugeCell';
3435
import { DataLinksCell } from './DataLinksCell';
3536
import { DefaultCell } from './DefaultCell';
@@ -41,13 +42,13 @@ import { RowExpander } from './RowExpander';
4142
import { SparklineCell } from './SparklineCell';
4243
import { TableStyles } from './styles';
4344
import {
45+
CellColors,
4446
CellComponent,
45-
TableCellOptions,
46-
TableFieldOptions,
4747
FooterItem,
4848
GrafanaTableColumn,
49+
TableCellOptions,
50+
TableFieldOptions,
4951
TableFooterCalc,
50-
CellColors,
5152
} from './types';
5253

5354
export const EXPANDER_WIDTH = 50;
@@ -191,6 +192,8 @@ export function getCellComponent(displayMode: TableCellDisplayMode, field: Field
191192
return JSONViewCell;
192193
case TableCellDisplayMode.DataLinks:
193194
return DataLinksCell;
195+
case TableCellDisplayMode.Actions:
196+
return ActionsCell;
194197
}
195198

196199
if (field.type === FieldType.geo) {

public/app/plugins/panel/table/TableCellOptionEditor.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { merge } from 'lodash';
33
import { useState } from 'react';
44

55
import { GrafanaTheme2, SelectableValue } from '@grafana/data';
6+
import { config } from '@grafana/runtime';
67
import { TableCellOptions } from '@grafana/schema';
78
import { Field, Select, TableCellDisplayMode, useStyles2 } from '@grafana/ui';
89

@@ -81,7 +82,7 @@ export const TableCellOptionEditor = ({ value, onChange }: Props) => {
8182
);
8283
};
8384

84-
const cellDisplayModeOptions: Array<SelectableValue<TableCellOptions>> = [
85+
let cellDisplayModeOptions: Array<SelectableValue<TableCellOptions>> = [
8586
{ value: { type: TableCellDisplayMode.Auto }, label: 'Auto' },
8687
{ value: { type: TableCellDisplayMode.Sparkline }, label: 'Sparkline' },
8788
{ value: { type: TableCellDisplayMode.ColorText }, label: 'Colored text' },
@@ -92,6 +93,13 @@ const cellDisplayModeOptions: Array<SelectableValue<TableCellOptions>> = [
9293
{ value: { type: TableCellDisplayMode.Image }, label: 'Image' },
9394
];
9495

96+
if (config.featureToggles.vizActions) {
97+
cellDisplayModeOptions = [
98+
...cellDisplayModeOptions,
99+
{ value: { type: TableCellDisplayMode.Actions }, label: 'Actions' },
100+
];
101+
}
102+
95103
const getStyles = (theme: GrafanaTheme2) => ({
96104
fixBottomMargin: css({
97105
marginBottom: theme.spacing(-2),

0 commit comments

Comments
 (0)