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