TableCellInspector: fix runtime error thrown when inspecting non-string table cells (#113865)

* fix: old table throwing error on inspect of dates and empty cells

---------

Co-authored-by: Paul Marbach <paul.marbach@grafana.com>
This commit is contained in:
Galen Kistler
2025-11-13 19:48:40 +00:00
committed by GitHub
co-authored by Paul Marbach
parent 6b26391cf2
commit 23c192f330
2 changed files with 26 additions and 3 deletions
@@ -0,0 +1,17 @@
import { screen, render } from '@testing-library/react';
import { TableCellInspector, TableCellInspectorMode } from './TableCellInspector';
describe('TableCellInspector', () => {
it.each([
{ type: 'string', value: 'simple string' },
{ type: 'number', value: 12345 },
{ type: 'object', value: { key: 'value', anotherKey: 42 } },
{ type: 'array', value: [1, 2, 3, 4, 5] },
{ type: 'null', value: null },
{ type: 'undefined', value: undefined },
])('should successfully render for input of type $type', ({ value }) => {
render(<TableCellInspector value={value} onDismiss={() => {}} mode={TableCellInspectorMode.text} />);
expect(screen.getByText('Copy to Clipboard')).toBeInTheDocument();
});
});
@@ -18,15 +18,21 @@ export enum TableCellInspectorMode {
}
interface TableCellInspectorProps {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
value: string;
value: unknown;
onDismiss: () => void;
mode: TableCellInspectorMode;
}
const toString = (value: unknown): string => {
if (typeof value === 'string') {
return value;
}
return value?.toString?.() ?? '';
};
export function TableCellInspector({ value, onDismiss, mode }: TableCellInspectorProps) {
const [currentMode, setMode] = useState(mode);
const text = value.trim();
const text = toString(value).trim();
const styles = useStyles2(getStyles);
const tabs = [