Skip to content

Commit ca71a79

Browse files
authored
fix: solve table display unexpected when column fixed (#925)
* fix: solve table display unexpected when column fixed * feat: code optimize
1 parent 99739ad commit ca71a79

File tree

8 files changed

+88
-21
lines changed

8 files changed

+88
-21
lines changed

src/Footer/Cell.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export default function SummaryCell({
2424
align,
2525
}: SummaryCellProps) {
2626
const { prefixCls, direction } = useContext(TableContext, ['prefixCls', 'direction']);
27-
const { scrollColumnIndex, stickyOffsets, flattenColumns } = React.useContext(SummaryContext);
27+
const { scrollColumnIndex, stickyOffsets, flattenColumns, columns } = React.useContext(SummaryContext);
2828
const lastIndex = index + colSpan - 1;
2929
const mergedColSpan = lastIndex + 1 === scrollColumnIndex ? colSpan + 1 : colSpan;
3030

@@ -34,6 +34,7 @@ export default function SummaryCell({
3434
flattenColumns,
3535
stickyOffsets,
3636
direction,
37+
columns?.[index]
3738
);
3839

3940
return (

src/Footer/SummaryContext.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import * as React from 'react';
2-
import type { ColumnType, StickyOffsets } from '../interface';
2+
import type { ColumnsType, ColumnType, StickyOffsets } from '../interface';
33

44
type FlattenColumns<RecordType> = readonly (ColumnType<RecordType> & { scrollbar?: boolean })[];
55

66
const SummaryContext = React.createContext<{
77
stickyOffsets?: StickyOffsets;
88
scrollColumnIndex?: number;
99
flattenColumns?: FlattenColumns<any>;
10+
columns?: ColumnsType<any>;
1011
}>({});
1112

1213
export default SummaryContext;

src/Footer/index.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { responseImmutable, useContext } from '@rc-component/context';
22
import * as React from 'react';
33
import TableContext from '../context/TableContext';
44
import devRenderTimes from '../hooks/useRenderTimes';
5-
import type { ColumnType, StickyOffsets } from '../interface';
5+
import type { ColumnsType, ColumnType, StickyOffsets } from '../interface';
66
import Summary from './Summary';
77
import SummaryContext from './SummaryContext';
88

@@ -12,14 +12,15 @@ export interface FooterProps<RecordType> {
1212
children: React.ReactNode;
1313
stickyOffsets: StickyOffsets;
1414
flattenColumns: FlattenColumns<RecordType>;
15+
columns: ColumnsType<RecordType>;
1516
}
1617

1718
function Footer<RecordType>(props: FooterProps<RecordType>) {
1819
if (process.env.NODE_ENV !== 'production') {
1920
devRenderTimes(props);
2021
}
2122

22-
const { children, stickyOffsets, flattenColumns } = props;
23+
const { children, stickyOffsets, flattenColumns, columns } = props;
2324

2425
const prefixCls = useContext(TableContext, 'prefixCls');
2526

@@ -31,8 +32,9 @@ function Footer<RecordType>(props: FooterProps<RecordType>) {
3132
stickyOffsets,
3233
flattenColumns,
3334
scrollColumnIndex: scrollColumn?.scrollbar ? lastColumnIndex : null,
35+
columns
3436
}),
35-
[scrollColumn, flattenColumns, lastColumnIndex, stickyOffsets],
37+
[scrollColumn, flattenColumns, lastColumnIndex, stickyOffsets, columns],
3638
);
3739

3840
return (

src/Header/HeaderRow.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ function HeaderRow<RecordType>({
3434
index,
3535
}: RowProps<RecordType>) {
3636
const { prefixCls, direction } = useContext(TableContext, ['prefixCls', 'direction']);
37-
3837
let rowProps: React.HTMLAttributes<HTMLElement>;
3938
if (onHeaderRow) {
4039
rowProps = onHeaderRow(
@@ -49,12 +48,13 @@ function HeaderRow<RecordType>({
4948
<RowComponent {...rowProps}>
5049
{cells.map((cell: CellType<RecordType>, cellIndex) => {
5150
const { column } = cell;
52-
const fixedInfo = getCellFixedInfo(
51+
const fixedInfo = getCellFixedInfo<RecordType>(
5352
cell.colStart,
5453
cell.colEnd,
5554
flattenColumns,
5655
stickyOffsets,
5756
direction,
57+
column
5858
);
5959

6060
let additionalProps: React.HTMLAttributes<HTMLElement>;

src/Table.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,7 @@ function Table<RecordType extends DefaultRecordType>(tableProps: TableProps<Reco
599599
{bodyColGroup}
600600
{bodyTable}
601601
{!fixFooter && summaryNode && (
602-
<Footer stickyOffsets={stickyOffsets} flattenColumns={flattenColumns}>
602+
<Footer stickyOffsets={stickyOffsets} flattenColumns={flattenColumns} columns={columns}>
603603
{summaryNode}
604604
</Footer>
605605
)}
@@ -680,7 +680,7 @@ function Table<RecordType extends DefaultRecordType>(tableProps: TableProps<Reco
680680
{showHeader !== false && <Header {...headerProps} {...columnContext} />}
681681
{bodyTable}
682682
{summaryNode && (
683-
<Footer stickyOffsets={stickyOffsets} flattenColumns={flattenColumns}>
683+
<Footer stickyOffsets={stickyOffsets} flattenColumns={flattenColumns} columns={columns}>
684684
{summaryNode}
685685
</Footer>
686686
)}
@@ -722,7 +722,7 @@ function Table<RecordType extends DefaultRecordType>(tableProps: TableProps<Reco
722722
fullTable = <ResizeObserver onResize={onFullTableResize}>{fullTable}</ResizeObserver>;
723723
}
724724

725-
const fixedInfoList = useFixedInfo(flattenColumns, stickyOffsets, direction);
725+
const fixedInfoList = useFixedInfo(flattenColumns, stickyOffsets, direction, columns);
726726

727727
const TableContextValue = React.useMemo(
728728
() => ({

src/hooks/useFixedInfo.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
import useMemo from 'rc-util/lib/hooks/useMemo';
22
import isEqual from 'rc-util/lib/isEqual';
3-
import type { ColumnType, Direction, StickyOffsets } from '../interface';
3+
import type { ColumnsType, ColumnType, Direction, StickyOffsets } from '../interface';
44
import { getCellFixedInfo } from '../utils/fixUtil';
55

66
export default function useFixedInfo<RecordType>(
77
flattenColumns: readonly ColumnType<RecordType>[],
88
stickyOffsets: StickyOffsets,
99
direction: Direction,
10+
columns: ColumnsType<RecordType>
1011
) {
1112
const fixedInfoList = flattenColumns.map((_, colIndex) =>
12-
getCellFixedInfo(colIndex, colIndex, flattenColumns, stickyOffsets, direction),
13+
getCellFixedInfo(colIndex, colIndex, flattenColumns, stickyOffsets, direction, columns?.[colIndex]),
1314
);
1415

1516
return useMemo(

src/utils/fixUtil.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { StickyOffsets, FixedType, Direction } from '../interface';
1+
import type { StickyOffsets, FixedType, Direction, ColumnType, ColumnGroupType } from '../interface';
22

33
export interface FixedInfo {
44
fixLeft: number | false;
@@ -13,12 +13,13 @@ export interface FixedInfo {
1313
isSticky: boolean;
1414
}
1515

16-
export function getCellFixedInfo(
16+
export function getCellFixedInfo<RecordType = any>(
1717
colStart: number,
1818
colEnd: number,
1919
columns: readonly { fixed?: FixedType }[],
2020
stickyOffsets: StickyOffsets,
2121
direction: Direction,
22+
curColumns?: ColumnType<RecordType> | ColumnGroupType<RecordType>
2223
): FixedInfo {
2324
const startColumn = columns[colStart] || {};
2425
const endColumn = columns[colEnd] || {};
@@ -41,20 +42,23 @@ export function getCellFixedInfo(
4142
const nextColumn = columns[colEnd + 1];
4243
const prevColumn = columns[colStart - 1];
4344

45+
// no children only
46+
const canLastFix = !(curColumns as ColumnGroupType<RecordType>)?.children;
47+
4448
if (direction === 'rtl') {
4549
if (fixLeft !== undefined) {
4650
const prevFixLeft = prevColumn && prevColumn.fixed === 'left';
47-
firstFixLeft = !prevFixLeft;
51+
firstFixLeft = !prevFixLeft && canLastFix;
4852
} else if (fixRight !== undefined) {
4953
const nextFixRight = nextColumn && nextColumn.fixed === 'right';
50-
lastFixRight = !nextFixRight;
54+
lastFixRight = !nextFixRight && canLastFix;
5155
}
5256
} else if (fixLeft !== undefined) {
5357
const nextFixLeft = nextColumn && nextColumn.fixed === 'left';
54-
lastFixLeft = !nextFixLeft;
58+
lastFixLeft = !nextFixLeft && canLastFix;
5559
} else if (fixRight !== undefined) {
5660
const prevFixRight = prevColumn && prevColumn.fixed === 'right';
57-
firstFixRight = !prevFixRight;
61+
firstFixRight = !prevFixRight && canLastFix;
5862
}
5963

6064
return {

tests/FixedHeader.spec.js

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import React from 'react';
21
import { mount } from 'enzyme';
3-
import { act } from 'react-dom/test-utils';
2+
import RcResizeObserver from 'rc-resize-observer';
43
import { spyElementPrototype } from 'rc-util/lib/test/domHook';
4+
import React from 'react';
5+
import { act } from 'react-dom/test-utils';
56
import Table, { INTERNAL_COL_DEFINE } from '../src';
6-
import RcResizeObserver from 'rc-resize-observer';
77

88
describe('Table.FixedHeader', () => {
99
let domSpy;
@@ -208,4 +208,62 @@ describe('Table.FixedHeader', () => {
208208

209209
jest.useRealTimers();
210210
});
211+
212+
it('do not mask as ant-table-cell-fix-left-last in nested table parent cell', () => {
213+
const columns = [
214+
{
215+
title: '父表头右侧的阴影导致整个表格最右侧有空隙',
216+
children: [
217+
{
218+
key: 'name',
219+
title: 'Name',
220+
fixed: 'left',
221+
dataIndex: 'name',
222+
},
223+
{
224+
key: 'name',
225+
title: 'Name',
226+
fixed: 'left',
227+
dataIndex: 'name',
228+
},
229+
{
230+
key: 'name',
231+
title: 'Name',
232+
dataIndex: 'name',
233+
},
234+
{
235+
key: 'name',
236+
title: 'Name',
237+
fixed: 'right',
238+
dataIndex: 'name',
239+
},
240+
],
241+
},
242+
];
243+
244+
const data = [
245+
{
246+
key: 0,
247+
name: 'Jack',
248+
},
249+
{
250+
key: 1,
251+
name: 'Jack1',
252+
},
253+
{
254+
key: 2,
255+
name: 'Jack1',
256+
},
257+
];
258+
const wrapper = mount(
259+
<Table
260+
columns={columns}
261+
data={data}
262+
scroll={{ x: true }}
263+
/>,
264+
);
265+
expect(wrapper.find('td').at(9).props().className).toContain('rc-table-cell-fix-left-last');
266+
expect(wrapper.find('th').first().props().className).not.toContain('rc-table-cell-fix-left-last');
267+
268+
});
211269
});

0 commit comments

Comments
 (0)