material-react-table 0.1.0 → 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (102) hide show
  1. package/README.md +32 -162
  2. package/dist/MaterialReactTable.d.ts +62 -26
  3. package/dist/{MRT_TableBody.d.ts → body/MRT_TableBody.d.ts} +0 -0
  4. package/dist/{MRT_TableBodyCell.d.ts → body/MRT_TableBodyCell.d.ts} +1 -1
  5. package/dist/body/MRT_TableBodyRow.d.ts +22 -0
  6. package/dist/{MRT_TableDetailPanel.d.ts → body/MRT_TableDetailPanel.d.ts} +1 -1
  7. package/dist/{MRT_TableBodyRow.d.ts → buttons/MRT_EditActionButtons.d.ts} +2 -2
  8. package/dist/buttons/MRT_ExpandAllButton.d.ts +5 -0
  9. package/dist/{MRT_TableExpandButton.d.ts → buttons/MRT_ExpandButton.d.ts} +2 -2
  10. package/dist/buttons/MRT_ShowHideColumnsButton.d.ts +5 -0
  11. package/dist/buttons/MRT_ToggleColumnActionMenuButton.d.ts +7 -0
  12. package/dist/buttons/MRT_ToggleFiltersButton.d.ts +4 -0
  13. package/dist/buttons/MRT_ToggleRowActionMenuButton.d.ts +7 -0
  14. package/dist/{MRT_TableFooter.d.ts → footer/MRT_TableFooter.d.ts} +0 -0
  15. package/dist/{MRT_TableFooterCell.d.ts → footer/MRT_TableFooterCell.d.ts} +0 -0
  16. package/dist/{MRT_TableFooterRow.d.ts → footer/MRT_TableFooterRow.d.ts} +1 -1
  17. package/dist/{MRT_TableHead.d.ts → head/MRT_TableHead.d.ts} +0 -0
  18. package/dist/head/MRT_TableHeadCell.d.ts +11 -0
  19. package/dist/{MRT_TableHeadRow.d.ts → head/MRT_TableHeadRow.d.ts} +1 -1
  20. package/dist/index.d.ts +2 -0
  21. package/dist/inputs/MRT_DensePaddingSwitch.d.ts +5 -0
  22. package/dist/inputs/MRT_EditCellTextfield.d.ts +7 -0
  23. package/dist/{MRT_TableHeadCell.d.ts → inputs/MRT_FilterTextField.d.ts} +1 -1
  24. package/dist/inputs/MRT_SearchTextField.d.ts +5 -0
  25. package/dist/inputs/MRT_SelectAllCheckbox.d.ts +2 -0
  26. package/dist/inputs/MRT_SelectCheckbox.d.ts +7 -0
  27. package/dist/material-react-table.cjs.development.js +2083 -191
  28. package/dist/material-react-table.cjs.development.js.map +1 -1
  29. package/dist/material-react-table.cjs.production.min.js +1 -1
  30. package/dist/material-react-table.cjs.production.min.js.map +1 -1
  31. package/dist/material-react-table.esm.js +2087 -195
  32. package/dist/material-react-table.esm.js.map +1 -1
  33. package/dist/menus/MRT_ColumnActionMenu.d.ts +9 -0
  34. package/dist/menus/MRT_RowActionMenu.d.ts +9 -0
  35. package/dist/menus/MRT_ShowHideColumnsMenu.d.ts +7 -0
  36. package/dist/{MRT_Table.d.ts → table/MRT_Table.d.ts} +0 -0
  37. package/dist/{MRT_TableContainer.d.ts → table/MRT_TableContainer.d.ts} +0 -0
  38. package/dist/table/MRT_TableSpacerCell.d.ts +6 -0
  39. package/dist/{MRT_TablePagination.d.ts → toolbar/MRT_TablePagination.d.ts} +0 -0
  40. package/dist/toolbar/MRT_ToolbarBottom.d.ts +5 -0
  41. package/dist/toolbar/MRT_ToolbarButtons.d.ts +5 -0
  42. package/dist/toolbar/MRT_ToolbarTop.d.ts +5 -0
  43. package/dist/useMaterialReactTable.d.ts +14 -12
  44. package/dist/utils/localization.d.ts +26 -0
  45. package/dist/utils/useMRTCalcs.d.ts +11 -0
  46. package/package.json +26 -21
  47. package/src/@types/faker.d.ts +4 -0
  48. package/src/@types/react-table-config.d.ts +153 -0
  49. package/src/MaterialReactTable.tsx +151 -37
  50. package/src/body/MRT_TableBody.tsx +35 -0
  51. package/src/body/MRT_TableBodyCell.tsx +69 -0
  52. package/src/body/MRT_TableBodyRow.tsx +84 -0
  53. package/src/body/MRT_TableDetailPanel.tsx +73 -0
  54. package/src/buttons/MRT_EditActionButtons.tsx +55 -0
  55. package/src/buttons/MRT_ExpandAllButton.tsx +47 -0
  56. package/src/buttons/MRT_ExpandButton.tsx +51 -0
  57. package/src/buttons/MRT_ShowHideColumnsButton.tsx +43 -0
  58. package/src/buttons/MRT_ToggleColumnActionMenuButton.tsx +51 -0
  59. package/src/buttons/MRT_ToggleFiltersButton.tsx +23 -0
  60. package/src/buttons/MRT_ToggleRowActionMenuButton.tsx +65 -0
  61. package/src/footer/MRT_TableFooter.tsx +21 -0
  62. package/src/footer/MRT_TableFooterCell.tsx +60 -0
  63. package/src/footer/MRT_TableFooterRow.tsx +62 -0
  64. package/src/head/MRT_TableHead.tsx +26 -0
  65. package/src/head/MRT_TableHeadCell.tsx +137 -0
  66. package/src/head/MRT_TableHeadRow.tsx +90 -0
  67. package/src/index.tsx +2 -0
  68. package/src/inputs/MRT_DensePaddingSwitch.tsx +21 -0
  69. package/src/inputs/MRT_EditCellTextfield.tsx +57 -0
  70. package/src/inputs/MRT_FilterTextField.tsx +63 -0
  71. package/src/inputs/MRT_SearchTextField.tsx +74 -0
  72. package/src/inputs/MRT_SelectAllCheckbox.tsx +26 -0
  73. package/src/inputs/MRT_SelectCheckbox.tsx +34 -0
  74. package/src/menus/MRT_ColumnActionMenu.tsx +107 -0
  75. package/src/menus/MRT_RowActionMenu.tsx +52 -0
  76. package/src/menus/MRT_ShowHideColumnsMenu.tsx +34 -0
  77. package/src/table/MRT_Table.tsx +30 -0
  78. package/src/table/MRT_TableContainer.tsx +50 -0
  79. package/src/table/MRT_TableSpacerCell.tsx +26 -0
  80. package/src/toolbar/MRT_TablePagination.tsx +37 -0
  81. package/src/toolbar/MRT_ToolbarBottom.tsx +43 -0
  82. package/src/toolbar/MRT_ToolbarButtons.tsx +27 -0
  83. package/src/toolbar/MRT_ToolbarTop.tsx +68 -0
  84. package/src/useMaterialReactTable.tsx +70 -32
  85. package/src/utils/localization.ts +51 -0
  86. package/src/utils/useMRTCalcs.tsx +42 -0
  87. package/dist/defaults.d.ts +0 -2
  88. package/src/MRT_Table.tsx +0 -20
  89. package/src/MRT_TableBody.tsx +0 -19
  90. package/src/MRT_TableBodyCell.tsx +0 -15
  91. package/src/MRT_TableBodyRow.tsx +0 -27
  92. package/src/MRT_TableContainer.tsx +0 -16
  93. package/src/MRT_TableDetailPanel.tsx +0 -27
  94. package/src/MRT_TableExpandButton.tsx +0 -25
  95. package/src/MRT_TableFooter.tsx +0 -31
  96. package/src/MRT_TableFooterCell.tsx +0 -25
  97. package/src/MRT_TableFooterRow.tsx +0 -22
  98. package/src/MRT_TableHead.tsx +0 -25
  99. package/src/MRT_TableHeadCell.tsx +0 -27
  100. package/src/MRT_TableHeadRow.tsx +0 -22
  101. package/src/MRT_TablePagination.tsx +0 -34
  102. package/src/defaults.ts +0 -11
@@ -0,0 +1,34 @@
1
+ import React, { FC } from 'react';
2
+ import { FormControlLabel, MenuItem, Switch, Typography } from '@mui/material';
3
+ import { useMaterialReactTable } from '../useMaterialReactTable';
4
+ import { ColumnInstance } from 'react-table';
5
+
6
+ interface Props {
7
+ column: ColumnInstance;
8
+ }
9
+
10
+ export const MRT_ShowHideColumnsMenu: FC<Props> = ({ column }) => {
11
+ const { maxColumnDepth } = useMaterialReactTable();
12
+
13
+ const isMaxDepth = column.depth === maxColumnDepth;
14
+
15
+ return (
16
+ <>
17
+ <MenuItem style={{ paddingLeft: `${column.depth + 1}rem` }}>
18
+ {isMaxDepth ? (
19
+ <FormControlLabel
20
+ checked={column.isVisible}
21
+ control={<Switch />}
22
+ label={column.Header as string}
23
+ onChange={() => isMaxDepth && column.toggleHidden()}
24
+ />
25
+ ) : (
26
+ <Typography>{column.Header}</Typography>
27
+ )}
28
+ </MenuItem>
29
+ {column.columns?.map((c, i) => (
30
+ <MRT_ShowHideColumnsMenu key={`${i}-${c.id}`} column={c} />
31
+ ))}
32
+ </>
33
+ );
34
+ };
@@ -0,0 +1,30 @@
1
+ import React, { FC } from 'react';
2
+ import { Table } from '@mui/material';
3
+ import { MRT_TableHead } from '../head/MRT_TableHead';
4
+ import { MRT_TableBody } from '../body/MRT_TableBody';
5
+ import { MRT_TableFooter } from '../footer/MRT_TableFooter';
6
+ import { useMaterialReactTable } from '../useMaterialReactTable';
7
+
8
+ interface Props {}
9
+
10
+ export const MRT_Table: FC<Props> = () => {
11
+ const { tableInstance, muiTableProps, hideTableHead, hideTableFooter } =
12
+ useMaterialReactTable();
13
+
14
+ const tableProps = {
15
+ ...muiTableProps,
16
+ ...tableInstance.getTableProps(),
17
+ style: {
18
+ ...tableInstance.getTableProps().style,
19
+ ...(muiTableProps?.style ?? {}),
20
+ },
21
+ };
22
+
23
+ return (
24
+ <Table {...tableProps}>
25
+ {!hideTableHead && <MRT_TableHead />}
26
+ <MRT_TableBody />
27
+ {!hideTableFooter && <MRT_TableFooter />}
28
+ </Table>
29
+ );
30
+ };
@@ -0,0 +1,50 @@
1
+ import React, { FC } from 'react';
2
+ import {
3
+ alpha,
4
+ CircularProgress,
5
+ LinearProgress,
6
+ Paper,
7
+ styled,
8
+ TableContainer,
9
+ } from '@mui/material';
10
+ import { useMaterialReactTable } from '../useMaterialReactTable';
11
+ import { MRT_Table } from './MRT_Table';
12
+ import { MRT_ToolbarTop } from '../toolbar/MRT_ToolbarTop';
13
+ import { MRT_ToolbarBottom } from '../toolbar/MRT_ToolbarBottom';
14
+
15
+ const CircularProgressWrapper = styled('div')(({ theme }) => ({
16
+ alignItems: 'center',
17
+ backgroundColor: alpha(theme.palette.background.paper, 0.5),
18
+ display: 'grid',
19
+ height: '100%',
20
+ justifyContent: 'center',
21
+ margin: 'auto',
22
+ position: 'absolute',
23
+ width: 'calc(100% - 2rem)',
24
+ }));
25
+
26
+ interface Props {}
27
+
28
+ export const MRT_TableContainer: FC<Props> = () => {
29
+ const {
30
+ muiTableContainerProps,
31
+ hideToolbarTop,
32
+ hideToolbarBottom,
33
+ isLoading,
34
+ isFetching,
35
+ } = useMaterialReactTable();
36
+
37
+ return (
38
+ <TableContainer component={Paper} {...muiTableContainerProps}>
39
+ {!hideToolbarTop && <MRT_ToolbarTop />}
40
+ {isFetching && <LinearProgress />}
41
+ {isLoading && (
42
+ <CircularProgressWrapper>
43
+ <CircularProgress aria-busy="true" aria-label="Loading data" />
44
+ </CircularProgressWrapper>
45
+ )}
46
+ <MRT_Table />
47
+ {!hideToolbarBottom && <MRT_ToolbarBottom />}
48
+ </TableContainer>
49
+ );
50
+ };
@@ -0,0 +1,26 @@
1
+ import React, { CSSProperties, FC } from 'react';
2
+ import { TableCell } from '@mui/material';
3
+ import { useMaterialReactTable } from '../useMaterialReactTable';
4
+
5
+ interface Props {
6
+ width?: CSSProperties['width'];
7
+ }
8
+
9
+ export const MRT_TableSpacerCell: FC<Props> = ({ width }) => {
10
+ const { muiTableBodyCellProps } = useMaterialReactTable();
11
+
12
+ const mTableBodyCellrops =
13
+ muiTableBodyCellProps instanceof Function
14
+ ? muiTableBodyCellProps()
15
+ : muiTableBodyCellProps;
16
+
17
+ const tableCellProps = {
18
+ ...mTableBodyCellrops,
19
+ style: {
20
+ width,
21
+ ...(mTableBodyCellrops?.style ?? {}),
22
+ },
23
+ };
24
+
25
+ return <TableCell {...tableCellProps} />;
26
+ };
@@ -0,0 +1,37 @@
1
+ import React, { ChangeEvent, FC } from 'react';
2
+ import { TablePagination } from '@mui/material';
3
+ import { useMaterialReactTable } from '../useMaterialReactTable';
4
+
5
+ interface Props {}
6
+
7
+ export const MRT_TablePagination: FC<Props> = () => {
8
+ const { tableInstance, muiTablePaginationProps } = useMaterialReactTable();
9
+
10
+ const tablePaginationProps =
11
+ muiTablePaginationProps instanceof Function
12
+ ? muiTablePaginationProps(tableInstance)
13
+ : muiTablePaginationProps;
14
+
15
+ const handleChangeRowsPerPage = (event: ChangeEvent<HTMLInputElement>) => {
16
+ tableInstance.setPageSize(+event.target.value);
17
+ tableInstance.gotoPage(0);
18
+ };
19
+
20
+ return (
21
+ <TablePagination
22
+ component={'div'}
23
+ count={tableInstance.rows.length}
24
+ onPageChange={(_, newPage) => tableInstance.gotoPage(newPage)}
25
+ onRowsPerPageChange={handleChangeRowsPerPage}
26
+ page={tableInstance.state.pageIndex}
27
+ rowsPerPage={tableInstance.state.pageSize}
28
+ showFirstButton={
29
+ tableInstance.rows.length / tableInstance.state.pageSize > 2
30
+ }
31
+ showLastButton={
32
+ tableInstance.rows.length / tableInstance.state.pageSize > 2
33
+ }
34
+ {...tablePaginationProps}
35
+ />
36
+ );
37
+ };
@@ -0,0 +1,43 @@
1
+ import React, { FC } from 'react';
2
+ import { styled, Toolbar as MuiToolbar } from '@mui/material';
3
+ import { useMaterialReactTable } from '../useMaterialReactTable';
4
+ import { MRT_TablePagination } from './MRT_TablePagination';
5
+ import { MRT_ToolbarButtons } from './MRT_ToolbarButtons';
6
+
7
+ const Toolbar = styled(MuiToolbar)({
8
+ padding: 0,
9
+ display: 'flex',
10
+ justifyContent: 'space-between',
11
+ });
12
+
13
+ interface Props {}
14
+
15
+ export const MRT_ToolbarBottom: FC<Props> = () => {
16
+ const {
17
+ hideToolbarActions,
18
+ manualPagination,
19
+ muiTableToolbarBottomProps,
20
+ positionPagination,
21
+ positionToolbarActions,
22
+ tableInstance,
23
+ } = useMaterialReactTable();
24
+
25
+ const toolbarProps =
26
+ muiTableToolbarBottomProps instanceof Function
27
+ ? muiTableToolbarBottomProps(tableInstance)
28
+ : muiTableToolbarBottomProps;
29
+
30
+ return (
31
+ <Toolbar variant="dense" {...toolbarProps}>
32
+ {!hideToolbarActions && positionToolbarActions === 'bottom' ? (
33
+ <MRT_ToolbarButtons />
34
+ ) : (
35
+ <span />
36
+ )}
37
+ {!manualPagination &&
38
+ ['bottom', 'both'].includes(positionPagination ?? '') && (
39
+ <MRT_TablePagination />
40
+ )}
41
+ </Toolbar>
42
+ );
43
+ };
@@ -0,0 +1,27 @@
1
+ import React, { FC } from 'react';
2
+ import { styled } from '@mui/material';
3
+ import { MRT_ToggleFiltersButton } from '../buttons/MRT_ToggleFiltersButton';
4
+ import { MRT_ShowHideColumnsButton } from '../buttons/MRT_ShowHideColumnsButton';
5
+ import { useMaterialReactTable } from '../useMaterialReactTable';
6
+ import { MRT_DensePaddingSwitch } from '../inputs/MRT_DensePaddingSwitch';
7
+
8
+ const ToolbarButtonsContainer = styled('div')({
9
+ display: 'flex',
10
+ gap: '0.5rem',
11
+ alignItems: 'center',
12
+ });
13
+
14
+ interface Props {}
15
+
16
+ export const MRT_ToolbarButtons: FC<Props> = () => {
17
+ const { disableFilters, disableColumnHiding, disableDensePaddingToggle } =
18
+ useMaterialReactTable();
19
+
20
+ return (
21
+ <ToolbarButtonsContainer>
22
+ {!disableFilters && <MRT_ToggleFiltersButton />}
23
+ {!disableColumnHiding && <MRT_ShowHideColumnsButton />}
24
+ {!disableDensePaddingToggle && <MRT_DensePaddingSwitch />}
25
+ </ToolbarButtonsContainer>
26
+ );
27
+ };
@@ -0,0 +1,68 @@
1
+ import React, { FC } from 'react';
2
+ import { styled, Toolbar as MuiToolbar, Typography } from '@mui/material';
3
+ import { MRT_SearchTextField } from '../inputs/MRT_SearchTextField';
4
+ import { useMaterialReactTable } from '../useMaterialReactTable';
5
+ import { MRT_ToolbarButtons } from './MRT_ToolbarButtons';
6
+ import { MRT_TablePagination } from './MRT_TablePagination';
7
+
8
+ const Toolbar = styled(MuiToolbar)({
9
+ display: 'grid',
10
+ });
11
+
12
+ const ToolbarTopRow = styled('div')({
13
+ padding: '0.5rem',
14
+ display: 'flex',
15
+ justifyContent: 'space-between',
16
+ });
17
+
18
+ const ToolbarActionsContainer = styled('div')({
19
+ display: 'flex',
20
+ gap: '0.5rem',
21
+ });
22
+
23
+ interface Props {}
24
+
25
+ export const MRT_ToolbarTop: FC<Props> = () => {
26
+ const {
27
+ disableGlobalFilter,
28
+ hideToolbarActions,
29
+ manualPagination,
30
+ muiTableTitleProps,
31
+ muiTableToolbarTopProps,
32
+ positionPagination,
33
+ positionToolbarActions,
34
+ tableInstance,
35
+ title,
36
+ } = useMaterialReactTable();
37
+
38
+ const toolbarProps =
39
+ muiTableToolbarTopProps instanceof Function
40
+ ? muiTableToolbarTopProps(tableInstance)
41
+ : muiTableToolbarTopProps;
42
+
43
+ return (
44
+ <Toolbar variant="dense" {...toolbarProps}>
45
+ <ToolbarTopRow>
46
+ {title ? (
47
+ <Typography variant="h5" {...muiTableTitleProps}>
48
+ {title}
49
+ </Typography>
50
+ ) : (
51
+ <span />
52
+ )}
53
+ <ToolbarActionsContainer>
54
+ {!disableGlobalFilter && <MRT_SearchTextField />}
55
+ {!hideToolbarActions && positionToolbarActions === 'top' && (
56
+ <MRT_ToolbarButtons />
57
+ )}
58
+ </ToolbarActionsContainer>
59
+ </ToolbarTopRow>
60
+ <div>
61
+ {!manualPagination &&
62
+ ['top', 'both'].includes(positionPagination ?? '') && (
63
+ <MRT_TablePagination />
64
+ )}
65
+ </div>
66
+ </Toolbar>
67
+ );
68
+ };
@@ -1,57 +1,95 @@
1
- import React, { createContext, FC, useContext } from 'react';
1
+ import React, {
2
+ Context,
3
+ createContext,
4
+ PropsWithChildren,
5
+ useContext,
6
+ useState,
7
+ } from 'react';
2
8
  import {
9
+ PluginHook,
10
+ Row,
3
11
  TableInstance,
4
12
  useExpanded,
13
+ useFilters,
14
+ useFlexLayout,
15
+ useGlobalFilter,
16
+ useGroupBy,
5
17
  usePagination,
18
+ useResizeColumns,
19
+ useRowSelect,
6
20
  useSortBy,
7
21
  useTable,
8
22
  } from 'react-table';
9
- import {
10
- MaterialReactTableOptionalProps,
11
- MaterialReactTableOptions,
12
- MaterialReactTableProps,
13
- } from '.';
14
- import { defaultOptions } from './defaults';
15
-
16
- interface IUseMaterialReactTable extends MaterialReactTableOptionalProps {
17
- tableInstance: TableInstance<object>;
18
- options: MaterialReactTableOptions;
19
- }
20
-
21
- const MaterialReactTableContext = createContext<IUseMaterialReactTable>(
22
- {} as IUseMaterialReactTable,
23
- );
23
+ import { MaterialReactTableProps } from './MaterialReactTable';
24
+ import { UseMRTCalcs, useMRTCalcs } from './utils/useMRTCalcs';
24
25
 
25
- interface ProviderProps extends MaterialReactTableProps {
26
- children: React.ReactNode;
26
+ export interface UseMaterialReactTable<D extends {}>
27
+ extends MaterialReactTableProps<D>,
28
+ UseMRTCalcs {
29
+ currentEditingRow: Row<D> | null;
30
+ densePadding: boolean;
31
+ setCurrentEditingRow: (currentRowEditingId: Row<D> | null) => void;
32
+ setDensePadding: (densePadding: boolean) => void;
33
+ setShowFilters: (showFilters: boolean) => void;
34
+ showFilters: boolean;
35
+ tableInstance: TableInstance<D>;
27
36
  }
28
37
 
29
- export const MaterialReactTableProvider: FC<ProviderProps> = ({
30
- children,
31
- columns,
32
- data,
33
- options,
34
- ...rest
35
- }) => {
36
- const tableInstance = useTable(
37
- { columns, data },
38
+ const MaterialReactTableContext = (<D extends {}>() =>
39
+ createContext<UseMaterialReactTable<D>>(
40
+ {} as UseMaterialReactTable<D>,
41
+ ) as Context<UseMaterialReactTable<D>>)();
42
+
43
+ export const MaterialReactTableProvider = <D extends {}>(
44
+ props: PropsWithChildren<MaterialReactTableProps<D>>,
45
+ ) => {
46
+ const hooks: PluginHook<D>[] = [
47
+ useResizeColumns,
48
+ useFilters,
49
+ useGlobalFilter,
50
+ useGroupBy,
38
51
  useSortBy,
39
52
  useExpanded,
40
53
  usePagination,
54
+ useRowSelect,
55
+ ];
56
+
57
+ if (props.enableColumnResizing) hooks.unshift(useFlexLayout);
58
+
59
+ const tableInstance = useTable<D>(props, ...hooks);
60
+
61
+ const mrtCalcs = useMRTCalcs({ tableInstance });
62
+
63
+ const [showFilters, setShowFilters] = useState<boolean>(
64
+ props.defaultShowFilters ?? false,
65
+ );
66
+ const [densePadding, setDensePadding] = useState<boolean>(
67
+ props.defaultDensePadding ?? false,
41
68
  );
69
+ const [currentEditingRow, setCurrentEditingRow] = useState<Row | null>(null);
42
70
 
43
71
  return (
44
72
  <MaterialReactTableContext.Provider
45
73
  value={{
46
- options: { ...defaultOptions, ...options },
74
+ ...mrtCalcs,
75
+ ...props,
76
+ densePadding,
77
+ setDensePadding,
78
+ setShowFilters,
79
+ currentEditingRow,
80
+ setCurrentEditingRow,
81
+ showFilters,
82
+ // @ts-ignore
47
83
  tableInstance,
48
- ...rest,
49
84
  }}
50
85
  >
51
- {children}
86
+ {props.children}
52
87
  </MaterialReactTableContext.Provider>
53
88
  );
54
89
  };
55
90
 
56
- export const useMaterialReactTable = (): IUseMaterialReactTable =>
57
- useContext(MaterialReactTableContext);
91
+ export const useMaterialReactTable = <
92
+ D extends {},
93
+ >(): UseMaterialReactTable<D> =>
94
+ //@ts-ignore
95
+ useContext<UseMaterialReactTable<D>>(MaterialReactTableContext);
@@ -0,0 +1,51 @@
1
+ export interface MRT_Localization {
2
+ actionsHeadColumnTitle: string;
3
+ columnActionMenuButtonTitle: string;
4
+ columnActionMenuItemClearSort: string;
5
+ columnActionMenuItemGroupBy: string;
6
+ columnActionMenuItemHideColumn: string;
7
+ columnActionMenuItemSortAsc: string;
8
+ columnActionMenuItemSortDesc: string;
9
+ columnActionMenuItemUnGroupBy: string;
10
+ expandAllButtonTitle: string;
11
+ expandButtonTitle: string;
12
+ filterTextFieldClearButtonTitle: string;
13
+ filterTextFieldPlaceholder: string;
14
+ rowActionButtonCancel: string;
15
+ rowActionButtonSave: string;
16
+ rowActionMenuButtonTitle: string;
17
+ rowActionMenuItemEdit: string;
18
+ rowActionsColumnTitle: string;
19
+ searchTextFieldClearButtonTitle: string;
20
+ searchTextFieldPlaceholder: string;
21
+ selectAllCheckboxTitle: string;
22
+ showHideColumnsButtonTitle: string;
23
+ toggleDensePaddingSwitchTitle: string;
24
+ toggleFilterButtonTitle: string;
25
+ }
26
+
27
+ export const defaultLocalization: MRT_Localization = {
28
+ actionsHeadColumnTitle: 'Actions',
29
+ columnActionMenuButtonTitle: 'Column Actions',
30
+ columnActionMenuItemClearSort: 'Clear sorting',
31
+ columnActionMenuItemGroupBy: 'Group by column',
32
+ columnActionMenuItemHideColumn: 'Hide column',
33
+ columnActionMenuItemSortAsc: 'Sort ascending',
34
+ columnActionMenuItemSortDesc: 'Sort descending',
35
+ columnActionMenuItemUnGroupBy: 'Ungroup column',
36
+ expandAllButtonTitle: 'Expand all',
37
+ expandButtonTitle: 'Expand',
38
+ filterTextFieldClearButtonTitle: 'Clear filter',
39
+ filterTextFieldPlaceholder: 'Filter',
40
+ rowActionButtonCancel: 'Cancel',
41
+ rowActionButtonSave: 'Save',
42
+ rowActionMenuButtonTitle: 'Row Actions',
43
+ rowActionMenuItemEdit: 'Edit',
44
+ rowActionsColumnTitle: 'Actions',
45
+ searchTextFieldClearButtonTitle: 'Clear search',
46
+ searchTextFieldPlaceholder: 'Search',
47
+ selectAllCheckboxTitle: 'Select all',
48
+ showHideColumnsButtonTitle: 'Show/Hide columns',
49
+ toggleDensePaddingSwitchTitle: 'Toggle dense padding',
50
+ toggleFilterButtonTitle: 'Toggle filters',
51
+ };
@@ -0,0 +1,42 @@
1
+ import { useMemo } from 'react';
2
+ import { TableInstance } from 'react-table';
3
+
4
+ export interface UseMRTCalcs {
5
+ anyRowsCanExpand: boolean;
6
+ anyRowsExpanded: boolean;
7
+ maxColumnDepth: number;
8
+ }
9
+
10
+ interface Props<D extends {}> {
11
+ tableInstance: TableInstance<D>;
12
+ }
13
+
14
+ export const useMRTCalcs = <D extends {}>({
15
+ tableInstance,
16
+ }: Props<D>): UseMRTCalcs => {
17
+ const anyRowsCanExpand = useMemo(
18
+ () => tableInstance.rows.some((row) => row.canExpand),
19
+ [tableInstance.rows],
20
+ );
21
+
22
+ const anyRowsExpanded = useMemo(
23
+ () => tableInstance.rows.some((row) => row.isExpanded),
24
+ [tableInstance.rows],
25
+ );
26
+
27
+ const maxColumnDepth = useMemo(() => {
28
+ let maxDepth = 1;
29
+ tableInstance.columns.forEach((column) => {
30
+ if (column.columns?.length) {
31
+ maxDepth = Math.max(maxDepth, column.columns.length);
32
+ }
33
+ });
34
+ return maxDepth - 1;
35
+ }, [tableInstance.columns]);
36
+
37
+ return {
38
+ anyRowsCanExpand,
39
+ anyRowsExpanded,
40
+ maxColumnDepth,
41
+ };
42
+ };
@@ -1,2 +0,0 @@
1
- import { MaterialReactTableOptions } from '.';
2
- export declare const defaultOptions: MaterialReactTableOptions;
package/src/MRT_Table.tsx DELETED
@@ -1,20 +0,0 @@
1
- import React, { FC } from 'react';
2
- import { Table } from '@mui/material';
3
- import { MRT_TableHead } from './MRT_TableHead';
4
- import { MRT_TableBody } from './MRT_TableBody';
5
- import { MRT_TableFooter } from './MRT_TableFooter';
6
- import { useMaterialReactTable } from './useMaterialReactTable';
7
-
8
- interface Props {}
9
-
10
- export const MRT_Table: FC<Props> = () => {
11
- const { tableInstance, tableProps, options } = useMaterialReactTable();
12
-
13
- return (
14
- <Table stickyHeader {...tableProps} {...tableInstance.getTableProps()}>
15
- {options.showHead && <MRT_TableHead />}
16
- <MRT_TableBody />
17
- {options.showFooter && <MRT_TableFooter />}
18
- </Table>
19
- );
20
- };
@@ -1,19 +0,0 @@
1
- import React, { FC } from 'react';
2
- import { TableBody } from '@mui/material';
3
- import { MRT_TableBodyRow } from './MRT_TableBodyRow';
4
- import { useMaterialReactTable } from './useMaterialReactTable';
5
-
6
- interface Props {}
7
-
8
- export const MRT_TableBody: FC<Props> = () => {
9
- const { tableInstance } = useMaterialReactTable();
10
-
11
- return (
12
- <TableBody {...tableInstance.getTableBodyProps()}>
13
- {tableInstance.page.map((row, index) => {
14
- tableInstance.prepareRow(row);
15
- return <MRT_TableBodyRow key={`${index}-${row.id}`} row={row} />;
16
- })}
17
- </TableBody>
18
- );
19
- };
@@ -1,15 +0,0 @@
1
- import React, { FC } from 'react';
2
- import { TableCell } from '@mui/material';
3
- import { Cell } from 'react-table';
4
-
5
- interface Props {
6
- cell: Cell<object>;
7
- }
8
-
9
- export const MRT_TableBodyCell: FC<Props> = ({ cell }) => {
10
- return (
11
- <TableCell variant="body" {...cell.getCellProps()}>
12
- {cell.render('Cell')}
13
- </TableCell>
14
- );
15
- };
@@ -1,27 +0,0 @@
1
- import React, { FC } from 'react';
2
- import { TableRow } from '@mui/material';
3
- import { Row } from 'react-table';
4
- import { MRT_TableBodyCell } from './MRT_TableBodyCell';
5
- import { useMaterialReactTable } from './useMaterialReactTable';
6
- import { MRT_TableDetailPanel } from './MRT_TableDetailPanel';
7
- import { MRT_TableExpandButton } from './MRT_TableExpandButton';
8
-
9
- interface Props {
10
- row: Row<object>;
11
- }
12
-
13
- export const MRT_TableBodyRow: FC<Props> = ({ row }) => {
14
- const { renderDetailPanel } = useMaterialReactTable();
15
-
16
- return (
17
- <>
18
- <TableRow {...row.getRowProps()}>
19
- {renderDetailPanel && <MRT_TableExpandButton row={row} />}
20
- {row.cells.map((cell, index) => (
21
- <MRT_TableBodyCell key={`${index}-${cell.value}`} cell={cell} />
22
- ))}
23
- </TableRow>
24
- {renderDetailPanel && <MRT_TableDetailPanel row={row} />}
25
- </>
26
- );
27
- };
@@ -1,16 +0,0 @@
1
- import React, { FC } from 'react';
2
- import { Paper, TableContainer } from '@mui/material';
3
- import { useMaterialReactTable } from './useMaterialReactTable';
4
- import { MRT_Table } from './MRT_Table';
5
-
6
- interface Props {}
7
-
8
- export const MRT_TableContainer: FC<Props> = () => {
9
- const { tableContainerProps } = useMaterialReactTable();
10
-
11
- return (
12
- <TableContainer component={Paper} {...tableContainerProps}>
13
- <MRT_Table />
14
- </TableContainer>
15
- );
16
- };
@@ -1,27 +0,0 @@
1
- import React, { FC } from 'react';
2
- import { Collapse, TableCell, TableRow } from '@mui/material';
3
- import { Row } from 'react-table';
4
- import { useMaterialReactTable } from './useMaterialReactTable';
5
-
6
- interface Props {
7
- row: Row<object>;
8
- }
9
-
10
- export const MRT_TableDetailPanel: FC<Props> = ({ row }) => {
11
- const { tableInstance, renderDetailPanel } = useMaterialReactTable();
12
-
13
- return (
14
- <TableRow {...row.getToggleRowExpandedProps()}>
15
- <TableCell
16
- colSpan={tableInstance.visibleColumns.length + 10}
17
- style={{
18
- paddingBottom: row.isExpanded ? '1rem' : 0,
19
- paddingTop: row.isExpanded ? '1rem' : 0,
20
- transition: 'padding 0.2s',
21
- }}
22
- >
23
- <Collapse in={row.isExpanded}>{renderDetailPanel?.(row)}</Collapse>
24
- </TableCell>
25
- </TableRow>
26
- );
27
- };