mirror of
https://github.com/grafana/grafana.git
synced 2024-11-24 09:50:29 -06:00
Grafana UI: Add experimental InteractiveTable component (#58223)
* wip * move table * refine example * move to experimental * add row expansion example * add expanded row to kitchen sink * add column prop docs * add props docs * remove useless example * WIP * use unique id per row & proper aria attrs for expander * add custom cell rendering example * Remove multisort * rename shrink to disableGrow * move isTruthy type guard to @grafana/data * add missing prop from TableData interface * make column id required * fix correlations table * expand on docs * remove leftover comment * rename to InteractiveTable * add some tests * add expansion tests * fix tests * revert unneeded changes * remove extra header rule
This commit is contained in:
parent
191ca1df86
commit
dc918f7e91
@ -177,3 +177,6 @@ export interface DataConfigSource {
|
||||
getFieldOverrideOptions: () => ApplyFieldOverrideOptions | undefined;
|
||||
snapshotData?: DataFrameDTO[];
|
||||
}
|
||||
|
||||
type Truthy<T> = T extends false | '' | 0 | null | undefined ? never : T;
|
||||
export const isTruthy = <T>(value: T): value is Truthy<T> => Boolean(value);
|
||||
|
@ -2,7 +2,7 @@ import { css } from '@emotion/css';
|
||||
import React from 'react';
|
||||
import { CellProps } from 'react-table';
|
||||
|
||||
import { IconButton } from '@grafana/ui';
|
||||
import { IconButton } from '../IconButton/IconButton';
|
||||
|
||||
const expanderContainerStyles = css`
|
||||
display: flex;
|
||||
@ -10,14 +10,18 @@ const expanderContainerStyles = css`
|
||||
height: 100%;
|
||||
`;
|
||||
|
||||
export function ExpanderCell<K extends object>({ row }: CellProps<K, void>) {
|
||||
export function ExpanderCell<K extends object>({ row, __rowID }: CellProps<K, void> & { __rowID: string }) {
|
||||
return (
|
||||
<div className={expanderContainerStyles}>
|
||||
<IconButton
|
||||
tooltip="toggle row expanded"
|
||||
aria-controls={__rowID}
|
||||
// @ts-expect-error react-table doesn't ship with useExpanded types and we can't use declaration merging without affecting the table viz
|
||||
name={row.isExpanded ? 'angle-down' : 'angle-right'}
|
||||
// @ts-expect-error same as the line above
|
||||
{...row.getToggleRowExpandedProps({})}
|
||||
aria-expanded={row.isExpanded}
|
||||
// @ts-expect-error same as the line above
|
||||
{...row.getToggleRowExpandedProps()}
|
||||
/>
|
||||
</div>
|
||||
);
|
@ -0,0 +1,166 @@
|
||||
import { Meta, Props, Story, Canvas } from '@storybook/addon-docs/blocks';
|
||||
|
||||
import { InteractiveTable } from './InteractiveTable';
|
||||
import { Badge } from '../Badge/Badge';
|
||||
|
||||
<Meta title="MDX|InteractiveTable" component={InteractiveTable} />
|
||||
|
||||
# InteractiveTable
|
||||
|
||||
<Badge text="Alpha" icon="rocket" color="blue" tooltip="This component is still experimental." />
|
||||
|
||||
The InteractiveTable is used to display and select data efficiently.
|
||||
It allows for the display and modification of detailed information.
|
||||
With additional functionality it allows for batch editing, as needed by your feature's users.
|
||||
|
||||
It is a wrapper around [React Table](https://react-table-v7.tanstack.com/), for more informations about it, refer to the [official documentation](https://react-table.tanstack.com/docs/overview).
|
||||
|
||||
### When to use
|
||||
|
||||
The InteractiveTable can be used to allow users to perform administrative tasks workflows.
|
||||
|
||||
### When not to use
|
||||
|
||||
Avoid using the InteractiveTable where mobile or responsiveness may be a requirement.
|
||||
Consider an alternative pattern where the user is presented with a summary list and can click/tap to an individual page for each row in that list.
|
||||
|
||||
### Usage
|
||||
|
||||
<Props of={InteractiveTable} />
|
||||
|
||||
#### About `columns` and `data` Props
|
||||
|
||||
To avoid unnecessary rerenders, `columns` and `data` must be memoized.
|
||||
|
||||
Columns are rendered in the same order defined in the `columns` prop.
|
||||
Each Cell's content is automatically rendered by matching the `id` of the column to the key of each object in the `data` array prop.
|
||||
|
||||
##### Example
|
||||
|
||||
```tsx
|
||||
interface TableData {
|
||||
projectName: string;
|
||||
repository: string;
|
||||
}
|
||||
|
||||
const columns = useMemo<Array<Column<TableData>>>(
|
||||
() => [
|
||||
id: 'projectName'
|
||||
header: "Project Name"
|
||||
],
|
||||
[
|
||||
id: 'repository',
|
||||
header: "Repository"
|
||||
],
|
||||
[]
|
||||
);
|
||||
|
||||
const data = useMemo<Array<TableData>>(
|
||||
() => [
|
||||
{
|
||||
projectName: 'Grafana',
|
||||
repository: 'https://github.com/grafana/grafana',
|
||||
}
|
||||
],
|
||||
[
|
||||
{
|
||||
projectName: 'Loki';
|
||||
repository: 'https://github.com/grafana/loki';
|
||||
}
|
||||
],
|
||||
[]
|
||||
);
|
||||
```
|
||||
|
||||
## Examples
|
||||
|
||||
### With row expansion
|
||||
|
||||
Individual rows can be expanded to display additional details or reconfigure properties previously defined when the row was created.
|
||||
The expanded row area should be used to declutter the primary presentation of data, carefully consider what the user needs to know at first glance and what can be hidden behind the Row Expander button.
|
||||
|
||||
In general, data-types that are consistent across all dataset are in the primary table, variances are pushed to the expanded section for each individual row.
|
||||
|
||||
<Story id="experimental-interactivetable--with-row-expansion" />
|
||||
|
||||
Row expansion is enabled whenever the `renderExpanded` prop is provided. The `renderExpanded` function is called with the row's data and should return a ReactNode.
|
||||
|
||||
```tsx
|
||||
interface TableData {
|
||||
datasource: string;
|
||||
repo: string;
|
||||
description: string;
|
||||
}
|
||||
|
||||
const tableData: TableData[] = [
|
||||
//...
|
||||
];
|
||||
|
||||
const columns: Array<Column<TableData>> = [
|
||||
//...
|
||||
];
|
||||
|
||||
const ExpandedCell = ({ description }: TableData) => {
|
||||
return <p>{description}</p>;
|
||||
};
|
||||
|
||||
export const MyComponent = () => {
|
||||
return (
|
||||
<InteractiveTable
|
||||
columns={columns}
|
||||
data={tableData}
|
||||
getRowId={(r) => r.datasource}
|
||||
renderExpandedRow={ExpandedCell}
|
||||
/>
|
||||
);
|
||||
};
|
||||
```
|
||||
|
||||
### Custom Cell Rendering
|
||||
|
||||
Individual cells can be rendered using custom content dy defining a `cell` property on the column definition.
|
||||
|
||||
<Story id="experimental-interactivetable--with-custom-cell" />
|
||||
|
||||
```tsx
|
||||
interface TableData {
|
||||
datasource: string;
|
||||
repo: string;
|
||||
}
|
||||
|
||||
const RepoCell = ({
|
||||
row: {
|
||||
original: { repo },
|
||||
},
|
||||
}: CellProps<WithCustomCellData, void>) => {
|
||||
return (
|
||||
<LinkButton href={repo} size="sm" icon="external-link-alt">
|
||||
Open on GitHub
|
||||
</LinkButton>
|
||||
);
|
||||
};
|
||||
|
||||
const tableData: WithCustomCellData[] = [
|
||||
{
|
||||
datasource: 'Prometheus',
|
||||
repo: 'https://github.com/prometheus/prometheus',
|
||||
},
|
||||
{
|
||||
datasource: 'Loki',
|
||||
repo: 'https://github.com/grafana/loki',
|
||||
},
|
||||
{
|
||||
datasource: 'Tempo',
|
||||
repo: 'https://github.com/grafana/tempo',
|
||||
},
|
||||
];
|
||||
|
||||
const columns: Array<Column<WithCustomCellData>> = [
|
||||
{ id: 'datasource', header: 'Data Source' },
|
||||
{ id: 'repo', header: 'Repo', cell: RepoCell },
|
||||
];
|
||||
|
||||
export const MyComponent = () => {
|
||||
return <InteractiveTable columns={columns} data={tableData} getRowId={(r) => r.datasource} />;
|
||||
};
|
||||
```
|
@ -0,0 +1,142 @@
|
||||
import { ComponentMeta, ComponentStory } from '@storybook/react';
|
||||
import React, { useMemo } from 'react';
|
||||
|
||||
import { InteractiveTable, Column, CellProps, LinkButton } from '@grafana/ui';
|
||||
|
||||
import { withCenteredStory } from '../../utils/storybook/withCenteredStory';
|
||||
|
||||
import mdx from './InteractiveTable.mdx';
|
||||
|
||||
const EXCLUDED_PROPS = ['className', 'renderExpandedRow', 'getRowId'];
|
||||
|
||||
const meta: ComponentMeta<typeof InteractiveTable> = {
|
||||
title: 'Experimental/InteractiveTable',
|
||||
component: InteractiveTable,
|
||||
decorators: [withCenteredStory],
|
||||
parameters: {
|
||||
docs: {
|
||||
page: mdx,
|
||||
},
|
||||
controls: {
|
||||
exclude: EXCLUDED_PROPS,
|
||||
},
|
||||
},
|
||||
args: {},
|
||||
argTypes: {},
|
||||
};
|
||||
|
||||
interface TableData {
|
||||
header1: string;
|
||||
header2?: number;
|
||||
noheader?: string;
|
||||
}
|
||||
|
||||
export const Basic: ComponentStory<typeof InteractiveTable> = (args) => {
|
||||
const columns = useMemo<Array<Column<TableData>>>(
|
||||
() => [
|
||||
{ id: 'header2', header: 'With missing values', sortType: 'number', disableGrow: true },
|
||||
{
|
||||
id: 'noheader',
|
||||
sortType: 'number',
|
||||
},
|
||||
],
|
||||
[]
|
||||
);
|
||||
const data: TableData[] = useMemo(
|
||||
() => [
|
||||
{ header1: 'a', header2: 1 },
|
||||
{ header1: 'b', noheader: "This column doesn't have an header" },
|
||||
{ header1: 'c', noheader: "But it's still sortable" },
|
||||
],
|
||||
[]
|
||||
);
|
||||
|
||||
return <InteractiveTable columns={columns} data={data} getRowId={(r) => r.header1} />;
|
||||
};
|
||||
|
||||
interface WithRowExpansionData {
|
||||
datasource: string;
|
||||
repo: string;
|
||||
description: string;
|
||||
}
|
||||
|
||||
const ExpandedCell = ({ description }: WithRowExpansionData) => {
|
||||
return <p>{description}</p>;
|
||||
};
|
||||
|
||||
export const WithRowExpansion: ComponentStory<typeof InteractiveTable> = (args) => {
|
||||
const tableData: WithRowExpansionData[] = [
|
||||
{
|
||||
datasource: 'Prometheus',
|
||||
repo: 'https://github.com/prometheus/prometheus',
|
||||
description: 'Open source time series database & alerting.',
|
||||
},
|
||||
{
|
||||
datasource: 'Loki',
|
||||
repo: 'https://github.com/grafana/loki',
|
||||
description: 'Like Prometheus but for logs. OSS logging solution from Grafana Labs.',
|
||||
},
|
||||
{
|
||||
datasource: 'Tempo',
|
||||
repo: 'https://github.com/grafana/tempo',
|
||||
description: 'High volume, minimal dependency trace storage. OSS tracing solution from Grafana Labs.',
|
||||
},
|
||||
];
|
||||
|
||||
const columns: Array<Column<WithRowExpansionData>> = [
|
||||
{ id: 'datasource', header: 'Data Source' },
|
||||
{ id: 'repo', header: 'Repo' },
|
||||
];
|
||||
|
||||
return (
|
||||
<InteractiveTable
|
||||
columns={columns}
|
||||
data={tableData}
|
||||
getRowId={(r) => r.datasource}
|
||||
renderExpandedRow={ExpandedCell}
|
||||
/>
|
||||
);
|
||||
};
|
||||
|
||||
interface WithCustomCellData {
|
||||
datasource: string;
|
||||
repo: string;
|
||||
}
|
||||
|
||||
const RepoCell = ({
|
||||
row: {
|
||||
original: { repo },
|
||||
},
|
||||
}: CellProps<WithCustomCellData, void>) => {
|
||||
return (
|
||||
<LinkButton href={repo} size="sm" icon="external-link-alt">
|
||||
Open on GithHub
|
||||
</LinkButton>
|
||||
);
|
||||
};
|
||||
|
||||
export const WithCustomCell: ComponentStory<typeof InteractiveTable> = (args) => {
|
||||
const tableData: WithCustomCellData[] = [
|
||||
{
|
||||
datasource: 'Prometheus',
|
||||
repo: 'https://github.com/prometheus/prometheus',
|
||||
},
|
||||
{
|
||||
datasource: 'Loki',
|
||||
repo: 'https://github.com/grafana/loki',
|
||||
},
|
||||
{
|
||||
datasource: 'Tempo',
|
||||
repo: 'https://github.com/grafana/tempo',
|
||||
},
|
||||
];
|
||||
|
||||
const columns: Array<Column<WithCustomCellData>> = [
|
||||
{ id: 'datasource', header: 'Data Source' },
|
||||
{ id: 'repo', header: 'Repo', cell: RepoCell },
|
||||
];
|
||||
|
||||
return <InteractiveTable columns={columns} data={tableData} getRowId={(r) => r.datasource} />;
|
||||
};
|
||||
|
||||
export default meta;
|
@ -0,0 +1,95 @@
|
||||
import { fireEvent, getByRole, render, screen } from '@testing-library/react';
|
||||
import React from 'react';
|
||||
|
||||
import { InteractiveTable } from './InteractiveTable';
|
||||
import { Column } from './types';
|
||||
|
||||
interface TableData {
|
||||
id: string;
|
||||
country?: string;
|
||||
value?: string;
|
||||
}
|
||||
function getRowId(row: TableData) {
|
||||
return row.id;
|
||||
}
|
||||
|
||||
describe('InteractiveTable', () => {
|
||||
it('should not render hidden columns', () => {
|
||||
const columns: Array<Column<TableData>> = [
|
||||
{ id: 'id', header: 'ID' },
|
||||
{ id: 'country', header: 'Country', visible: () => false },
|
||||
];
|
||||
const data: TableData[] = [
|
||||
{ id: '1', country: 'Sweden' },
|
||||
{ id: '2', country: 'Portugal' },
|
||||
];
|
||||
render(<InteractiveTable columns={columns} data={data} getRowId={getRowId} />);
|
||||
|
||||
expect(screen.getByRole('columnheader', { name: 'ID' })).toBeInTheDocument();
|
||||
expect(screen.queryByRole('columnheader', { name: 'Country' })).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('should correctly sort rows', () => {
|
||||
// We are not testing the sorting logic here since it is already tested in react-table,
|
||||
// but instead we are testing that the sorting is applied correctly to the table and correct aria attributes are set
|
||||
// according to https://www.w3.org/WAI/ARIA/apg/example-index/table/sortable-table
|
||||
const columns: Array<Column<TableData>> = [
|
||||
{ id: 'id', header: 'ID' },
|
||||
{ id: 'value', header: 'Value', sortType: 'string' },
|
||||
{ id: 'country', header: 'Country', sortType: 'number' },
|
||||
];
|
||||
const data: TableData[] = [
|
||||
{ id: '1', value: '1', country: 'Sweden' },
|
||||
{ id: '2', value: '3', country: 'Portugal' },
|
||||
{ id: '3', value: '2', country: 'Italy' },
|
||||
];
|
||||
render(<InteractiveTable columns={columns} data={data} getRowId={getRowId} />);
|
||||
|
||||
const valueColumnHeader = screen.getByRole('columnheader', { name: 'Value' });
|
||||
const countryColumnHeader = screen.getByRole('columnheader', { name: 'Country' });
|
||||
const valueColumnSortButton = getByRole(valueColumnHeader, 'button');
|
||||
const countryColumnSortButton = getByRole(countryColumnHeader, 'button');
|
||||
|
||||
expect(valueColumnHeader).not.toHaveAttribute('aria-sort');
|
||||
expect(countryColumnHeader).not.toHaveAttribute('aria-sort');
|
||||
|
||||
fireEvent.click(countryColumnSortButton);
|
||||
expect(valueColumnHeader).not.toHaveAttribute('aria-sort');
|
||||
expect(countryColumnHeader).toHaveAttribute('aria-sort', 'ascending');
|
||||
|
||||
fireEvent.click(valueColumnSortButton);
|
||||
expect(valueColumnHeader).toHaveAttribute('aria-sort', 'ascending');
|
||||
expect(countryColumnHeader).not.toHaveAttribute('aria-sort');
|
||||
|
||||
fireEvent.click(valueColumnSortButton);
|
||||
expect(valueColumnHeader).toHaveAttribute('aria-sort', 'descending');
|
||||
expect(countryColumnHeader).not.toHaveAttribute('aria-sort');
|
||||
|
||||
fireEvent.click(valueColumnSortButton);
|
||||
expect(valueColumnHeader).not.toHaveAttribute('aria-sort');
|
||||
expect(countryColumnHeader).not.toHaveAttribute('aria-sort');
|
||||
});
|
||||
|
||||
it('correctly expands rows', () => {
|
||||
const columns: Array<Column<TableData>> = [{ id: 'id', header: 'ID' }];
|
||||
const data: TableData[] = [{ id: '1', value: '1', country: 'Sweden' }];
|
||||
render(
|
||||
<InteractiveTable
|
||||
columns={columns}
|
||||
data={data}
|
||||
getRowId={getRowId}
|
||||
renderExpandedRow={(row) => <div data-testid={`test-${row.id}`}>{row.country}</div>}
|
||||
/>
|
||||
);
|
||||
|
||||
const expanderButton = screen.getByRole('button', { name: /toggle row expanded/i });
|
||||
fireEvent.click(expanderButton);
|
||||
|
||||
expect(screen.getByTestId('test-1')).toHaveTextContent('Sweden');
|
||||
|
||||
expect(expanderButton.getAttribute('aria-controls')).toBe(
|
||||
// anchestor tr's id should match the expander button's aria-controls attribute
|
||||
screen.getByTestId('test-1').parentElement?.parentElement?.id
|
||||
);
|
||||
});
|
||||
});
|
@ -1,20 +1,14 @@
|
||||
import { cx, css } from '@emotion/css';
|
||||
import React, { useMemo, Fragment, ReactNode } from 'react';
|
||||
import {
|
||||
CellProps,
|
||||
SortByFn,
|
||||
useExpanded,
|
||||
useSortBy,
|
||||
useTable,
|
||||
DefaultSortTypes,
|
||||
TableOptions,
|
||||
IdType,
|
||||
} from 'react-table';
|
||||
import { uniqueId } from 'lodash';
|
||||
import React, { useMemo, Fragment, ReactNode, useCallback } from 'react';
|
||||
import { useExpanded, useSortBy, useTable, TableOptions, Row, HeaderGroup } from 'react-table';
|
||||
|
||||
import { GrafanaTheme2 } from '@grafana/data';
|
||||
import { Icon, useStyles2 } from '@grafana/ui';
|
||||
import { isTruthy } from 'app/core/utils/types';
|
||||
import { GrafanaTheme2, isTruthy } from '@grafana/data';
|
||||
|
||||
import { useStyles2 } from '../../themes';
|
||||
import { Icon } from '../Icon/Icon';
|
||||
|
||||
import { Column } from './types';
|
||||
import { EXPANDER_CELL_ID, getColumns } from './utils';
|
||||
|
||||
const getStyles = (theme: GrafanaTheme2) => ({
|
||||
@ -23,47 +17,74 @@ const getStyles = (theme: GrafanaTheme2) => ({
|
||||
border: solid 1px ${theme.colors.border.weak};
|
||||
background-color: ${theme.colors.background.secondary};
|
||||
width: 100%;
|
||||
|
||||
td {
|
||||
padding: ${theme.spacing(1)};
|
||||
}
|
||||
|
||||
td,
|
||||
th {
|
||||
padding: ${theme.spacing(1)};
|
||||
min-width: ${theme.spacing(3)};
|
||||
}
|
||||
`,
|
||||
evenRow: css`
|
||||
background: ${theme.colors.background.primary};
|
||||
`,
|
||||
shrink: css`
|
||||
disableGrow: css`
|
||||
width: 0%;
|
||||
`,
|
||||
header: css`
|
||||
&,
|
||||
& > button {
|
||||
position: relative;
|
||||
white-space: nowrap;
|
||||
padding: ${theme.spacing(1)};
|
||||
}
|
||||
& > button {
|
||||
&:after {
|
||||
content: '\\00a0';
|
||||
}
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
background: none;
|
||||
border: none;
|
||||
padding-right: ${theme.spacing(2.5)};
|
||||
text-align: left;
|
||||
&:hover {
|
||||
background-color: ${theme.colors.emphasize(theme.colors.background.secondary, 0.05)};
|
||||
}
|
||||
}
|
||||
`,
|
||||
sortableHeader: css`
|
||||
/* increases selector's specificity so that it always takes precedence over default styles */
|
||||
&& {
|
||||
padding: 0;
|
||||
}
|
||||
`,
|
||||
});
|
||||
|
||||
export interface Column<TableData extends object> {
|
||||
/**
|
||||
* ID of the column.
|
||||
* Set this to the matching object key of your data or `undefined` if the column doesn't have any associated data with it.
|
||||
* This must be unique among all other columns.
|
||||
*/
|
||||
id?: IdType<TableData>;
|
||||
cell?: (props: CellProps<TableData>) => ReactNode;
|
||||
header?: (() => ReactNode | string) | string;
|
||||
sortType?: DefaultSortTypes | SortByFn<TableData>;
|
||||
shrink?: boolean;
|
||||
visible?: (col: TableData[]) => boolean;
|
||||
}
|
||||
|
||||
interface Props<TableData extends object> {
|
||||
/**
|
||||
* Table's columns definition. Must be memoized.
|
||||
*/
|
||||
columns: Array<Column<TableData>>;
|
||||
/**
|
||||
* The data to display in the table. Must be memoized.
|
||||
*/
|
||||
data: TableData[];
|
||||
renderExpandedRow?: (row: TableData) => JSX.Element;
|
||||
/**
|
||||
* Render function for the expanded row. if not provided, the tables rows will not be expandable.
|
||||
*/
|
||||
renderExpandedRow?: (row: TableData) => ReactNode;
|
||||
className?: string;
|
||||
/**
|
||||
* Must return a unique id for each row
|
||||
*/
|
||||
getRowId: TableOptions<TableData>['getRowId'];
|
||||
}
|
||||
|
||||
/**
|
||||
* non-viz table component.
|
||||
* Will need most likely to be moved in @grafana/ui
|
||||
*/
|
||||
export function Table<TableData extends object>({
|
||||
/** @alpha */
|
||||
export function InteractiveTable<TableData extends object>({
|
||||
data,
|
||||
className,
|
||||
columns,
|
||||
@ -75,6 +96,13 @@ export function Table<TableData extends object>({
|
||||
const cols = getColumns<TableData>(columns);
|
||||
return cols;
|
||||
}, [columns]);
|
||||
const id = useUniqueId();
|
||||
const getRowHTMLID = useCallback(
|
||||
(row: Row<TableData>) => {
|
||||
return `${id}-${row.id}`.replace(/\s/g, '');
|
||||
},
|
||||
[id]
|
||||
);
|
||||
|
||||
const { getTableProps, getTableBodyProps, headerGroups, rows, prepareRow } = useTable<TableData>(
|
||||
{
|
||||
@ -82,12 +110,13 @@ export function Table<TableData extends object>({
|
||||
data,
|
||||
autoResetExpanded: false,
|
||||
autoResetSortBy: false,
|
||||
disableMultiSort: true,
|
||||
getRowId,
|
||||
initialState: {
|
||||
hiddenColumns: [
|
||||
!renderExpandedRow && EXPANDER_CELL_ID,
|
||||
...tableColumns
|
||||
.filter((col) => !(col.visible?.(data) ?? true))
|
||||
.filter((col) => !(col.visible ? col.visible(data) : true))
|
||||
.map((c) => c.id)
|
||||
.filter(isTruthy),
|
||||
].filter(isTruthy),
|
||||
@ -96,6 +125,7 @@ export function Table<TableData extends object>({
|
||||
useSortBy,
|
||||
useExpanded
|
||||
);
|
||||
|
||||
// This should be called only for rows thar we'd want to actually render, which is all at this stage.
|
||||
// We may want to revisit this if we decide to add pagination and/or virtualized tables.
|
||||
rows.forEach(prepareRow);
|
||||
@ -109,16 +139,19 @@ export function Table<TableData extends object>({
|
||||
return (
|
||||
<tr key={key} {...headerRowProps}>
|
||||
{headerGroup.headers.map((column) => {
|
||||
// TODO: if the column is a function, it should also provide an accessible name as a string to be used a the column title in getSortByToggleProps
|
||||
const { key, ...headerCellProps } = column.getHeaderProps(
|
||||
column.canSort ? column.getSortByToggleProps() : undefined
|
||||
);
|
||||
const { key, ...headerCellProps } = column.getHeaderProps();
|
||||
|
||||
return (
|
||||
<th key={key} className={cx(column.width === 0 && styles.shrink)} {...headerCellProps}>
|
||||
{column.render('Header')}
|
||||
|
||||
{column.isSorted && <Icon name={column.isSortedDesc ? 'angle-down' : 'angle-up'} />}
|
||||
<th
|
||||
key={key}
|
||||
className={cx(styles.header, {
|
||||
[styles.disableGrow]: column.width === 0,
|
||||
[styles.sortableHeader]: column.canSort,
|
||||
})}
|
||||
{...headerCellProps}
|
||||
{...(column.isSorted && { 'aria-sort': column.isSortedDesc ? 'descending' : 'ascending' })}
|
||||
>
|
||||
<ColumnHeader column={column} />
|
||||
</th>
|
||||
);
|
||||
})}
|
||||
@ -131,6 +164,7 @@ export function Table<TableData extends object>({
|
||||
{rows.map((row, rowIndex) => {
|
||||
const className = cx(rowIndex % 2 === 0 && styles.evenRow);
|
||||
const { key, ...otherRowProps } = row.getRowProps();
|
||||
const rowId = getRowHTMLID(row);
|
||||
|
||||
return (
|
||||
<Fragment key={key}>
|
||||
@ -139,7 +173,7 @@ export function Table<TableData extends object>({
|
||||
const { key, ...otherCellProps } = cell.getCellProps();
|
||||
return (
|
||||
<td key={key} {...otherCellProps}>
|
||||
{cell.render('Cell')}
|
||||
{cell.render('Cell', { __rowID: rowId })}
|
||||
</td>
|
||||
);
|
||||
})}
|
||||
@ -147,7 +181,7 @@ export function Table<TableData extends object>({
|
||||
{
|
||||
// @ts-expect-error react-table doesn't ship with useExpanded types and we can't use declaration merging without affecting the table viz
|
||||
row.isExpanded && renderExpandedRow && (
|
||||
<tr className={className} {...otherRowProps}>
|
||||
<tr className={className} {...otherRowProps} id={rowId}>
|
||||
<td colSpan={row.cells.length}>{renderExpandedRow(row.original)}</td>
|
||||
</tr>
|
||||
)
|
||||
@ -159,3 +193,45 @@ export function Table<TableData extends object>({
|
||||
</table>
|
||||
);
|
||||
}
|
||||
|
||||
const useUniqueId = () => {
|
||||
return useMemo(() => uniqueId('InteractiveTable'), []);
|
||||
};
|
||||
|
||||
const getColumnheaderStyles = (theme: GrafanaTheme2) => ({
|
||||
sortIcon: css`
|
||||
position: absolute;
|
||||
top: ${theme.spacing(1)};
|
||||
`,
|
||||
});
|
||||
|
||||
function ColumnHeader<T extends object>({
|
||||
column: { canSort, render, isSorted, isSortedDesc, getSortByToggleProps },
|
||||
}: {
|
||||
column: HeaderGroup<T>;
|
||||
}) {
|
||||
const styles = useStyles2(getColumnheaderStyles);
|
||||
const { onClick } = getSortByToggleProps();
|
||||
|
||||
const children = (
|
||||
<>
|
||||
{render('Header')}
|
||||
|
||||
{isSorted && (
|
||||
<span aria-hidden="true" className={styles.sortIcon}>
|
||||
<Icon name={isSortedDesc ? 'angle-down' : 'angle-up'} />
|
||||
</span>
|
||||
)}
|
||||
</>
|
||||
);
|
||||
|
||||
if (canSort) {
|
||||
return (
|
||||
<button type="button" onClick={onClick}>
|
||||
{children}
|
||||
</button>
|
||||
);
|
||||
}
|
||||
|
||||
return children;
|
||||
}
|
29
packages/grafana-ui/src/components/InteractiveTable/types.ts
Normal file
29
packages/grafana-ui/src/components/InteractiveTable/types.ts
Normal file
@ -0,0 +1,29 @@
|
||||
import { ReactNode } from 'react';
|
||||
import { CellProps, DefaultSortTypes, IdType, SortByFn } from 'react-table';
|
||||
|
||||
export interface Column<TableData extends object> {
|
||||
/**
|
||||
* ID of the column. Must be unique among all other columns
|
||||
*/
|
||||
id: IdType<TableData>;
|
||||
/**
|
||||
* Custom render function for te cell
|
||||
*/
|
||||
cell?: (props: CellProps<TableData>) => ReactNode;
|
||||
/**
|
||||
* Header name. if `undefined` the header will be empty. Useful for action columns.
|
||||
*/
|
||||
header?: string;
|
||||
/**
|
||||
* Column sort type. If `undefined` the column will not be sortable.
|
||||
* */
|
||||
sortType?: DefaultSortTypes | SortByFn<TableData>;
|
||||
/**
|
||||
* If `true` prevents the column from growing more than its content.
|
||||
*/
|
||||
disableGrow?: boolean;
|
||||
/**
|
||||
* If the provided function returns `false` the column will be hidden.
|
||||
*/
|
||||
visible?: (data: TableData[]) => boolean;
|
||||
}
|
@ -1,11 +1,9 @@
|
||||
import { uniqueId } from 'lodash';
|
||||
import { Column as RTColumn } from 'react-table';
|
||||
|
||||
import { ExpanderCell } from './ExpanderCell';
|
||||
import { Column } from './types';
|
||||
|
||||
import { Column } from '.';
|
||||
|
||||
export const EXPANDER_CELL_ID = '__expander';
|
||||
export const EXPANDER_CELL_ID = '__expander' as const;
|
||||
|
||||
type InternalColumn<T extends object> = RTColumn<T> & {
|
||||
visible?: (data: T[]) => boolean;
|
||||
@ -24,11 +22,12 @@ export function getColumns<K extends object>(columns: Array<Column<K>>): Array<I
|
||||
// column.id being possibly undefined and not keyof T (where T is the data object)
|
||||
// We do not want to be that strict as we simply pass undefined to cells that do not have data associated with them.
|
||||
...columns.map((column) => ({
|
||||
id: column.id,
|
||||
accessor: column.id,
|
||||
Header: column.header || (() => null),
|
||||
accessor: column.id || uniqueId(),
|
||||
sortType: column.sortType || 'alphanumeric',
|
||||
disableSortBy: !Boolean(column.sortType),
|
||||
width: column.shrink ? 0 : undefined,
|
||||
width: column.disableGrow ? 0 : undefined,
|
||||
visible: column.visible,
|
||||
...(column.cell && { Cell: column.cell }),
|
||||
})),
|
@ -42,6 +42,7 @@ export {
|
||||
} from './DateTimePickers/DatePickerWithInput/DatePickerWithInput';
|
||||
export { DateTimePicker } from './DateTimePickers/DateTimePicker/DateTimePicker';
|
||||
export { List } from './List/List';
|
||||
export { InteractiveTable } from './InteractiveTable/InteractiveTable';
|
||||
export { TagsInput } from './TagsInput/TagsInput';
|
||||
export { Pagination } from './Pagination/Pagination';
|
||||
export { Tag, type OnTagClick } from './Tags/Tag';
|
||||
|
@ -6,3 +6,4 @@ export * from './forms';
|
||||
export * from './icon';
|
||||
export * from './select';
|
||||
export * from './size';
|
||||
export * from './interactiveTable';
|
||||
|
2
packages/grafana-ui/src/types/interactiveTable.ts
Normal file
2
packages/grafana-ui/src/types/interactiveTable.ts
Normal file
@ -0,0 +1,2 @@
|
||||
export type { Column } from '../components/InteractiveTable/types';
|
||||
export type { CellProps, SortByFn } from 'react-table';
|
@ -1,3 +0,0 @@
|
||||
type Truthy<T> = T extends false | '' | 0 | null | undefined ? never : T;
|
||||
|
||||
export const isTruthy = <T>(value: T): value is Truthy<T> => Boolean(value);
|
@ -1,4 +1,13 @@
|
||||
import { render, waitFor, screen, fireEvent, waitForElementToBeRemoved, within, Matcher } from '@testing-library/react';
|
||||
import {
|
||||
render,
|
||||
waitFor,
|
||||
screen,
|
||||
fireEvent,
|
||||
waitForElementToBeRemoved,
|
||||
within,
|
||||
Matcher,
|
||||
getByRole,
|
||||
} from '@testing-library/react';
|
||||
import { merge, uniqueId } from 'lodash';
|
||||
import React from 'react';
|
||||
import { DeepPartial } from 'react-hook-form';
|
||||
@ -411,7 +420,7 @@ describe('CorrelationsPage', () => {
|
||||
});
|
||||
|
||||
it('correctly sorts by source', async () => {
|
||||
const sourceHeader = getHeaderByName('Source');
|
||||
const sourceHeader = getByRole(getHeaderByName('Source'), 'button');
|
||||
fireEvent.click(sourceHeader);
|
||||
let cells = queryCellsByColumnName('Source');
|
||||
cells.forEach((cell, i, allCells) => {
|
||||
|
@ -1,11 +1,22 @@
|
||||
import { css } from '@emotion/css';
|
||||
import { negate } from 'lodash';
|
||||
import React, { memo, useCallback, useEffect, useMemo, useState } from 'react';
|
||||
import { CellProps, SortByFn } from 'react-table';
|
||||
|
||||
import { GrafanaTheme2 } from '@grafana/data';
|
||||
import { isFetchError, reportInteraction } from '@grafana/runtime';
|
||||
import { Badge, Button, DeleteButton, HorizontalGroup, LoadingPlaceholder, useStyles2, Alert } from '@grafana/ui';
|
||||
import {
|
||||
Badge,
|
||||
Button,
|
||||
DeleteButton,
|
||||
HorizontalGroup,
|
||||
LoadingPlaceholder,
|
||||
useStyles2,
|
||||
Alert,
|
||||
InteractiveTable,
|
||||
type Column,
|
||||
type CellProps,
|
||||
type SortByFn,
|
||||
} from '@grafana/ui';
|
||||
import { Page } from 'app/core/components/Page/Page';
|
||||
import { contextSrv } from 'app/core/core';
|
||||
import { useNavModel } from 'app/core/hooks/useNavModel';
|
||||
@ -14,7 +25,6 @@ import { AccessControlAction } from 'app/types';
|
||||
import { AddCorrelationForm } from './Forms/AddCorrelationForm';
|
||||
import { EditCorrelationForm } from './Forms/EditCorrelationForm';
|
||||
import { EmptyCorrelationsCTA } from './components/EmptyCorrelationsCTA';
|
||||
import { Column, Table } from './components/Table';
|
||||
import type { RemoveCorrelationParams } from './types';
|
||||
import { CorrelationData, useCorrelations } from './useCorrelations';
|
||||
|
||||
@ -97,8 +107,9 @@ export default function CorrelationsPage() {
|
||||
const columns = useMemo<Array<Column<CorrelationData>>>(
|
||||
() => [
|
||||
{
|
||||
id: 'info',
|
||||
cell: InfoCell,
|
||||
shrink: true,
|
||||
disableGrow: true,
|
||||
visible: (data) => data.some(isSourceReadOnly),
|
||||
},
|
||||
{
|
||||
@ -115,8 +126,9 @@ export default function CorrelationsPage() {
|
||||
},
|
||||
{ id: 'label', header: 'Label', sortType: 'alphanumeric' },
|
||||
{
|
||||
id: 'actions',
|
||||
cell: RowActions,
|
||||
shrink: true,
|
||||
disableGrow: true,
|
||||
visible: (data) => canWriteCorrelations && data.some(negate(isSourceReadOnly)),
|
||||
},
|
||||
],
|
||||
@ -166,7 +178,7 @@ export default function CorrelationsPage() {
|
||||
{isAdding && <AddCorrelationForm onClose={() => setIsAdding(false)} onCreated={handleAdded} />}
|
||||
|
||||
{data && data.length >= 1 && (
|
||||
<Table
|
||||
<InteractiveTable
|
||||
renderExpandedRow={(correlation) => (
|
||||
<ExpendedRow
|
||||
correlation={correlation}
|
||||
|
@ -3,6 +3,7 @@ import { inRange } from 'lodash';
|
||||
import React, { useEffect, useRef, useState } from 'react';
|
||||
import { useWindowSize } from 'react-use';
|
||||
|
||||
import { isTruthy } from '@grafana/data';
|
||||
import { locationService } from '@grafana/runtime';
|
||||
import { ErrorBoundaryAlert, usePanelContext } from '@grafana/ui';
|
||||
import { SplitPaneWrapper } from 'app/core/components/SplitPaneWrapper/SplitPaneWrapper';
|
||||
@ -10,7 +11,6 @@ import { useGrafana } from 'app/core/context/GrafanaContext';
|
||||
import { useAppNotification } from 'app/core/copy/appNotification';
|
||||
import { useNavModel } from 'app/core/hooks/useNavModel';
|
||||
import { GrafanaRouteComponentProps } from 'app/core/navigation/types';
|
||||
import { isTruthy } from 'app/core/utils/types';
|
||||
import { useDispatch, useSelector } from 'app/types';
|
||||
import { ExploreId, ExploreQueryParams } from 'app/types/explore';
|
||||
|
||||
|
@ -1,9 +1,8 @@
|
||||
import React from 'react';
|
||||
import { valid } from 'semver';
|
||||
|
||||
import { DataSourceSettings, SelectableValue } from '@grafana/data';
|
||||
import { DataSourceSettings, SelectableValue, isTruthy } from '@grafana/data';
|
||||
import { FieldSet, InlineField, Input, Select, InlineSwitch } from '@grafana/ui';
|
||||
import { isTruthy } from 'app/core/utils/types';
|
||||
|
||||
import { ElasticsearchOptions, Interval } from '../types';
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user