schummar-table 0.10.5 → 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)), []);
@@ -596,7 +614,11 @@ function syncSelections(table) {
596
614
  }, { runNow: true }), [table]);
597
615
  }
598
616
  function useTableState(_props) {
617
+ const [key, setKey] = react.useState({});
599
618
  const props = calcProps(_props);
619
+ function reset() {
620
+ setKey({});
621
+ }
600
622
  const state = react.useMemo(() => {
601
623
  var _a, _b, _c, _d;
602
624
  return new react$1.Store({
@@ -618,7 +640,7 @@ function useTableState(_props) {
618
640
  activeItemsById: /* @__PURE__ */ new Map(),
619
641
  lastSelectedId: void 0
620
642
  });
621
- }, []);
643
+ }, [key]);
622
644
  filterColumns(state);
623
645
  calcItems(state);
624
646
  normalizeExpanded(state);
@@ -643,7 +665,7 @@ function useTableState(_props) {
643
665
  }
644
666
  });
645
667
  }, [state, props]);
646
- return state;
668
+ return [state, reset];
647
669
  }
648
670
  function useCssVariables() {
649
671
  return useTheme(({ spacing, colors }) => ({
@@ -1094,10 +1116,13 @@ function ColumnSelection() {
1094
1116
  const IconButton2 = useTheme((t) => t.components.IconButton);
1095
1117
  const Popover = useTheme((t) => t.components.Popover);
1096
1118
  const Checkbox2 = useTheme((t) => t.components.Checkbox);
1119
+ const Button2 = useTheme((t) => t.components.Button);
1097
1120
  const Settings2 = useTheme((t) => t.icons.Settings);
1098
1121
  const selectColumns = useTheme((t) => t.text.selectColumns);
1122
+ const resetAll = useTheme((t) => t.text.resetAll);
1099
1123
  const cssVariables = useCssVariables();
1100
1124
  const table = useTableContext();
1125
+ const reset = react.useContext(TableResetContext);
1101
1126
  const columns = table.useState("props.columns");
1102
1127
  const hiddenColumns = table.useState("hiddenColumns");
1103
1128
  const [anchorEl, setAnchorEl] = react.useState(null);
@@ -1146,7 +1171,11 @@ function ColumnSelection() {
1146
1171
  disabled: column.cannotHide
1147
1172
  }),
1148
1173
  label: column.header
1149
- }, column.id))]
1174
+ }, column.id)), /* @__PURE__ */ jsxRuntime.jsx(Button2, {
1175
+ variant: "outlined",
1176
+ onClick: reset,
1177
+ children: resetAll
1178
+ })]
1150
1179
  })
1151
1180
  })]
1152
1181
  });
@@ -1638,6 +1667,7 @@ function Virtualized(_c) {
1638
1667
  }));
1639
1668
  }
1640
1669
  const TableContext = react.createContext(null);
1670
+ const TableResetContext = react.createContext(() => void 0);
1641
1671
  const ColumnContext = react.createContext(null);
1642
1672
  function useTableContext() {
1643
1673
  const value = react.useContext(TableContext);
@@ -1652,21 +1682,32 @@ function useColumnContext() {
1652
1682
  return value;
1653
1683
  }
1654
1684
  function Table(props) {
1655
- 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
+ }
1656
1691
  react.useLayoutEffect(() => {
1657
1692
  var _a, _b;
1658
1693
  return (_b = (_a = table.getState().props).debugRender) == null ? void 0 : _b.call(_a, "render table");
1659
1694
  });
1660
1695
  return /* @__PURE__ */ jsxRuntime.jsx(TableContext.Provider, {
1661
1696
  value: table,
1662
- children: /* @__PURE__ */ jsxRuntime.jsx(TableMemoContextProvider, {
1663
- 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
+ })
1664
1704
  })
1665
1705
  });
1666
1706
  }
1667
- function TableLoadingState() {
1707
+ function TableLoadingState({
1708
+ isHydrated
1709
+ }) {
1668
1710
  const loadingText = useTheme((t) => t.text.loading);
1669
- const isHydrated = useTableStateStorage();
1670
1711
  const [showLoading, setShowLoading] = react.useState(false);
1671
1712
  react.useEffect(() => {
1672
1713
  const handle = setTimeout(() => setShowLoading(true), 500);
@@ -2199,7 +2240,8 @@ const defaultTexts = {
2199
2240
  clearFilters: "Clear all filters",
2200
2241
  reset: "Reset",
2201
2242
  loading: "Loading",
2202
- deselectAll: "Deselect all"
2243
+ deselectAll: "Deselect all",
2244
+ resetAll: "Reset table state"
2203
2245
  };
2204
2246
  const defaultTableTheme = {
2205
2247
  text: defaultTexts,