2022-08-11 04:33:29 +00:00
|
|
|
import {
|
2023-05-02 06:42:16 +00:00
|
|
|
Table as TableInstance,
|
2022-08-11 04:33:29 +00:00
|
|
|
TableState,
|
2023-05-02 06:42:16 +00:00
|
|
|
useReactTable,
|
|
|
|
Row,
|
2023-08-06 22:30:38 +00:00
|
|
|
Column,
|
2023-05-02 06:42:16 +00:00
|
|
|
getCoreRowModel,
|
|
|
|
getPaginationRowModel,
|
|
|
|
getFilteredRowModel,
|
|
|
|
getSortedRowModel,
|
|
|
|
getFacetedRowModel,
|
|
|
|
getFacetedUniqueValues,
|
|
|
|
getFacetedMinMaxValues,
|
|
|
|
getExpandedRowModel,
|
|
|
|
TableOptions,
|
2023-07-16 07:59:58 +00:00
|
|
|
TableMeta,
|
2023-05-02 06:42:16 +00:00
|
|
|
} from '@tanstack/react-table';
|
|
|
|
import { ReactNode, useMemo } from 'react';
|
2022-10-11 01:05:53 +00:00
|
|
|
import clsx from 'clsx';
|
2023-05-02 06:42:16 +00:00
|
|
|
import _ from 'lodash';
|
2022-08-11 04:33:29 +00:00
|
|
|
|
2023-06-22 14:11:10 +00:00
|
|
|
import { AutomationTestingProps } from '@/types';
|
|
|
|
|
2022-09-13 13:56:38 +00:00
|
|
|
import { IconProps } from '@@/Icon';
|
2022-08-11 04:33:29 +00:00
|
|
|
|
2022-11-22 12:16:34 +00:00
|
|
|
import { DatatableHeader } from './DatatableHeader';
|
|
|
|
import { DatatableFooter } from './DatatableFooter';
|
|
|
|
import { defaultGetRowId } from './defaultGetRowId';
|
2023-05-02 06:42:16 +00:00
|
|
|
import { Table } from './Table';
|
2022-11-22 12:16:34 +00:00
|
|
|
import { useGoToHighlightedRow } from './useGoToHighlightedRow';
|
2023-05-02 06:42:16 +00:00
|
|
|
import { BasicTableSettings } from './types';
|
|
|
|
import { DatatableContent } from './DatatableContent';
|
|
|
|
import { createSelectColumn } from './select-column';
|
|
|
|
import { TableRow } from './TableRow';
|
2023-07-16 07:59:58 +00:00
|
|
|
import { type TableState as GlobalTableState } from './useTableState';
|
2022-11-22 12:16:34 +00:00
|
|
|
|
2023-08-22 06:36:31 +00:00
|
|
|
export type PaginationProps =
|
|
|
|
| {
|
|
|
|
isServerSidePagination?: false;
|
|
|
|
totalCount?: never;
|
|
|
|
page?: never;
|
|
|
|
onPageChange?: never;
|
|
|
|
}
|
|
|
|
| {
|
|
|
|
isServerSidePagination: true;
|
|
|
|
totalCount: number;
|
|
|
|
page: number;
|
|
|
|
onPageChange(page: number): void;
|
|
|
|
};
|
|
|
|
|
2023-05-02 06:42:16 +00:00
|
|
|
export interface Props<
|
|
|
|
D extends Record<string, unknown>,
|
2023-07-16 07:59:58 +00:00
|
|
|
TMeta extends TableMeta<D> = TableMeta<D>
|
2023-06-22 14:11:10 +00:00
|
|
|
> extends AutomationTestingProps {
|
2022-08-11 04:33:29 +00:00
|
|
|
dataset: D[];
|
2023-05-02 06:42:16 +00:00
|
|
|
columns: TableOptions<D>['columns'];
|
2022-08-11 04:33:29 +00:00
|
|
|
renderTableSettings?(instance: TableInstance<D>): ReactNode;
|
|
|
|
renderTableActions?(selectedRows: D[]): ReactNode;
|
|
|
|
disableSelect?: boolean;
|
|
|
|
getRowId?(row: D): string;
|
|
|
|
isRowSelectable?(row: Row<D>): boolean;
|
|
|
|
emptyContentLabel?: string;
|
2022-11-22 12:16:34 +00:00
|
|
|
title?: string;
|
|
|
|
titleIcon?: IconProps['icon'];
|
2023-05-02 06:42:16 +00:00
|
|
|
initialTableState?: Partial<TableState>;
|
2022-08-11 04:33:29 +00:00
|
|
|
isLoading?: boolean;
|
2022-11-22 12:16:34 +00:00
|
|
|
description?: ReactNode;
|
|
|
|
highlightedItemId?: string;
|
2023-07-16 07:59:58 +00:00
|
|
|
settingsManager: GlobalTableState<BasicTableSettings>;
|
2023-05-02 06:42:16 +00:00
|
|
|
renderRow?(row: Row<D>, highlightedItemId?: string): ReactNode;
|
|
|
|
getRowCanExpand?(row: Row<D>): boolean;
|
2022-11-22 12:16:34 +00:00
|
|
|
noWidget?: boolean;
|
2023-07-16 07:59:58 +00:00
|
|
|
meta?: TMeta;
|
2022-08-11 04:33:29 +00:00
|
|
|
}
|
|
|
|
|
2023-07-16 07:59:58 +00:00
|
|
|
export function Datatable<
|
|
|
|
D extends Record<string, unknown>,
|
|
|
|
TMeta extends TableMeta<D> = TableMeta<D>
|
|
|
|
>({
|
2022-08-11 04:33:29 +00:00
|
|
|
columns,
|
|
|
|
dataset,
|
2022-11-22 12:16:34 +00:00
|
|
|
renderTableSettings = () => null,
|
|
|
|
renderTableActions = () => null,
|
2022-08-11 04:33:29 +00:00
|
|
|
disableSelect,
|
|
|
|
getRowId = defaultGetRowId,
|
|
|
|
isRowSelectable = () => true,
|
2022-11-22 12:16:34 +00:00
|
|
|
title,
|
|
|
|
titleIcon,
|
2022-08-11 04:33:29 +00:00
|
|
|
emptyContentLabel,
|
|
|
|
initialTableState = {},
|
|
|
|
isLoading,
|
2022-09-26 19:43:24 +00:00
|
|
|
description,
|
2023-05-02 06:42:16 +00:00
|
|
|
settingsManager: settings,
|
2022-11-22 12:16:34 +00:00
|
|
|
renderRow = defaultRenderRow,
|
|
|
|
highlightedItemId,
|
|
|
|
noWidget,
|
2023-05-02 06:42:16 +00:00
|
|
|
getRowCanExpand,
|
2023-06-22 14:11:10 +00:00
|
|
|
'data-cy': dataCy,
|
2023-07-16 07:59:58 +00:00
|
|
|
meta,
|
2023-08-22 06:36:31 +00:00
|
|
|
onPageChange = () => {},
|
|
|
|
page,
|
|
|
|
totalCount = dataset.length,
|
|
|
|
isServerSidePagination = false,
|
|
|
|
}: Props<D, TMeta> & PaginationProps) {
|
|
|
|
const pageCount = useMemo(
|
|
|
|
() => Math.ceil(totalCount / settings.pageSize),
|
|
|
|
[settings.pageSize, totalCount]
|
|
|
|
);
|
|
|
|
|
2023-05-02 06:42:16 +00:00
|
|
|
const enableRowSelection = getIsSelectionEnabled(
|
|
|
|
disableSelect,
|
|
|
|
isRowSelectable
|
|
|
|
);
|
|
|
|
|
|
|
|
const allColumns = useMemo(
|
|
|
|
() => _.compact([!disableSelect && createSelectColumn<D>(), ...columns]),
|
|
|
|
[disableSelect, columns]
|
|
|
|
);
|
2022-08-11 04:33:29 +00:00
|
|
|
|
2023-05-02 06:42:16 +00:00
|
|
|
const tableInstance = useReactTable<D>({
|
|
|
|
columns: allColumns,
|
|
|
|
data: dataset,
|
|
|
|
initialState: {
|
|
|
|
pagination: {
|
|
|
|
pageSize: settings.pageSize,
|
2023-08-16 21:53:28 +00:00
|
|
|
pageIndex: page || 0,
|
2022-08-11 04:33:29 +00:00
|
|
|
},
|
2023-05-02 06:42:16 +00:00
|
|
|
sorting: settings.sortBy ? [settings.sortBy] : [],
|
|
|
|
globalFilter: settings.search,
|
|
|
|
|
|
|
|
...initialTableState,
|
2022-08-11 04:33:29 +00:00
|
|
|
},
|
2023-05-02 06:42:16 +00:00
|
|
|
defaultColumn: {
|
|
|
|
enableColumnFilter: false,
|
|
|
|
enableHiding: true,
|
2023-08-22 06:36:31 +00:00
|
|
|
sortingFn: 'alphanumeric',
|
2023-05-02 06:42:16 +00:00
|
|
|
},
|
|
|
|
enableRowSelection,
|
|
|
|
autoResetExpanded: false,
|
|
|
|
globalFilterFn,
|
|
|
|
getRowId,
|
|
|
|
getCoreRowModel: getCoreRowModel(),
|
|
|
|
getFilteredRowModel: getFilteredRowModel(),
|
|
|
|
getPaginationRowModel: getPaginationRowModel(),
|
|
|
|
getFacetedRowModel: getFacetedRowModel(),
|
|
|
|
getFacetedUniqueValues: getFacetedUniqueValues(),
|
|
|
|
getFacetedMinMaxValues: getFacetedMinMaxValues(),
|
|
|
|
getExpandedRowModel: getExpandedRowModel(),
|
|
|
|
getRowCanExpand,
|
2023-08-06 22:30:38 +00:00
|
|
|
getColumnCanGlobalFilter,
|
2023-08-22 06:36:31 +00:00
|
|
|
...(isServerSidePagination
|
|
|
|
? { manualPagination: true, pageCount }
|
|
|
|
: {
|
|
|
|
getSortedRowModel: getSortedRowModel(),
|
|
|
|
}),
|
2023-07-16 07:59:58 +00:00
|
|
|
meta,
|
2023-05-02 06:42:16 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const tableState = tableInstance.getState();
|
2022-08-11 04:33:29 +00:00
|
|
|
|
2022-11-22 12:16:34 +00:00
|
|
|
useGoToHighlightedRow(
|
|
|
|
isServerSidePagination,
|
2023-05-02 06:42:16 +00:00
|
|
|
tableState.pagination.pageSize,
|
|
|
|
tableInstance.getCoreRowModel().rows,
|
2022-11-22 12:16:34 +00:00
|
|
|
handlePageChange,
|
|
|
|
highlightedItemId
|
|
|
|
);
|
2022-08-11 04:33:29 +00:00
|
|
|
|
2023-05-02 06:42:16 +00:00
|
|
|
const selectedRowModel = tableInstance.getSelectedRowModel();
|
|
|
|
const selectedItems = selectedRowModel.rows.map((row) => row.original);
|
2022-08-11 04:33:29 +00:00
|
|
|
|
|
|
|
return (
|
2022-11-22 12:16:34 +00:00
|
|
|
<Table.Container noWidget={noWidget}>
|
|
|
|
<DatatableHeader
|
|
|
|
onSearchChange={handleSearchBarChange}
|
2023-05-02 06:42:16 +00:00
|
|
|
searchValue={settings.search}
|
2022-11-22 12:16:34 +00:00
|
|
|
title={title}
|
|
|
|
titleIcon={titleIcon}
|
2023-05-02 06:42:16 +00:00
|
|
|
description={description}
|
2022-11-22 12:16:34 +00:00
|
|
|
renderTableActions={() => renderTableActions(selectedItems)}
|
|
|
|
renderTableSettings={() => renderTableSettings(tableInstance)}
|
|
|
|
/>
|
2023-08-16 21:53:28 +00:00
|
|
|
|
2022-11-22 12:16:34 +00:00
|
|
|
<DatatableContent<D>
|
|
|
|
tableInstance={tableInstance}
|
2023-05-02 06:42:16 +00:00
|
|
|
renderRow={(row) => renderRow(row, highlightedItemId)}
|
2022-11-22 12:16:34 +00:00
|
|
|
emptyContentLabel={emptyContentLabel}
|
|
|
|
isLoading={isLoading}
|
|
|
|
onSortChange={handleSortChange}
|
2023-06-22 14:11:10 +00:00
|
|
|
data-cy={dataCy}
|
2022-11-22 12:16:34 +00:00
|
|
|
/>
|
|
|
|
|
|
|
|
<DatatableFooter
|
|
|
|
onPageChange={handlePageChange}
|
|
|
|
onPageSizeChange={handlePageSizeChange}
|
2023-08-15 03:05:10 +00:00
|
|
|
page={typeof page === 'number' ? page : tableState.pagination.pageIndex}
|
2023-05-02 06:42:16 +00:00
|
|
|
pageSize={tableState.pagination.pageSize}
|
2023-08-22 06:36:31 +00:00
|
|
|
pageCount={tableInstance.getPageCount()}
|
2022-11-22 12:16:34 +00:00
|
|
|
totalSelected={selectedItems.length}
|
|
|
|
/>
|
|
|
|
</Table.Container>
|
2022-08-11 04:33:29 +00:00
|
|
|
);
|
|
|
|
|
2022-11-22 12:16:34 +00:00
|
|
|
function handleSearchBarChange(value: string) {
|
|
|
|
tableInstance.setGlobalFilter(value);
|
2023-05-02 06:42:16 +00:00
|
|
|
settings.setSearch(value);
|
2022-08-11 04:33:29 +00:00
|
|
|
}
|
|
|
|
|
2022-11-22 12:16:34 +00:00
|
|
|
function handlePageChange(page: number) {
|
2023-05-02 06:42:16 +00:00
|
|
|
tableInstance.setPageIndex(page);
|
2022-11-22 12:16:34 +00:00
|
|
|
onPageChange(page);
|
2022-08-11 04:33:29 +00:00
|
|
|
}
|
|
|
|
|
2022-11-22 12:16:34 +00:00
|
|
|
function handleSortChange(colId: string, desc: boolean) {
|
2023-05-02 06:42:16 +00:00
|
|
|
settings.setSortBy(colId, desc);
|
2022-08-11 04:33:29 +00:00
|
|
|
}
|
|
|
|
|
2022-11-22 12:16:34 +00:00
|
|
|
function handlePageSizeChange(pageSize: number) {
|
|
|
|
tableInstance.setPageSize(pageSize);
|
2023-05-02 06:42:16 +00:00
|
|
|
settings.setPageSize(pageSize);
|
2022-11-22 12:16:34 +00:00
|
|
|
}
|
2022-08-11 04:33:29 +00:00
|
|
|
}
|
|
|
|
|
2022-11-22 12:16:34 +00:00
|
|
|
function defaultRenderRow<D extends Record<string, unknown>>(
|
|
|
|
row: Row<D>,
|
|
|
|
highlightedItemId?: string
|
|
|
|
) {
|
|
|
|
return (
|
2023-05-02 06:42:16 +00:00
|
|
|
<TableRow<D>
|
|
|
|
cells={row.getVisibleCells()}
|
|
|
|
className={clsx({
|
2022-11-22 12:16:34 +00:00
|
|
|
active: highlightedItemId === row.id,
|
|
|
|
})}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
2023-05-02 06:42:16 +00:00
|
|
|
|
|
|
|
function getIsSelectionEnabled<D extends Record<string, unknown>>(
|
|
|
|
disabledSelect?: boolean,
|
|
|
|
isRowSelectable?: Props<D>['isRowSelectable']
|
|
|
|
) {
|
|
|
|
if (disabledSelect) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isRowSelectable) {
|
|
|
|
return isRowSelectable;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function globalFilterFn<D>(
|
|
|
|
row: Row<D>,
|
|
|
|
columnId: string,
|
|
|
|
filterValue: null | string
|
|
|
|
): boolean {
|
|
|
|
const value = row.getValue(columnId);
|
|
|
|
|
|
|
|
if (filterValue === null || filterValue === '') {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (value == null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const filterValueLower = filterValue.toLowerCase();
|
|
|
|
|
|
|
|
if (
|
|
|
|
typeof value === 'string' ||
|
|
|
|
typeof value === 'number' ||
|
|
|
|
typeof value === 'boolean'
|
|
|
|
) {
|
|
|
|
return value.toString().toLowerCase().includes(filterValueLower);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Array.isArray(value)) {
|
|
|
|
return value.some((item) => item.toLowerCase().includes(filterValueLower));
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2023-08-06 22:30:38 +00:00
|
|
|
|
|
|
|
function getColumnCanGlobalFilter<D>(column: Column<D, unknown>): boolean {
|
|
|
|
if (column.id === 'select') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|