material-react-table 2.0.0-alpha.3 → 2.0.0-alpha.5

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 (47) hide show
  1. package/dist/cjs/index.js +489 -435
  2. package/dist/cjs/index.js.map +1 -1
  3. package/dist/cjs/types/icons.d.ts +4 -0
  4. package/dist/cjs/types/locales/hy.d.ts +2 -0
  5. package/dist/cjs/types/toolbar/MRT_TablePagination.d.ts +2 -2
  6. package/dist/cjs/types/toolbar/MRT_ToolbarAlertBanner.d.ts +1 -1
  7. package/dist/cjs/types/types.d.ts +18 -7
  8. package/dist/esm/material-react-table.esm.js +457 -410
  9. package/dist/esm/material-react-table.esm.js.map +1 -1
  10. package/dist/esm/types/icons.d.ts +4 -0
  11. package/dist/esm/types/locales/hy.d.ts +2 -0
  12. package/dist/esm/types/toolbar/MRT_TablePagination.d.ts +2 -2
  13. package/dist/esm/types/toolbar/MRT_ToolbarAlertBanner.d.ts +1 -1
  14. package/dist/esm/types/types.d.ts +18 -7
  15. package/dist/index.d.ts +25 -10
  16. package/locales/hy.d.ts +2 -0
  17. package/locales/hy.esm.d.ts +2 -0
  18. package/locales/hy.esm.js +93 -0
  19. package/locales/hy.esm.js.map +1 -0
  20. package/locales/hy.js +97 -0
  21. package/locales/hy.js.map +1 -0
  22. package/package.json +7 -7
  23. package/src/body/MRT_TableBody.tsx +13 -21
  24. package/src/body/MRT_TableBodyCell.tsx +2 -2
  25. package/src/body/MRT_TableBodyRow.tsx +3 -3
  26. package/src/body/MRT_TableDetailPanel.tsx +3 -3
  27. package/src/buttons/MRT_GrabHandleButton.tsx +1 -2
  28. package/src/buttons/MRT_ShowHideColumnsButton.tsx +1 -2
  29. package/src/buttons/MRT_ToggleDensePaddingButton.tsx +1 -2
  30. package/src/buttons/MRT_ToggleFiltersButton.tsx +1 -2
  31. package/src/buttons/MRT_ToggleFullScreenButton.tsx +2 -3
  32. package/src/column.utils.ts +15 -9
  33. package/src/footer/MRT_TableFooter.tsx +1 -1
  34. package/src/footer/MRT_TableFooterCell.tsx +1 -1
  35. package/src/footer/MRT_TableFooterRow.tsx +2 -2
  36. package/src/head/MRT_TableHead.tsx +36 -14
  37. package/src/head/MRT_TableHeadCell.tsx +1 -1
  38. package/src/head/MRT_TableHeadRow.tsx +2 -2
  39. package/src/hooks/useMRT_Effects.ts +13 -2
  40. package/src/hooks/useMRT_TableOptions.ts +7 -2
  41. package/src/icons.ts +12 -0
  42. package/src/locales/hy.ts +93 -0
  43. package/src/table/MRT_Table.tsx +4 -2
  44. package/src/toolbar/MRT_TablePagination.tsx +135 -68
  45. package/src/toolbar/MRT_ToolbarAlertBanner.tsx +53 -15
  46. package/src/toolbar/MRT_TopToolbar.tsx +1 -1
  47. package/src/types.ts +22 -9
@@ -1,14 +1,22 @@
1
- import { type ChangeEvent } from 'react';
2
- import TablePagination from '@mui/material/TablePagination';
1
+ import Box from '@mui/material/Box';
2
+ import IconButton from '@mui/material/IconButton';
3
+ import InputLabel from '@mui/material/InputLabel';
4
+ import MenuItem from '@mui/material/MenuItem';
5
+ import Pagination, { type PaginationProps } from '@mui/material/Pagination';
6
+ import PaginationItem from '@mui/material/PaginationItem';
7
+ import Select from '@mui/material/Select';
8
+ import Typography from '@mui/material/Typography';
3
9
  import { parseFromValuesOrFunc } from '../column.utils';
4
10
  import { type MRT_TableInstance } from '../types';
5
11
 
6
- interface Props<TData extends Record<string, any>> {
12
+ const defaultRowsPerPage = [5, 10, 15, 20, 25, 30, 50, 100];
13
+
14
+ interface Props<TData extends Record<string, any> = {}> {
7
15
  position?: 'bottom' | 'top';
8
16
  table: MRT_TableInstance<TData>;
9
17
  }
10
18
 
11
- export const MRT_TablePagination = <TData extends Record<string, any>>({
19
+ export const MRT_TablePagination = <TData extends Record<string, any> = {}>({
12
20
  position = 'bottom',
13
21
  table,
14
22
  }: Props<TData>) => {
@@ -17,8 +25,10 @@ export const MRT_TablePagination = <TData extends Record<string, any>>({
17
25
  getState,
18
26
  options: {
19
27
  enableToolbarInternalActions,
28
+ icons: { ChevronLeftIcon, ChevronRightIcon, FirstPageIcon, LastPageIcon },
20
29
  localization,
21
- muiTablePaginationProps,
30
+ muiPaginationProps,
31
+ paginationDisplayMode,
22
32
  rowCount,
23
33
  },
24
34
  setPageIndex,
@@ -29,80 +39,137 @@ export const MRT_TablePagination = <TData extends Record<string, any>>({
29
39
  showGlobalFilter,
30
40
  } = getState();
31
41
 
32
- const totalRowCount = rowCount ?? getPrePaginationRowModel().rows.length;
33
- const showFirstLastPageButtons = totalRowCount / pageSize > 2;
34
-
35
- const tablePaginationProps = parseFromValuesOrFunc(muiTablePaginationProps, {
42
+ const paginationProps = parseFromValuesOrFunc(muiPaginationProps, {
36
43
  table,
37
44
  });
38
45
 
39
- const handleChangeRowsPerPage = (event: ChangeEvent<HTMLInputElement>) => {
40
- setPageSize(+event.target.value);
41
- };
46
+ const totalRowCount = rowCount ?? getPrePaginationRowModel().rows.length;
47
+ const numberOfPages = Math.ceil(totalRowCount / pageSize);
48
+ const showFirstLastPageButtons = numberOfPages > 2;
49
+ const firstRowIndex = pageIndex * pageSize;
50
+ const lastRowIndex = Math.min(pageIndex * pageSize + pageSize, totalRowCount);
51
+
52
+ const {
53
+ showRowsPerPage = true,
54
+ rowsPerPageOptions = defaultRowsPerPage,
55
+ showFirstButton = showFirstLastPageButtons,
56
+ showLastButton = showFirstLastPageButtons,
57
+
58
+ ...rest
59
+ } = paginationProps ?? {};
42
60
 
43
61
  return (
44
- <TablePagination
45
- component="div"
46
- count={totalRowCount}
47
- getItemAriaLabel={(type) =>
48
- type === 'first'
49
- ? localization.goToFirstPage
50
- : type === 'last'
51
- ? localization.goToLastPage
52
- : type === 'next'
53
- ? localization.goToNextPage
54
- : localization.goToPreviousPage
55
- }
56
- labelDisplayedRows={({ count, from, to }) =>
57
- `${from}-${to} ${localization.of} ${count}`
58
- }
59
- labelRowsPerPage={localization.rowsPerPage}
60
- onPageChange={(_: any, newPage: number) => setPageIndex(newPage)}
61
- onRowsPerPageChange={handleChangeRowsPerPage}
62
- page={Math.max(
63
- Math.min(pageIndex, Math.ceil(totalRowCount / pageSize) - 1),
64
- 0,
65
- )}
66
- rowsPerPage={pageSize}
67
- rowsPerPageOptions={[5, 10, 15, 20, 25, 30, 50, 100]}
68
- showFirstButton={showFirstLastPageButtons}
69
- showLastButton={showFirstLastPageButtons}
70
- {...tablePaginationProps}
71
- SelectProps={{
72
- MenuProps: { MenuListProps: { disablePadding: true }, sx: { m: 0 } },
73
- sx: { m: '0 1rem 0 1ch' },
74
- ...tablePaginationProps?.SelectProps,
75
- }}
76
- sx={(theme) => ({
77
- '& . MuiTablePagination-select': {
78
- m: '0 1px',
79
- },
80
- '& .MuiTablePagination-actions': {
81
- m: '0 1px',
82
- },
83
- '& .MuiTablePagination-displayedRows': {
84
- m: '0 1px',
85
- },
86
- '& .MuiTablePagination-selectLabel': {
87
- m: '0 -1px',
88
- },
89
- '& .MuiTablePagination-toolbar': {
90
- alignItems: 'center',
91
- display: 'flex',
92
- },
93
- '&. MuiInputBase-root': {
94
- m: '0 1px',
95
- },
62
+ <Box
63
+ sx={{
64
+ alignItems: 'center',
65
+ display: 'flex',
66
+ gap: '8px',
67
+ justifyContent: 'space-between',
68
+ justifySelf: 'flex-end',
96
69
  mt:
97
70
  position === 'top' &&
98
71
  enableToolbarInternalActions &&
99
72
  !showGlobalFilter
100
- ? '3.5rem'
73
+ ? '3rem'
101
74
  : undefined,
102
75
  position: 'relative',
76
+ px: '4px',
77
+ py: '12px',
103
78
  zIndex: 2,
104
- ...(parseFromValuesOrFunc(tablePaginationProps?.sx, theme) as any),
105
- })}
106
- />
79
+ }}
80
+ >
81
+ {showRowsPerPage && (
82
+ <Box sx={{ alignItems: 'center', display: 'flex', gap: '8px' }}>
83
+ <InputLabel htmlFor="mrt-rows-per-page" sx={{ mb: 0 }}>
84
+ {localization.rowsPerPage}
85
+ </InputLabel>
86
+ <Select
87
+ inputProps={{ 'aria-label': localization.rowsPerPage }}
88
+ id="mrt-rows-per-page"
89
+ label={localization.rowsPerPage}
90
+ onChange={(event) => setPageSize(+event.target.value)}
91
+ sx={{ '&::before': { border: 'none' }, mb: 0 }}
92
+ value={pageSize}
93
+ variant="standard"
94
+ >
95
+ {rowsPerPageOptions.map((value) => (
96
+ <MenuItem key={value} sx={{ m: 0 }} value={value}>
97
+ {value}
98
+ </MenuItem>
99
+ ))}
100
+ </Select>
101
+ </Box>
102
+ )}
103
+ {paginationDisplayMode === 'pages' ? (
104
+ <Pagination
105
+ count={numberOfPages}
106
+ onChange={(_e, newPageIndex) => setPageIndex(newPageIndex - 1)}
107
+ page={pageIndex + 1}
108
+ renderItem={(item) => (
109
+ <PaginationItem
110
+ slots={{
111
+ first: FirstPageIcon,
112
+ last: LastPageIcon,
113
+ next: ChevronRightIcon,
114
+ previous: ChevronLeftIcon,
115
+ }}
116
+ {...item}
117
+ />
118
+ )}
119
+ showFirstButton={showFirstButton}
120
+ showLastButton={showLastButton}
121
+ {...(rest as PaginationProps)}
122
+ />
123
+ ) : paginationDisplayMode === 'default' ? (
124
+ <>
125
+ <Typography
126
+ sx={{ mb: 0, mx: '4px', minWidth: '10ch' }}
127
+ variant="body2"
128
+ >{`${
129
+ lastRowIndex === 0 ? 0 : (firstRowIndex + 1).toLocaleString()
130
+ }-${lastRowIndex.toLocaleString()} ${
131
+ localization.of
132
+ } ${totalRowCount.toLocaleString()}`}</Typography>
133
+ <Box gap="xs">
134
+ {showFirstButton && (
135
+ <IconButton
136
+ aria-label={localization.goToFirstPage}
137
+ disabled={pageIndex <= 0}
138
+ onClick={() => setPageIndex(0)}
139
+ size="small"
140
+ >
141
+ <FirstPageIcon />
142
+ </IconButton>
143
+ )}
144
+ <IconButton
145
+ aria-label={localization.goToPreviousPage}
146
+ disabled={pageIndex <= 0}
147
+ onClick={() => setPageIndex(pageIndex - 1)}
148
+ size="small"
149
+ >
150
+ <ChevronLeftIcon />
151
+ </IconButton>
152
+ <IconButton
153
+ aria-label={localization.goToNextPage}
154
+ disabled={lastRowIndex >= totalRowCount}
155
+ onClick={() => setPageIndex(pageIndex + 1)}
156
+ size="small"
157
+ >
158
+ <ChevronRightIcon />
159
+ </IconButton>
160
+ {showLastButton && (
161
+ <IconButton
162
+ aria-label={localization.goToLastPage}
163
+ disabled={lastRowIndex >= totalRowCount}
164
+ onClick={() => setPageIndex(numberOfPages - 1)}
165
+ size="small"
166
+ >
167
+ <LastPageIcon />
168
+ </IconButton>
169
+ )}
170
+ </Box>
171
+ </>
172
+ ) : null}
173
+ </Box>
107
174
  );
108
175
  };
@@ -4,11 +4,13 @@ import AlertTitle from '@mui/material/AlertTitle';
4
4
  import Box from '@mui/material/Box';
5
5
  import Chip from '@mui/material/Chip';
6
6
  import Collapse from '@mui/material/Collapse';
7
+ import Stack from '@mui/material/Stack';
7
8
  import { parseFromValuesOrFunc } from '../column.utils';
9
+ import { MRT_SelectCheckbox } from '../inputs';
8
10
  import { type MRT_TableInstance } from '../types';
9
11
 
10
12
  interface Props<TData extends Record<string, any>> {
11
- stackAlertBanner: boolean;
13
+ stackAlertBanner?: boolean;
12
14
  table: MRT_TableInstance<TData>;
13
15
  }
14
16
 
@@ -21,14 +23,18 @@ export const MRT_ToolbarAlertBanner = <TData extends Record<string, any>>({
21
23
  getSelectedRowModel,
22
24
  getState,
23
25
  options: {
26
+ enableRowSelection,
27
+ enableSelectAll,
24
28
  localization,
25
29
  muiToolbarAlertBannerChipProps,
26
30
  muiToolbarAlertBannerProps,
27
31
  positionToolbarAlertBanner,
32
+ renderToolbarAlertBannerContent,
28
33
  rowCount,
29
34
  },
35
+ refs: { tablePaperRef },
30
36
  } = table;
31
- const { grouping, showAlertBanner } = getState();
37
+ const { density, grouping, showAlertBanner } = getState();
32
38
 
33
39
  const alertProps = parseFromValuesOrFunc(muiToolbarAlertBannerProps, {
34
40
  table,
@@ -38,7 +44,7 @@ export const MRT_ToolbarAlertBanner = <TData extends Record<string, any>>({
38
44
  table,
39
45
  });
40
46
 
41
- const selectMessage =
47
+ const selectedAlert =
42
48
  getSelectedRowModel().rows.length > 0
43
49
  ? localization.selectedCountOfRowCountRowsSelected
44
50
  ?.replace(
@@ -51,7 +57,7 @@ export const MRT_ToolbarAlertBanner = <TData extends Record<string, any>>({
51
57
  )
52
58
  : null;
53
59
 
54
- const groupedByMessage =
60
+ const groupedAlert =
55
61
  grouping.length > 0 ? (
56
62
  <span>
57
63
  {localization.groupedBy}{' '}
@@ -70,7 +76,7 @@ export const MRT_ToolbarAlertBanner = <TData extends Record<string, any>>({
70
76
 
71
77
  return (
72
78
  <Collapse
73
- in={showAlertBanner || !!selectMessage || !!groupedByMessage}
79
+ in={showAlertBanner || !!selectedAlert || !!groupedAlert}
74
80
  timeout={stackAlertBanner ? 200 : 0}
75
81
  >
76
82
  <Alert
@@ -78,6 +84,12 @@ export const MRT_ToolbarAlertBanner = <TData extends Record<string, any>>({
78
84
  icon={false}
79
85
  {...alertProps}
80
86
  sx={(theme) => ({
87
+ '& .MuiAlert-message': {
88
+ maxWidth: `calc(${
89
+ tablePaperRef.current?.clientWidth ?? 360
90
+ }px - 1rem)`,
91
+ width: '100%',
92
+ },
81
93
  borderRadius: 0,
82
94
  fontSize: '1rem',
83
95
  left: 0,
@@ -95,16 +107,42 @@ export const MRT_ToolbarAlertBanner = <TData extends Record<string, any>>({
95
107
  ...(parseFromValuesOrFunc(alertProps?.sx, theme) as any),
96
108
  })}
97
109
  >
98
- {alertProps?.title && <AlertTitle>{alertProps.title}</AlertTitle>}
99
- <Box sx={{ p: '0.5rem 1rem' }}>
100
- {alertProps?.children}
101
- {alertProps?.children && (selectMessage || groupedByMessage) && (
102
- <br />
103
- )}
104
- {selectMessage}
105
- {selectMessage && groupedByMessage && <br />}
106
- {groupedByMessage}
107
- </Box>
110
+ {renderToolbarAlertBannerContent?.({
111
+ groupedAlert,
112
+ selectedAlert,
113
+ table,
114
+ }) ?? (
115
+ <>
116
+ {alertProps?.title && <AlertTitle>{alertProps.title}</AlertTitle>}
117
+ <Stack
118
+ sx={{
119
+ p:
120
+ positionToolbarAlertBanner !== 'head-overlay'
121
+ ? '0.5rem 1rem'
122
+ : density === 'spacious'
123
+ ? '0.75rem 1.25rem'
124
+ : density === 'comfortable'
125
+ ? '0.5rem 0.75rem'
126
+ : '0.25rem 0.5rem',
127
+ }}
128
+ >
129
+ {alertProps?.children}
130
+ {alertProps?.children && (selectedAlert || groupedAlert) && (
131
+ <br />
132
+ )}
133
+ <Box sx={{ display: 'flex' }}>
134
+ {enableRowSelection &&
135
+ enableSelectAll &&
136
+ positionToolbarAlertBanner === 'head-overlay' && (
137
+ <MRT_SelectCheckbox selectAll table={table} />
138
+ )}{' '}
139
+ {selectedAlert}
140
+ </Box>
141
+ {selectedAlert && groupedAlert && <br />}
142
+ {groupedAlert}
143
+ </Stack>
144
+ </>
145
+ )}
108
146
  </Alert>
109
147
  </Collapse>
110
148
  );
@@ -14,7 +14,7 @@ import { type MRT_TableInstance } from '../types';
14
14
 
15
15
  export const commonToolbarStyles = ({ theme }: { theme: Theme }) => ({
16
16
  alignItems: 'flex-start',
17
- backgroundColor: lighten(theme.palette.background.default, 0.04),
17
+ backgroundColor: lighten(theme.palette.background.default, 0.05),
18
18
  backgroundImage: 'none',
19
19
  display: 'grid',
20
20
  flexWrap: 'wrap-reverse',
package/src/types.ts CHANGED
@@ -38,14 +38,15 @@ import {
38
38
  type Virtualizer,
39
39
  type VirtualizerOptions,
40
40
  } from '@tanstack/react-virtual';
41
- import { type AutocompleteProps } from '@mui/material';
42
41
  import { type AlertProps } from '@mui/material/Alert';
42
+ import { type AutocompleteProps } from '@mui/material/Autocomplete';
43
43
  import { type ButtonProps } from '@mui/material/Button';
44
44
  import { type CheckboxProps } from '@mui/material/Checkbox';
45
45
  import { type ChipProps } from '@mui/material/Chip';
46
46
  import { type DialogProps } from '@mui/material/Dialog';
47
47
  import { type IconButtonProps } from '@mui/material/IconButton';
48
48
  import { type LinearProgressProps } from '@mui/material/LinearProgress';
49
+ import { type PaginationProps } from '@mui/material/Pagination';
49
50
  import { type PaperProps } from '@mui/material/Paper';
50
51
  import { type RadioProps } from '@mui/material/Radio';
51
52
  import { type SkeletonProps } from '@mui/material/Skeleton';
@@ -56,7 +57,6 @@ import { type TableCellProps } from '@mui/material/TableCell';
56
57
  import { type TableContainerProps } from '@mui/material/TableContainer';
57
58
  import { type TableFooterProps } from '@mui/material/TableFooter';
58
59
  import { type TableHeadProps } from '@mui/material/TableHead';
59
- import { type TablePaginationProps } from '@mui/material/TablePagination';
60
60
  import { type TableRowProps } from '@mui/material/TableRow';
61
61
  import { type TextFieldProps } from '@mui/material/TextField';
62
62
  import { type ToolbarProps } from '@mui/material/Toolbar';
@@ -727,7 +727,7 @@ export type MRT_TableOptions<TData extends Record<string, any>> = Omit<
727
727
  /**
728
728
  * Changes which kind of CSS layout is used to render the table. `semantic` uses default semantic HTML elements, while `grid` adds CSS grid and flexbox styles
729
729
  */
730
- layoutMode?: 'grid' | 'semantic';
730
+ layoutMode?: 'grid' | 'grid-no-grow' | 'semantic';
731
731
  /**
732
732
  * Pass in either a locale imported from `material-react-table/locales/*` or a custom locale object.
733
733
  *
@@ -838,6 +838,19 @@ export type MRT_TableOptions<TData extends Record<string, any>> = Omit<
838
838
  table: MRT_TableInstance<TData>;
839
839
  }) => LinearProgressProps)
840
840
  | LinearProgressProps;
841
+ muiPaginationProps?:
842
+ | ((props: { table: MRT_TableInstance<TData> }) => Partial<
843
+ PaginationProps & {
844
+ rowsPerPageOptions?: number[];
845
+ showRowsPerPage?: boolean;
846
+ }
847
+ >)
848
+ | Partial<
849
+ PaginationProps & {
850
+ rowsPerPageOptions?: number[];
851
+ showRowsPerPage?: boolean;
852
+ }
853
+ >;
841
854
  muiRowDragHandleProps?:
842
855
  | ((props: {
843
856
  row: MRT_Row<TData>;
@@ -916,11 +929,6 @@ export type MRT_TableOptions<TData extends Record<string, any>> = Omit<
916
929
  table: MRT_TableInstance<TData>;
917
930
  }) => TableRowProps)
918
931
  | TableRowProps;
919
- muiTablePaginationProps?:
920
- | ((props: {
921
- table: MRT_TableInstance<TData>;
922
- }) => Partial<Omit<TablePaginationProps, 'rowsPerPage'>>)
923
- | Partial<Omit<TablePaginationProps, 'rowsPerPage'>>;
924
932
  muiTablePaperProps?:
925
933
  | ((props: { table: MRT_TableInstance<TData> }) => PaperProps)
926
934
  | PaperProps;
@@ -976,7 +984,7 @@ export type MRT_TableOptions<TData extends Record<string, any>> = Omit<
976
984
  positionExpandColumn?: 'first' | 'last';
977
985
  positionGlobalFilter?: 'left' | 'none' | 'right';
978
986
  positionPagination?: 'both' | 'bottom' | 'none' | 'top';
979
- positionToolbarAlertBanner?: 'bottom' | 'none' | 'top';
987
+ positionToolbarAlertBanner?: 'bottom' | 'head-overlay' | 'none' | 'top';
980
988
  positionToolbarDropZone?: 'both' | 'bottom' | 'none' | 'top';
981
989
  renderBottomToolbar?:
982
990
  | ((props: { table: MRT_TableInstance<TData> }) => ReactNode)
@@ -1028,6 +1036,11 @@ export type MRT_TableOptions<TData extends Record<string, any>> = Omit<
1028
1036
  row: MRT_Row<TData>;
1029
1037
  table: MRT_TableInstance<TData>;
1030
1038
  }) => ReactNode;
1039
+ renderToolbarAlertBannerContent?: (props: {
1040
+ groupedAlert: ReactNode | null;
1041
+ selectedAlert: ReactNode | null;
1042
+ table: MRT_TableInstance<TData>;
1043
+ }) => ReactNode;
1031
1044
  renderToolbarInternalActions?: (props: {
1032
1045
  table: MRT_TableInstance<TData>;
1033
1046
  }) => ReactNode;