Skip to content

Commit

Permalink
fix(table): 修复 Safari 中设置 virtual 后只能表头滚动问题 (#2758)
Browse files Browse the repository at this point in the history
  • Loading branch information
zyprepare committed Mar 8, 2024
1 parent 5a002ac commit 68afb78
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/fresh-bananas-double.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hi-ui/table": patch
---

fix: 修复 Safari 中设置 virtual 后只能表头滚动问题
5 changes: 5 additions & 0 deletions .changeset/ninety-colts-heal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hi-ui/hiui": patch
---

fix(table): 修复 Safari 中设置 virtual 后只能表头滚动问题
16 changes: 4 additions & 12 deletions packages/ui/table/src/TableBody.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { forwardRef, useCallback, useMemo, useState } from 'react'
import React, { forwardRef, useCallback, useEffect, useMemo, useState } from 'react'
import VirtualList from 'rc-virtual-list'
import { cx, getPrefixCls } from '@hi-ui/classname'
import { __DEV__ } from '@hi-ui/env'
Expand Down Expand Up @@ -31,6 +31,7 @@ export const TableBody = forwardRef<HTMLDivElement | null, TableBodyProps>(
virtual,
measureRowElementRef,
scrollbar,
scrollLeft,
} = useTableContext()

const cls = cx(`${prefixCls}-body`)
Expand All @@ -49,21 +50,12 @@ export const TableBody = forwardRef<HTMLDivElement | null, TableBodyProps>(
}
)

const [scrollLeft, setScrollLeft] = useState(0)
const rowWidth = useMemo(() => {
let tmpWidth = 0
colWidths.forEach((width) => (tmpWidth += width))
return tmpWidth
}, [colWidths])

const onVirtualContainerScroll = useCallback(
(evt: any) => {
setScrollLeft(scrollBodyElementRef?.current?.scrollLeft || 0)
onTableBodyScroll(evt)
},
[scrollBodyElementRef, onTableBodyScroll]
)

if (virtual) {
// TODO: avg和summay row的逻辑

Expand All @@ -78,7 +70,7 @@ export const TableBody = forwardRef<HTMLDivElement | null, TableBodyProps>(
<div
ref={scrollBodyElementRef}
className={cls}
onScroll={onVirtualContainerScroll}
onScroll={onTableBodyScroll}
onWheel={onTableBodyScrollMock}
style={{
// 表格宽度大于div宽度才出现横向滚动条
Expand All @@ -92,7 +84,7 @@ export const TableBody = forwardRef<HTMLDivElement | null, TableBodyProps>(
></div>
<div
ref={bodyTableRef}
style={{ height: 1, marginTop: -1, background: 'transparent', width: rowWidth }}
style={{ height: 2, marginTop: -1, background: 'transparent', width: rowWidth }}
></div>
{isArrayNonEmpty(transitionData) ? (
<div style={{ width: '100%', position: 'sticky', left: 0 }}>
Expand Down

0 comments on commit 68afb78

Please sign in to comment.