Skip to content

Commit 31738f9

Browse files
authored
chore: Not render 0 width col (#446)
* keep last col * fix test case
1 parent 535fcc8 commit 31738f9

File tree

4 files changed

+22
-50
lines changed

4 files changed

+22
-50
lines changed

src/ColGroup.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,18 @@ function ColGroup<RecordType>({ colWidths, columns, columCount }: ColGroupProps<
1212
const cols: React.ReactElement[] = [];
1313
const len = columCount || columns.length;
1414

15-
for (let i = 0; i < len; i += 1) {
15+
// Only insert col with width & additional props
16+
// Skip if rest col do not have any useful info
17+
let mustInsert = false;
18+
for (let i = len - 1; i >= 0; i -= 1) {
1619
const width = colWidths[i];
1720
const column = columns && columns[i];
1821
const additionalProps = column && column[INTERNAL_COL_DEFINE];
19-
cols.push(<col key={i} style={{ width, minWidth: width }} {...additionalProps} />);
22+
23+
if (width || additionalProps || mustInsert) {
24+
cols.unshift(<col key={i} style={{ width, minWidth: width }} {...additionalProps} />);
25+
mustInsert = true;
26+
}
2027
}
2128

2229
return <colgroup>{cols}</colgroup>;

tests/Colgroup.spec.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ describe('Table.ColGroup', () => {
1919
const columns = [
2020
{
2121
key: 0,
22+
width: 1,
2223
},
2324
];
2425

tests/__snapshots__/ExpandRow.spec.js.snap

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@ exports[`Table.Expand childrenColumnName 1`] = `
1313
<table
1414
style="table-layout: auto;"
1515
>
16-
<colgroup>
17-
<col />
18-
<col />
19-
</colgroup>
16+
<colgroup />
2017
<thead
2118
class="rc-table-thead"
2219
>
@@ -124,9 +121,6 @@ exports[`Table.Expand renders fixed column correctly work 1`] = `
124121
<col
125122
class="rc-table-expand-icon-col"
126123
/>
127-
<col />
128-
<col />
129-
<col />
130124
</colgroup>
131125
<thead
132126
class="rc-table-thead"
@@ -290,10 +284,7 @@ exports[`Table.Expand renders tree row correctly 1`] = `
290284
<table
291285
style="table-layout: auto;"
292286
>
293-
<colgroup>
294-
<col />
295-
<col />
296-
</colgroup>
287+
<colgroup />
297288
<thead
298289
class="rc-table-thead"
299290
>
@@ -396,10 +387,7 @@ exports[`Table.Expand renders tree row correctly with different children 1`] = `
396387
<table
397388
style="table-layout: auto;"
398389
>
399-
<colgroup>
400-
<col />
401-
<col />
402-
</colgroup>
390+
<colgroup />
403391
<thead
404392
class="rc-table-thead"
405393
>

tests/__snapshots__/Table.spec.js.snap

Lines changed: 9 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@ exports[`Table.Basic custom components renders correctly 1`] = `
1414
name="my-table"
1515
style="table-layout: auto;"
1616
>
17-
<colgroup>
18-
<col />
19-
</colgroup>
17+
<colgroup />
2018
<thead
2119
class="rc-table-thead"
2220
name="my-header-wrapper"
@@ -133,11 +131,7 @@ exports[`Table.Basic custom components renders fixed column and header correctly
133131
name="my-table"
134132
style="width: 100px; min-width: 100%; table-layout: fixed;"
135133
>
136-
<colgroup>
137-
<col />
138-
<col />
139-
<col />
140-
</colgroup>
134+
<colgroup />
141135
<tbody
142136
class="rc-table-tbody"
143137
name="my-body-wrapper"
@@ -252,11 +246,7 @@ exports[`Table.Basic internal api transformColumns 1`] = `
252246
<table
253247
style="table-layout: auto;"
254248
>
255-
<colgroup>
256-
<col />
257-
<col />
258-
<col />
259-
</colgroup>
249+
<colgroup />
260250
<thead
261251
class="rc-table-thead"
262252
>
@@ -333,10 +323,7 @@ exports[`Table.Basic renders colSpan correctly 1`] = `
333323
<table
334324
style="table-layout: auto;"
335325
>
336-
<colgroup>
337-
<col />
338-
<col />
339-
</colgroup>
326+
<colgroup />
340327
<thead
341328
class="rc-table-thead"
342329
>
@@ -398,9 +385,7 @@ exports[`Table.Basic renders correctly RTL 1`] = `
398385
<table
399386
style="table-layout: auto;"
400387
>
401-
<colgroup>
402-
<col />
403-
</colgroup>
388+
<colgroup />
404389
<thead
405390
class="test-prefix-thead"
406391
>
@@ -455,9 +440,7 @@ exports[`Table.Basic renders correctly basic 1`] = `
455440
<table
456441
style="table-layout: auto;"
457442
>
458-
<colgroup>
459-
<col />
460-
</colgroup>
443+
<colgroup />
461444
<thead
462445
class="test-prefix-thead"
463446
>
@@ -512,9 +495,7 @@ exports[`Table.Basic renders correctly no columns 1`] = `
512495
<table
513496
style="table-layout: auto;"
514497
>
515-
<colgroup>
516-
<col />
517-
</colgroup>
498+
<colgroup />
518499
<thead
519500
class="rc-table-thead"
520501
>
@@ -563,10 +544,7 @@ exports[`Table.Basic renders rowSpan correctly 1`] = `
563544
<table
564545
style="table-layout: auto;"
565546
>
566-
<colgroup>
567-
<col />
568-
<col />
569-
</colgroup>
547+
<colgroup />
570548
<thead
571549
class="rc-table-thead"
572550
>
@@ -632,9 +610,7 @@ exports[`Table.Basic syntactic sugar 1`] = `
632610
<table
633611
style="table-layout: auto;"
634612
>
635-
<colgroup>
636-
<col />
637-
</colgroup>
613+
<colgroup />
638614
<thead
639615
class="rc-table-thead"
640616
>

0 commit comments

Comments
 (0)