element-plus
Version:
A Component Library for Vue 3
1 lines • 2.98 kB
Source Map (JSON)
{"version":3,"file":"use-data.mjs","names":[],"sources":["../../../../../../../packages/components/table-v2/src/composables/use-data.ts"],"sourcesContent":["import { computed, ref, unref, watch } from 'vue'\nimport { isArray } from '@element-plus/utils'\n\nimport type { TableV2Props } from '../table'\nimport type { KeyType } from '../types'\nimport type { UseRowReturn } from './use-row'\n\ntype UseDataProps = {\n expandedRowKeys: UseRowReturn['expandedRowKeys']\n lastRenderedRowIndex: UseRowReturn['lastRenderedRowIndex']\n resetAfterIndex: UseRowReturn['resetAfterIndex']\n}\n\nexport const useData = (\n props: TableV2Props,\n { expandedRowKeys, lastRenderedRowIndex, resetAfterIndex }: UseDataProps\n) => {\n const depthMap = ref<Record<KeyType, number>>({})\n\n const flattenedData = computed(() => {\n const depths: Record<KeyType, number> = {}\n const { data, rowKey } = props\n\n const _expandedRowKeys = unref(expandedRowKeys)\n\n if (!_expandedRowKeys || !_expandedRowKeys.length) return data\n\n const array: any[] = []\n const keysSet = new Set()\n _expandedRowKeys.forEach((x) => keysSet.add(x))\n\n let copy: any[] = data.slice()\n copy.forEach((x) => (depths[x[rowKey]] = 0))\n while (copy.length > 0) {\n const item = copy.shift()!\n\n array.push(item)\n if (\n keysSet.has(item[rowKey]) &&\n isArray(item.children) &&\n item.children.length > 0\n ) {\n copy = [...item.children, ...copy]\n item.children.forEach(\n (child: any) => (depths[child[rowKey]] = depths[item[rowKey]] + 1)\n )\n }\n }\n\n depthMap.value = depths\n return array\n })\n\n const data = computed(() => {\n const { data, expandColumnKey } = props\n return expandColumnKey ? unref(flattenedData) : data\n })\n\n watch(data, (val, prev) => {\n if (val !== prev) {\n lastRenderedRowIndex.value = -1\n resetAfterIndex(0, true)\n }\n })\n\n return {\n data,\n depthMap,\n }\n}\n\nexport type UseDataReturn = ReturnType<typeof useData>\n"],"mappings":";;;;AAaA,MAAa,WACX,OACA,EAAE,iBAAiB,sBAAsB,sBACtC;CACH,MAAM,WAAW,IAA6B,EAAE,CAAC;CAEjD,MAAM,gBAAgB,eAAe;EACnC,MAAM,SAAkC,EAAE;EAC1C,MAAM,EAAE,MAAM,WAAW;EAEzB,MAAM,mBAAmB,MAAM,gBAAgB;AAE/C,MAAI,CAAC,oBAAoB,CAAC,iBAAiB,OAAQ,QAAO;EAE1D,MAAM,QAAe,EAAE;EACvB,MAAM,0BAAU,IAAI,KAAK;AACzB,mBAAiB,SAAS,MAAM,QAAQ,IAAI,EAAE,CAAC;EAE/C,IAAI,OAAc,KAAK,OAAO;AAC9B,OAAK,SAAS,MAAO,OAAO,EAAE,WAAW,EAAG;AAC5C,SAAO,KAAK,SAAS,GAAG;GACtB,MAAM,OAAO,KAAK,OAAO;AAEzB,SAAM,KAAK,KAAK;AAChB,OACE,QAAQ,IAAI,KAAK,QAAQ,IACzB,QAAQ,KAAK,SAAS,IACtB,KAAK,SAAS,SAAS,GACvB;AACA,WAAO,CAAC,GAAG,KAAK,UAAU,GAAG,KAAK;AAClC,SAAK,SAAS,SACX,UAAgB,OAAO,MAAM,WAAW,OAAO,KAAK,WAAW,EACjE;;;AAIL,WAAS,QAAQ;AACjB,SAAO;GACP;CAEF,MAAM,OAAO,eAAe;EAC1B,MAAM,EAAE,MAAM,oBAAoB;AAClC,SAAO,kBAAkB,MAAM,cAAc,GAAG;GAChD;AAEF,OAAM,OAAO,KAAK,SAAS;AACzB,MAAI,QAAQ,MAAM;AAChB,wBAAqB,QAAQ;AAC7B,mBAAgB,GAAG,KAAK;;GAE1B;AAEF,QAAO;EACL;EACA;EACD"}