From ca71a796e4b556c8e1d2386173cb7e542b4852a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?kiner-tang=28=E6=96=87=E8=BE=89=29?= <1127031143@qq.com> Date: Sun, 8 Jan 2023 15:25:11 +0800 Subject: [PATCH] fix: solve table display unexpected when column fixed (#925) * fix: solve table display unexpected when column fixed * feat: code optimize --- src/Footer/Cell.tsx | 3 +- src/Footer/SummaryContext.tsx | 3 +- src/Footer/index.tsx | 8 +++-- src/Header/HeaderRow.tsx | 4 +-- src/Table.tsx | 6 ++-- src/hooks/useFixedInfo.ts | 5 +-- src/utils/fixUtil.ts | 16 +++++---- tests/FixedHeader.spec.js | 64 +++++++++++++++++++++++++++++++++-- 8 files changed, 88 insertions(+), 21 deletions(-) diff --git a/src/Footer/Cell.tsx b/src/Footer/Cell.tsx index 2ecbd407a..c36ca9a9f 100644 --- a/src/Footer/Cell.tsx +++ b/src/Footer/Cell.tsx @@ -24,7 +24,7 @@ export default function SummaryCell({ align, }: SummaryCellProps) { const { prefixCls, direction } = useContext(TableContext, ['prefixCls', 'direction']); - const { scrollColumnIndex, stickyOffsets, flattenColumns } = React.useContext(SummaryContext); + const { scrollColumnIndex, stickyOffsets, flattenColumns, columns } = React.useContext(SummaryContext); const lastIndex = index + colSpan - 1; const mergedColSpan = lastIndex + 1 === scrollColumnIndex ? colSpan + 1 : colSpan; @@ -34,6 +34,7 @@ export default function SummaryCell({ flattenColumns, stickyOffsets, direction, + columns?.[index] ); return ( diff --git a/src/Footer/SummaryContext.tsx b/src/Footer/SummaryContext.tsx index 5e57c9200..04fc13cda 100644 --- a/src/Footer/SummaryContext.tsx +++ b/src/Footer/SummaryContext.tsx @@ -1,5 +1,5 @@ import * as React from 'react'; -import type { ColumnType, StickyOffsets } from '../interface'; +import type { ColumnsType, ColumnType, StickyOffsets } from '../interface'; type FlattenColumns = readonly (ColumnType & { scrollbar?: boolean })[]; @@ -7,6 +7,7 @@ const SummaryContext = React.createContext<{ stickyOffsets?: StickyOffsets; scrollColumnIndex?: number; flattenColumns?: FlattenColumns; + columns?: ColumnsType; }>({}); export default SummaryContext; diff --git a/src/Footer/index.tsx b/src/Footer/index.tsx index 0912f5ac8..73c2b51ce 100644 --- a/src/Footer/index.tsx +++ b/src/Footer/index.tsx @@ -2,7 +2,7 @@ import { responseImmutable, useContext } from '@rc-component/context'; import * as React from 'react'; import TableContext from '../context/TableContext'; import devRenderTimes from '../hooks/useRenderTimes'; -import type { ColumnType, StickyOffsets } from '../interface'; +import type { ColumnsType, ColumnType, StickyOffsets } from '../interface'; import Summary from './Summary'; import SummaryContext from './SummaryContext'; @@ -12,6 +12,7 @@ export interface FooterProps { children: React.ReactNode; stickyOffsets: StickyOffsets; flattenColumns: FlattenColumns; + columns: ColumnsType; } function Footer(props: FooterProps) { @@ -19,7 +20,7 @@ function Footer(props: FooterProps) { devRenderTimes(props); } - const { children, stickyOffsets, flattenColumns } = props; + const { children, stickyOffsets, flattenColumns, columns } = props; const prefixCls = useContext(TableContext, 'prefixCls'); @@ -31,8 +32,9 @@ function Footer(props: FooterProps) { stickyOffsets, flattenColumns, scrollColumnIndex: scrollColumn?.scrollbar ? lastColumnIndex : null, + columns }), - [scrollColumn, flattenColumns, lastColumnIndex, stickyOffsets], + [scrollColumn, flattenColumns, lastColumnIndex, stickyOffsets, columns], ); return ( diff --git a/src/Header/HeaderRow.tsx b/src/Header/HeaderRow.tsx index c6db6d7e0..87ff81042 100644 --- a/src/Header/HeaderRow.tsx +++ b/src/Header/HeaderRow.tsx @@ -34,7 +34,6 @@ function HeaderRow({ index, }: RowProps) { const { prefixCls, direction } = useContext(TableContext, ['prefixCls', 'direction']); - let rowProps: React.HTMLAttributes; if (onHeaderRow) { rowProps = onHeaderRow( @@ -49,12 +48,13 @@ function HeaderRow({ {cells.map((cell: CellType, cellIndex) => { const { column } = cell; - const fixedInfo = getCellFixedInfo( + const fixedInfo = getCellFixedInfo( cell.colStart, cell.colEnd, flattenColumns, stickyOffsets, direction, + column ); let additionalProps: React.HTMLAttributes; diff --git a/src/Table.tsx b/src/Table.tsx index de275e7b6..8e74719e8 100644 --- a/src/Table.tsx +++ b/src/Table.tsx @@ -599,7 +599,7 @@ function Table(tableProps: TableProps +
{summaryNode}
)} @@ -680,7 +680,7 @@ function Table(tableProps: TableProps} {bodyTable} {summaryNode && ( -