From 6ad8375293c0068a1309ad73b1a02ddd25f7c24a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Tue, 18 Feb 2020 14:16:42 +0100 Subject: [PATCH] Table: Fixed header alignment (#22236) --- .../grafana-ui/src/components/Table/Table.tsx | 17 +++++++++++------ .../src/components/Table/TableCell.tsx | 2 ++ 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/packages/grafana-ui/src/components/Table/Table.tsx b/packages/grafana-ui/src/components/Table/Table.tsx index ca38f01b6b1..b3facd3f66a 100644 --- a/packages/grafana-ui/src/components/Table/Table.tsx +++ b/packages/grafana-ui/src/components/Table/Table.tsx @@ -1,5 +1,5 @@ import React, { useMemo } from 'react'; -import { DataFrame } from '@grafana/data'; +import { DataFrame, Field } from '@grafana/data'; import { useSortBy, useTable, useBlockLayout, Cell } from 'react-table'; import { FixedSizeList } from 'react-window'; import useMeasure from 'react-use/lib/useMeasure'; @@ -8,6 +8,8 @@ import { useTheme } from '../../themes'; import { TableFilterActionCallback } from './types'; import { getTableStyles } from './styles'; import { TableCell } from './TableCell'; +import { Icon } from '../Icon/Icon'; +import { getTextAlign } from './utils'; export interface Props { data: DataFrame; @@ -56,7 +58,9 @@ export const Table = ({ data, height, onCellClick, width }: Props) => {
{headerGroups.map((headerGroup: any) => (
- {headerGroup.headers.map((column: any) => renderHeaderCell(column, tableStyles.headerCell))} + {headerGroup.headers.map((column: any) => + renderHeaderCell(column, tableStyles.headerCell, data.fields[column.index]) + )}
))}
@@ -72,17 +76,18 @@ export const Table = ({ data, height, onCellClick, width }: Props) => { ); }; -function renderHeaderCell(column: any, className: string) { +function renderHeaderCell(column: any, className: string, field: Field) { const headerProps = column.getHeaderProps(column.getSortByToggleProps()); + const fieldTextAlign = getTextAlign(field); - if (column.textAlign) { - headerProps.style.textAlign = column.textAlign; + if (fieldTextAlign) { + headerProps.style.textAlign = fieldTextAlign; } return (
{column.render('Header')} - {column.isSorted ? (column.isSortedDesc ? ' 🔽' : ' 🔼') : ''} + {column.isSorted && (column.isSortedDesc ? : )}
); } diff --git a/packages/grafana-ui/src/components/Table/TableCell.tsx b/packages/grafana-ui/src/components/Table/TableCell.tsx index 9c574714067..551f667c4d8 100644 --- a/packages/grafana-ui/src/components/Table/TableCell.tsx +++ b/packages/grafana-ui/src/components/Table/TableCell.tsx @@ -17,6 +17,7 @@ export const TableCell: FC = ({ cell, field, tableStyles, onCellClick }) const cellProps = cell.getCellProps(); let onClick: ((event: React.SyntheticEvent) => void) | undefined = undefined; + if (filterable && onCellClick) { if (cellProps.style) { cellProps.style.cursor = 'pointer'; @@ -24,6 +25,7 @@ export const TableCell: FC = ({ cell, field, tableStyles, onCellClick }) onClick = () => onCellClick(cell.column.Header as string, cell.value); } + const fieldTextAlign = getTextAlign(field); if (fieldTextAlign && cellProps.style) { cellProps.style.textAlign = fieldTextAlign;