|
@@ -5,6 +5,7 @@ import { useState, useMemo } from 'react'
|
|
|
import { useDebounceFn, useUpdateLayoutEffect } from 'ahooks'
|
|
import { useDebounceFn, useUpdateLayoutEffect } from 'ahooks'
|
|
|
import { getViewportOffset } from '@/utils/domUtils'
|
|
import { getViewportOffset } from '@/utils/domUtils'
|
|
|
import { useWindowSizeFn } from '@/hooks/event/useWindomSizeFn'
|
|
import { useWindowSizeFn } from '@/hooks/event/useWindomSizeFn'
|
|
|
|
|
+import type { DensitySize } from '@ant-design/pro-table/lib/components/ToolBar/DensityIcon'
|
|
|
|
|
|
|
|
const MutationObserver =
|
|
const MutationObserver =
|
|
|
window.MutationObserver || window.webkitMutationObserver || window.MozMutationObserver
|
|
window.MutationObserver || window.webkitMutationObserver || window.MozMutationObserver
|
|
@@ -16,19 +17,10 @@ export function useTableScroll(
|
|
|
// let bodyEl: HTMLElement | null
|
|
// let bodyEl: HTMLElement | null
|
|
|
const [tableHeight, setTableHeight] = useState(null)
|
|
const [tableHeight, setTableHeight] = useState(null)
|
|
|
|
|
|
|
|
- function calcTableHeight(dataSource: any[]) {
|
|
|
|
|
- const tableEl = tableElRef.current?.querySelector('.ant-table-container')
|
|
|
|
|
|
|
+ function calcTableHeight(dataSource: any[], size: DensitySize) {
|
|
|
|
|
+ const tableEl = tableElRef.current?.querySelector('.ant-table-wrapper')
|
|
|
if (!tableEl) return
|
|
if (!tableEl) return
|
|
|
|
|
|
|
|
- // if (!bodyEl) {
|
|
|
|
|
- // bodyEl = tableEl?.querySelector('.ant-table-tbody')
|
|
|
|
|
- // console.log('bodyEl', tableEl.children)
|
|
|
|
|
-
|
|
|
|
|
- // if (!bodyEl) return
|
|
|
|
|
- // }
|
|
|
|
|
- // bodyEl!.style.height = 'unset'
|
|
|
|
|
- // Add a delay to get the correct bottomIncludeBody paginationHeight footerHeight headerHeight
|
|
|
|
|
-
|
|
|
|
|
const headEl = tableEl.querySelector('.ant-table-thead ')
|
|
const headEl = tableEl.querySelector('.ant-table-thead ')
|
|
|
if (!headEl) return
|
|
if (!headEl) return
|
|
|
// Table height from bottom
|
|
// Table height from bottom
|
|
@@ -38,18 +30,43 @@ export function useTableScroll(
|
|
|
|
|
|
|
|
let paginationHeight = 0
|
|
let paginationHeight = 0
|
|
|
if (dataSource && dataSource?.length) {
|
|
if (dataSource && dataSource?.length) {
|
|
|
- paginationHeight += 56
|
|
|
|
|
|
|
+ paginationHeight += 32
|
|
|
|
|
+ if (size) {
|
|
|
|
|
+ switch (size) {
|
|
|
|
|
+ case 'large':
|
|
|
|
|
+ paginationHeight += 32
|
|
|
|
|
+ break
|
|
|
|
|
+ case 'small':
|
|
|
|
|
+ paginationHeight += 24
|
|
|
|
|
+ break
|
|
|
|
|
+
|
|
|
|
|
+ default:
|
|
|
|
|
+ paginationHeight += 24
|
|
|
|
|
+ break
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
let headerHeight = 0
|
|
let headerHeight = 0
|
|
|
- if (headEl) {
|
|
|
|
|
- headerHeight = headEl.offsetHeight
|
|
|
|
|
|
|
+ if (size) {
|
|
|
|
|
+ switch (size) {
|
|
|
|
|
+ case 'large':
|
|
|
|
|
+ headerHeight += 55
|
|
|
|
|
+ break
|
|
|
|
|
+ case 'small':
|
|
|
|
|
+ headerHeight += 39
|
|
|
|
|
+ break
|
|
|
|
|
+
|
|
|
|
|
+ default:
|
|
|
|
|
+ headerHeight += 47
|
|
|
|
|
+ break
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
const height = bottomIncludeBody - paddingHeight - paginationHeight - headerHeight
|
|
const height = bottomIncludeBody - paddingHeight - paginationHeight - headerHeight
|
|
|
|
|
|
|
|
- setTableHeight(height)
|
|
|
|
|
- // bodyEl!.style.height = `${height}px`
|
|
|
|
|
|
|
+ if (height !== tableHeight) setTableHeight(height)
|
|
|
|
|
+
|
|
|
const mutationObserver = new MutationObserver(() => {
|
|
const mutationObserver = new MutationObserver(() => {
|
|
|
const tbodyEl: HTMLElement =
|
|
const tbodyEl: HTMLElement =
|
|
|
tableElRef.current?.querySelector('.ant-table-container')?.children[1]
|
|
tableElRef.current?.querySelector('.ant-table-container')?.children[1]
|
|
@@ -57,12 +74,14 @@ export function useTableScroll(
|
|
|
})
|
|
})
|
|
|
mutationObserver.observe(tableEl, {
|
|
mutationObserver.observe(tableEl, {
|
|
|
childList: true, // 子节点的变动(新增、删除或者更改)
|
|
childList: true, // 子节点的变动(新增、删除或者更改)
|
|
|
- subtree: true // 是否将观察器应用于该节点的所有后代节点
|
|
|
|
|
|
|
+ // attributes: true, // 目标属性的改变
|
|
|
|
|
+ subtree: true // 将观察器应用于该节点的所有后代节点
|
|
|
|
|
+ // attributeFilter: ['class'] // Array - 观察指定属性
|
|
|
})
|
|
})
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- function redoHeight(dataSource?: any[]) {
|
|
|
|
|
- calcTableHeight(dataSource)
|
|
|
|
|
|
|
+ function redoHeight(dataSource?: any[], size: DensitySize = 'middle') {
|
|
|
|
|
+ calcTableHeight(dataSource, size)
|
|
|
}
|
|
}
|
|
|
const { run: debounceRedoHeight } = useDebounceFn(redoHeight, { wait: 300 })
|
|
const { run: debounceRedoHeight } = useDebounceFn(redoHeight, { wait: 300 })
|
|
|
|
|
|
|
@@ -96,6 +115,8 @@ export function useTableScroll(
|
|
|
}
|
|
}
|
|
|
const tableEl = tableElRef.current
|
|
const tableEl = tableElRef.current
|
|
|
|
|
|
|
|
|
|
+ console.log('tableEl', tableEl?.offsetWidth)
|
|
|
|
|
+
|
|
|
const tableWidth = tableEl && tableEl?.offsetWidth ? tableEl?.offsetWidth : 0
|
|
const tableWidth = tableEl && tableEl?.offsetWidth ? tableEl?.offsetWidth : 0
|
|
|
|
|
|
|
|
return tableWidth > width ? '100%' : width
|
|
return tableWidth > width ? '100%' : width
|