Skip to content

Commit 02badc3

Browse files
committed
fix: always include trailing scrollbar col in header colgroup
1 parent e63fbf5 commit 02badc3

5 files changed

Lines changed: 95 additions & 36 deletions

File tree

‎src/FixedHolder/index.tsx‎

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ function useColumnWidth(colWidths: readonly number[], columCount: number) {
2727
export interface FixedHeaderProps<RecordType> extends HeaderProps<RecordType> {
2828
className: string;
2929
style?: React.CSSProperties;
30-
noData: boolean;
3130
maxContentScroll: boolean;
3231
colWidths: readonly number[];
3332
columCount: number;
@@ -40,7 +39,6 @@ export interface FixedHeaderProps<RecordType> extends HeaderProps<RecordType> {
4039
tableLayout?: TableLayout;
4140
onScroll: (info: { currentTarget: HTMLDivElement; scrollLeft?: number }) => void;
4241
children: (info: HeaderProps<RecordType>) => React.ReactNode;
43-
colGroup?: React.ReactNode;
4442
}
4543

4644
const FixedHolder = React.forwardRef<HTMLDivElement, FixedHeaderProps<any>>((props, ref) => {
@@ -51,11 +49,9 @@ const FixedHolder = React.forwardRef<HTMLDivElement, FixedHeaderProps<any>>((pro
5149
const {
5250
className,
5351
style,
54-
noData,
5552
columns,
5653
flattenColumns,
5754
colWidths,
58-
colGroup,
5955
columCount,
6056
stickyOffsets,
6157
direction,
@@ -158,14 +154,16 @@ const FixedHolder = React.forwardRef<HTMLDivElement, FixedHeaderProps<any>>((pro
158154
}, [combinationScrollBarSize, stickyOffsets, isSticky]);
159155

160156
const mergedColumnWidth = useColumnWidth(colWidths, columCount);
161-
162-
const isColGroupEmpty = useMemo<boolean>(() => {
163-
// use original ColGroup if no data or no calculated column width, otherwise use calculated column width
164-
// Return original colGroup if no data, or mergedColumnWidth is empty, or all widths are falsy
165-
const noWidth =
166-
!mergedColumnWidth || !mergedColumnWidth.length || mergedColumnWidth.every(w => !w);
167-
return noData || noWidth;
168-
}, [noData, mergedColumnWidth]);
157+
const hasMergedColumnWidth = !!mergedColumnWidth && mergedColumnWidth.some(width => width);
158+
159+
// Use the declared column width when the measured one is unavailable
160+
// (e.g. there is no data to measure). Both cases always reserve the width
161+
// of the trailing scrollbar column, so the extra header cell keeps a stable
162+
// size and the table does not jump when data arrives.
163+
const fallbackColWidths = React.useMemo(
164+
() => flattenColumns.map(({ width }) => width),
165+
[flattenColumns],
166+
);
169167

170168
return (
171169
<div
@@ -187,15 +185,14 @@ const FixedHolder = React.forwardRef<HTMLDivElement, FixedHeaderProps<any>>((pro
187185
width: scrollX,
188186
}}
189187
>
190-
{isColGroupEmpty ? (
191-
colGroup
192-
) : (
193-
<ColGroup
194-
colWidths={[...mergedColumnWidth, combinationScrollBarSize]}
195-
columCount={columCount + 1}
196-
columns={flattenColumnsWithScrollbar}
197-
/>
198-
)}
188+
<ColGroup
189+
colWidths={[
190+
...(hasMergedColumnWidth ? mergedColumnWidth : fallbackColWidths),
191+
combinationScrollBarSize,
192+
]}
193+
columCount={columCount + 1}
194+
columns={flattenColumnsWithScrollbar}
195+
/>
199196
{children({
200197
...restProps,
201198
stickyOffsets: headerStickyOffsets,

‎src/Table.tsx‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -743,7 +743,6 @@ const Table = <RecordType extends DefaultRecordType>(
743743

744744
// Fixed holder share the props
745745
const fixedHolderProps = {
746-
noData: !mergedData.length,
747746
maxContentScroll: horizonScroll && mergedScrollX === 'max-content',
748747
...headerProps,
749748
...columnContext,
@@ -763,7 +762,6 @@ const Table = <RecordType extends DefaultRecordType>(
763762
stickyTopOffset={offsetHeader}
764763
className={`${prefixCls}-header`}
765764
ref={scrollHeaderRef}
766-
colGroup={bodyColGroup}
767765
>
768766
{renderFixedHeaderTable}
769767
</FixedHolder>
@@ -779,7 +777,6 @@ const Table = <RecordType extends DefaultRecordType>(
779777
stickyBottomOffset={offsetSummary}
780778
className={`${prefixCls}-summary`}
781779
ref={scrollSummaryRef}
782-
colGroup={bodyColGroup}
783780
>
784781
{renderFixedFooterTable}
785782
</FixedHolder>

‎tests/Scroll.spec.jsx‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,4 +168,40 @@ describe('Table.Scroll', () => {
168168
});
169169
expect(isTriggerScroll).toEqual(true);
170170
});
171+
172+
describe('scrollbar placeholder colgroup', () => {
173+
const scrollColumns = [
174+
{ title: 'A', dataIndex: 'a', key: 'a', width: 100 },
175+
{ title: 'B', dataIndex: 'b', key: 'b', width: 200 },
176+
];
177+
178+
const renderScrollTable = data =>
179+
render(<Table columns={scrollColumns} data={data} scroll={{ y: 200 }} tableLayout="fixed" />);
180+
181+
const serializeHeaderCols = container =>
182+
[...container.querySelectorAll('.rc-table-header col')].map(col => col.getAttribute('style'));
183+
184+
it('keep scrollbar column width in header colgroup when data is empty', () => {
185+
const { container } = renderScrollTable([]);
186+
187+
const headerTable = container.querySelector('.rc-table-header table');
188+
const cols = headerTable.querySelectorAll('col');
189+
190+
// Real columns + a trailing scrollbar column
191+
expect(cols).toHaveLength(scrollColumns.length + 1);
192+
expect(cols[scrollColumns.length]).toHaveStyle({ width: '15px' });
193+
expect(headerTable.querySelectorAll('th.rc-table-cell-scrollbar')).toHaveLength(1);
194+
});
195+
196+
it('keep header colgroup stable between empty and filled data', () => {
197+
const emptyRender = renderScrollTable([]);
198+
const filledRender = renderScrollTable([{ key: 1, a: 'x', b: 'y' }]);
199+
200+
// Loaded data should not change the widths declared in the header colgroup,
201+
// otherwise the columns visually jump when the table gets its first rows.
202+
expect(serializeHeaderCols(emptyRender.container)).toEqual(
203+
serializeHeaderCols(filledRender.container),
204+
);
205+
});
206+
});
171207
});

‎tests/__snapshots__/FixedColumn.spec.tsx.snap‎

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2698,22 +2698,43 @@ exports[`Table.FixedColumn > renders correctly > scrollXY - without data 1`] = `
26982698
>
26992699
<colgroup>
27002700
<col
2701-
style="width: 100px;"
2701+
style="width: 1000px;"
27022702
/>
27032703
<col
2704-
style="width: 100px;"
2704+
style="width: 1000px;"
27052705
/>
2706-
<col />
2707-
<col />
2708-
<col />
2709-
<col />
2710-
<col />
2711-
<col />
2712-
<col />
2713-
<col />
2714-
<col />
27152706
<col
2716-
style="width: 100px;"
2707+
style="width: 1000px;"
2708+
/>
2709+
<col
2710+
style="width: 1000px;"
2711+
/>
2712+
<col
2713+
style="width: 1000px;"
2714+
/>
2715+
<col
2716+
style="width: 1000px;"
2717+
/>
2718+
<col
2719+
style="width: 1000px;"
2720+
/>
2721+
<col
2722+
style="width: 1000px;"
2723+
/>
2724+
<col
2725+
style="width: 1000px;"
2726+
/>
2727+
<col
2728+
style="width: 1000px;"
2729+
/>
2730+
<col
2731+
style="width: 1000px;"
2732+
/>
2733+
<col
2734+
style="width: 1000px;"
2735+
/>
2736+
<col
2737+
style="width: 15px;"
27172738
/>
27182739
</colgroup>
27192740
<thead

‎tests/__snapshots__/Table.spec.jsx.snap‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,14 @@ exports[`Table.Basic > custom components > renders fixed column and header corre
8787
<table
8888
style="table-layout: fixed; min-width: 100%; width: 100px;"
8989
>
90+
<colgroup>
91+
<col />
92+
<col />
93+
<col />
94+
<col
95+
style="width: 15px;"
96+
/>
97+
</colgroup>
9098
<thead
9199
class="rc-table-thead"
92100
name="my-header-wrapper"

0 commit comments

Comments
 (0)