material-react-table 0.37.2 → 0.38.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.
package/README.md CHANGED
@@ -43,6 +43,7 @@ See all [Props and Options](https://www.material-react-table.com/docs/api)
43
43
  - [Basic Table](https://www.material-react-table.com/docs/examples/basic/) (See Default Features)
44
44
  - [Minimal Table](https://www.material-react-table.com/docs/examples/minimal/) (Turn off Features)
45
45
  - [Advanced Table](https://www.material-react-table.com/docs/examples/advanced/) (See some of the Advanced Features)
46
+ - [Aggregation/Grouping](https://www.material-react-table.com/docs/examples/aggregation-and-grouping/) (Aggregation features such as Sum, Average, Count, etc.)
46
47
  - [Data Export Table](https://www.material-react-table.com/docs/examples/data-export/) (Export to CSV, Excel, etc.)
47
48
  - [Editing CRUD Table](https://www.material-react-table.com/docs/examples/editing-crud/) (Create, Edit, and Delete Rows)
48
49
  - [Remote Data](https://www.material-react-table.com/docs/examples/remote/) (Server-side Pagination, Sorting, and Filtering)
@@ -60,9 +61,9 @@ _All features can easily be enabled/disabled_
60
61
 
61
62
  - [x] < 37kb gzipped - [Bundlephobia](https://bundlephobia.com/package/material-react-table)
62
63
  - [x] Advanced TypeScript Generics Support (TypeScript Optional)
64
+ - [x] Aggregation and Grouping (Sum, Average, Count, etc.)
63
65
  - [x] Click To Copy Cell Values
64
66
  - [x] Column Action Dropdown Menu
65
- - [x] Column/Row Grouping (Group By and Aggregates)
66
67
  - [x] Column Hiding
67
68
  - [x] Column Ordering via Drag'n'Drop
68
69
  - [x] Column Pinning (Freeze Columns)
@@ -1,5 +1,5 @@
1
1
  import { Dispatch, DragEvent, MutableRefObject, ReactNode, SetStateAction } from 'react';
2
- import type { AlertProps, ButtonProps, CheckboxProps, IconButtonProps, LinearProgressProps, PaperProps, SkeletonProps, TableBodyProps, TableCellProps, TableContainerProps, TableFooterProps, TableHeadProps, TablePaginationProps, TableProps, TableRowProps, TextFieldProps, ToolbarProps } from '@mui/material';
2
+ import type { AlertProps, ButtonProps, CheckboxProps, ChipProps, IconButtonProps, LinearProgressProps, PaperProps, SkeletonProps, TableBodyProps, TableCellProps, TableContainerProps, TableFooterProps, TableHeadProps, TablePaginationProps, TableProps, TableRowProps, TextFieldProps, ToolbarProps } from '@mui/material';
3
3
  import type { Cell, Column, ColumnDef, DeepKeys, FilterFn, Header, HeaderGroup, OnChangeFn, Row, SortingFn, Table, TableOptions, TableState } from '@tanstack/react-table';
4
4
  import type { Options as VirtualizerOptions } from 'react-virtual';
5
5
  import { MRT_Icons } from './icons';
@@ -48,8 +48,8 @@ export declare type MRT_TableInstance<TData extends Record<string, any> = {}> =
48
48
  setDensity: Dispatch<SetStateAction<'comfortable' | 'compact' | 'spacious'>>;
49
49
  setDraggingColumn: Dispatch<SetStateAction<MRT_Column<TData> | null>>;
50
50
  setDraggingRow: Dispatch<SetStateAction<MRT_Row<TData> | null>>;
51
- setEditingCell: Dispatch<SetStateAction<MRT_Cell | null>>;
52
- setEditingRow: Dispatch<SetStateAction<MRT_Row | null>>;
51
+ setEditingCell: Dispatch<SetStateAction<MRT_Cell<TData> | null>>;
52
+ setEditingRow: Dispatch<SetStateAction<MRT_Row<TData> | null>>;
53
53
  setGlobalFilterFn: Dispatch<SetStateAction<MRT_FilterOption>>;
54
54
  setHoveredColumn: Dispatch<SetStateAction<MRT_Column<TData> | {
55
55
  id: string;
@@ -113,6 +113,12 @@ export declare type MRT_ColumnDef<TData extends Record<string, any> = {}> = Omit
113
113
  footer: MRT_Header<TData>;
114
114
  table: MRT_TableInstance<TData>;
115
115
  }) => ReactNode);
116
+ GroupedCell?: ({ cell, column, row, table, }: {
117
+ cell: MRT_Cell<TData>;
118
+ column: MRT_Column<TData>;
119
+ row: MRT_Row<TData>;
120
+ table: MRT_TableInstance<TData>;
121
+ }) => ReactNode;
116
122
  Header?: ReactNode | (({ column, header, table, }: {
117
123
  column: MRT_Column<TData>;
118
124
  header: MRT_Header<TData>;
@@ -292,6 +298,9 @@ export declare type MaterialReactTableProps<TData extends Record<string, any> =
292
298
  icons?: Partial<MRT_Icons>;
293
299
  initialState?: Partial<MRT_TableState<TData>>;
294
300
  localization?: Partial<MRT_Localization>;
301
+ muiBottomToolbarProps?: ToolbarProps | (({ table }: {
302
+ table: MRT_TableInstance<TData>;
303
+ }) => ToolbarProps);
295
304
  muiExpandAllButtonProps?: IconButtonProps | (({ table }: {
296
305
  table: MRT_TableInstance<TData>;
297
306
  }) => IconButtonProps);
@@ -346,9 +355,6 @@ export declare type MaterialReactTableProps<TData extends Record<string, any> =
346
355
  table: MRT_TableInstance<TData>;
347
356
  row: MRT_Row<TData>;
348
357
  }) => TableRowProps);
349
- muiTableBottomToolbarProps?: ToolbarProps | (({ table }: {
350
- table: MRT_TableInstance<TData>;
351
- }) => ToolbarProps);
352
358
  muiTableContainerProps?: TableContainerProps | (({ table, }: {
353
359
  table: MRT_TableInstance<TData>;
354
360
  }) => TableContainerProps);
@@ -400,10 +406,13 @@ export declare type MaterialReactTableProps<TData extends Record<string, any> =
400
406
  muiTableProps?: TableProps | (({ table }: {
401
407
  table: MRT_TableInstance<TData>;
402
408
  }) => TableProps);
403
- muiTableToolbarAlertBannerProps?: AlertProps | (({ table }: {
409
+ muiToolbarAlertBannerChipProps?: ChipProps | (({ table }: {
410
+ table: MRT_TableInstance<TData>;
411
+ }) => ChipProps);
412
+ muiToolbarAlertBannerProps?: AlertProps | (({ table }: {
404
413
  table: MRT_TableInstance<TData>;
405
414
  }) => AlertProps);
406
- muiTableTopToolbarProps?: ToolbarProps | (({ table }: {
415
+ muiTopToolbarProps?: ToolbarProps | (({ table }: {
407
416
  table: MRT_TableInstance<TData>;
408
417
  }) => ToolbarProps);
409
418
  onColumnDrop?: ({ event, draggedColumn, targetColumn, }: {
@@ -416,13 +425,13 @@ export declare type MaterialReactTableProps<TData extends Record<string, any> =
416
425
  onDensityChange?: OnChangeFn<boolean>;
417
426
  onDraggingColumnChange?: OnChangeFn<MRT_Column<TData> | null>;
418
427
  onDraggingRowChange?: OnChangeFn<MRT_Row<TData> | null>;
428
+ onEditingCellChange?: OnChangeFn<MRT_Cell<TData> | null>;
419
429
  onEditingRowSave?: ({ exitEditingMode, row, table, values, }: {
420
430
  exitEditingMode: () => void;
421
431
  row: MRT_Row<TData>;
422
432
  table: MRT_TableInstance<TData>;
423
433
  values: Record<LiteralUnion<string & DeepKeys<TData>>, any>;
424
434
  }) => Promise<void> | void;
425
- onEditingCellChange?: OnChangeFn<MRT_Cell<TData> | null>;
426
435
  onEditingRowChange?: OnChangeFn<MRT_Row<TData> | null>;
427
436
  onFilterFnsChange?: OnChangeFn<{
428
437
  [key: string]: MRT_FilterOption;
package/dist/cjs/index.js CHANGED
@@ -1053,11 +1053,14 @@ const MRT_TablePagination = ({ table, position }) => {
1053
1053
 
1054
1054
  const MRT_ToolbarAlertBanner = ({ stackAlertBanner, table, }) => {
1055
1055
  var _a, _b;
1056
- const { getPrePaginationRowModel, getSelectedRowModel, getState, options: { localization, muiTableToolbarAlertBannerProps }, } = table;
1056
+ const { getPrePaginationRowModel, getSelectedRowModel, getState, options: { localization, muiToolbarAlertBannerProps, muiToolbarAlertBannerChipProps, }, } = table;
1057
1057
  const { grouping, showAlertBanner } = getState();
1058
- const alertProps = muiTableToolbarAlertBannerProps instanceof Function
1059
- ? muiTableToolbarAlertBannerProps({ table })
1060
- : muiTableToolbarAlertBannerProps;
1058
+ const alertProps = muiToolbarAlertBannerProps instanceof Function
1059
+ ? muiToolbarAlertBannerProps({ table })
1060
+ : muiToolbarAlertBannerProps;
1061
+ const chipProps = muiToolbarAlertBannerChipProps instanceof Function
1062
+ ? muiToolbarAlertBannerChipProps({ table })
1063
+ : muiToolbarAlertBannerChipProps;
1061
1064
  const selectMessage = getSelectedRowModel().rows.length > 0
1062
1065
  ? (_b = (_a = localization.selectedCountOfRowCountRowsSelected) === null || _a === void 0 ? void 0 : _a.replace('{selectedCount}', getSelectedRowModel().rows.length.toString())) === null || _b === void 0 ? void 0 : _b.replace('{rowCount}', getPrePaginationRowModel().rows.length.toString())
1063
1066
  : null;
@@ -1066,7 +1069,7 @@ const MRT_ToolbarAlertBanner = ({ stackAlertBanner, table, }) => {
1066
1069
  ' ',
1067
1070
  grouping.map((columnId, index) => (React__default["default"].createElement(React.Fragment, { key: `${index}-${columnId}` },
1068
1071
  index > 0 ? localization.thenBy : '',
1069
- React__default["default"].createElement(material.Chip, { color: "secondary", label: table.getColumn(columnId).columnDef.header, onDelete: () => table.getColumn(columnId).toggleGrouping() })))))) : null;
1072
+ React__default["default"].createElement(material.Chip, Object.assign({ label: table.getColumn(columnId).columnDef.header, onDelete: () => table.getColumn(columnId).toggleGrouping() }, chipProps))))))) : null;
1070
1073
  return (React__default["default"].createElement(material.Collapse, { in: showAlertBanner || !!selectMessage || !!groupedByMessage, timeout: stackAlertBanner ? 200 : 0 },
1071
1074
  React__default["default"].createElement(material.Alert, Object.assign({ color: "info", icon: false }, alertProps, { sx: (theme) => (Object.assign({ borderRadius: 0, fontSize: '1rem', left: 0, p: 0, position: 'relative', right: 0, top: 0, width: '100%', zIndex: 2 }, ((alertProps === null || alertProps === void 0 ? void 0 : alertProps.sx) instanceof Function
1072
1075
  ? alertProps.sx(theme)
@@ -1197,12 +1200,12 @@ const commonToolbarStyles = ({ theme }) => ({
1197
1200
  });
1198
1201
  const MRT_TopToolbar = ({ table }) => {
1199
1202
  var _a;
1200
- const { getState, options: { enableGlobalFilter, enablePagination, enableToolbarInternalActions, muiTableTopToolbarProps, positionGlobalFilter, positionPagination, positionToolbarAlertBanner, positionToolbarDropZone, renderTopToolbarCustomActions, }, refs: { topToolbarRef }, } = table;
1203
+ const { getState, options: { enableGlobalFilter, enablePagination, enableToolbarInternalActions, muiTopToolbarProps, positionGlobalFilter, positionPagination, positionToolbarAlertBanner, positionToolbarDropZone, renderTopToolbarCustomActions, }, refs: { topToolbarRef }, } = table;
1201
1204
  const { isFullScreen, showGlobalFilter } = getState();
1202
1205
  const isMobile = material.useMediaQuery('(max-width:720px)');
1203
- const toolbarProps = muiTableTopToolbarProps instanceof Function
1204
- ? muiTableTopToolbarProps({ table })
1205
- : muiTableTopToolbarProps;
1206
+ const toolbarProps = muiTopToolbarProps instanceof Function
1207
+ ? muiTopToolbarProps({ table })
1208
+ : muiTopToolbarProps;
1206
1209
  const stackAlertBanner = isMobile || !!renderTopToolbarCustomActions || showGlobalFilter;
1207
1210
  return (React__default["default"].createElement(material.Toolbar, Object.assign({ variant: "dense" }, toolbarProps, { ref: (ref) => {
1208
1211
  topToolbarRef.current = ref;
@@ -1235,12 +1238,12 @@ const MRT_TopToolbar = ({ table }) => {
1235
1238
  };
1236
1239
 
1237
1240
  const MRT_BottomToolbar = ({ table }) => {
1238
- const { getState, options: { enablePagination, muiTableBottomToolbarProps, positionPagination, positionToolbarAlertBanner, positionToolbarDropZone, renderBottomToolbarCustomActions, }, refs: { bottomToolbarRef }, } = table;
1241
+ const { getState, options: { enablePagination, muiBottomToolbarProps, positionPagination, positionToolbarAlertBanner, positionToolbarDropZone, renderBottomToolbarCustomActions, }, refs: { bottomToolbarRef }, } = table;
1239
1242
  const { isFullScreen } = getState();
1240
1243
  const isMobile = material.useMediaQuery('(max-width:720px)');
1241
- const toolbarProps = muiTableBottomToolbarProps instanceof Function
1242
- ? muiTableBottomToolbarProps({ table })
1243
- : muiTableBottomToolbarProps;
1244
+ const toolbarProps = muiBottomToolbarProps instanceof Function
1245
+ ? muiBottomToolbarProps({ table })
1246
+ : muiBottomToolbarProps;
1244
1247
  const stackAlertBanner = isMobile || !!renderBottomToolbarCustomActions;
1245
1248
  return (React__default["default"].createElement(material.Toolbar, Object.assign({ variant: "dense" }, toolbarProps, { ref: (ref) => {
1246
1249
  bottomToolbarRef.current = ref;
@@ -1449,7 +1452,7 @@ const MRT_FilterTextField = ({ header, rangeFilterIndex, table, }) => {
1449
1452
  if (textFieldProps.inputRef) {
1450
1453
  textFieldProps.inputRef = inputRef;
1451
1454
  }
1452
- }, sx: (theme) => (Object.assign({ p: 0, minWidth: !filterChipLabel ? '6rem' : 'auto', width: '100%', '& .MuiSelect-icon': {
1455
+ }, sx: (theme) => (Object.assign({ p: 0, minWidth: !filterChipLabel ? '150px' : 'auto', width: '100%', '& .MuiSelect-icon': {
1453
1456
  mr: '1.5rem',
1454
1457
  } }, ((textFieldProps === null || textFieldProps === void 0 ? void 0 : textFieldProps.sx) instanceof Function
1455
1458
  ? textFieldProps.sx(theme)
@@ -1778,30 +1781,43 @@ const MRT_EditCellTextField = ({ cell, showLabel, table, }) => {
1778
1781
  })
1779
1782
  : columnDef.muiTableBodyCellEditTextFieldProps;
1780
1783
  const textFieldProps = Object.assign(Object.assign({}, mTableBodyCellEditTextFieldProps), mcTableBodyCellEditTextFieldProps);
1784
+ const saveRow = (newValue) => {
1785
+ if (editingRow) {
1786
+ setEditingRow(Object.assign(Object.assign({}, editingRow), { _valuesCache: Object.assign(Object.assign({}, editingRow._valuesCache), { [column.id]: newValue }) }));
1787
+ }
1788
+ };
1781
1789
  const handleChange = (event) => {
1782
1790
  var _a;
1783
1791
  (_a = textFieldProps.onChange) === null || _a === void 0 ? void 0 : _a.call(textFieldProps, event);
1784
1792
  setValue(event.target.value);
1793
+ if (textFieldProps === null || textFieldProps === void 0 ? void 0 : textFieldProps.select) {
1794
+ saveRow(event.target.value);
1795
+ }
1785
1796
  };
1786
1797
  const handleBlur = (event) => {
1787
1798
  var _a;
1788
1799
  (_a = textFieldProps.onBlur) === null || _a === void 0 ? void 0 : _a.call(textFieldProps, event);
1789
- if (editingRow) {
1790
- setEditingRow(Object.assign(Object.assign({}, editingRow), { _valuesCache: Object.assign(Object.assign({}, editingRow._valuesCache), { [column.id]: value }) }));
1791
- }
1800
+ saveRow(value);
1792
1801
  setEditingCell(null);
1793
1802
  };
1803
+ const handleEnterKeyDown = (event) => {
1804
+ var _a, _b;
1805
+ (_a = textFieldProps.onKeyDown) === null || _a === void 0 ? void 0 : _a.call(textFieldProps, event);
1806
+ if (event.key === 'Enter') {
1807
+ (_b = editInputRefs.current[column.id]) === null || _b === void 0 ? void 0 : _b.blur();
1808
+ }
1809
+ };
1794
1810
  if (columnDef.Edit) {
1795
1811
  return React__default["default"].createElement(React__default["default"].Fragment, null, (_a = columnDef.Edit) === null || _a === void 0 ? void 0 : _a.call(columnDef, { cell, column, row, table }));
1796
1812
  }
1797
- return (React__default["default"].createElement(material.TextField, Object.assign({ disabled: columnDef.enableEditing === false, fullWidth: true, label: showLabel ? column.columnDef.header : undefined, margin: "none", name: cell.id, onClick: (e) => e.stopPropagation(), placeholder: columnDef.header, value: value, variant: "standard" }, textFieldProps, { inputRef: (inputRef) => {
1813
+ return (React__default["default"].createElement(material.TextField, Object.assign({ disabled: columnDef.enableEditing === false, fullWidth: true, inputRef: (inputRef) => {
1798
1814
  if (inputRef) {
1799
1815
  editInputRefs.current[column.id] = inputRef;
1800
1816
  if (textFieldProps.inputRef) {
1801
1817
  textFieldProps.inputRef = inputRef;
1802
1818
  }
1803
1819
  }
1804
- }, onBlur: handleBlur, onChange: handleChange })));
1820
+ }, label: showLabel ? column.columnDef.header : undefined, margin: "none", name: column.id, onClick: (e) => e.stopPropagation(), placeholder: columnDef.header, value: value, variant: "standard" }, textFieldProps, { onBlur: handleBlur, onChange: handleChange, onKeyDown: handleEnterKeyDown })));
1805
1821
  };
1806
1822
 
1807
1823
  const MRT_CopyButton = ({ cell, children, table, }) => {
@@ -1853,7 +1869,7 @@ const MRT_TableBodyRowGrabHandle = ({ cell, rowRef, table, }) => {
1853
1869
  };
1854
1870
 
1855
1871
  const MRT_TableBodyCellValue = ({ cell, table }) => {
1856
- var _a, _b, _c;
1872
+ var _a, _b, _c, _d;
1857
1873
  const { column, row } = cell;
1858
1874
  const { columnDef } = column;
1859
1875
  return (React__default["default"].createElement(React__default["default"].Fragment, null, cell.getIsAggregated() && column.columnDef.aggregationFn
@@ -1865,7 +1881,14 @@ const MRT_TableBodyCellValue = ({ cell, table }) => {
1865
1881
  })
1866
1882
  : row.getIsGrouped() && !cell.getIsGrouped()
1867
1883
  ? null
1868
- : (_c = (_b = columnDef === null || columnDef === void 0 ? void 0 : columnDef.Cell) === null || _b === void 0 ? void 0 : _b.call(columnDef, { cell, column, row, table })) !== null && _c !== void 0 ? _c : cell.renderValue()));
1884
+ : (cell.getIsGrouped() &&
1885
+ ((_b = columnDef.GroupedCell) === null || _b === void 0 ? void 0 : _b.call(columnDef, {
1886
+ cell,
1887
+ column,
1888
+ row,
1889
+ table,
1890
+ }))) ||
1891
+ ((_d = (_c = columnDef === null || columnDef === void 0 ? void 0 : columnDef.Cell) === null || _c === void 0 ? void 0 : _c.call(columnDef, { cell, column, row, table })) !== null && _d !== void 0 ? _d : cell.renderValue())));
1869
1892
  };
1870
1893
 
1871
1894
  const MRT_TableBodyCell = ({ cell, enableHover, rowIndex, rowRef, table, }) => {
@@ -2001,10 +2024,10 @@ const MRT_TableBodyCell = ({ cell, enableHover, rowIndex, rowRef, table, }) => {
2001
2024
  !row.getIsGrouped()) ? ((_a = columnDef.Cell) === null || _a === void 0 ? void 0 : _a.call(columnDef, { cell, column, row, table })) : isEditing ? (React__default["default"].createElement(MRT_EditCellTextField, { cell: cell, table: table })) : (enableClickToCopy || columnDef.enableClickToCopy) &&
2002
2025
  columnDef.enableClickToCopy !== false ? (React__default["default"].createElement(MRT_CopyButton, { cell: cell, table: table },
2003
2026
  React__default["default"].createElement(MRT_TableBodyCellValue, { cell: cell, table: table }))) : (React__default["default"].createElement(MRT_TableBodyCellValue, { cell: cell, table: table }))),
2004
- cell.getIsGrouped() && React__default["default"].createElement(React__default["default"].Fragment, null,
2027
+ cell.getIsGrouped() && !columnDef.GroupedCell && (React__default["default"].createElement(React__default["default"].Fragment, null,
2005
2028
  " (", (_b = row.subRows) === null || _b === void 0 ? void 0 :
2006
2029
  _b.length,
2007
- ")")));
2030
+ ")"))));
2008
2031
  };
2009
2032
 
2010
2033
  const MRT_TableDetailPanel = ({ row, table }) => {