Skip to content

Commit 3237897

Browse files
author
losgif
authored
Table Expand Column Title Customization (#854)
* feat: Table Expand Column Title Customization(#853) * style: rename expandedColumnTitle to columnTitle * test: add render expend column title case * style: recover the code style
1 parent 3927df5 commit 3237897

File tree

5 files changed

+28
-1
lines changed

5 files changed

+28
-1
lines changed

docs/examples/expandedRowRender.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ for (let i = 0; i < 10; i += 1) {
3333
const Demo = () => {
3434
const [data, setData] = React.useState(tableData);
3535
const [expandedRowKeys, setExpandedRowKeys] = React.useState([]);
36+
const [columnTitle, columnTitleProps] = useCheckbox(false);
3637
const [expandRowByClick, expandRowByClickProps] = useCheckbox(false);
3738
const [fixColumns, fixColumnsProps] = useCheckbox(false);
3839
const [scrollX, scrollXProps] = useCheckbox(false);
@@ -104,6 +105,10 @@ const Demo = () => {
104105
return (
105106
<div>
106107
{toggleButton}
108+
<label>
109+
<input {...columnTitleProps} />
110+
Expand Column Title
111+
</label>
107112
<label>
108113
<input {...expandRowByClickProps} />
109114
Expand Row by Click
@@ -131,6 +136,7 @@ const Demo = () => {
131136
<Table<RecordType>
132137
columns={columns}
133138
expandable={{
139+
columnTitle: columnTitle ? <span>title</span> : '',
134140
expandRowByClick,
135141
expandedRowRender: (record, index, indent, expanded) =>
136142
expanded ? <p>extra: {record.a}</p> : null,

src/Table.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ function Table<RecordType extends DefaultRecordType>(props: TableProps<RecordTyp
260260
defaultExpandedRowKeys,
261261
defaultExpandAllRows,
262262
expandedRowRender,
263+
columnTitle,
263264
onExpand,
264265
onExpandedRowsChange,
265266
expandRowByClick,
@@ -356,6 +357,7 @@ function Table<RecordType extends DefaultRecordType>(props: TableProps<RecordTyp
356357
...props,
357358
...expandableConfig,
358359
expandable: !!expandedRowRender,
360+
columnTitle: columnTitle,
359361
expandedKeys: mergedExpandedKeys,
360362
getRowKey,
361363
// https://github.com/ant-design/ant-design/issues/23894

src/hooks/useColumns.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ function useColumns<RecordType>(
113113
children,
114114
expandable,
115115
expandedKeys,
116+
columnTitle,
116117
getRowKey,
117118
onTriggerExpand,
118119
expandIcon,
@@ -128,6 +129,7 @@ function useColumns<RecordType>(
128129
children?: React.ReactNode;
129130
expandable: boolean;
130131
expandedKeys: Set<Key>;
132+
columnTitle?: React.ReactNode;
131133
getRowKey: GetRowKey<RecordType>;
132134
onTriggerExpand: TriggerEventHandler<RecordType>;
133135
expandIcon?: RenderExpandIcon<RecordType>;
@@ -196,7 +198,7 @@ function useColumns<RecordType>(
196198
className: `${prefixCls}-expand-icon-col`,
197199
columnType: 'EXPAND_COLUMN',
198200
},
199-
title: '',
201+
title: columnTitle,
200202
fixed: fixedColumn,
201203
className: `${prefixCls}-row-expand-icon-cell`,
202204
width: columnWidth,

src/interface.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ export interface ExpandableConfig<RecordType> {
200200
expandedRowKeys?: readonly Key[];
201201
defaultExpandedRowKeys?: readonly Key[];
202202
expandedRowRender?: ExpandedRowRender<RecordType>;
203+
columnTitle?: React.ReactNode;
203204
expandRowByClick?: boolean;
204205
expandIcon?: RenderExpandIcon<RecordType>;
205206
onExpand?: (expanded: boolean, record: RecordType) => void;

tests/ExpandRow.spec.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,22 @@ describe('Table.Expand', () => {
414414
expect(wrapper.find('tbody tr').at(1).hasClass('expand-row-test-class-name')).toBeTruthy();
415415
});
416416

417+
it('renders expend column title', () => {
418+
const wrapper = mount(
419+
createTable({
420+
expandable: {
421+
expandedRowRender,
422+
columnTitle: 'column title',
423+
},
424+
}),
425+
);
426+
427+
expect(
428+
wrapper.find('thead tr th').first().hasClass('rc-table-row-expand-icon-cell'),
429+
).toBeTruthy();
430+
expect(wrapper.find('thead tr th').first().html()).toContain('column title');
431+
});
432+
417433
it('fires expand change event', () => {
418434
const onExpand = jest.fn();
419435
const wrapper = mount(

0 commit comments

Comments
 (0)