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/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)), []);
@@ -594,7 +612,11 @@ function syncSelections(table) {
594
612
  }, { runNow: true }), [table]);
595
613
  }
596
614
  function useTableState(_props) {
615
+ const [key, setKey] = useState({});
597
616
  const props = calcProps(_props);
617
+ function reset() {
618
+ setKey({});
619
+ }
598
620
  const state = useMemo(() => {
599
621
  var _a, _b, _c, _d;
600
622
  return new Store({
@@ -616,7 +638,7 @@ function useTableState(_props) {
616
638
  activeItemsById: /* @__PURE__ */ new Map(),
617
639
  lastSelectedId: void 0
618
640
  });
619
- }, []);
641
+ }, [key]);
620
642
  filterColumns(state);
621
643
  calcItems(state);
622
644
  normalizeExpanded(state);
@@ -641,7 +663,7 @@ function useTableState(_props) {
641
663
  }
642
664
  });
643
665
  }, [state, props]);
644
- return state;
666
+ return [state, reset];
645
667
  }
646
668
  function useCssVariables() {
647
669
  return useTheme(({ spacing, colors }) => ({
@@ -1092,10 +1114,13 @@ function ColumnSelection() {
1092
1114
  const IconButton2 = useTheme((t) => t.components.IconButton);
1093
1115
  const Popover2 = useTheme((t) => t.components.Popover);
1094
1116
  const Checkbox2 = useTheme((t) => t.components.Checkbox);
1117
+ const Button2 = useTheme((t) => t.components.Button);
1095
1118
  const Settings2 = useTheme((t) => t.icons.Settings);
1096
1119
  const selectColumns = useTheme((t) => t.text.selectColumns);
1120
+ const resetAll = useTheme((t) => t.text.resetAll);
1097
1121
  const cssVariables = useCssVariables();
1098
1122
  const table = useTableContext();
1123
+ const reset = useContext(TableResetContext);
1099
1124
  const columns = table.useState("props.columns");
1100
1125
  const hiddenColumns = table.useState("hiddenColumns");
1101
1126
  const [anchorEl, setAnchorEl] = useState(null);
@@ -1144,7 +1169,11 @@ function ColumnSelection() {
1144
1169
  disabled: column.cannotHide
1145
1170
  }),
1146
1171
  label: column.header
1147
- }, column.id))]
1172
+ }, column.id)), /* @__PURE__ */ jsx(Button2, {
1173
+ variant: "outlined",
1174
+ onClick: reset,
1175
+ children: resetAll
1176
+ })]
1148
1177
  })
1149
1178
  })]
1150
1179
  });
@@ -1636,6 +1665,7 @@ function Virtualized(_c) {
1636
1665
  }));
1637
1666
  }
1638
1667
  const TableContext = createContext(null);
1668
+ const TableResetContext = createContext(() => void 0);
1639
1669
  const ColumnContext = createContext(null);
1640
1670
  function useTableContext() {
1641
1671
  const value = useContext(TableContext);
@@ -1650,21 +1680,32 @@ function useColumnContext() {
1650
1680
  return value;
1651
1681
  }
1652
1682
  function Table(props) {
1653
- 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
+ }
1654
1689
  useLayoutEffect(() => {
1655
1690
  var _a, _b;
1656
1691
  return (_b = (_a = table.getState().props).debugRender) == null ? void 0 : _b.call(_a, "render table");
1657
1692
  });
1658
1693
  return /* @__PURE__ */ jsx(TableContext.Provider, {
1659
1694
  value: table,
1660
- children: /* @__PURE__ */ jsx(TableMemoContextProvider, {
1661
- 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
+ })
1662
1702
  })
1663
1703
  });
1664
1704
  }
1665
- function TableLoadingState() {
1705
+ function TableLoadingState({
1706
+ isHydrated
1707
+ }) {
1666
1708
  const loadingText = useTheme((t) => t.text.loading);
1667
- const isHydrated = useTableStateStorage();
1668
1709
  const [showLoading, setShowLoading] = useState(false);
1669
1710
  useEffect(() => {
1670
1711
  const handle = setTimeout(() => setShowLoading(true), 500);
@@ -2197,7 +2238,8 @@ const defaultTexts = {
2197
2238
  clearFilters: "Clear all filters",
2198
2239
  reset: "Reset",
2199
2240
  loading: "Loading",
2200
- deselectAll: "Deselect all"
2241
+ deselectAll: "Deselect all",
2242
+ resetAll: "Reset table state"
2201
2243
  };
2202
2244
  const defaultTableTheme = {
2203
2245
  text: defaultTexts,