schummar-table 0.10.3 → 0.11.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.
package/dist/cjs/index.js CHANGED
@@ -83,6 +83,9 @@ class Queue {
83
83
  this.start();
84
84
  });
85
85
  }
86
+ clear() {
87
+ this.q = [];
88
+ }
86
89
  async start() {
87
90
  this.isRunning = true;
88
91
  let next;
@@ -134,9 +137,9 @@ function parse(value) {
134
137
  return value2;
135
138
  });
136
139
  }
137
- function useTableStateStorage() {
138
- const table = useTableContext();
140
+ function useTableStateStorage(table) {
139
141
  const [isHydrated, setIsHydrated] = react.useState(false);
142
+ const [q] = react.useState(() => new Queue());
140
143
  react.useEffect(() => {
141
144
  const { persist } = table.getState().props;
142
145
  if (!persist) {
@@ -185,7 +188,6 @@ function useTableStateStorage() {
185
188
  react.useEffect(() => {
186
189
  if (!isHydrated)
187
190
  return;
188
- const q = new Queue();
189
191
  return table.subscribe((state) => {
190
192
  var _a, _b;
191
193
  if (!state.props.persist)
@@ -218,7 +220,23 @@ function useTableStateStorage() {
218
220
  }, true);
219
221
  }, { throttle: 1e3 });
220
222
  }, [isHydrated]);
221
- return isHydrated;
223
+ async function clear() {
224
+ await q.run(async () => {
225
+ const persist = table.getState().props.persist;
226
+ if (!persist) {
227
+ return;
228
+ }
229
+ const { storage } = persist;
230
+ const keys = "keys" in storage ? await storage.keys() : await Promise.all(Array(storage.length).fill(0).map((_x, i) => storage.key(i)));
231
+ for (const key of keys) {
232
+ if (key !== null) {
233
+ await storage.removeItem(key);
234
+ }
235
+ }
236
+ q.clear();
237
+ });
238
+ }
239
+ return [isHydrated, clear];
222
240
  }
223
241
  const flatMap = (arr, flatMap2) => {
224
242
  return [...arr].reduce((out, cur) => out.concat(flatMap2(cur)), []);
@@ -543,6 +561,39 @@ function filterColumns(state) {
543
561
  draft.activeColumns = orderBy(columns.filter((column) => !hiddenColumns.has(column.id)), [(column) => columnOrder.indexOf(column.id)]);
544
562
  }, { runNow: true }), [state]);
545
563
  }
564
+ function normalizeExpanded(table) {
565
+ react.useEffect(() => table.addReaction((state) => [state.props.expandOnlyOne, state.expanded, state.itemsById], ([expandOnlyOne, , itemsById], state) => {
566
+ var _a, _b, _c, _d, _e;
567
+ let expanded = state.expanded;
568
+ let hasChanged = false;
569
+ const allAncestors = [...expanded].map((id) => {
570
+ const item = itemsById.get(id);
571
+ return {
572
+ id,
573
+ ancestors: item ? getAncestors(itemsById, item) : /* @__PURE__ */ new Set()
574
+ };
575
+ });
576
+ if (expandOnlyOne) {
577
+ const withMostAncestors = orderBy(allAncestors, [(x) => x.ancestors.size], ["desc"])[0];
578
+ if ([...expanded].some((id) => id !== (withMostAncestors == null ? void 0 : withMostAncestors.id) && !(withMostAncestors == null ? void 0 : withMostAncestors.ancestors.has(id)))) {
579
+ expanded = state.expanded = (_a = withMostAncestors == null ? void 0 : withMostAncestors.ancestors) != null ? _a : /* @__PURE__ */ new Set();
580
+ (_c = (_b = state.props).onExpandedChange) == null ? void 0 : _c.call(_b, expanded);
581
+ return;
582
+ }
583
+ }
584
+ for (const { ancestors } of allAncestors) {
585
+ for (const ancestor of ancestors != null ? ancestors : []) {
586
+ if (!expanded.has(ancestor)) {
587
+ expanded.add(ancestor);
588
+ hasChanged = true;
589
+ }
590
+ }
591
+ }
592
+ if (hasChanged) {
593
+ (_e = (_d = state.props).onExpandedChange) == null ? void 0 : _e.call(_d, new Set(expanded));
594
+ }
595
+ }, { runNow: true }), [table]);
596
+ }
546
597
  function syncSelections(table) {
547
598
  react.useEffect(() => table.addReaction((state) => [state.props.selectSyncChildren, state.selection, state.activeItems], ([selectSyncChildren, selection, activeItems], state) => {
548
599
  if (!selectSyncChildren)
@@ -563,7 +614,11 @@ function syncSelections(table) {
563
614
  }, { runNow: true }), [table]);
564
615
  }
565
616
  function useTableState(_props) {
617
+ const [key, setKey] = react.useState({});
566
618
  const props = calcProps(_props);
619
+ function reset() {
620
+ setKey({});
621
+ }
567
622
  const state = react.useMemo(() => {
568
623
  var _a, _b, _c, _d;
569
624
  return new react$1.Store({
@@ -585,9 +640,10 @@ function useTableState(_props) {
585
640
  activeItemsById: /* @__PURE__ */ new Map(),
586
641
  lastSelectedId: void 0
587
642
  });
588
- }, []);
643
+ }, [key]);
589
644
  filterColumns(state);
590
645
  calcItems(state);
646
+ normalizeExpanded(state);
591
647
  syncSelections(state);
592
648
  cleanupState(state);
593
649
  react.useEffect(() => {
@@ -609,7 +665,7 @@ function useTableState(_props) {
609
665
  }
610
666
  });
611
667
  }, [state, props]);
612
- return state;
668
+ return [state, reset];
613
669
  }
614
670
  function useCssVariables() {
615
671
  return useTheme(({ spacing, colors }) => ({
@@ -1060,10 +1116,13 @@ function ColumnSelection() {
1060
1116
  const IconButton2 = useTheme((t) => t.components.IconButton);
1061
1117
  const Popover = useTheme((t) => t.components.Popover);
1062
1118
  const Checkbox2 = useTheme((t) => t.components.Checkbox);
1119
+ const Button2 = useTheme((t) => t.components.Button);
1063
1120
  const Settings2 = useTheme((t) => t.icons.Settings);
1064
1121
  const selectColumns = useTheme((t) => t.text.selectColumns);
1122
+ const resetAll = useTheme((t) => t.text.resetAll);
1065
1123
  const cssVariables = useCssVariables();
1066
1124
  const table = useTableContext();
1125
+ const reset = react.useContext(TableResetContext);
1067
1126
  const columns = table.useState("props.columns");
1068
1127
  const hiddenColumns = table.useState("hiddenColumns");
1069
1128
  const [anchorEl, setAnchorEl] = react.useState(null);
@@ -1112,7 +1171,11 @@ function ColumnSelection() {
1112
1171
  disabled: column.cannotHide
1113
1172
  }),
1114
1173
  label: column.header
1115
- }, column.id))]
1174
+ }, column.id)), /* @__PURE__ */ jsxRuntime.jsx(Button2, {
1175
+ variant: "outlined",
1176
+ onClick: reset,
1177
+ children: resetAll
1178
+ })]
1116
1179
  })
1117
1180
  })]
1118
1181
  });
@@ -1604,6 +1667,7 @@ function Virtualized(_c) {
1604
1667
  }));
1605
1668
  }
1606
1669
  const TableContext = react.createContext(null);
1670
+ const TableResetContext = react.createContext(() => void 0);
1607
1671
  const ColumnContext = react.createContext(null);
1608
1672
  function useTableContext() {
1609
1673
  const value = react.useContext(TableContext);
@@ -1618,21 +1682,32 @@ function useColumnContext() {
1618
1682
  return value;
1619
1683
  }
1620
1684
  function Table(props) {
1621
- const table = useTableState(props);
1685
+ const [table, resetState] = useTableState(props);
1686
+ const [isHydrated, clearStorage] = useTableStateStorage(table);
1687
+ async function reset() {
1688
+ await clearStorage();
1689
+ resetState();
1690
+ }
1622
1691
  react.useLayoutEffect(() => {
1623
1692
  var _a, _b;
1624
1693
  return (_b = (_a = table.getState().props).debugRender) == null ? void 0 : _b.call(_a, "render table");
1625
1694
  });
1626
1695
  return /* @__PURE__ */ jsxRuntime.jsx(TableContext.Provider, {
1627
1696
  value: table,
1628
- children: /* @__PURE__ */ jsxRuntime.jsx(TableMemoContextProvider, {
1629
- children: /* @__PURE__ */ jsxRuntime.jsx(TableLoadingState, {})
1697
+ children: /* @__PURE__ */ jsxRuntime.jsx(TableResetContext.Provider, {
1698
+ value: reset,
1699
+ children: /* @__PURE__ */ jsxRuntime.jsx(TableMemoContextProvider, {
1700
+ children: /* @__PURE__ */ jsxRuntime.jsx(TableLoadingState, {
1701
+ isHydrated
1702
+ })
1703
+ })
1630
1704
  })
1631
1705
  });
1632
1706
  }
1633
- function TableLoadingState() {
1707
+ function TableLoadingState({
1708
+ isHydrated
1709
+ }) {
1634
1710
  const loadingText = useTheme((t) => t.text.loading);
1635
- const isHydrated = useTableStateStorage();
1636
1711
  const [showLoading, setShowLoading] = react.useState(false);
1637
1712
  react.useEffect(() => {
1638
1713
  const handle = setTimeout(() => setShowLoading(true), 500);
@@ -2165,7 +2240,8 @@ const defaultTexts = {
2165
2240
  clearFilters: "Clear all filters",
2166
2241
  reset: "Reset",
2167
2242
  loading: "Loading",
2168
- deselectAll: "Deselect all"
2243
+ deselectAll: "Deselect all",
2244
+ resetAll: "Reset table state"
2169
2245
  };
2170
2246
  const defaultTableTheme = {
2171
2247
  text: defaultTexts,