Skip to content

Commit d3eead2

Browse files
committed
chore: add scrollInfo context
1 parent 2f40da5 commit d3eead2

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

src/Table.tsx

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import * as React from 'react';
3737
import Body from './Body';
3838
import ColGroup from './ColGroup';
3939
import { EXPAND_COLUMN, INTERNAL_HOOKS } from './constant';
40-
import TableContext, { makeImmutable } from './context/TableContext';
40+
import TableContext, { makeImmutable, type ScrollInfoType } from './context/TableContext';
4141
import type { FixedHeaderProps } from './FixedHolder';
4242
import FixedHolder from './FixedHolder';
4343
import Footer, { FooterComponents } from './Footer';
@@ -75,6 +75,7 @@ import Column from './sugar/Column';
7575
import ColumnGroup from './sugar/ColumnGroup';
7676
import { getColumnsKey, validateValue, validNumberValue } from './utils/valueUtil';
7777
import { getDOM } from '@rc-component/util/lib/Dom/findDOMNode';
78+
import isEqual from '@rc-component/util/lib/isEqual';
7879

7980
export const DEFAULT_PREFIX = 'rc-table';
8081

@@ -435,6 +436,8 @@ function Table<RecordType extends DefaultRecordType>(
435436
}
436437
}
437438

439+
const [scrollInfo, setScrollInfo] = React.useState<ScrollInfoType>([0, 0]);
440+
438441
const onInternalScroll = useEvent(
439442
({ currentTarget, scrollLeft }: { currentTarget: HTMLElement; scrollLeft?: number }) => {
440443
const isRTL = direction === 'rtl';
@@ -459,6 +462,12 @@ function Table<RecordType extends DefaultRecordType>(
459462
? mergedScrollX
460463
: measureTarget.scrollWidth;
461464
const clientWidth = measureTarget.clientWidth;
465+
466+
setScrollInfo(ori => {
467+
const nextScrollInfo: ScrollInfoType = [mergedScrollLeft, scrollWidth - clientWidth];
468+
return isEqual(ori, nextScrollInfo) ? ori : nextScrollInfo;
469+
});
470+
462471
// There is no space to scroll
463472
if (scrollWidth === clientWidth) {
464473
setPingedLeft(false);
@@ -798,6 +807,7 @@ function Table<RecordType extends DefaultRecordType>(
798807
() => ({
799808
// Scroll
800809
scrollX: mergedScrollX,
810+
scrollInfo,
801811

802812
// Table
803813
prefixCls,
@@ -847,6 +857,7 @@ function Table<RecordType extends DefaultRecordType>(
847857
[
848858
// Scroll
849859
mergedScrollX,
860+
scrollInfo,
850861

851862
// Table
852863
prefixCls,

src/context/TableContext.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import type { FixedInfo } from '../utils/fixUtil';
1818
const { makeImmutable, responseImmutable, useImmutableMark } = createImmutable();
1919
export { makeImmutable, responseImmutable, useImmutableMark };
2020

21+
export type ScrollInfoType = [scrollLeft: number, scrollRange: number];
22+
2123
export interface TableContextProps<RecordType = any> {
2224
// Scroll
2325
scrollX: number | string | true;
@@ -33,6 +35,7 @@ export interface TableContextProps<RecordType = any> {
3335
fixHeader: boolean;
3436
fixColumn: boolean;
3537
horizonScroll: boolean;
38+
scrollInfo: ScrollInfoType;
3639

3740
// Body
3841
rowClassName: string | RowClassName<RecordType>;

0 commit comments

Comments
 (0)