@grafana/ui
Version:
Grafana Components Library
1 lines • 8.16 kB
Source Map (JSON)
{"version":3,"file":"renderers.mjs","sources":["../../../../../../src/components/Table/TableNG/Cells/renderers.tsx"],"sourcesContent":["import { ReactNode } from 'react';\n\nimport { Field, FieldType, isDataFrame, isTimeSeriesFrame } from '@grafana/data';\n\nimport { TableCellDisplayMode, TableCellOptions, TableCustomCellOptions } from '../../types';\nimport { TableCellRendererProps } from '../types';\n\nimport { ActionsCell } from './ActionsCell';\nimport AutoCell from './AutoCell';\nimport { BarGaugeCell } from './BarGaugeCell';\nimport { DataLinksCell } from './DataLinksCell';\nimport { GeoCell } from './GeoCell';\nimport { ImageCell } from './ImageCell';\nimport { JSONCell } from './JSONCell';\nimport { PillCell } from './PillCell';\nimport { SparklineCell } from './SparklineCell';\n\nexport type TableCellRenderer = (props: TableCellRendererProps) => ReactNode;\n\nconst GAUGE_RENDERER: TableCellRenderer = (props) => (\n <BarGaugeCell\n field={props.field}\n value={props.value}\n theme={props.theme}\n height={props.height}\n width={props.width}\n rowIdx={props.rowIdx}\n />\n);\n\nconst AUTO_RENDERER: TableCellRenderer = (props) => (\n <AutoCell\n value={props.value}\n field={props.field}\n justifyContent={props.justifyContent}\n rowIdx={props.rowIdx}\n cellOptions={props.cellOptions}\n />\n);\n\nconst SPARKLINE_RENDERER: TableCellRenderer = (props) => (\n <SparklineCell\n value={props.value}\n field={props.field}\n justifyContent={props.justifyContent}\n timeRange={props.timeRange}\n rowIdx={props.rowIdx}\n theme={props.theme}\n width={props.width}\n />\n);\n\nconst JSON_RENDERER: TableCellRenderer = (props) => (\n <JSONCell justifyContent={props.justifyContent} value={props.value} field={props.field} rowIdx={props.rowIdx} />\n);\n\nconst GEO_RENDERER: TableCellRenderer = (props) => (\n <GeoCell value={props.value} justifyContent={props.justifyContent} height={props.height} />\n);\n\nconst IMAGE_RENDERER: TableCellRenderer = (props) => (\n <ImageCell\n cellOptions={props.cellOptions}\n field={props.field}\n height={props.height}\n justifyContent={props.justifyContent}\n value={props.value}\n rowIdx={props.rowIdx}\n />\n);\n\nconst DATA_LINKS_RENDERER: TableCellRenderer = (props) => <DataLinksCell field={props.field} rowIdx={props.rowIdx} />;\n\nconst ACTIONS_RENDERER: TableCellRenderer = ({ field, rowIdx, getActions = () => [] }) => (\n <ActionsCell field={field} rowIdx={rowIdx} getActions={getActions} />\n);\n\nconst PILL_RENDERER: TableCellRenderer = (props) => <PillCell {...props} />;\n\nfunction isCustomCellOptions(options: TableCellOptions): options is TableCustomCellOptions {\n return options.type === TableCellDisplayMode.Custom;\n}\n\nconst CUSTOM_RENDERER: TableCellRenderer = (props) => {\n if (!isCustomCellOptions(props.cellOptions) || !props.cellOptions.cellComponent) {\n return null; // nonsensical case, but better to typeguard it than throw.\n }\n const CustomCellComponent = props.cellOptions.cellComponent;\n return <CustomCellComponent field={props.field} rowIndex={props.rowIdx} frame={props.frame} value={props.value} />;\n};\n\nconst CELL_RENDERERS: Record<TableCellOptions['type'], TableCellRenderer> = {\n [TableCellDisplayMode.Sparkline]: SPARKLINE_RENDERER,\n [TableCellDisplayMode.Gauge]: GAUGE_RENDERER,\n [TableCellDisplayMode.JSONView]: JSON_RENDERER,\n [TableCellDisplayMode.Image]: IMAGE_RENDERER,\n [TableCellDisplayMode.DataLinks]: DATA_LINKS_RENDERER,\n [TableCellDisplayMode.Actions]: ACTIONS_RENDERER,\n [TableCellDisplayMode.Custom]: CUSTOM_RENDERER,\n [TableCellDisplayMode.ColorText]: AUTO_RENDERER,\n [TableCellDisplayMode.ColorBackground]: AUTO_RENDERER,\n [TableCellDisplayMode.Auto]: AUTO_RENDERER,\n [TableCellDisplayMode.Pill]: PILL_RENDERER,\n};\n\n/** @internal */\nexport function getCellRenderer(field: Field, cellOptions: TableCellOptions): TableCellRenderer {\n const cellType = cellOptions?.type ?? TableCellDisplayMode.Auto;\n if (cellType === TableCellDisplayMode.Auto) {\n return getAutoRendererResult(field);\n }\n return CELL_RENDERERS[cellType];\n}\n\n/** @internal */\nexport function getAutoRendererResult(field: Field): TableCellRenderer {\n if (field.type === FieldType.geo) {\n return GEO_RENDERER;\n }\n if (field.type === FieldType.frame) {\n const firstValue = field.values[0];\n if (isDataFrame(firstValue) && isTimeSeriesFrame(firstValue)) {\n return SPARKLINE_RENDERER;\n } else {\n return JSON_RENDERER;\n }\n }\n if (field.type === FieldType.other) {\n return JSON_RENDERER;\n }\n return AUTO_RENDERER;\n}\n"],"names":[],"mappings":";;;;;;;;;;;;;AAmBA,MAAM,cAAA,GAAoC,CAAC,KACzC,qBAAA,GAAA;AAAA,EAAC,YAAA;AAAA,EAAA;AAAA,IACC,OAAO,KAAM,CAAA,KAAA;AAAA,IACb,OAAO,KAAM,CAAA,KAAA;AAAA,IACb,OAAO,KAAM,CAAA,KAAA;AAAA,IACb,QAAQ,KAAM,CAAA,MAAA;AAAA,IACd,OAAO,KAAM,CAAA,KAAA;AAAA,IACb,QAAQ,KAAM,CAAA;AAAA;AAChB,CAAA;AAGF,MAAM,aAAA,GAAmC,CAAC,KACxC,qBAAA,GAAA;AAAA,EAAC,QAAA;AAAA,EAAA;AAAA,IACC,OAAO,KAAM,CAAA,KAAA;AAAA,IACb,OAAO,KAAM,CAAA,KAAA;AAAA,IACb,gBAAgB,KAAM,CAAA,cAAA;AAAA,IACtB,QAAQ,KAAM,CAAA,MAAA;AAAA,IACd,aAAa,KAAM,CAAA;AAAA;AACrB,CAAA;AAGF,MAAM,kBAAA,GAAwC,CAAC,KAC7C,qBAAA,GAAA;AAAA,EAAC,aAAA;AAAA,EAAA;AAAA,IACC,OAAO,KAAM,CAAA,KAAA;AAAA,IACb,OAAO,KAAM,CAAA,KAAA;AAAA,IACb,gBAAgB,KAAM,CAAA,cAAA;AAAA,IACtB,WAAW,KAAM,CAAA,SAAA;AAAA,IACjB,QAAQ,KAAM,CAAA,MAAA;AAAA,IACd,OAAO,KAAM,CAAA,KAAA;AAAA,IACb,OAAO,KAAM,CAAA;AAAA;AACf,CAAA;AAGF,MAAM,gBAAmC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