@dooherceg/material-react-table 3.2.8 → 3.2.10

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/dist/index.d.ts CHANGED
@@ -647,6 +647,7 @@ type MRT_TableInstance<TData extends MRT_RowData> = Omit<Table<TData>, 'getAllCo
647
647
  setRows: (rows: MRT_RowManipulationInput<TData>) => void;
648
648
  upsertRow: (rows: MRT_RowManipulationInput<TData>) => void;
649
649
  removeRow: (rows: string | string[]) => void;
650
+ refetch?: () => void;
650
651
  };
651
652
  type MRT_DefinedTableOptions<TData extends MRT_RowData> = Omit<MRT_TableOptions<TData>, 'icons' | 'localization' | 'mrtTheme'> & {
652
653
  icons: MRT_Icons;
@@ -1518,7 +1519,9 @@ type UseServerTableStateReturn = {
1518
1519
  pagination: MRT_PaginationState;
1519
1520
  sorting: MRT_SortingState;
1520
1521
  columnVisibilityShowTrigger: number;
1522
+ refetchIndex: number;
1521
1523
  };
1524
+ refetch: () => void;
1522
1525
  };
1523
1526
  type ColumnType = 'string' | 'number' | 'date' | 'dateTime' | 'enum' | 'icon' | 'dimension' | 'boolean' | 'actions' | 'object';
1524
1527
  type MRT_FilterOperator = 'between' | 'contains' | 'current-month' | 'current-week' | 'endsWith' | 'equals' | 'from-today' | 'fuzzy' | 'greaterThan' | 'greaterThanOrEqualTo' | 'isAnyOf' | 'isEmpty' | 'isNotEmpty' | 'last-7-days' | 'last-month' | 'last-week' | 'lessThan' | 'lessThanOrEqualTo' | 'notContains' | 'notEquals' | 'startsWith' | 'to-today';
@@ -1860,13 +1863,19 @@ type MaterialReactServerTableInstanceProps<TData extends MRT_RowData> = Partial<
1860
1863
  resetState?: (table: MRT_TableInstance<TData>) => Promise<void> | void;
1861
1864
  extendColumns?: Partial<MRT_ColumnDef<TData>>[];
1862
1865
  };
1866
+ type MaterialReactServerTableHandle<TData extends MRT_RowData> = {
1867
+ refetch: () => void;
1868
+ table: MRT_TableInstance<TData>;
1869
+ };
1863
1870
 
1864
1871
  type MaterialReactServerTableProps<TData extends MRT_RowData> = Omit<MaterialReactServerTableInstanceProps<TData>, 'config'> & {
1865
1872
  loadConfig: () => Promise<MRT_TableConfig<TData>>;
1866
1873
  };
1867
1874
  declare const MaterialReactServerTable: <TData extends MRT_RowData & {
1868
1875
  id: string;
1869
- }>({ loadConfig, saveState, localization, ...instanceProps }: MaterialReactServerTableProps<TData>) => react_jsx_runtime.JSX.Element;
1876
+ }>(props: MaterialReactServerTableProps<TData> & {
1877
+ ref?: Ref<MaterialReactServerTableHandle<TData>>;
1878
+ }) => ReactElement;
1870
1879
 
1871
1880
  type TableInstanceProp<TData extends MRT_RowData> = {
1872
1881
  table: MRT_TableInstance<TData>;
package/dist/index.esm.js CHANGED
@@ -771,7 +771,12 @@ function MRT_DeleteRowButton({ onClick, }) {
771
771
  const iconButtonStyle = {
772
772
  p: 0,
773
773
  };
774
- return (jsx(IconButton, { disableRipple: true, color: "error", sx: iconButtonStyle, onClick: onClick, children: jsx(DeleteOutlineIcon, {}) }));
774
+ const handleClick = (event) => {
775
+ // Prevent bubbling to muiTableBodyRowProps onClick (row click handlers)
776
+ event.stopPropagation();
777
+ onClick();
778
+ };
779
+ return (jsx(IconButton, { disableRipple: true, color: "error", sx: iconButtonStyle, onClick: handleClick, children: jsx(DeleteOutlineIcon, {}) }));
775
780
  }
776
781
 
777
782
  const DeleteRowAction = ({ onDeleteConfirm, table, deleteConfirmation, }) => {
@@ -792,14 +797,25 @@ const DeleteRowAction = ({ onDeleteConfirm, table, deleteConfirmation, }) => {
792
797
  };
793
798
  const handleConfirmDelete = () => __awaiter(void 0, void 0, void 0, function* () {
794
799
  setDeleting(true);
795
- yield onDeleteConfirm();
796
- setDeleting(false);
797
- setOpen(false);
800
+ try {
801
+ yield onDeleteConfirm();
802
+ setOpen(false);
803
+ }
804
+ catch (error) {
805
+ console.error('[MRT] Error while deleting row(s):', error);
806
+ }
807
+ finally {
808
+ setDeleting(false);
809
+ }
798
810
  });
799
811
  const handleClose = () => __awaiter(void 0, void 0, void 0, function* () {
800
812
  setOpen(false);
801
813
  });
802
- return (jsxs(Fragment, { children: [jsx(Dialog, { open: open, onClose: () => setOpen(false), keepMounted: true, "aria-describedby": "alert-dialog-slide-description", role: "alertdialog", sx: dialogSx, children: jsxs(Stack, { flexDirection: "column", height: "min-content", sx: contentContainerSx, children: [jsxs(Stack, { flexDirection: "row", alignItems: "center", justifyContent: "space-between", sx: Object.assign({ width: '100%' }, headerSx), children: [jsx(DialogTitle, { sx: Object.assign({ p: '0.7rem' }, titleSx), children: deleteConfirmationTitle }), jsx(DialogActions, { sx: {
814
+ return (jsxs(Fragment, { children: [jsx(Dialog, { open: open, onClose: () => setOpen(false), keepMounted: true, "aria-describedby": "alert-dialog-slide-description", role: "alertdialog", sx: dialogSx, children: jsxs(Stack, { flexDirection: "column", height: "min-content", sx: contentContainerSx,
815
+ // Dialog renders via a portal, but React still bubbles synthetic events up
816
+ // through the component tree — stop here so clicks inside the confirmation
817
+ // dialog don't reach muiTableBodyRowProps onClick (row click handlers).
818
+ onClick: (event) => event.stopPropagation(), children: [jsxs(Stack, { flexDirection: "row", alignItems: "center", justifyContent: "space-between", sx: Object.assign({ width: '100%' }, headerSx), children: [jsx(DialogTitle, { sx: Object.assign({ p: '0.7rem' }, titleSx), children: deleteConfirmationTitle }), jsx(DialogActions, { sx: {
803
819
  p: '0.7rem',
804
820
  }, children: jsx(IconButton, Object.assign({ color: "error", "aria-label": "close", onClick: handleClose }, closeButtonProps, { children: jsx(CancelIcon, {}) })) })] }), jsxs(Stack, { children: [jsx(DialogContent, { sx: Object.assign({ pr: '6rem' }, bodySx), children: deleteConfirmationMessage }), jsxs(DialogActions, { sx: footerSx, children: [jsx(Button, Object.assign({ onClick: () => setOpen(false), disabled: deleting, autoFocus: true, variant: "soft" }, cancelButtonProps, { children: cancelButtonLabel })), jsx(Button, Object.assign({ onClick: handleConfirmDelete, disabled: deleting, variant: "contained", color: "error" }, confirmButtonProps, { children: deleting ? deletingLabel : confirmButtonLabel }))] })] })] }) }), jsx(MRT_DeleteRowButton, { onClick: handleDeleteRowButtonClick })] }));
805
821
  };
@@ -835,16 +851,15 @@ const createDeleteAction = (_a = {}) => {
835
851
  return executeDelete({ table, rowsToDelete: [row] });
836
852
  };
837
853
  // Wraps row context into a zero-argument callback expected by row action UI.
854
+ // Must return the promise (not fire-and-forget it) so callers like DeleteRowAction
855
+ // can `await` it and keep the confirmation modal open until delete actually finishes.
838
856
  const createRowDeleteExecutor = (context) => {
839
- return () => {
840
- void handleSingleRowDelete(context);
841
- };
857
+ return () => handleSingleRowDelete(context);
842
858
  };
843
859
  // Wraps toolbar context into a zero-argument callback expected by toolbar action UI.
860
+ // Same reasoning as createRowDeleteExecutor — the returned promise must propagate.
844
861
  const createToolbarDeleteExecutor = (context) => {
845
- return () => {
846
- void handleMultipleRowDelete(context);
847
- };
862
+ return () => handleMultipleRowDelete(context);
848
863
  };
849
864
  // Uses custom row renderer when provided; otherwise renders default delete action UI.
850
865
  const renderRowAction = (context) => {
@@ -866,7 +881,12 @@ const createDeleteAction = (_a = {}) => {
866
881
  };
867
882
 
868
883
  const MRT_EditRowButton = ({ onClick }) => {
869
- return (jsx(IconButton, { sx: { p: 0 }, disableRipple: true, color: "primary", onClick: onClick, children: jsx(EditNoteIcon, {}) }));
884
+ const handleClick = (event) => {
885
+ // Prevent bubbling to muiTableBodyRowProps onClick (row click handlers)
886
+ event.stopPropagation();
887
+ onClick === null || onClick === void 0 ? void 0 : onClick(event);
888
+ };
889
+ return (jsx(IconButton, { sx: { p: 0 }, disableRipple: true, color: "primary", onClick: handleClick, children: jsx(EditNoteIcon, {}) }));
870
890
  };
871
891
 
872
892
  const EditRowAction = ({ onEditButtonClick, }) => {
@@ -2628,6 +2648,11 @@ const useMRT_Effects = (table) => {
2628
2648
  }, [density]);
2629
2649
  };
2630
2650
 
2651
+ // Keeps the local rows array from growing past the current page size. New rows are
2652
+ // inserted at the front, so overflow is trimmed off the tail (the oldest existing
2653
+ // rows), since the local state is expected to be reconciled with the source of
2654
+ // truth on the next fetch.
2655
+ const capRowsToPageSize = (rows, pageSize) => pageSize && rows.length > pageSize ? rows.slice(0, pageSize) : rows;
2631
2656
  const normalizeRowInput = (rowInput) => (Array.isArray(rowInput) ? rowInput : [rowInput]);
2632
2657
  const getResolvedRowId = (row, index, parentRow, getRowId) => {
2633
2658
  const customRowId = getRowId === null || getRowId === void 0 ? void 0 : getRowId(row, index, parentRow);
@@ -2638,7 +2663,7 @@ const getResolvedRowId = (row, index, parentRow, getRowId) => {
2638
2663
  return String(row.id);
2639
2664
  return undefined;
2640
2665
  };
2641
- const handleAddRow = ({ setRowsState, }) => {
2666
+ const handleAddRow = ({ pageSize, setRowsState, }) => {
2642
2667
  return (rowInput) => {
2643
2668
  const rowsToAdd = normalizeRowInput(rowInput);
2644
2669
  const hasValidRowIds = rowsToAdd.every((row, index) => {
@@ -2648,7 +2673,7 @@ const handleAddRow = ({ setRowsState, }) => {
2648
2673
  if (!hasValidRowIds) {
2649
2674
  throw new Error('Each row must have a valid `id` property or a `getRowId` function must be provided to generate unique IDs.');
2650
2675
  }
2651
- setRowsState((previousRows) => [...previousRows, ...rowsToAdd]);
2676
+ setRowsState((previousRows) => capRowsToPageSize([...rowsToAdd, ...previousRows], pageSize));
2652
2677
  };
2653
2678
  };
2654
2679
  const handleUpdateRow = ({ getRowId, setRowsState, }) => {
@@ -2678,7 +2703,7 @@ const handleSetRows = ({ setRowsState, }) => {
2678
2703
  setRowsState(normalizeRowInput(rowInput));
2679
2704
  };
2680
2705
  };
2681
- const handleUpsertRow = ({ getRowId, setRowsState, }) => {
2706
+ const handleUpsertRow = ({ getRowId, pageSize, setRowsState, }) => {
2682
2707
  return (rowInput) => {
2683
2708
  const rowsToUpsert = normalizeRowInput(rowInput);
2684
2709
  setRowsState((previousRows) => {
@@ -2692,19 +2717,17 @@ const handleUpsertRow = ({ getRowId, setRowsState, }) => {
2692
2717
  indexByRowId.set(rowId, index);
2693
2718
  }
2694
2719
  });
2720
+ const rowsToInsert = [];
2695
2721
  rowsToUpsert.forEach((row, index) => {
2696
2722
  const rowId = getResolvedRowId(row, index, undefined, getRowId);
2697
2723
  const existingIndex = rowId !== undefined ? indexByRowId.get(rowId) : undefined;
2698
2724
  if (existingIndex === undefined) {
2699
- if (rowId !== undefined) {
2700
- indexByRowId.set(rowId, nextRows.length);
2701
- }
2702
- nextRows.push(row);
2725
+ rowsToInsert.push(row);
2703
2726
  return;
2704
2727
  }
2705
2728
  nextRows[existingIndex] = row;
2706
2729
  });
2707
- return nextRows;
2730
+ return capRowsToPageSize([...rowsToInsert, ...nextRows], pageSize);
2708
2731
  });
2709
2732
  };
2710
2733
  };
@@ -3134,6 +3157,7 @@ const useMRT_TableInstance = (definedTableOptions) => {
3134
3157
  table.addRow = handleAddRow({
3135
3158
  setRowsState,
3136
3159
  getRowId: resolveRowId,
3160
+ pageSize: statefulTableOptions.state.pagination.pageSize,
3137
3161
  });
3138
3162
  table.updateRow = handleUpdateRow({
3139
3163
  setRowsState,
@@ -3146,6 +3170,7 @@ const useMRT_TableInstance = (definedTableOptions) => {
3146
3170
  table.upsertRow = handleUpsertRow({
3147
3171
  setRowsState,
3148
3172
  getRowId: resolveRowId,
3173
+ pageSize: statefulTableOptions.state.pagination.pageSize,
3149
3174
  });
3150
3175
  table.removeRow = handleRemoveRow({
3151
3176
  setRowsState,
@@ -6297,7 +6322,9 @@ const MRT_NewEntryButton = (_a) => {
6297
6322
  var _b;
6298
6323
  var { table } = _a, rest = __rest(_a, ["table"]);
6299
6324
  const { options: { icons: { AddIcon }, localization, }, setNewEntryModal, } = table;
6300
- const handleOpenNewEntryModal = () => {
6325
+ const handleOpenNewEntryModal = (event) => {
6326
+ // Prevent bubbling to muiTableBodyRowProps onClick (row click handlers)
6327
+ event.stopPropagation();
6301
6328
  setNewEntryModal({ open: true });
6302
6329
  };
6303
6330
  return (jsx(Tooltip, { title: (_b = rest === null || rest === void 0 ? void 0 : rest.title) !== null && _b !== void 0 ? _b : localization.newEntry, children: jsx(Button, Object.assign({ "aria-label": localization.newEntry, onClick: handleOpenNewEntryModal, size: "small", startIcon: jsx(AddIcon, { fontSize: "small" }), variant: "text" }, rest, { title: undefined, children: localization.newEntry })) }));
@@ -8504,6 +8531,9 @@ const useServerTableState = ({ initialState, saveState, saveDebounceMs = 500, })
8504
8531
  const [expanded, setExpanded] = useState((_k = initialState === null || initialState === void 0 ? void 0 : initialState.expanded) !== null && _k !== void 0 ? _k : {});
8505
8532
  const [rowSelection, setRowSelection] = useState((_l = initialState === null || initialState === void 0 ? void 0 : initialState.rowSelection) !== null && _l !== void 0 ? _l : {});
8506
8533
  const [activeExports, setActiveExports] = useState(initialState === null || initialState === void 0 ? void 0 : initialState.activeExports);
8534
+ // Counter that increments on every manual refetch() call, without changing any other state
8535
+ const [refetchIndex, setRefetchIndex] = useState(0);
8536
+ const refetch = () => setRefetchIndex((prev) => prev + 1);
8507
8537
  // --- Debounced save ---
8508
8538
  // saveState is optional — if not provided, do nothing
8509
8539
  const debouncedSave = useDebouncedCallback((partial) => {
@@ -8586,7 +8616,9 @@ const useServerTableState = ({ initialState, saveState, saveDebounceMs = 500, })
8586
8616
  pagination,
8587
8617
  sorting,
8588
8618
  columnVisibilityShowTrigger,
8619
+ refetchIndex,
8589
8620
  },
8621
+ refetch,
8590
8622
  };
8591
8623
  };
8592
8624
 
@@ -8630,7 +8662,7 @@ const buildInitialExportState = (availableExports, savedActiveExports) => {
8630
8662
  grouped: false,
8631
8663
  });
8632
8664
  };
8633
- const MaterialReactServerTableInstance = (_a) => {
8665
+ const MaterialReactServerTableInstanceComponent = (_a, ref) => {
8634
8666
  var _b, _c;
8635
8667
  var { config, loadData, saveState, getAllSelectableRowIds, getTotalRows, onSaveFilters, onDeleteSavedFilter, initialSavedFilters, loadExport, exportPermissions, enableNewEntryButton, actions, deleteRowsFn, editRowFn, enableResetState, resetState, extendColumns } = _a,
8636
8668
  // Extra MRT_TableOptions props (e.g. formConfig) passed directly through to useMaterialReactTable
@@ -8658,7 +8690,7 @@ const MaterialReactServerTableInstance = (_a) => {
8658
8690
  });
8659
8691
  return Object.fromEntries(allowedEntries);
8660
8692
  }, [config.availableExports, exportPermissions]);
8661
- const { tableState, handlers, fetchTrigger } = useServerTableState({
8693
+ const { tableState, handlers, fetchTrigger, refetch } = useServerTableState({
8662
8694
  initialState: Object.assign(Object.assign({}, config.initialState), { activeExports: buildInitialExportState(allowedExports, (_b = config.initialState) === null || _b === void 0 ? void 0 : _b.activeExports) }),
8663
8695
  saveState,
8664
8696
  });
@@ -8678,6 +8710,9 @@ const MaterialReactServerTableInstance = (_a) => {
8678
8710
  editRowFn,
8679
8711
  onSaveFilters,
8680
8712
  onDeleteSavedFilter, initialSavedFilters: initialSavedFilters !== null && initialSavedFilters !== void 0 ? initialSavedFilters : (_c = config.initialState) === null || _c === void 0 ? void 0 : _c.savedFilters, availableExports: hasAvailableExports ? allowedExports : undefined, loadExport: hasAvailableExports ? loadExport : undefined, enableNewEntryButton }, tableOptionsOverrides), handlers));
8713
+ // Lets any code that already has `table` (row actions, deleteRowsFn/editRowFn,
8714
+ // custom cell renderers, etc.) trigger a manual reload without needing a ref.
8715
+ table.refetch = refetch;
8681
8716
  const fetchData = useCallback((tableInstance) => __awaiter(void 0, void 0, void 0, function* () {
8682
8717
  setIsLoading(true);
8683
8718
  try {
@@ -8705,9 +8740,12 @@ const MaterialReactServerTableInstance = (_a) => {
8705
8740
  fetchTrigger.pagination,
8706
8741
  fetchTrigger.sorting,
8707
8742
  fetchTrigger.columnVisibilityShowTrigger,
8743
+ fetchTrigger.refetchIndex,
8708
8744
  ]);
8745
+ useImperativeHandle(ref, () => ({ refetch, table }), [refetch, table]);
8709
8746
  return jsx(MaterialReactTable, { table: table });
8710
8747
  };
8748
+ const MaterialReactServerTableInstance = forwardRef(MaterialReactServerTableInstanceComponent);
8711
8749
 
8712
8750
  const MaterialReactServerTableSkeleton = () => (jsxs(Paper, { "data-testid": "server-table-skeleton", sx: {
8713
8751
  display: 'flex',
@@ -8730,7 +8768,7 @@ const MaterialReactServerTableSkeleton = () => (jsxs(Paper, { "data-testid": "se
8730
8768
  px: '1.3rem',
8731
8769
  }), children: jsxs(Box, { sx: { alignItems: 'center', display: 'flex', gap: '1rem' }, children: [jsx(Skeleton, { animation: "wave", height: 36, variant: "rounded", width: 136 }), jsx(Skeleton, { animation: "wave", height: 31, variant: "text", width: 176 }), jsx(Skeleton, { animation: "wave", height: 22, variant: "text", width: 70 }), jsx(Skeleton, { animation: "wave", height: 24, variant: "circular", width: 24 }), jsx(Skeleton, { animation: "wave", height: 24, variant: "circular", width: 24 })] }) })] }));
8732
8770
 
8733
- const MaterialReactServerTable = (_a) => {
8771
+ const MaterialReactServerTableComponent = (_a, ref) => {
8734
8772
  var { loadConfig, saveState, localization } = _a, instanceProps = __rest(_a, ["loadConfig", "saveState", "localization"]);
8735
8773
  const mergedLocalization = Object.assign(Object.assign({}, MRT_Localization_HR), localization);
8736
8774
  const [configLoading, setConfigLoading] = useState(true);
@@ -8766,8 +8804,9 @@ const MaterialReactServerTable = (_a) => {
8766
8804
  if (!config) {
8767
8805
  return (jsx(MaterialReactServerTableError, { description: mergedLocalization.serverTableErrorMessage }));
8768
8806
  }
8769
- return (jsx(MaterialReactServerTableInstance, Object.assign({ config: config, saveState: saveState }, instanceProps)));
8807
+ return (jsx(MaterialReactServerTableInstance, Object.assign({ ref: ref, config: config, saveState: saveState }, instanceProps)));
8770
8808
  };
8809
+ const MaterialReactServerTable = forwardRef(MaterialReactServerTableComponent);
8771
8810
 
8772
8811
  const MRT_ColumnPinningButtons = (_a) => {
8773
8812
  var { column, table } = _a, rest = __rest(_a, ["column", "table"]);