{"version":3,"file":"table-grid.mjs","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","data","rowHeight","estimatedRowHeight","headerHeight","gridHeight","height","Math","max","unref","hasHeader","itemKey","rowIndex","rowCacheStart","rowCacheEnd","rowVisibleEnd","forceUpdate","scrollTo","isObject","header$","scrollToLeft","scrollLeft","scrollTop","top","scrollToTop","fixedRowHeight","totalHeight","scrollToRow","TableGrid","name","expose","ns","inject","TableV2InjectionKey","onItemRendered","resetAfterRowIndex","_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,GAAAA,EAAAA,CAAD;EACnB,MAAMC,OAAAA,GAAYC,GAAAA,EAAG,CAArB;EACA,MAAMC,WAAUD,GAAhB,QAAA,CAAA,MAAA;AAEA,IAAA,MAAiB;MACT,IAAA;MAAEE,SAAF;MAAQC,kBAAR;AAAmBC,KAAAA,GAAAA,KAAAA,CAAAA;AAAnB,IAAA,IAA0CN,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;AAAaK,GAAAA,CAAAA,CAAAA;AAAb,EAAA,MAA2BL,YAAjC,GAAA,QAAA,CAAA,MAAA,GAAA,CAAA,KAAA,CAAA,YAAA,CAAA,CAAA,CAAA;AAEA,EAAA,MAAA,UAAiB,GAAA,QAAT,CAAqB;AAC9B,IAJD,MAAA;MAMMO,MAAAA;AAEN,KAAA,GAAMC,KAAU,CAAA;IACd,OAAM,IAAA,CAAA,GAAA,CAAA,CAAA,EAAA,MAAA,GAAA,KAAA,CAAA,YAAA,CAAA,GAAA,KAAA,CAAA,cAAA,CAAA,CAAA,CAAA;AAAEC,GAAAA,CAAAA,CAAAA;AAAF,EA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