schummar-table 1.0.0-beta.5 → 1.0.0-beta.7

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/index.cjs CHANGED
@@ -269,42 +269,63 @@ function useColumns(props, displaySize) {
269
269
  //#region src/state/useExpanded.ts
270
270
  function useExpanded(props, itemsById, matching) {
271
271
  const [value, setExpanded, setExpandedInternal] = useControllableState(props.expanded, () => props.defaultExpanded ?? /* @__PURE__ */ new Set(), props.onExpandedChange);
272
- return {
273
- expanded: useNormalized(value, (0, react.useMemo)(() => {
274
- let result = value;
275
- const add = (id) => {
276
- if (result.has(id)) return;
272
+ const expandOnlyOne = props.expandOnlyOne && !(props.revealFiltered && matching);
273
+ const expanded = useNormalized(value, (0, react.useMemo)(() => {
274
+ let result = value;
275
+ const add = (id) => {
276
+ if (result.has(id)) return;
277
+ if (result === value) result = new Set(value);
278
+ result.add(id);
279
+ };
280
+ if (expandOnlyOne && value.size > 1) {
281
+ const deepest = [...value].map((id) => itemsById.get(id)).filter((item) => item !== void 0).sort((a, b) => b.depth - a.depth)[0];
282
+ const branch = deepest ? require_useTheme.getAncestors(itemsById, deepest).add(deepest.id) : /* @__PURE__ */ new Set();
283
+ if ([...value].some((id) => !branch.has(id))) return branch;
284
+ }
285
+ for (const id of value) {
286
+ const item = itemsById.get(id);
287
+ if (item) require_useTheme.getAncestors(itemsById, item).forEach(add);
288
+ }
289
+ if (props.items !== void 0) {
290
+ for (const id of value) if (!itemsById.has(id)) {
277
291
  if (result === value) result = new Set(value);
278
- result.add(id);
279
- };
280
- if (props.expandOnlyOne && value.size > 1) {
281
- const deepest = [...value].map((id) => itemsById.get(id)).filter((item) => item !== void 0).sort((a, b) => b.depth - a.depth)[0];
282
- const branch = deepest ? require_useTheme.getAncestors(itemsById, deepest).add(deepest.id) : /* @__PURE__ */ new Set();
283
- if ([...value].some((id) => !branch.has(id))) return branch;
284
- }
285
- for (const id of value) {
286
- const item = itemsById.get(id);
287
- if (item) require_useTheme.getAncestors(itemsById, item).forEach(add);
288
- }
289
- if (props.items !== void 0) {
290
- for (const id of value) if (!itemsById.has(id)) {
291
- if (result === value) result = new Set(value);
292
- result.delete(id);
293
- }
292
+ result.delete(id);
294
293
  }
295
- if (props.revealFiltered && matching) for (const id of matching) {
296
- const parentId = itemsById.get(id)?.parentId;
297
- if (parentId !== void 0 && parentId !== null) add(parentId);
298
- }
299
- return result;
300
- }, [
301
- value,
302
- itemsById,
303
- matching,
304
- props.items,
305
- props.expandOnlyOne,
306
- props.revealFiltered
307
- ]), setExpanded),
294
+ }
295
+ return result;
296
+ }, [
297
+ value,
298
+ itemsById,
299
+ props.items,
300
+ expandOnlyOne
301
+ ]), setExpanded);
302
+ const latest = (0, react.useRef)({
303
+ expanded,
304
+ itemsById
305
+ });
306
+ (0, react.useEffect)(() => {
307
+ latest.current = {
308
+ expanded,
309
+ itemsById
310
+ };
311
+ });
312
+ (0, react.useEffect)(() => {
313
+ if (!props.revealFiltered || !matching) return;
314
+ const { expanded, itemsById } = latest.current;
315
+ const next = new Set(expanded);
316
+ for (const id of matching) {
317
+ const parentId = itemsById.get(id)?.parentId;
318
+ if (parentId !== void 0 && parentId !== null) next.add(parentId);
319
+ }
320
+ if (next.size > expanded.size) setExpanded(next);
321
+ }, [
322
+ matching,
323
+ props.revealFiltered,
324
+ setExpanded
325
+ ]);
326
+ return {
327
+ expanded,
328
+ expandOnlyOne,
308
329
  setExpanded,
309
330
  setExpandedInternal
310
331
  };
@@ -898,11 +919,11 @@ function useTable(raw, onReset, isReset = false) {
898
919
  setExpanded: (value) => get().expanded.setExpanded(value),
899
920
  toggleExpanded(itemId) {
900
921
  const { state, expanded } = get();
901
- const { activeItemsById, props } = state;
922
+ const { activeItemsById } = state;
902
923
  const item = activeItemsById.get(itemId);
903
924
  const isExpanded = state.expanded.has(itemId);
904
925
  const next = new Set(state.expanded);
905
- if (props.expandOnlyOne) {
926
+ if (expanded.expandOnlyOne) {
906
927
  next.clear();
907
928
  for (const id of item ? require_useTheme.getAncestors(activeItemsById, item) : []) next.add(id);
908
929
  }