From ac800f49dde77e1e9b78dd9bab5c0c9d381520da Mon Sep 17 00:00:00 2001 From: Vincenzo Scamporlino Date: Fri, 10 Nov 2023 13:27:41 +0100 Subject: [PATCH] core-components: create BaseTable without filters Signed-off-by: Vincenzo Scamporlino --- .../src/components/Table/Table.tsx | 131 +++++++++++------- .../src/components/Table/index.ts | 2 +- 2 files changed, 81 insertions(+), 52 deletions(-) diff --git a/packages/core-components/src/components/Table/Table.tsx b/packages/core-components/src/components/Table/Table.tsx index 4e9c3b5fb7..bcda04bab5 100644 --- a/packages/core-components/src/components/Table/Table.tsx +++ b/packages/core-components/src/components/Table/Table.tsx @@ -53,6 +53,7 @@ import React, { import { SelectProps } from '../Select/Select'; import { Filter, Filters, SelectedFilters, Without } from './Filters'; +import { TableLoadingBody } from './TableLoadingBody'; // Material-table is not using the standard icons available in in material-ui. https://github.com/mbrn/material-table/issues/51 const tableIcons: Icons = { @@ -315,8 +316,6 @@ export function Table(props: TableProps) { } = props; const tableClasses = tableStyles(); - const theme = useTheme(); - const calculatedInitialState = { ...defaultInitialState, ...initialState }; const [filtersOpen, setFiltersOpen] = useState( @@ -332,8 +331,6 @@ export function Table(props: TableProps) { calculatedInitialState.filters, ); - const MTColumns = convertColumns(columns, theme); - const [search, setSearch] = useState(calculatedInitialState.search); useEffect(() => { @@ -351,12 +348,6 @@ export function Table(props: TableProps) { } }, [search, filtersOpen, selectedFilters, onStateChange]); - const defaultOptions: Options = { - headerStyle: { - textTransform: 'uppercase', - }, - }; - const getFieldByTitle = useCallback( (titleValue: string | keyof T) => columns.find(el => el.title === titleValue)?.field, @@ -468,11 +459,59 @@ export function Table(props: TableProps) { [toggleFilters, hasFilters, selectedFiltersLength, setSearch], ); + return ( + + {filtersOpen && data && typeof data !== 'function' && filters?.length && ( + + )} + + components={{ + Toolbar, + ...components, + }} + options={options} + columns={columns} + title={title} + subtitle={subtitle} + data={typeof data === 'function' ? data : tableData} + {...restProps} + /> + + ); +} + +Table.icons = Object.freeze(tableIcons); + +export function BaseTable( + props: TableProps & { + loading?: boolean; + emptyContent?: ReactNode; + subtitle?: string; + }, +) { + const { + columns, + components, + data, + emptyContent, + loading, + options, + title, + subtitle, + localization, + ...restProps + } = props; + const hasNoRows = typeof data !== 'function' && data.length === 0; + const columnCount = columns.length; const Body = useCallback( (bodyProps: any /* no type for this in material-table */) => { - if (isLoading) { + if (loading) { return ; } @@ -488,49 +527,39 @@ export function Table(props: TableProps) { return ; }, - [hasNoRows, emptyContent, columnCount, isLoading], + [hasNoRows, emptyContent, columnCount, loading], ); + const theme = useTheme(); return ( - - {filtersOpen && data && typeof data !== 'function' && filters?.length && ( - - )} - - components={{ - Header: StyledMTableHeader, - Toolbar, - Body, - ...components, - }} - options={{ ...defaultOptions, ...options }} - columns={MTColumns} - icons={tableIcons} - title={ - <> - - {title} + + components={{ + Header: StyledMTableHeader, + Body, + ...components, + }} + options={{ headerStyle: { textTransform: 'uppercase' }, ...options }} + columns={convertColumns(columns, theme)} + icons={tableIcons} + title={ + <> + + {title} + + {subtitle && ( + + {subtitle} - {subtitle && ( - - {subtitle} - - )} - - } - data={typeof data === 'function' ? data : tableData} - style={{ width: '100%' }} - localization={{ - toolbar: { searchPlaceholder: 'Filter', searchTooltip: 'Filter' }, - }} - {...restProps} - /> - + )} + + } + data={data} + style={{ width: '100%' }} + localization={{ + ...localization, + toolbar: { searchPlaceholder: 'Filter', searchTooltip: 'Filter' }, + }} + {...restProps} + /> ); } - -Table.icons = Object.freeze(tableIcons); diff --git a/packages/core-components/src/components/Table/index.ts b/packages/core-components/src/components/Table/index.ts index d1f2e9d342..2ddfb52ab3 100644 --- a/packages/core-components/src/components/Table/index.ts +++ b/packages/core-components/src/components/Table/index.ts @@ -17,7 +17,7 @@ export type { TableFiltersClassKey } from './Filters'; export { SubvalueCell } from './SubvalueCell'; export type { SubvalueCellClassKey } from './SubvalueCell'; -export { Table, tableStyles } from './Table'; +export { Table, BaseTable, tableStyles } from './Table'; export type { TableColumn, TableFilter,