@monolith-forensics/monolith-ui 1.5.1 → 1.6.0

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.
@@ -16,23 +16,24 @@ const StyledCaption = styled.span `
16
16
  `;
17
17
  const TableHeader = ({ headerRowElm }) => {
18
18
  var _a;
19
- const { columnState, sortState, setColumnState, onColumnResize, handleColumnReorder, enableColumnReorder, handleColumnHeaderClick, enableColumnResize, headerRowHeight, enableActionButton, enableSelection, selectionState, disableSelectAll, selectAllRows, clearSelections, compactState, } = useTable();
19
+ const { columnState, sortState, setColumnState: updateColumnState, onColumnResize, handleColumnReorder, enableColumnReorder, handleColumnHeaderClick, enableColumnResize, headerRowHeight, enableActionButton, enableSelection, selectionState, disableSelectAll, selectAllRows, clearSelections, compactState, } = useTable();
20
20
  const [dragColumn, setDragColumn] = useState(null);
21
21
  const [dropColumn, setDropColumn] = useState(null);
22
22
  const handleResize = (e) => {
23
23
  const columnId = e.column.columnId;
24
- setColumnState((prevColumnState) => {
25
- const newState = prevColumnState.map((col) => {
24
+ const newWidth = Number(e.newWidth);
25
+ updateColumnState((prevColumnState) => {
26
+ const nextColumnState = prevColumnState.map((col) => {
26
27
  if (col.columnId === columnId) {
27
- return Object.assign(Object.assign({}, col), { width: Number(e.newWidth) });
28
+ return Object.assign(Object.assign({}, col), { width: newWidth });
28
29
  }
29
30
  return col;
30
31
  });
31
32
  onColumnResize === null || onColumnResize === void 0 ? void 0 : onColumnResize({
32
- column: Object.assign(Object.assign({}, e.column), { width: Number(e.newWidth) }),
33
- columnState: newState,
33
+ column: Object.assign(Object.assign({}, e.column), { width: newWidth }),
34
+ columnState: nextColumnState,
34
35
  });
35
- return newState;
36
+ return nextColumnState;
36
37
  });
37
38
  };
38
39
  const handleChangeSelection = (e) => {
@@ -19,9 +19,10 @@ var __rest = (this && this.__rest) || function (s, e) {
19
19
  return t;
20
20
  };
21
21
  import { jsx as _jsx } from "react/jsx-runtime";
22
- import { createContext, useMemo, useState } from "react";
22
+ import { createContext, useEffect, useMemo, useRef, useState } from "react";
23
23
  import StateStorage from "./StateStorage";
24
24
  import { resolveColumnResize, syncColumnState } from "./Utils";
25
+ import { notifyAndPersistColumnState, resolveColumnStateUpdate, } from "./Utils/columnStateUpdate";
25
26
  import TableDefaults from "./TableDefaults";
26
27
  import shortUUID from "short-uuid";
27
28
  import { SelectionStatus } from "./enums";
@@ -72,7 +73,11 @@ const TableProvider = (_a) => {
72
73
  }, [stateStorage === null || stateStorage === void 0 ? void 0 : stateStorage.key]);
73
74
  const { columnState: savedColumnState, selectionState: savedSelectionState, sortState: savedSortState, searchState: savedSearchState, filterState: savedFilterState, } = savedTableState || {};
74
75
  const [compactState, setCompactState] = useState(compact || false);
75
- const [columnState, setColumnState] = useState(syncColumnState(_columns, savedColumnState));
76
+ const [columnState, _setColumnState] = useState(syncColumnState(_columns, savedColumnState));
77
+ const columnStateRef = useRef(columnState);
78
+ useEffect(() => {
79
+ columnStateRef.current = columnState;
80
+ }, [columnState]);
76
81
  const [search, setSearch] = useState(savedSearchState || "");
77
82
  const [_filterState, _setFilterState] = useControlled(filterState, defaultFilterState ||
78
83
  savedFilterState || {
@@ -115,14 +120,20 @@ const TableProvider = (_a) => {
115
120
  const getColumnState = (dataField) => {
116
121
  return columnState.find((col) => col.dataField === dataField);
117
122
  };
118
- const updateColumnState = (columnState) => {
119
- setColumnState(columnState);
120
- onColumnStateChange === null || onColumnStateChange === void 0 ? void 0 : onColumnStateChange(columnState);
121
- if (stateStorage === null || stateStorage === void 0 ? void 0 : stateStorage.enabled) {
122
- if ((stateStorage === null || stateStorage === void 0 ? void 0 : stateStorage.type) === "localStorage") {
123
- StateStorage.setColumnState(stateStorage.key, columnState);
124
- }
125
- }
123
+ const updateColumnState = (columnStateUpdate) => {
124
+ const previousColumnState = columnStateRef.current;
125
+ const nextColumnState = resolveColumnStateUpdate(columnStateUpdate, previousColumnState);
126
+ columnStateRef.current = nextColumnState;
127
+ _setColumnState(nextColumnState);
128
+ notifyAndPersistColumnState({
129
+ nextColumnState,
130
+ onColumnStateChange,
131
+ persistColumnState: (stateStorage === null || stateStorage === void 0 ? void 0 : stateStorage.enabled) && (stateStorage === null || stateStorage === void 0 ? void 0 : stateStorage.type) === "localStorage"
132
+ ? (columnState) => {
133
+ StateStorage.setColumnState(stateStorage.key, columnState);
134
+ }
135
+ : undefined,
136
+ });
126
137
  };
127
138
  const handleColumnReorder = (dragColumn, dropColumn) => {
128
139
  var _a, _b;
@@ -605,9 +616,7 @@ const TableProvider = (_a) => {
605
616
  }
606
617
  return processedData;
607
618
  }, [data, columnState, search, sortState, _filterState]);
608
- return (_jsx(TableContext.Provider, { value: Object.assign({ columnState,
609
- setColumnState,
610
- sortState, searchState: search, totalRecords,
619
+ return (_jsx(TableContext.Provider, { value: Object.assign({ columnState, setColumnState: updateColumnState, sortState, searchState: search, totalRecords,
611
620
  keyField,
612
621
  handleColumnReorder,
613
622
  handleColumnHeaderClick, selectionState: _selectionState, selectRow,
@@ -14,7 +14,7 @@ const TableRow = ({ rowData, loading, rowStyle }) => {
14
14
  const handleSelectionChange = (e) => {
15
15
  e === true ? selectRow(rowData) : deselectRow(rowData);
16
16
  };
17
- return (_jsxs(TR, { className: "mfui-tr", style: rowStyle, "data-key": rowData.__key, "data-selected": selected, "data-focused": focused, children: [enableSelection && (_jsx(ActionCell, { className: `mfui-td column-select`, children: _jsx(InnerCellContent, { className: "mfui inner-cell-content row-action", children: _jsx(CheckBox, { className: `mfui-checkbox`, value: selected, onChange: (e) => handleSelectionChange(e) }) }) })), enableActionButton && (_jsx(ActionCell, { className: `mfui-td column-action`, children: _jsx(InnerCellContent, { className: "mfui inner-cell-content row-action", children: _jsx(ActionButton, { variant: "subtle", onClick: () => onActionButtonClick === null || onActionButtonClick === void 0 ? void 0 : onActionButtonClick(rowData), children: Icon ? _jsx(Icon, { size: 14 }) : _jsx(Maximize2Icon, { size: 14 }) }) }) })), columnState.map((column, index) => {
17
+ return (_jsxs(TR, { className: "mfui-tr", style: rowStyle, "data-key": rowData.__key, "data-selected": selected, "data-focused": focused, children: [enableSelection && (_jsx(ActionCell, { className: `mfui-td column-select`, children: _jsx(InnerCellContent, { className: "mfui inner-cell-content row-action row-selection-action", children: _jsx(CheckBox, { className: `mfui-checkbox`, value: selected, onChange: (e) => handleSelectionChange(e) }) }) })), enableActionButton && (_jsx(ActionCell, { className: `mfui-td column-action`, children: _jsx(InnerCellContent, { className: "mfui inner-cell-content row-action", children: _jsx(ActionButton, { variant: "subtle", onClick: () => onActionButtonClick === null || onActionButtonClick === void 0 ? void 0 : onActionButtonClick(rowData), children: Icon ? _jsx(Icon, { size: 14 }) : _jsx(Maximize2Icon, { size: 14 }) }) }) })), columnState.map((column, index) => {
18
18
  if (column.visible === false)
19
19
  return null;
20
20
  if (loading) {
@@ -0,0 +1,7 @@
1
+ import type { ColumnState, ColumnStateUpdate } from "../types";
2
+ export declare const resolveColumnStateUpdate: (columnStateUpdate: ColumnStateUpdate, previousColumnState: ColumnState[]) => ColumnState[];
3
+ export declare const notifyAndPersistColumnState: ({ nextColumnState, onColumnStateChange, persistColumnState, }: {
4
+ nextColumnState: ColumnState[];
5
+ onColumnStateChange?: (columnState: ColumnState[]) => void;
6
+ persistColumnState?: (columnState: ColumnState[]) => void;
7
+ }) => void;
@@ -0,0 +1,9 @@
1
+ export const resolveColumnStateUpdate = (columnStateUpdate, previousColumnState) => {
2
+ return typeof columnStateUpdate === "function"
3
+ ? columnStateUpdate(previousColumnState)
4
+ : columnStateUpdate;
5
+ };
6
+ export const notifyAndPersistColumnState = ({ nextColumnState, onColumnStateChange, persistColumnState, }) => {
7
+ onColumnStateChange === null || onColumnStateChange === void 0 ? void 0 : onColumnStateChange(nextColumnState);
8
+ persistColumnState === null || persistColumnState === void 0 ? void 0 : persistColumnState(nextColumnState);
9
+ };
@@ -68,6 +68,7 @@ export interface onResizeProps {
68
68
  width: number;
69
69
  }[];
70
70
  }
71
+ export type ColumnStateUpdate = ColumnState[] | ((prevColumnState: ColumnState[]) => ColumnState[]);
71
72
  export interface ResizeHandlerProps {
72
73
  event: React.MouseEvent<HTMLDivElement>;
73
74
  columnId?: string;
@@ -95,7 +96,7 @@ export type TableDimensions = {
95
96
  };
96
97
  export type TableContextType = {
97
98
  columnState: ColumnState[];
98
- setColumnState: React.Dispatch<React.SetStateAction<ColumnState[]>>;
99
+ setColumnState: (columnStateUpdate: ColumnStateUpdate) => void;
99
100
  sortState?: SortState | null;
100
101
  filterState?: Query | null;
101
102
  searchState?: string | null;
@@ -337,7 +338,7 @@ export interface TableProps {
337
338
  }
338
339
  export type TableInstance = {
339
340
  columnState: ColumnState[];
340
- setColumnState: (e: ColumnState[]) => void;
341
+ setColumnState: (columnStateUpdate: ColumnStateUpdate) => void;
341
342
  getColumnState: (dataField: string) => ColumnState | undefined;
342
343
  onColumnResize?: (e: OnColumnChangeProps) => void;
343
344
  onColumnReorder?: (e: OnColumnChangeProps) => void;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@monolith-forensics/monolith-ui",
3
- "version": "1.5.1",
3
+ "version": "1.6.0",
4
4
  "main": "./dist/index.js",
5
5
  "types": "./dist/index.d.ts",
6
6
  "author": "Matt Danner (Monolith Forensics LLC)",