Skip to content

Commit 1b75be2

Browse files
afc163Copilot
andauthored
fix: fix sticky header column width with empty data (#1323)
Co-authored-by: Copilot <[email protected]>
1 parent d22b38c commit 1b75be2

File tree

4 files changed

+74
-48
lines changed

4 files changed

+74
-48
lines changed

docs/examples/stickyHeader.tsx

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,13 @@ const columns: ColumnType<RecordType>[] = [
7979
},
8080
];
8181

82+
const columnsWithWidth: ColumnType<RecordType>[] = [
83+
{ title: 'title1', dataIndex: 'a', key: 'a', width: 100 },
84+
{ title: 'title2', dataIndex: 'b', key: 'b', width: 100 },
85+
{ title: 'title3', dataIndex: 'c', key: 'c', width: 200 },
86+
{ title: 'title4', dataIndex: 'd', key: 'd', width: 100 },
87+
];
88+
8289
const data = [
8390
{ a: '123', key: '1' },
8491
{ a: 'cdd', b: 'edd', key: '2' },
@@ -105,11 +112,7 @@ const data = [
105112
const Demo = () => {
106113
const container = useRef();
107114
return (
108-
<div
109-
style={{
110-
height: 10000,
111-
}}
112-
>
115+
<div>
113116
<h2>Sticky</h2>
114117
<Table<RecordType>
115118
columns={columns}
@@ -212,6 +215,52 @@ const Demo = () => {
212215
suscipit asperiores, id ullam in iste soluta dignissimos vero incidunt, rem ex consectetur
213216
beatae totam aperiam. Sunt, laudantium?
214217
</div>
218+
219+
<h2>Sticky header with empty data</h2>
220+
<Table
221+
columns={fixedColumns}
222+
data={[]}
223+
scroll={{
224+
x: 'max-content',
225+
}}
226+
sticky
227+
/>
228+
<br />
229+
<Table
230+
columns={fixedColumns}
231+
data={[]}
232+
scroll={{
233+
x: 1200,
234+
}}
235+
sticky
236+
/>
237+
<br />
238+
<Table
239+
columns={columnsWithWidth}
240+
data={[]}
241+
scroll={{
242+
x: 'max-content',
243+
}}
244+
sticky
245+
/>
246+
<br />
247+
<Table
248+
columns={fixedColumns}
249+
data={[{}]}
250+
scroll={{
251+
x: 'max-content',
252+
}}
253+
sticky
254+
/>
255+
<br />
256+
<Table
257+
columns={columnsWithWidth}
258+
data={[{}]}
259+
scroll={{
260+
x: 1200,
261+
}}
262+
sticky
263+
/>
215264
</div>
216265
);
217266
};

src/FixedHolder/index.tsx

Lines changed: 6 additions & 9 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
noData: boolean;
30-
maxContentScroll: boolean;
3130
colWidths: readonly number[];
3231
columCount: number;
3332
direction: Direction;
@@ -37,6 +36,7 @@ export interface FixedHeaderProps<RecordType> extends HeaderProps<RecordType> {
3736
stickyClassName?: string;
3837
onScroll: (info: { currentTarget: HTMLDivElement; scrollLeft?: number }) => void;
3938
children: (info: HeaderProps<RecordType>) => React.ReactNode;
39+
colGroup?: React.ReactNode;
4040
}
4141

4242
const FixedHolder = React.forwardRef<HTMLDivElement, FixedHeaderProps<any>>((props, ref) => {
@@ -50,6 +50,7 @@ const FixedHolder = React.forwardRef<HTMLDivElement, FixedHeaderProps<any>>((pro
5050
columns,
5151
flattenColumns,
5252
colWidths,
53+
colGroup,
5354
columCount,
5455
stickyOffsets,
5556
direction,
@@ -58,7 +59,6 @@ const FixedHolder = React.forwardRef<HTMLDivElement, FixedHeaderProps<any>>((pro
5859
stickyBottomOffset,
5960
stickyClassName,
6061
onScroll,
61-
maxContentScroll,
6262
children,
6363
...restProps
6464
} = props;
@@ -98,12 +98,6 @@ const FixedHolder = React.forwardRef<HTMLDivElement, FixedHeaderProps<any>>((pro
9898
};
9999
}, []);
100100

101-
// Check if all flattenColumns has width
102-
const allFlattenColumnsWithWidth = React.useMemo(
103-
() => flattenColumns.every(column => column.width),
104-
[flattenColumns],
105-
);
106-
107101
// Add scrollbar column
108102
const lastColumn = flattenColumns[flattenColumns.length - 1];
109103
const ScrollBarColumn: ColumnType<unknown> & { scrollbar: true } = {
@@ -156,7 +150,10 @@ const FixedHolder = React.forwardRef<HTMLDivElement, FixedHeaderProps<any>>((pro
156150
visibility: noData || mergedColumnWidth ? null : 'hidden',
157151
}}
158152
>
159-
{(!noData || !maxContentScroll || allFlattenColumnsWithWidth) && (
153+
{/* use original ColGroup if no data, otherwise use calculated column width */}
154+
{noData ? (
155+
colGroup
156+
) : (
160157
<ColGroup
161158
colWidths={mergedColumnWidth ? [...mergedColumnWidth, combinationScrollBarSize] : []}
162159
columCount={columCount + 1}

src/Table.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -680,7 +680,6 @@ function Table<RecordType extends DefaultRecordType>(
680680
// Fixed holder share the props
681681
const fixedHolderProps = {
682682
noData: !mergedData.length,
683-
maxContentScroll: horizonScroll && mergedScrollX === 'max-content',
684683
...headerProps,
685684
...columnContext,
686685
direction,
@@ -697,6 +696,7 @@ function Table<RecordType extends DefaultRecordType>(
697696
stickyTopOffset={offsetHeader}
698697
className={`${prefixCls}-header`}
699698
ref={scrollHeaderRef}
699+
colGroup={bodyColGroup}
700700
>
701701
{renderFixedHeaderTable}
702702
</FixedHolder>
@@ -712,6 +712,7 @@ function Table<RecordType extends DefaultRecordType>(
712712
stickyBottomOffset={offsetSummary}
713713
className={`${prefixCls}-summary`}
714714
ref={scrollSummaryRef}
715+
colGroup={bodyColGroup}
715716
>
716717
{renderFixedFooterTable}
717718
</FixedHolder>

tests/__snapshots__/FixedColumn.spec.tsx.snap

Lines changed: 12 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2818,43 +2818,22 @@ LoadedCheerio {
28182818
>
28192819
<colgroup>
28202820
<col
2821-
style="width: 93px;"
2822-
/>
2823-
<col
2824-
style="width: 1000px;"
2825-
/>
2826-
<col
2827-
style="width: 1000px;"
2828-
/>
2829-
<col
2830-
style="width: 1000px;"
2831-
/>
2832-
<col
2833-
style="width: 1000px;"
2834-
/>
2835-
<col
2836-
style="width: 1000px;"
2837-
/>
2838-
<col
2839-
style="width: 1000px;"
2840-
/>
2841-
<col
2842-
style="width: 1000px;"
2843-
/>
2844-
<col
2845-
style="width: 1000px;"
2846-
/>
2847-
<col
2848-
style="width: 1000px;"
2849-
/>
2850-
<col
2851-
style="width: 1000px;"
2821+
style="width: 100px;"
28522822
/>
28532823
<col
2854-
style="width: 1000px;"
2824+
style="width: 100px;"
28552825
/>
2826+
<col />
2827+
<col />
2828+
<col />
2829+
<col />
2830+
<col />
2831+
<col />
2832+
<col />
2833+
<col />
2834+
<col />
28562835
<col
2857-
style="width: 15px;"
2836+
style="width: 100px;"
28582837
/>
28592838
</colgroup>
28602839
<thead

0 commit comments

Comments
 (0)