Skip to content

Commit c6af77e

Browse files
authored
Adds ability to define a caption (#856)
* Adds ability to define <caption> * Deprecates title prop * Update Table.tsx
1 parent 991428f commit c6af77e

File tree

6 files changed

+80
-1
lines changed

6 files changed

+80
-1
lines changed

docs/demo/caption.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## caption
2+
3+
<code src="../examples/caption.tsx">

docs/examples/caption.tsx

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import React from 'react';
2+
import Table from 'rc-table';
3+
import '../../assets/index.less';
4+
5+
const columns = [
6+
{
7+
title: 'Name',
8+
dataIndex: 'name',
9+
key: 'name',
10+
},
11+
{
12+
title: 'Age',
13+
dataIndex: 'age',
14+
key: 'age',
15+
},
16+
{
17+
title: 'Address',
18+
dataIndex: 'address',
19+
key: 'address',
20+
},
21+
];
22+
23+
const data = [
24+
{ name: 'John', age: '25', address: '1 A Street' },
25+
{ name: 'Fred', age: '38', address: '2 B Street' },
26+
{ name: 'Anne', age: '47', address: '3 C Street' },
27+
];
28+
29+
const Demo = () => (
30+
<div>
31+
<h2>Table with basic caption</h2>
32+
<Table columns={columns} data={data} caption="Users including age and address" />
33+
<br />
34+
<h2>Table with complex caption</h2>
35+
<Table
36+
columns={columns}
37+
data={data}
38+
caption={
39+
<div style={{ textAlign: 'right' }}>
40+
Users who are <em>old</em>
41+
</div>
42+
}
43+
/>
44+
</div>
45+
);
46+
47+
export default Demo;

src/Table.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,9 @@ export interface TableProps<RecordType = unknown>
129129
rowClassName?: string | RowClassName<RecordType>;
130130

131131
// Additional Part
132-
title?: PanelRender<RecordType>;
133132
footer?: PanelRender<RecordType>;
134133
summary?: (data: readonly RecordType[]) => React.ReactNode;
134+
caption?: string | React.ReactNode;
135135

136136
// Customize
137137
id?: string;
@@ -187,6 +187,7 @@ function Table<RecordType extends DefaultRecordType>(props: TableProps<RecordTyp
187187
title,
188188
footer,
189189
summary,
190+
caption,
190191

191192
// Customize
192193
id,
@@ -611,6 +612,9 @@ function Table<RecordType extends DefaultRecordType>(props: TableProps<RecordTyp
611612
<ColGroup colWidths={flattenColumns.map(({ width }) => width)} columns={flattenColumns} />
612613
);
613614

615+
const captionElement =
616+
caption !== null && caption !== undefined ? <caption className={`${prefixCls}-caption`}>{caption}</caption> : undefined;
617+
614618
const customizeScrollBody = getComponent(['body']) as CustomizeScrollBody<RecordType>;
615619

616620
if (
@@ -662,6 +666,7 @@ function Table<RecordType extends DefaultRecordType>(props: TableProps<RecordTyp
662666
tableLayout: mergedTableLayout,
663667
}}
664668
>
669+
{captionElement}
665670
{bodyColGroup}
666671
{bodyTable}
667672
{!fixFooter && summaryNode && (
@@ -743,6 +748,7 @@ function Table<RecordType extends DefaultRecordType>(props: TableProps<RecordTyp
743748
ref={scrollBodyRef}
744749
>
745750
<TableComponent style={{ ...scrollTableStyle, tableLayout: mergedTableLayout }}>
751+
{captionElement}
746752
{bodyColGroup}
747753
{showHeader !== false && <Header {...headerProps} {...columnContext} />}
748754
{bodyTable}

src/interface.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,8 @@ export interface LegacyExpandableProps<RecordType> {
175175
expandedRowClassName?: RowClassName<RecordType>;
176176
/** @deprecated Use `expandable.childrenColumnName` instead */
177177
childrenColumnName?: string;
178+
/** @deprecated Use `caption` instead */
179+
title?: PanelRender<RecordType>;
178180
}
179181

180182
export type ExpandedRowRender<ValueType> = (

src/utils/legacyUtil.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export function getExpandableProps<RecordType>(
3232
'expandedRowClassName',
3333
'expandIconColumnIndex',
3434
'showExpandColumn',
35+
'title',
3536
].some(prop => prop in props)
3637
) {
3738
warning(false, 'expanded related props have been moved into `expandable`.');

tests/Table.spec.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,26 @@ describe('Table.Basic', () => {
162162
});
163163
});
164164

165+
describe('caption', () => {
166+
it('renders string caption', () => {
167+
const miscProps = { caption: 'test_caption' };
168+
const wrapper = mount(createTable(miscProps));
169+
expect(wrapper.find('.rc-table-caption')).toBeTruthy();
170+
});
171+
172+
it('renders React.Node caption', () => {
173+
const miscProps = { caption: <div className="caption_inner" /> };
174+
const wrapper = mount(createTable(miscProps));
175+
expect(wrapper.find('.rc-table-caption .caption_inner')).toBeTruthy();
176+
});
177+
178+
it('renders without caption', () => {
179+
const miscProps = {};
180+
const wrapper = mount(createTable(miscProps));
181+
expect(wrapper.find('.rc-table-caption').length).toBeFalsy();
182+
});
183+
});
184+
165185
it('renders tableLayout', () => {
166186
const wrapper = mount(createTable({ tableLayout: 'fixed' }));
167187
expect(wrapper.find('table').props().style.tableLayout).toEqual('fixed');

0 commit comments

Comments
 (0)