Repository navigation
feat: virtual column culling, scrollbar header align, scrollTo left #1529
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
46a0f8e
23f5ce8
07ad29f
a7c25a2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,6 +10,7 @@ import type { | |
| VirtualScrollConfig, | ||
| } from '../interface'; | ||
| import BodyLine from './BodyLine'; | ||
| import { getVisibleColumnRange } from './colVirtual'; | ||
| import { GridContext, StaticContext } from './context'; | ||
|
|
||
| const ALIGN_MAP: Record<string, 'top' | 'bottom' | 'auto'> = { | ||
|
|
@@ -41,6 +42,7 @@ const Grid = React.forwardRef<GridRef, GridProps>((props, ref) => { | |
| childrenColumnName, | ||
| scrollX, | ||
| direction, | ||
| componentWidth, | ||
| } = useContext(TableContext, [ | ||
| 'flattenColumns', | ||
| 'onColumnResize', | ||
|
|
@@ -50,6 +52,7 @@ const Grid = React.forwardRef<GridRef, GridProps>((props, ref) => { | |
| 'childrenColumnName', | ||
| 'scrollX', | ||
| 'direction', | ||
| 'componentWidth', | ||
| ]); | ||
|
|
||
| const { | ||
|
|
@@ -81,6 +84,13 @@ const Grid = React.forwardRef<GridRef, GridProps>((props, ref) => { | |
| [columnsWidth], | ||
| ); | ||
|
|
||
| const [offsetX, setOffsetX] = React.useState(0); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 程序化水平滚动也必须更新 在允许的 例如,20个100px列、300px视口从0滚动到1000后,列表已经移动,但这里仍按0计算范围。目标视口会显示占位而不是单元格。请同步
|
||
|
|
||
| const { startCol: colStart, endCol: colEnd } = React.useMemo( | ||
| () => getVisibleColumnRange(columnsOffset, offsetX, componentWidth || 0), | ||
| [columnsOffset, offsetX, componentWidth], | ||
| ); | ||
|
|
||
| React.useEffect(() => { | ||
| columnsWidth.forEach(([key, width]) => { | ||
| onColumnResize(key, width); | ||
|
|
@@ -89,6 +99,22 @@ const Grid = React.forwardRef<GridRef, GridProps>((props, ref) => { | |
|
|
||
| // =========================== Ref ============================ | ||
| React.useImperativeHandle(ref, () => { | ||
| const syncOffsetXAfterScroll = (queuedLeft?: number) => { | ||
| if (queuedLeft !== undefined) { | ||
| setOffsetX(Math.abs(queuedLeft)); | ||
| } | ||
| // List may clamp left; sync actual after apply. Skip when left omitted so a | ||
| // vertical-only scrollTo does not wipe offsetX from a stale snapshot. | ||
| if (queuedLeft !== undefined) { | ||
| requestAnimationFrame(() => { | ||
| const x = listRef.current?.getScrollInfo()?.x; | ||
| if (x !== undefined) { | ||
| setOffsetX(Math.abs(x)); | ||
| } | ||
| }); | ||
| } | ||
| }; | ||
|
|
||
| const obj = { | ||
| scrollTo: (config: VirtualScrollConfig) => { | ||
| const { align, offset, ...restConfig } = config; | ||
|
|
@@ -100,6 +126,8 @@ const Grid = React.forwardRef<GridRef, GridProps>((props, ref) => { | |
| offset, | ||
| align: virtualAlign, | ||
| }); | ||
| // scrollTo may not emit onVirtualScroll (esp. left-only); keep col virtual in sync | ||
| syncOffsetXAfterScroll(restConfig.left); | ||
| }, | ||
| nativeElement: listRef.current?.nativeElement, | ||
| } as unknown as GridRef; | ||
|
|
@@ -111,6 +139,7 @@ const Grid = React.forwardRef<GridRef, GridProps>((props, ref) => { | |
| listRef.current?.scrollTo({ | ||
| left: value, | ||
| }); | ||
| syncOffsetXAfterScroll(value); | ||
| }, | ||
| }); | ||
|
|
||
|
|
@@ -243,7 +272,10 @@ const Grid = React.forwardRef<GridRef, GridProps>((props, ref) => { | |
| }; | ||
|
|
||
| // ========================= Context ========================== | ||
| const gridContext = React.useMemo(() => ({ columnsOffset }), [columnsOffset]); | ||
| const gridContext = React.useMemo( | ||
| () => ({ columnsOffset, colStart, colEnd }), | ||
| [columnsOffset, colStart, colEnd], | ||
| ); | ||
|
|
||
| // ========================== Render ========================== | ||
| const tblPrefixCls = `${prefixCls}-tbody`; | ||
|
|
@@ -277,12 +309,20 @@ const Grid = React.forwardRef<GridRef, GridProps>((props, ref) => { | |
| scrollWidth={scrollX as number} | ||
| direction={direction} | ||
| onVirtualScroll={({ x }) => { | ||
| // Column virtualization uses LTR offset math; list may report negative x in RTL. | ||
| setOffsetX(Math.abs(x)); | ||
| onScroll({ | ||
| currentTarget: listRef.current?.nativeElement, | ||
| scrollLeft: x, | ||
| }); | ||
| }} | ||
| onScroll={onTablePropScroll} | ||
| onScroll={e => { | ||
| // Simulated horizontal uses list state (holder.scrollLeft stays 0); prefer getScrollInfo. | ||
| const listX = listRef.current?.getScrollInfo()?.x; | ||
| const domLeft = Math.abs(e.currentTarget.scrollLeft); | ||
| setOffsetX(Math.abs(listX ?? domLeft)); | ||
| onTablePropScroll?.(e as React.UIEvent<HTMLDivElement>); | ||
| }} | ||
| extraRender={extraRender} | ||
| > | ||
| {(item, index, itemProps) => { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
在纵向溢出变化时重新测量 gutter。
启用虚拟表格的原生滚动时,数据从少量行增加到超过容器高度,可以使 gutter 从 0 变为非零。此时
hasData和其他依赖都可能不变,因此效果不会重新测量。FixedHolder会继续使用 0,导致表头与表体在水平滚动末端错位。请在滚动容器的溢出状态变化时重新测量,例如观察容器和内容尺寸。数据减少导致 gutter 消失时也需要更新。