{"version":3,"file":"table-grid.js","sources":["../../../../../../packages/components/table-v2/src/table-grid.tsx"],"sourcesContent":["import { computed, defineComponent, inject, ref, unref } from 'vue'\nimport {\n DynamicSizeGrid,\n FixedSizeGrid,\n} from '@element-plus/components/virtual-list'\nimport { isNumber, isObject } from '@element-plus/utils'\nimport { Header } from './components'\nimport { TableV2InjectionKey } from './tokens'\nimport { tableV2GridProps } from './grid'\nimport { sum } from './utils'\n\nimport type { UnwrapRef } from 'vue'\nimport type {\n DynamicSizeGridInstance,\n GridDefaultSlotParams,\n GridItemKeyGetter,\n GridItemRenderedEvtParams,\n GridScrollOptions,\n ResetAfterIndex,\n Alignment as ScrollStrategy,\n} from '@element-plus/components/virtual-list'\nimport type { TableV2HeaderInstance } from './components'\nimport type { TableV2GridProps } from './grid'\n\nconst COMPONENT_NAME = 'ElTableV2Grid'\n\nconst useTableGrid = (props: TableV2GridProps) => {\n const headerRef = ref()\n const bodyRef = ref()\n\n const totalHeight = computed(() => {\n const { data, rowHeight, estimatedRowHeight } = props\n\n if (estimatedRowHeight) {\n return\n }\n\n return data.length * (rowHeight as number)\n })\n\n const fixedRowHeight = computed(() => {\n const { fixedData, rowHeight } = props\n\n return (fixedData?.length || 0) * (rowHeight as number)\n })\n\n const headerHeight = computed(() => sum(props.headerHeight))\n\n const gridHeight = computed(() => {\n const { height } = props\n return Math.max(0, height - unref(headerHeight) - unref(fixedRowHeight))\n })\n\n const hasHeader = computed(() => {\n return unref(headerHeight) + unref(fixedRowHeight) > 0\n })\n\n const itemKey: GridItemKeyGetter = ({ data, rowIndex }) =>\n data[rowIndex][props.rowKey]\n\n function onItemRendered({\n rowCacheStart,\n rowCacheEnd,\n rowVisibleStart,\n rowVisibleEnd,\n }: GridItemRenderedEvtParams) {\n props.onRowsRendered?.({\n rowCacheStart,\n rowCacheEnd,\n rowVisibleStart,\n rowVisibleEnd,\n })\n }\n\n function resetAfterRowIndex(index: number, forceUpdate: boolean) {\n bodyRef.value?.resetAfterRowIndex(index, forceUpdate)\n }\n\n function scrollTo(x: number, y: number): void\n function scrollTo(options: GridScrollOptions): void\n function scrollTo(leftOrOptions: number | GridScrollOptions, top?: number) {\n const header$ = unref(headerRef)\n const body$ = unref(bodyRef)\n\n if (isObject(leftOrOptions)) {\n header$?.scrollToLeft(leftOrOptions.scrollLeft)\n body$?.scrollTo(leftOrOptions)\n } else {\n header$?.scrollToLeft(leftOrOptions)\n body$?.scrollTo({\n scrollLeft: leftOrOptions,\n scrollTop: top,\n })\n }\n }\n\n function scrollToTop(scrollTop: number) {\n unref(bodyRef)?.scrollTo({\n scrollTop,\n })\n }\n\n function scrollToRow(row: number, strategy: ScrollStrategy) {\n unref(bodyRef)?.scrollToItem(row, 1, strategy)\n }\n\n function forceUpdate() {\n unref(bodyRef)?.$forceUpdate()\n unref(headerRef)?.$forceUpdate()\n }\n\n return {\n bodyRef,\n forceUpdate,\n fixedRowHeight,\n gridHeight,\n hasHeader,\n headerHeight,\n headerRef,\n totalHeight,\n\n itemKey,\n onItemRendered,\n resetAfterRowIndex,\n scrollTo,\n scrollToTop,\n scrollToRow,\n }\n}\n\nconst TableGrid = defineComponent({\n name: COMPONENT_NAME,\n props: tableV2GridProps,\n setup(props, { slots, expose }) {\n const { ns } = inject(TableV2InjectionKey)!\n\n const {\n bodyRef,\n fixedRowHeight,\n gridHeight,\n hasHeader,\n headerRef,\n headerHeight,\n totalHeight,\n\n forceUpdate,\n itemKey,\n onItemRendered,\n resetAfterRowIndex,\n scrollTo,\n scrollToTop,\n scrollToRow,\n } = useTableGrid(props)\n\n expose({\n forceUpdate,\n /**\n * @description fetch total height\n */\n totalHeight,\n /**\n * @description scroll to a position\n */\n scrollTo,\n /**\n * @description scroll vertically to position y\n */\n scrollToTop,\n /**\n * @description scroll to a given row\n * @params row {Number} which row to scroll to\n * @params strategy {ScrollStrategy} use what strategy to scroll to\n */\n scrollToRow,\n /**\n * @description reset rendered state after row index\n */\n resetAfterRowIndex,\n })\n\n const getColumnWidth = () => props.bodyWidth\n\n return () => {\n const {\n cache,\n columns,\n data,\n fixedData,\n useIsScrolling,\n scrollbarAlwaysOn,\n scrollbarEndGap,\n scrollbarStartGap,\n style,\n rowHeight,\n bodyWidth,\n estimatedRowHeight,\n headerWidth,\n height,\n width,\n\n getRowHeight,\n onScroll,\n } = props\n\n const isDynamicRowEnabled = isNumber(estimatedRowHeight)\n const Grid = isDynamicRowEnabled ? DynamicSizeGrid : FixedSizeGrid\n const _headerHeight = unref(headerHeight)\n\n return (\n
\n \n {{\n default: (params: GridDefaultSlotParams) => {\n const rowData = data[params.rowIndex]\n return slots.row?.({\n ...params,\n columns,\n rowData,\n })\n },\n }}\n \n {unref(hasHeader) && (\n \n {{\n dynamic: slots.header,\n fixed: slots.row,\n }}\n \n )}\n
\n )\n }\n },\n})\n\nexport default TableGrid\n\nexport type TableGridRowSlotParams = {\n columns: TableV2GridProps['columns']\n rowData: any\n} & GridDefaultSlotParams\n\nexport type TableGridInstance = InstanceType &\n UnwrapRef<{\n forceUpdate: () => void\n /**\n * @description fetch total height\n */\n totalHeight: number\n\n /**\n * @description scrollTo a position\n * @param { number | ScrollToOptions } arg1\n * @param { number } arg2\n */\n scrollTo(leftOrOptions: number | GridScrollOptions, top?: number): void\n\n /**\n * @description scroll vertically to position y\n */\n scrollToTop(scrollTop: number): void\n /**\n * @description scroll to a given row\n * @params row {Number} which row to scroll to\n * @params @optional strategy {ScrollStrategy} use what strategy to scroll to\n */\n scrollToRow(row: number, strategy: ScrollStrategy): void\n /**\n * @description reset rendered state after row index\n * @param { number } rowIndex\n * @param { boolean } forceUpdate\n */\n resetAfterRowIndex: ResetAfterIndex\n }>\n"],"names":["COMPONENT_NAME","useTableGrid","props","headerRef","ref","bodyRef","computed","data","rowHeight","estimatedRowHeight","sum","headerHeight","gridHeight","unref","height","Math","max","hasHeader","itemKey","rowIndex","rowCacheStart","rowCacheEnd","rowVisibleEnd","forceUpdate","isObject","scrollTo","header$","scrollToLeft","scrollLeft","scrollTop","top","scrollToTop","fixedRowHeight","totalHeight","defineComponent","tableV2GridProps","scrollToRow","TableGrid","name","TableV2InjectionKey","expose","ns","inject","onItemRendered","resetAfterRowIndex","isNumber","DynamicSizeGrid","FixedSizeGrid","_createVNode","cache","columns","fixedData","useIsScrolling","scrollbarAlwaysOn","scrollbarEndGap","scrollbarStartGap","style","bodyWidth","headerWidth","width","onScroll","isDynamicRowEnabled","Grid","_headerHeight","Header","e","class"],"mappings":";;;;;;;;;;;;;;;;;AAwBA,MAAMA,cAAc,GAAG,eAAvB,CAAA;;AAEA,EAAMC,MAAAA,SAAAA,GAAgBC,OAAAA,EAAAA,CAAD;EACnB,MAAMC,OAAAA,GAAYC,OAAAA,EAAG,CAArB;EACA,MAAMC,WAAUD,GAAhBE,YAAA,CAAA,MAAA;AAEA,IAAA,MAAiB;MACT,IAAA;MAAEC,SAAF;MAAQC,kBAAR;AAAmBC,KAAAA,GAAAA,KAAAA,CAAAA;AAAnB,IAAA,IAA0CP,kBAAhD,EAAA;;AAEA,KAAA;AACE,IAAA,OAAA,IAAA,CAAA,MAAA,GAAA,SAAA,CAAA;AACD,GAAA,CAAA,CAAA;;AAED,IAAA,MAAA;AACD,MARD,SAAA;AAUA,MAAA,SAAoB;KACZ,GAAA,KAAA,CAAA;WAAA,CAAA,CAAA,SAAA,IAAA,IAAA,GAAA,KAAA,CAAA,GAAA,SAAA,CAAA,MAAA,KAAA,CAAA,IAAA,SAAA,CAAA;AAAaM,GAAAA,CAAAA,CAAAA;AAAb,EAAA,MAA2BN,YAAjC,GAAAI,YAAA,CAAA,MAAAI,SAAA,CAAA,KAAA,CAAA,YAAA,CAAA,CAAA,CAAA;AAEA,EAAA,MAAA,UAAiB,GAAAJ,YAAT,CAAqB;AAC9B,IAJD,MAAA;MAMMK,MAAAA;AAEN,KAAA,GAAMC,KAAU,CAAA;IACd,OAAM,IAAA,CAAA,GAAA,CAAA,CAAA,EAAA,MAAA,GAAAC,SAAA,CAAA,YAAA,CAAA,GAAAA,SAAA,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