Skip to content

Commit c3ac429

Browse files
authored
feat: add onClick prop in Table.Summary.Row (#985)
1 parent a099306 commit c3ac429

File tree

5 files changed

+114
-2
lines changed

5 files changed

+114
-2
lines changed

README.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ React.render(<Table columns={columns} data={data} />, mountNode);
115115
| columns | Object[] | | The columns config of table, see table below |
116116
| components | Object | | Override table elements, see [#171](https://github.com/react-component/table/pull/171) for more details |
117117
| sticky | boolean \| {offsetHeader?: number, offsetScroll?: number, getContainer?: () => Window \| HTMLElement } | false | stick header and scroll bar |
118+
| summary | (data: readonly RecordType[]) => React.ReactNode | - | "summary" attribute in Ant Design's "Table" component is used to define the summary row of the table. The summary row is a special row that is usually used to display summary information of all rows in the table, such as total, average, etc. |
118119

119120
## Column Props
120121

@@ -129,11 +130,30 @@ React.render(<Table columns={columns} data={data} />, mountNode);
129130
| fixed | String \| Boolean | | this column will be fixed when table scroll horizontally: true or 'left' or 'right' |
130131
| align | String | | specify how cell content is aligned |
131132
| ellipsis | Boolean | | specify whether cell content be ellipsized |
132-
| rowScope | 'row' \| 'rowgroup' | | Set scope attribute for all cells in this column |
133+
| rowScope | 'row' \| 'rowgroup' | | Set scope attribute for all cells in this column |
133134
| onCell | Function(record, index) | | Set custom props per each cell. |
134135
| onHeaderCell | Function(record) | | Set custom props per each header cell. |
135136
| render | Function(value, row, index) | | The render function of cell, has three params: the text of this cell, the record of this row, the index of this row, it's return an object:{ children: value, props: { colSpan: 1, rowSpan:1 } } ==> 'children' is the text of this cell, props is some setting of this cell, eg: 'colspan' set td colspan, 'rowspan' set td rowspan |
136137

138+
## Summary Props
139+
140+
### Table.Summary
141+
142+
| Name | Type | Default | Description |
143+
| --- | --- | --- | --- |
144+
| key | String | | key of this summary |
145+
| fixed | boolean \| 'top' \| 'bottom' | - | "true" fixes the summary row at the bottom of the table. |
146+
147+
"top" fixes the summary row at the top of the table, while "bottom" fixes it at the bottom. "undefined" or "false" makes the summary row scrollable along with the table. |
148+
149+
### Table.Summary.Row
150+
151+
| Name | Type | Default | Description |
152+
| --- | --- | --- | --- |
153+
| key | String | | key of this summary |
154+
| className | String | - | className of this summary row |
155+
| onClick | (e?: React.MouseEvent<HTMLElement>) => void | - | The onClick attribute in Ant Design's Table.Summary.Row component can be used to set a click event handler for the summary row. |
156+
137157
## License
138158

139159
rc-table is released under the MIT license.

docs/demo/click-summary-row.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
title: click-summary-row
3+
nav:
4+
title: Demo
5+
path: /demo
6+
---
7+
8+
<code src="../examples/click-summary-row.tsx"></code>
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/* eslint-disable no-console,func-names,react/no-multi-comp, no-nested-ternary */
2+
import type { ColumnType } from '@/interface';
3+
import Table from 'rc-table';
4+
import React from 'react';
5+
import '../../assets/index.less';
6+
7+
interface RecordType {
8+
a: string;
9+
b?: string;
10+
c?: string;
11+
d: number;
12+
key: string;
13+
}
14+
15+
const columns: ColumnType<RecordType>[] = [
16+
{ title: 'title1', dataIndex: 'a', key: 'a' },
17+
{ title: 'title2', dataIndex: 'b', key: 'b' },
18+
{ title: 'title3', dataIndex: 'c', key: 'c' },
19+
{ title: 'title4', dataIndex: 'd', key: 'd' },
20+
];
21+
22+
const data: RecordType[] = [
23+
{ a: 'cdd', b: 'edd12221', d: 3, key: '2' },
24+
{ a: '133', c: 'edd12221', d: 2, key: '3' },
25+
{ a: '133', c: 'edd12221', d: 2, key: '4' },
26+
];
27+
28+
const Demo = () => {
29+
return (
30+
<div style={{ width: 800 }}>
31+
<Table
32+
columns={columns}
33+
data={data}
34+
summary={() => (
35+
<Table.Summary>
36+
<Table.Summary.Row
37+
onClick={e => {
38+
e.stopPropagation();
39+
alert('click summary row will trigger the click event');
40+
}}
41+
>
42+
<Table.Summary.Cell index={0} />
43+
<Table.Summary.Cell index={1}>Summary</Table.Summary.Cell>
44+
<Table.Summary.Cell index={3}>Content</Table.Summary.Cell>
45+
<Table.Summary.Cell index={11}>Right</Table.Summary.Cell>
46+
</Table.Summary.Row>
47+
</Table.Summary>
48+
)}
49+
/>
50+
</div>
51+
);
52+
};
53+
54+
export default Demo;

src/Footer/Row.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export interface FooterRowProps {
44
children?: React.ReactNode;
55
className?: string;
66
style?: React.CSSProperties;
7+
onClick?: (e?: React.MouseEvent<HTMLElement>) => void;
78
}
89

910
export default function FooterRow({ children, ...props }: FooterRowProps) {

tests/Summary.spec.tsx

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import React from 'react';
21
import { mount } from 'enzyme';
2+
import React from 'react';
33
import Table from '../src';
44

55
describe('Table.Summary', () => {
@@ -53,6 +53,35 @@ describe('Table.Summary', () => {
5353
expect(wrapper.find('tfoot').render()).toMatchSnapshot();
5454
});
5555

56+
it('summary row click', async () => {
57+
const onClick = jest.fn();
58+
const wrapper = mount(
59+
<Table
60+
columns={[
61+
{ dataIndex: 'a', fixed: 'left', width: 10 },
62+
{ dataIndex: 'b', fixed: 'left', width: 20 },
63+
{ dataIndex: 'c', width: 30 },
64+
]}
65+
data={[{ key: 1, a: 2, b: 3, c: 4 }]}
66+
summary={() => (
67+
<Table.Summary.Row onClick={onClick}>
68+
<Table.Summary.Cell colSpan={2} index={0}>
69+
Light
70+
</Table.Summary.Cell>
71+
<Table.Summary.Cell index={2}>Bamboo</Table.Summary.Cell>
72+
<Table.Summary.Cell index={3} align="right">
73+
112.5
74+
</Table.Summary.Cell>
75+
</Table.Summary.Row>
76+
)}
77+
/>,
78+
);
79+
80+
const tr = wrapper.find('tfoot tr').first();
81+
tr.simulate('click');
82+
expect(onClick).toHaveBeenCalled();
83+
});
84+
5685
describe('fixed summary', () => {
5786
const getSummaryTable = (fixed: boolean | 'top' | 'bottom') =>
5887
mount(

0 commit comments

Comments
 (0)