From 1c893c50556512a0dfcfd5cebd6c16743d3c574e Mon Sep 17 00:00:00 2001 From: Ihor Yeromin Date: Wed, 30 Oct 2024 23:15:15 +0100 Subject: [PATCH] TableNG: Column width options (#95426) * feat(table-ng): column width * mouse handle drag event * move resizing task --- .../src/components/Table/TableNG/TableNG.tsx | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/packages/grafana-ui/src/components/Table/TableNG/TableNG.tsx b/packages/grafana-ui/src/components/Table/TableNG/TableNG.tsx index 1bfacd56fb4..66026cd5623 100644 --- a/packages/grafana-ui/src/components/Table/TableNG/TableNG.tsx +++ b/packages/grafana-ui/src/components/Table/TableNG/TableNG.tsx @@ -38,10 +38,17 @@ interface TableHeaderProps { } export function TableNG(props: TableNGProps) { - const { height, width, timeRange, cellHeight, noHeader } = props; + const { height, width, timeRange, cellHeight, noHeader, fieldConfig } = props; const theme = useTheme2(); const styles = useStyles2(getStyles); + // TODO: this is a hack to force the column width to update when the fieldConfig changes + const [revId, setRevId] = useState(0); + const columnWidth = useMemo(() => { + setRevId(revId + 1); + return fieldConfig?.defaults?.custom?.width || 'auto'; + }, [fieldConfig]); // eslint-disable-line react-hooks/exhaustive-deps + const [contextMenuProps, setContextMenuProps] = useState<{ rowIdx: number; value: string; @@ -131,13 +138,13 @@ export function TableNG(props: TableNGProps) { const rows: Array<{ [key: string]: string }> = []; main.fields.map((field) => { - const key = field.name; + const key = `${field.name}-${revId}`; const { values: _, ...shallowField } = field; // Add a column for each field columns.push({ key, - name: key, + name: field.name, field: shallowField, rowHeight: rowHeightNumber, cellClass: (row) => { @@ -171,6 +178,7 @@ export function TableNG(props: TableNGProps) { renderHeaderCell: ({ column, sortDirection }) => ( ), + width: columnWidth, }); // Create row objects @@ -244,7 +252,6 @@ export function TableNG(props: TableNGProps) { defaultColumnOptions={{ sortable: true, resizable: true, - maxWidth: 200, }} rowHeight={rowHeightNumber} // TODO: This doesn't follow current table behavior