diff --git a/packages/grafana-ui/src/components/Table/BackgroundColorCell.tsx b/packages/grafana-ui/src/components/Table/BackgroundColorCell.tsx deleted file mode 100644 index dc76c362125..00000000000 --- a/packages/grafana-ui/src/components/Table/BackgroundColorCell.tsx +++ /dev/null @@ -1,33 +0,0 @@ -import React, { CSSProperties, FC } from 'react'; -import { TableCellProps } from './types'; -import tinycolor from 'tinycolor2'; -import { formattedValueToString } from '@grafana/data'; - -export const BackgroundColoredCell: FC = props => { - const { cell, tableStyles, field } = props; - - if (!field.display) { - return null; - } - - const themeFactor = tableStyles.theme.isDark ? 1 : -0.7; - const displayValue = field.display(cell.value); - - const bgColor2 = tinycolor(displayValue.color) - .darken(10 * themeFactor) - .spin(5) - .toRgbString(); - - const styles: CSSProperties = { - background: `linear-gradient(120deg, ${bgColor2}, ${displayValue.color})`, - color: 'white', - height: tableStyles.cellHeight, - padding: tableStyles.cellPadding, - }; - - return ( -
- {formattedValueToString(displayValue)} -
- ); -}; diff --git a/packages/grafana-ui/src/components/Table/TableCell.tsx b/packages/grafana-ui/src/components/Table/TableCell.tsx index 1911bb466a0..2b4c4909c1b 100644 --- a/packages/grafana-ui/src/components/Table/TableCell.tsx +++ b/packages/grafana-ui/src/components/Table/TableCell.tsx @@ -26,9 +26,8 @@ export const TableCell: FC = ({ cell, field, tableStyles, onCellClick }) onClick = () => onCellClick(cell.column.Header as string, cell.value); } - const fieldTextAlign = getTextAlign(field); - if (fieldTextAlign && cellProps.style) { - cellProps.style.textAlign = fieldTextAlign; + if (cellProps.style) { + cellProps.style.textAlign = getTextAlign(field); } return ( diff --git a/packages/grafana-ui/src/components/Table/types.ts b/packages/grafana-ui/src/components/Table/types.ts index ebc43b7fcd9..145fd967823 100644 --- a/packages/grafana-ui/src/components/Table/types.ts +++ b/packages/grafana-ui/src/components/Table/types.ts @@ -1,6 +1,7 @@ import { CellProps } from 'react-table'; import { Field } from '@grafana/data'; import { TableStyles } from './styles'; +import { FC } from 'react'; export interface TableFieldOptions { width: number; @@ -29,3 +30,5 @@ export interface TableCellProps extends CellProps { tableStyles: TableStyles; field: Field; } + +export type CellComponent = FC; diff --git a/packages/grafana-ui/src/components/Table/utils.ts b/packages/grafana-ui/src/components/Table/utils.ts index 71203f41504..ab2bc13232b 100644 --- a/packages/grafana-ui/src/components/Table/utils.ts +++ b/packages/grafana-ui/src/components/Table/utils.ts @@ -3,8 +3,10 @@ import { DataFrame, Field, FieldType } from '@grafana/data'; import { Column } from 'react-table'; import { DefaultCell } from './DefaultCell'; import { BarGaugeCell } from './BarGaugeCell'; -import { BackgroundColoredCell } from './BackgroundColorCell'; -import { TableCellDisplayMode, TableFieldOptions, TableRow } from './types'; +import { TableCellDisplayMode, TableCellProps, TableFieldOptions, TableRow } from './types'; +import { css, cx } from 'emotion'; +import { withTableStyles } from './withTableStyles'; +import tinycolor from 'tinycolor2'; export function getTableRows(data: DataFrame): TableRow[] { const tableData = []; @@ -81,8 +83,10 @@ export function getColumns(data: DataFrame, availableWidth: number, columnMinWid function getCellComponent(displayMode: TableCellDisplayMode) { switch (displayMode) { + case TableCellDisplayMode.ColorText: + return withTableStyles(DefaultCell, getTextColorStyle); case TableCellDisplayMode.ColorBackground: - return BackgroundColoredCell; + return withTableStyles(DefaultCell, getBackgroundColorStyle); case TableCellDisplayMode.LcdGauge: case TableCellDisplayMode.GradientGauge: return BarGaugeCell; @@ -90,3 +94,55 @@ function getCellComponent(displayMode: TableCellDisplayMode) { return DefaultCell; } } + +function getTextColorStyle(props: TableCellProps) { + const { field, cell, tableStyles } = props; + + if (!field.display) { + return tableStyles; + } + + const displayValue = field.display(cell.value); + if (!displayValue.color) { + return tableStyles; + } + + const extendedStyle = css` + color: ${displayValue.color}; + `; + + return { + ...tableStyles, + tableCell: cx(tableStyles.tableCell, extendedStyle), + }; +} + +function getBackgroundColorStyle(props: TableCellProps) { + const { field, cell, tableStyles } = props; + if (!field.display) { + return tableStyles; + } + + const displayValue = field.display(cell.value); + if (!displayValue.color) { + return tableStyles; + } + + const themeFactor = tableStyles.theme.isDark ? 1 : -0.7; + const bgColor2 = tinycolor(displayValue.color) + .darken(10 * themeFactor) + .spin(5) + .toRgbString(); + + const extendedStyle = css` + background: linear-gradient(120deg, ${bgColor2}, ${displayValue.color}); + color: white; + height: ${tableStyles.cellHeight}px; + padding: ${tableStyles.cellPadding}px; + `; + + return { + ...tableStyles, + tableCell: cx(tableStyles.tableCell, extendedStyle), + }; +} diff --git a/packages/grafana-ui/src/components/Table/withTableStyles.tsx b/packages/grafana-ui/src/components/Table/withTableStyles.tsx new file mode 100644 index 00000000000..9dbf13bc697 --- /dev/null +++ b/packages/grafana-ui/src/components/Table/withTableStyles.tsx @@ -0,0 +1,14 @@ +import { CellComponent, TableCellProps } from './types'; +import { TableStyles } from './styles'; + +export const withTableStyles = ( + CellComponent: CellComponent, + getExtendedStyles: (props: TableCellProps) => TableStyles +): CellComponent => { + function WithTableStyles(props: TableCellProps) { + return CellComponent({ ...props, tableStyles: getExtendedStyles(props) }); + } + + WithTableStyles.displayName = CellComponent.displayName || CellComponent.name; + return WithTableStyles; +}; diff --git a/public/app/plugins/panel/table2/module.tsx b/public/app/plugins/panel/table2/module.tsx index 3b115d5f3c7..786215d4747 100644 --- a/public/app/plugins/panel/table2/module.tsx +++ b/public/app/plugins/panel/table2/module.tsx @@ -33,10 +33,11 @@ export const plugin = new PanelPlugin(TablePanel) .addSelect({ path: 'displayMode', name: 'Cell display mode', - description: 'Color value, background, show as gauge, etc', + description: 'Color text, background, show as gauge, etc', settings: { options: [ { value: 'auto', label: 'Auto' }, + { value: 'color-text', label: 'Color text' }, { value: 'color-background', label: 'Color background' }, { value: 'gradient-gauge', label: 'Gradient gauge' }, { value: 'lcd-gauge', label: 'LCD gauge' },