From 4497601ebccae71f0164840221852a00288d82ec Mon Sep 17 00:00:00 2001 From: VED PRAKASH KASHYAP Date: Thu, 11 May 2023 19:57:24 +0530 Subject: [PATCH] feat(chart-controls): Show detailed data type tooltip when hovering type icon (#23970) --- .../src/components/ColumnOption.tsx | 23 ++++++++++++-- .../src/components/labelUtils.tsx | 13 ++++++++ .../test/components/labelUtils.test.tsx | 30 +++++++++++++++++++ 3 files changed, 64 insertions(+), 2 deletions(-) diff --git a/superset-frontend/packages/superset-ui-chart-controls/src/components/ColumnOption.tsx b/superset-frontend/packages/superset-ui-chart-controls/src/components/ColumnOption.tsx index 074d816c829d7..ca2a73247b60c 100644 --- a/superset-frontend/packages/superset-ui-chart-controls/src/components/ColumnOption.tsx +++ b/superset-frontend/packages/superset-ui-chart-controls/src/components/ColumnOption.tsx @@ -22,7 +22,11 @@ import { Tooltip } from './Tooltip'; import { ColumnTypeLabel } from './ColumnTypeLabel/ColumnTypeLabel'; import CertifiedIconWithTooltip from './CertifiedIconWithTooltip'; import { ColumnMeta } from '../types'; -import { getColumnLabelText, getColumnTooltipNode } from './labelUtils'; +import { + getColumnLabelText, + getColumnTooltipNode, + getColumnTypeTooltipNode, +} from './labelUtils'; import { SQLPopover } from './SQLPopover'; export type ColumnOptionProps = { @@ -48,14 +52,29 @@ export function ColumnOption({ const hasExpression = expression && expression !== column_name; const type = hasExpression ? 'expression' : type_generic; const [tooltipText, setTooltipText] = useState(column.column_name); + const [columnTypeTooltipText, setcolumnTypeTooltipText] = useState( + column.type, + ); useLayoutEffect(() => { setTooltipText(getColumnTooltipNode(column, labelRef)); + setcolumnTypeTooltipText(getColumnTypeTooltipNode(column)); }, [labelRef, column]); return ( - {showType && type !== undefined && } + {showType && type !== undefined && ( + + + + + + )} ): boolean => export const getColumnLabelText = (column: ColumnMeta): string => column.verbose_name || column.column_name; +export const getColumnTypeTooltipNode = (column: ColumnMeta): ReactNode => { + if (!column.type) { + return null; + } + + return ( + + ); +}; + export const getColumnTooltipNode = ( column: ColumnMeta, labelRef?: React.RefObject, diff --git a/superset-frontend/packages/superset-ui-chart-controls/test/components/labelUtils.test.tsx b/superset-frontend/packages/superset-ui-chart-controls/test/components/labelUtils.test.tsx index 6d3c89c14659d..8fd0bf267da23 100644 --- a/superset-frontend/packages/superset-ui-chart-controls/test/components/labelUtils.test.tsx +++ b/superset-frontend/packages/superset-ui-chart-controls/test/components/labelUtils.test.tsx @@ -24,6 +24,7 @@ import { getColumnLabelText, getColumnTooltipNode, getMetricTooltipNode, + getColumnTypeTooltipNode, } from '../../src/components/labelUtils'; const renderWithTheme = (ui: ReactElement) => @@ -64,6 +65,35 @@ test('should get null as tooltip', () => { ).toBe(null); }); +test('should get null for column datatype tooltip when type is blank', () => { + expect( + getColumnTypeTooltipNode({ + id: 123, + column_name: 'column name', + verbose_name: '', + description: '', + type: '', + }), + ).toBe(null); +}); + +test('should get column datatype rendered as tooltip when column has a type', () => { + renderWithTheme( + <> + {getColumnTypeTooltipNode({ + id: 123, + column_name: 'column name', + verbose_name: 'verbose name', + description: 'A very important column', + type: 'text', + })} + , + ); + + expect(screen.getByText('Column datatype')).toBeVisible(); + expect(screen.getByText('text')).toBeVisible(); +}); + test('should get column name, verbose name and description when it has a verbose name', () => { const ref = { current: { scrollWidth: 100, clientWidth: 100 } }; renderWithTheme(