Skip to content

Commit f4ff62a

Browse files
committed
Add column[align]
1 parent 5c9df88 commit f4ff62a

File tree

4 files changed

+97
-2
lines changed

4 files changed

+97
-2
lines changed

src/TableCell.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,14 @@ export default class TableCell extends React.Component {
4949
} else {
5050
text = get(record, dataIndex);
5151
}
52-
let tdProps;
52+
let tdProps = {};
5353
let colSpan;
5454
let rowSpan;
5555

5656
if (render) {
5757
text = render(text, record, index);
5858
if (this.isInvalidRenderCellText(text)) {
59-
tdProps = text.props || {};
59+
tdProps = text.props || tdProps;
6060
colSpan = tdProps.colSpan;
6161
rowSpan = tdProps.rowSpan;
6262
text = text.children;
@@ -82,6 +82,11 @@ export default class TableCell extends React.Component {
8282
if (rowSpan === 0 || colSpan === 0) {
8383
return null;
8484
}
85+
86+
if (column.align) {
87+
tdProps.style = { textAlign: column.align };
88+
}
89+
8590
return (
8691
<BodyCell
8792
className={className}

src/TableHeaderRow.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ function TableHeaderRow({ row, index, height, components, onHeaderRow }) {
1313
{row.map((cell, i) => {
1414
const { column, ...cellProps } = cell;
1515
const customProps = column.onHeaderCell ? column.onHeaderCell(column) : {};
16+
if (column.align) {
17+
cellProps.style = { textAlign: column.align };
18+
}
1619
return <HeaderCell {...cellProps} {...customProps} key={i} />;
1720
})}
1821
</HeaderRow>

tests/Table.spec.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,4 +536,13 @@ describe('Table', () => {
536536
expect(wrapper).toMatchSnapshot();
537537
});
538538
});
539+
540+
it('align column', () => {
541+
const columns = [
542+
{ title: 'Name', dataIndex: 'name', key: 'name' },
543+
{ title: 'Age', dataIndex: 'age', key: 'age', align: 'center' },
544+
];
545+
const wrapper = render(createTable({ columns }));
546+
expect(wrapper).toMatchSnapshot();
547+
});
539548
});

tests/__snapshots__/Table.spec.js.snap

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,83 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`Table align column 1`] = `
4+
<div
5+
class="rc-table rc-table-scroll-position-left"
6+
>
7+
<div
8+
class="rc-table-content"
9+
>
10+
<div
11+
class="rc-table-body"
12+
>
13+
<table
14+
class=""
15+
>
16+
<colgroup>
17+
<col />
18+
<col />
19+
</colgroup>
20+
<thead
21+
class="rc-table-thead"
22+
>
23+
<tr>
24+
<th
25+
class=""
26+
>
27+
Name
28+
</th>
29+
<th
30+
class=""
31+
style="text-align:center"
32+
>
33+
Age
34+
</th>
35+
</tr>
36+
</thead>
37+
<tbody
38+
class="rc-table-tbody"
39+
>
40+
<tr
41+
class="rc-table-row rc-table-row-level-0"
42+
>
43+
<td
44+
class=""
45+
>
46+
<span
47+
class="rc-table-row-indent indent-level-0"
48+
style="padding-left:0px"
49+
/>
50+
Lucy
51+
</td>
52+
<td
53+
class=""
54+
style="text-align:center"
55+
/>
56+
</tr>
57+
<tr
58+
class="rc-table-row rc-table-row-level-0"
59+
>
60+
<td
61+
class=""
62+
>
63+
<span
64+
class="rc-table-row-indent indent-level-0"
65+
style="padding-left:0px"
66+
/>
67+
Jack
68+
</td>
69+
<td
70+
class=""
71+
style="text-align:center"
72+
/>
73+
</tr>
74+
</tbody>
75+
</table>
76+
</div>
77+
</div>
78+
</div>
79+
`;
80+
381
exports[`Table custom components renders correctly 1`] = `
482
<div
583
class="rc-table rc-table-scroll-position-left"

0 commit comments

Comments
 (0)