element-plus
Version:
A Component Library for Vue 3
1 lines • 5.69 kB
Source Map (JSON)
{"version":3,"file":"index.mjs","names":[],"sources":["../../../../../../../packages/components/table/src/table-body/index.ts"],"sourcesContent":["import {\n defineComponent,\n getCurrentInstance,\n h,\n inject,\n onUnmounted,\n watch,\n} from 'vue'\nimport { addClass, isClient, rAF, removeClass } from '@element-plus/utils'\nimport { useNamespace } from '@element-plus/hooks'\nimport useLayoutObserver from '../layout-observer'\nimport { removePopper } from '../util'\nimport { TABLE_INJECTION_KEY } from '../tokens'\nimport useRender from './render-helper'\nimport defaultProps from './defaults'\n\nimport type { VNode } from 'vue'\n\nexport default defineComponent({\n name: 'ElTableBody',\n props: defaultProps,\n setup(props) {\n const instance = getCurrentInstance()\n const parent = inject(TABLE_INJECTION_KEY)\n const ns = useNamespace('table')\n const { wrappedRowRender, tooltipContent, tooltipTrigger } =\n useRender(props)\n const { onColumnsChange, onScrollableChange } = useLayoutObserver(parent!)\n\n const hoveredCellList: HTMLTableCellElement[] = []\n watch(props.store?.states.hoverRow, (newVal: any, oldVal: any) => {\n const el = instance?.vnode.el as HTMLElement\n const rows = Array.from(el?.children || []).filter((e) =>\n e?.classList.contains(`${ns.e('row')}`)\n )\n\n // hover rowSpan > 1 choose the whole row\n let rowNum = newVal\n const childNodes = rows[rowNum]\n ?.childNodes as NodeListOf<HTMLTableCellElement>\n if (childNodes?.length) {\n let control = 0\n const indexes = Array.from(childNodes).reduce((acc, item, index) => {\n // drop colsSpan\n if (childNodes[index]?.colSpan > 1) {\n control = childNodes[index]?.colSpan\n }\n if (item.nodeName !== 'TD' && control === 0) {\n acc.push(index)\n }\n control > 0 && control--\n return acc\n }, [] as number[])\n\n indexes.forEach((rowIndex) => {\n rowNum = newVal\n while (rowNum > 0) {\n // find from previous\n const preChildNodes = rows[rowNum - 1]\n ?.childNodes as NodeListOf<HTMLTableCellElement>\n\n if (\n preChildNodes[rowIndex] &&\n preChildNodes[rowIndex].nodeName === 'TD' &&\n preChildNodes[rowIndex].rowSpan > 1\n ) {\n addClass(preChildNodes[rowIndex], 'hover-cell')\n hoveredCellList.push(preChildNodes[rowIndex])\n break\n }\n rowNum--\n }\n })\n } else {\n hoveredCellList.forEach((item) => removeClass(item, 'hover-cell'))\n hoveredCellList.length = 0\n }\n if (!props.store?.states.isComplex.value || !isClient) return\n\n rAF(() => {\n // just get first level children; fix #9723\n const oldRow = rows[oldVal]\n const newRow = rows[newVal]\n // when there is fixed row, hover on rowSpan > 1 should not clear the class\n if (oldRow && !oldRow.classList.contains('hover-fixed-row')) {\n removeClass(oldRow, 'hover-row')\n }\n if (newRow) {\n addClass(newRow, 'hover-row')\n }\n })\n })\n\n onUnmounted(() => {\n removePopper?.()\n })\n\n return {\n ns,\n onColumnsChange,\n onScrollableChange,\n wrappedRowRender,\n tooltipContent,\n tooltipTrigger,\n }\n },\n render() {\n const { wrappedRowRender, store } = this\n const data = store?.states.data.value || []\n // Why do we need tabIndex: -1 ?\n // If you set the tabindex attribute on an element ,\n // then its child content cannot be scrolled with the arrow keys,\n // unless you set tabindex on the content too\n // See https://github.com/facebook/react/issues/25462#issuecomment-1274775248 or https://developer.mozilla.org/zh-CN/docs/Web/HTML/Global_attributes/tabindex\n return h('tbody', { tabIndex: -1 }, [\n data.reduce((acc: VNode[], row) => {\n return acc.concat(wrappedRowRender(row, acc.length) as VNode[])\n }, []),\n ])\n },\n})\n"],"mappings":";;;;;;;;;;;AAkBA,IAAA,qBAAe,gBAAgB;CAC7B,MAAM;CACN,OAAO;CACP,MAAM,OAAO;EACX,MAAM,WAAW,oBAAoB;EACrC,MAAM,SAAS,OAAO,oBAAoB;EAC1C,MAAM,KAAK,aAAa,QAAQ;EAChC,MAAM,EAAE,kBAAkB,gBAAgB,mBACxC,UAAU,MAAM;EAClB,MAAM,EAAE,iBAAiB,uBAAuB,kBAAkB,OAAQ;EAE1E,MAAM,kBAA0C,EAAE;EAClD,MAAM,MAAM,OAAO,OAAO,WAAW,QAAa,WAAgB;GAChE,MAAM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