TableCell: show JSON rather than [object Object] (#23683)

This commit is contained in:
Ryan McKinley
2020-04-20 11:10:03 -07:00
committed by GitHub
parent 229176f1b0
commit 9464115fc0
8 changed files with 111 additions and 9 deletions

View File

@@ -0,0 +1,43 @@
import React, { FC } from 'react';
import { css, cx } from 'emotion';
import { TableCellProps } from './types';
import { Tooltip } from '../Tooltip/Tooltip';
import { JSONFormatter } from '../JSONFormatter/JSONFormatter';
export const JSONViewCell: FC<TableCellProps> = props => {
const { field, cell, tableStyles } = props;
if (!field.display) {
return null;
}
const txt = css`
cursor: pointer;
font-family: monospace;
`;
const displayValue = JSON.stringify(cell.value);
const content = <JSONTooltip value={cell.value} />;
return (
<div className={cx(txt, tableStyles.tableCell)}>
<Tooltip placement="auto" content={content} theme={'info'}>
<span>{displayValue}</span>
</Tooltip>
</div>
);
};
interface PopupProps {
value: any;
}
const JSONTooltip: FC<PopupProps> = props => {
const clazz = css`
padding: 10px;
`;
return (
<div className={clazz}>
<JSONFormatter json={props.value} open={4} />
</div>
);
};

View File

@@ -15,6 +15,7 @@ export enum TableCellDisplayMode {
ColorBackground = 'color-background',
GradientGauge = 'gradient-gauge',
LcdGauge = 'lcd-gauge',
JSONView = 'json-view',
}
export type FieldTextAlignment = 'auto' | 'left' | 'right' | 'center';

View File

@@ -7,6 +7,7 @@ import { TableCellDisplayMode, TableCellProps, TableFieldOptions } from './types
import { css, cx } from 'emotion';
import { withTableStyles } from './withTableStyles';
import tinycolor from 'tinycolor2';
import { JSONViewCell } from './JSONViewCell';
export function getTextAlign(field?: Field): TextAlignProperty {
if (!field) {
@@ -46,7 +47,7 @@ export function getColumns(data: DataFrame, availableWidth: number, columnMinWid
fieldCountWithoutWidth -= 1;
}
const Cell = getCellComponent(fieldTableOptions.displayMode);
const Cell = getCellComponent(fieldTableOptions.displayMode, field);
columns.push({
Cell,
@@ -71,7 +72,7 @@ export function getColumns(data: DataFrame, availableWidth: number, columnMinWid
return columns;
}
function getCellComponent(displayMode: TableCellDisplayMode) {
function getCellComponent(displayMode: TableCellDisplayMode, field: Field) {
switch (displayMode) {
case TableCellDisplayMode.ColorText:
return withTableStyles(DefaultCell, getTextColorStyle);
@@ -80,9 +81,15 @@ function getCellComponent(displayMode: TableCellDisplayMode) {
case TableCellDisplayMode.LcdGauge:
case TableCellDisplayMode.GradientGauge:
return BarGaugeCell;
default:
return DefaultCell;
case TableCellDisplayMode.JSONView:
return JSONViewCell;
}
// Default or Auto
if (field.type === FieldType.other) {
return JSONViewCell;
}
return DefaultCell;
}
function getTextColorStyle(props: TableCellProps) {