Skip to content

Commit a177051

Browse files
yoyo837zombieJ
andauthored
fix: Fix column children undefined (#448)
* column should be render when children is undefined * column should be render when children is undefined * add test case * update test case * Update Table.spec.js Co-authored-by: 二货机器人 <[email protected]>
1 parent ccca810 commit a177051

File tree

4 files changed

+109
-8
lines changed

4 files changed

+109
-8
lines changed

src/Header/Header.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ function parseHeaderRows<RecordType>(
3535

3636
let colSpan: number = 1;
3737

38-
if ((column as ColumnGroupType<RecordType>).children) {
39-
colSpan = fillRowCells(
40-
(column as ColumnGroupType<RecordType>).children,
41-
currentColIndex,
42-
rowIndex + 1,
43-
).reduce((total, count) => total + count, 0);
38+
const subColumns = (column as ColumnGroupType<RecordType>).children;
39+
if (subColumns && subColumns.length > 0) {
40+
colSpan = fillRowCells(subColumns, currentColIndex, rowIndex + 1).reduce(
41+
(total, count) => total + count,
42+
0,
43+
);
4444
cell.hasSubColumns = true;
4545
}
4646

src/hooks/useColumns.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,11 @@ function flatColumns<RecordType>(columns: ColumnsType<RecordType>): ColumnType<R
4040
// Convert `fixed='true'` to `fixed='left'` instead
4141
const parsedFixed = fixed === true ? 'left' : fixed;
4242

43-
if ((column as ColumnGroupType<RecordType>).children) {
43+
const subColumns = (column as ColumnGroupType<RecordType>).children;
44+
if (subColumns && subColumns.length > 0) {
4445
return [
4546
...list,
46-
...flatColumns((column as ColumnGroupType<RecordType>).children).map(subColum => ({
47+
...flatColumns(subColumns).map(subColum => ({
4748
fixed: parsedFixed,
4849
...subColum,
4950
})),

tests/Table.spec.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,40 @@ describe('Table.Basic', () => {
3838
const wrapper = mount(createTable({ columns: [] }));
3939
expect(wrapper.render()).toMatchSnapshot();
4040
});
41+
42+
it('column children undefined', () => {
43+
const wrapper = mount(
44+
createTable({
45+
columns: [
46+
{
47+
title: '姓名',
48+
dataIndex: 'name',
49+
key: 'name',
50+
children: [],
51+
},
52+
{
53+
title: '年龄',
54+
dataIndex: 'age',
55+
key: 'age',
56+
children: undefined,
57+
},
58+
],
59+
}),
60+
);
61+
expect(wrapper.render()).toMatchSnapshot();
62+
expect(
63+
wrapper
64+
.find('th')
65+
.at(0)
66+
.text(),
67+
).toEqual('姓名');
68+
expect(
69+
wrapper
70+
.find('th')
71+
.at(1)
72+
.text(),
73+
).toEqual('年龄');
74+
});
4175
});
4276

4377
describe('renders empty text correctly', () => {

tests/__snapshots__/Table.spec.js.snap

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,72 @@ exports[`Table.Basic renders correctly basic 1`] = `
482482
</div>
483483
`;
484484

485+
exports[`Table.Basic renders correctly column children undefined 1`] = `
486+
<div
487+
class="rc-table"
488+
>
489+
<div
490+
class="rc-table-container"
491+
>
492+
<div
493+
class="rc-table-content"
494+
>
495+
<table
496+
style="table-layout: auto;"
497+
>
498+
<colgroup />
499+
<thead
500+
class="rc-table-thead"
501+
>
502+
<tr>
503+
<th
504+
class="rc-table-cell"
505+
>
506+
姓名
507+
</th>
508+
<th
509+
class="rc-table-cell"
510+
>
511+
年龄
512+
</th>
513+
</tr>
514+
</thead>
515+
<tbody
516+
class="rc-table-tbody"
517+
>
518+
<tr
519+
class="rc-table-row rc-table-row-level-0"
520+
data-row-key="key0"
521+
>
522+
<td
523+
class="rc-table-cell"
524+
>
525+
Lucy
526+
</td>
527+
<td
528+
class="rc-table-cell"
529+
/>
530+
</tr>
531+
<tr
532+
class="rc-table-row rc-table-row-level-0"
533+
data-row-key="key1"
534+
>
535+
<td
536+
class="rc-table-cell"
537+
>
538+
Jack
539+
</td>
540+
<td
541+
class="rc-table-cell"
542+
/>
543+
</tr>
544+
</tbody>
545+
</table>
546+
</div>
547+
</div>
548+
</div>
549+
`;
550+
485551
exports[`Table.Basic renders correctly no columns 1`] = `
486552
<div
487553
class="rc-table"

0 commit comments

Comments
 (0)