Skip to content

Commit d37eb35

Browse files
authored
feat: Table summary support fixed column (#463)
* support data type * update test case * use jsx * clean up props * patch summary className * Add Summary Components
1 parent 2b91d49 commit d37eb35

File tree

12 files changed

+163
-42
lines changed

12 files changed

+163
-42
lines changed

assets/index.less

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,4 +281,10 @@
281281
border-top: 0;
282282
padding: @cell-padding;
283283
}
284+
285+
tfoot {
286+
td {
287+
background: #fff;
288+
}
289+
}
284290
}

examples/fixedColumns.tsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,20 @@ const Demo = () => (
6363
expandedRowRender={({ b, c }) => b || c}
6464
scroll={{ x: 1200 }}
6565
data={data}
66+
summary={() => (
67+
<>
68+
<Table.Summary.Row>
69+
<Table.Summary.Cell index={0} />
70+
<Table.Summary.Cell index={1} colSpan={2}>
71+
Summary
72+
</Table.Summary.Cell>
73+
<Table.Summary.Cell index={3} colSpan={9}>
74+
Content
75+
</Table.Summary.Cell>
76+
<Table.Summary.Cell index={12}>Right</Table.Summary.Cell>
77+
</Table.Summary.Row>
78+
</>
79+
)}
6680
/>
6781
</div>
6882
);

src/Body/BodyRow.tsx

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,14 @@ import Cell from '../Cell';
44
import TableContext from '../context/TableContext';
55
import BodyContext from '../context/BodyContext';
66
import { getColumnsKey } from '../utils/valueUtil';
7-
import {
8-
ColumnType,
9-
StickyOffsets,
10-
CustomizeComponent,
11-
GetComponentProps,
12-
Key,
13-
GetRowKey,
14-
} from '../interface';
15-
import { getCellFixedInfo } from '../utils/fixUtil';
7+
import { ColumnType, CustomizeComponent, GetComponentProps, Key, GetRowKey } from '../interface';
168
import ExpandedRow from './ExpandedRow';
179

1810
export interface BodyRowProps<RecordType> {
1911
record: RecordType;
2012
index: number;
2113
className?: string;
2214
style?: React.CSSProperties;
23-
stickyOffsets: StickyOffsets;
2415
recordKey: Key;
2516
expandedKeys: Set<Key>;
2617
rowComponent: CustomizeComponent;
@@ -37,19 +28,19 @@ function BodyRow<RecordType extends { children?: RecordType[] }>(props: BodyRowP
3728
const {
3829
className,
3930
style,
40-
stickyOffsets,
4131
record,
4232
index,
4333
rowKey,
4434
getRowKey,
4535
rowExpandable,
36+
expandedKeys,
4637
onRow,
4738
indent = 0,
4839
rowComponent: RowComponent,
4940
cellComponent,
5041
childrenColumnName,
5142
} = props;
52-
const { prefixCls, direction } = React.useContext(TableContext);
43+
const { prefixCls, fixedInfoList } = React.useContext(TableContext);
5344
const {
5445
fixHeader,
5546
fixColumn,
@@ -68,23 +59,19 @@ function BodyRow<RecordType extends { children?: RecordType[] }>(props: BodyRowP
6859
} = React.useContext(BodyContext);
6960
const [expandRended, setExpandRended] = React.useState(false);
7061

71-
const expanded = props.expandedKeys.has(props.recordKey);
62+
const expanded = expandedKeys && expandedKeys.has(props.recordKey);
7263

7364
React.useEffect(() => {
7465
if (expanded) {
7566
setExpandRended(true);
7667
}
7768
}, [expanded]);
7869

79-
// Move to Body to enhance performance
80-
const fixedInfoList = flattenColumns.map((column, colIndex) =>
81-
getCellFixedInfo(colIndex, colIndex, flattenColumns, stickyOffsets, direction),
82-
);
83-
8470
const rowSupportExpand = expandableType === 'row' && (!rowExpandable || rowExpandable(record));
8571
// Only when row is not expandable and `children` exist in record
8672
const nestExpandable = expandableType === 'nest';
87-
const hasNestChildren = childrenColumnName in record && record[childrenColumnName];
73+
const hasNestChildren =
74+
childrenColumnName && childrenColumnName in record && record[childrenColumnName];
8875
const mergedExpandable = rowSupportExpand || nestExpandable;
8976

9077
// =========================== onRow ===========================

src/Body/index.tsx

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as React from 'react';
22
import ResizeObserver from 'rc-resize-observer';
33
import BodyRow from './BodyRow';
44
import TableContext from '../context/TableContext';
5-
import { GetRowKey, StickyOffsets, Key, GetComponentProps } from '../interface';
5+
import { GetRowKey, Key, GetComponentProps } from '../interface';
66
import ExpandedRow from './ExpandedRow';
77
import BodyContext from '../context/BodyContext';
88
import { getColumnsKey } from '../utils/valueUtil';
@@ -12,7 +12,6 @@ export interface BodyProps<RecordType> {
1212
data: RecordType[];
1313
getRowKey: GetRowKey<RecordType>;
1414
measureColumnWidth: boolean;
15-
stickyOffsets: StickyOffsets;
1615
expandedKeys: Set<Key>;
1716
onRow: GetComponentProps<RecordType>;
1817
rowExpandable: (record: RecordType) => boolean;
@@ -24,7 +23,6 @@ function Body<RecordType>({
2423
data,
2524
getRowKey,
2625
measureColumnWidth,
27-
stickyOffsets,
2826
expandedKeys,
2927
onRow,
3028
rowExpandable,
@@ -56,7 +54,6 @@ function Body<RecordType>({
5654
index={index}
5755
rowComponent={trComponent}
5856
cellComponent={tdComponent}
59-
stickyOffsets={stickyOffsets}
6057
expandedKeys={expandedKeys}
6158
onRow={onRow}
6259
getRowKey={getRowKey}
@@ -112,7 +109,6 @@ function Body<RecordType>({
112109
prefixCls,
113110
onRow,
114111
measureColumnWidth,
115-
stickyOffsets,
116112
expandedKeys,
117113
getRowKey,
118114
getComponent,

src/Footer/Cell.tsx

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import * as React from 'react';
2+
import Cell from '../Cell';
3+
import TableContext from '../context/TableContext';
4+
5+
export interface SummaryCellProps {
6+
className?: string;
7+
children?: React.ReactNode;
8+
index: number;
9+
colSpan?: number;
10+
rowSpan?: number;
11+
}
12+
13+
export default function SummaryCell({
14+
className,
15+
index,
16+
children,
17+
colSpan,
18+
rowSpan,
19+
}: SummaryCellProps) {
20+
const { prefixCls, fixedInfoList } = React.useContext(TableContext);
21+
22+
const fixedInfo = fixedInfoList[index];
23+
24+
return (
25+
<Cell
26+
className={className}
27+
index={index}
28+
component="td"
29+
prefixCls={prefixCls}
30+
record={null}
31+
dataIndex={null}
32+
render={() => ({
33+
children,
34+
props: {
35+
colSpan,
36+
rowSpan,
37+
},
38+
})}
39+
{...fixedInfo}
40+
/>
41+
);
42+
}

src/Footer/Row.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import * as React from 'react';
2+
3+
export interface FooterRowProps {
4+
children?: React.ReactNode;
5+
className?: string;
6+
style?: React.CSSProperties;
7+
}
8+
9+
export default function FooterRow(props: FooterRowProps) {
10+
return <tr {...props} />;
11+
}

src/Footer/index.tsx

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
import * as React from 'react';
2+
import TableContext from '../context/TableContext';
3+
import Cell from './Cell';
4+
import Row from './Row';
25

3-
export interface FooterProps {
6+
export interface FooterProps<RecordType> {
47
children: React.ReactNode;
58
}
69

7-
function Footer({ children }: FooterProps) {
8-
return <tfoot>{children}</tfoot>;
10+
function Footer<RecordType>({ children }: FooterProps<RecordType>) {
11+
const { prefixCls } = React.useContext(TableContext);
12+
return <tfoot className={`${prefixCls}-summary`}>{children}</tfoot>;
913
}
1014

1115
export default Footer;
16+
17+
export const FooterComponents = {
18+
Cell,
19+
Row,
20+
};

src/Table.tsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,9 @@ import useStickyOffsets from './hooks/useStickyOffsets';
6464
import ColGroup from './ColGroup';
6565
import { getExpandableProps, getDataAndAriaProps } from './utils/legacyUtil';
6666
import Panel from './Panel';
67-
import Footer from './Footer';
67+
import Footer, { FooterComponents } from './Footer';
6868
import { findAllChildrenKeys, renderExpandIcon } from './utils/expandUtil';
69+
import { getCellFixedInfo } from './utils/fixUtil';
6970

7071
// Used for conditions cache
7172
const EMPTY_DATA = [];
@@ -503,7 +504,6 @@ function Table<RecordType extends DefaultRecordType>(props: TableProps<RecordTyp
503504
<Body
504505
data={mergedData}
505506
measureColumnWidth={fixHeader || horizonScroll}
506-
stickyOffsets={stickyOffsets}
507507
expandedKeys={mergedExpandedKeys}
508508
rowExpandable={rowExpandable}
509509
getRowKey={getRowKey}
@@ -661,8 +661,11 @@ function Table<RecordType extends DefaultRecordType>(props: TableProps<RecordTyp
661661
getComponent,
662662
scrollbarSize,
663663
direction,
664+
fixedInfoList: flattenColumns.map((_, colIndex) =>
665+
getCellFixedInfo(colIndex, colIndex, flattenColumns, stickyOffsets, direction),
666+
),
664667
}),
665-
[prefixCls, getComponent, scrollbarSize, direction],
668+
[prefixCls, getComponent, scrollbarSize, direction, flattenColumns, stickyOffsets, direction],
666669
);
667670

668671
const BodyContextValue = React.useMemo(
@@ -717,6 +720,8 @@ Table.Column = Column;
717720

718721
Table.ColumnGroup = ColumnGroup;
719722

723+
Table.Summary = FooterComponents;
724+
720725
Table.defaultProps = {
721726
rowKey: 'key',
722727
prefixCls: 'rc-table',

src/context/TableContext.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
import * as React from 'react';
22
import { GetComponent } from '../interface';
3+
import { FixedInfo } from '../utils/fixUtil';
34

45
export interface TableContextProps {
56
// Table context
67
prefixCls: string;
8+
79
getComponent: GetComponent;
810

911
scrollbarSize: number;
1012

1113
direction: 'ltr' | 'rtl';
14+
15+
fixedInfoList: FixedInfo[];
1216
}
1317

1418
const TableContext = React.createContext<TableContextProps>(null);

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import Table from './Table';
2+
import { FooterComponents as Summary } from './Footer';
23
import Column from './sugar/Column';
34
import ColumnGroup from './sugar/ColumnGroup';
45
import { INTERNAL_COL_DEFINE } from './utils/legacyUtil';
56

6-
export { Column, ColumnGroup, INTERNAL_COL_DEFINE };
7+
export { Summary, Column, ColumnGroup, INTERNAL_COL_DEFINE };
78

89
export default Table;

0 commit comments

Comments
 (0)