671c934137e93533477c5b5f0fe81c721b15b1763bba4b53e5b83349e0d457944ee684659dcc482f8059756f9c01679eccaba382d7e4dd1626a727909384c0 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import { ref, computed, unref, watch } from 'vue';
  2. import { isArray } from '@vue/shared';
  3. const useData = (props, { expandedRowKeys, lastRenderedRowIndex, resetAfterIndex }) => {
  4. const depthMap = ref({});
  5. const flattenedData = computed(() => {
  6. const depths = {};
  7. const { data: data2, rowKey } = props;
  8. const _expandedRowKeys = unref(expandedRowKeys);
  9. if (!_expandedRowKeys || !_expandedRowKeys.length)
  10. return data2;
  11. const array = [];
  12. const keysSet = /* @__PURE__ */ new Set();
  13. _expandedRowKeys.forEach((x) => keysSet.add(x));
  14. let copy = data2.slice();
  15. copy.forEach((x) => depths[x[rowKey]] = 0);
  16. while (copy.length > 0) {
  17. const item = copy.shift();
  18. array.push(item);
  19. if (keysSet.has(item[rowKey]) && isArray(item.children) && item.children.length > 0) {
  20. copy = [...item.children, ...copy];
  21. item.children.forEach((child) => depths[child[rowKey]] = depths[item[rowKey]] + 1);
  22. }
  23. }
  24. depthMap.value = depths;
  25. return array;
  26. });
  27. const data = computed(() => {
  28. const { data: data2, expandColumnKey } = props;
  29. return expandColumnKey ? unref(flattenedData) : data2;
  30. });
  31. watch(data, (val, prev) => {
  32. if (val !== prev) {
  33. lastRenderedRowIndex.value = -1;
  34. resetAfterIndex(0, true);
  35. }
  36. });
  37. return {
  38. data,
  39. depthMap
  40. };
  41. };
  42. export { useData };
  43. //# sourceMappingURL=use-data.mjs.map