Skip to content

feat: add headerAlign and bodyAlign #155

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,6 @@ React table component.

[![rc-table](https://nodei.co/npm/rc-table.png)](https://npmjs.org/package/rc-table)

## Development

```
npm install
npm start
```

## Example

http://react-component.github.io/table/examples/
Expand Down Expand Up @@ -230,6 +223,22 @@ React.render(<Table columns={columns} data={data} />, mountNode);
<td>`No Data`</td>
<td>Display text when data is empty</td>
</tr>
<tr>
<td>headerAlign</td>
<td>String</td>
<td>'left'</td>
<td>
how table head text align
</td>
</tr>
<tr>
<td>bodyAlign</td>
<td>String</td>
<td>'left'</td>
<td>
how table body text align
</td>
</tr>
<tr>
<td>columns</td>
<td>Object[]<Object></td>
Expand Down Expand Up @@ -310,6 +319,13 @@ React.render(<Table columns={columns} data={data} />, mountNode);
</tbody>
</table>

## Development

```
npm install
npm start
```

## License

rc-table is released under the MIT license.
Empty file added examples/textAlign.html
Empty file.
26 changes: 26 additions & 0 deletions examples/textAlign.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/* eslint-disable no-console,func-names,react/no-multi-comp */
import React from 'react';
import ReactDOM from 'react-dom';
import Table from '../src/index';
import 'rc-table/assets/index.less';

const columns = [
{ title: 'Project', dataIndex: 'project', key: 'project', width: 150 },
{ title: 'User', dataIndex: 'user', key: 'user', width: 150 },
{ title: 'Role', dataIndex: 'role', key: 'role', width: 150 },
{ title: 'Remark', dataIndex: 'remark', key: 'remark' },
];

const data = [
{ key: '1', project: 'ant-design', user: 'yiminghe', role: 'maintainer', remark: '' },
{ key: '2', project: 'ant-design', user: 'afc163', role: 'maintainer', remark: '' },
{ key: '3', project: 'ant-design', user: 'marswong', role: 'contributor', remark: '' },
];

ReactDOM.render(
<div>
<h2>table with custom textAlign</h2>
<Table columns={columns} data={data} headerAlign="center" bodyAlign="right" />
</div>,
document.getElementById('__react-content')
);
5 changes: 3 additions & 2 deletions src/ColumnManager.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';

export default class ColumnManager {
_cached = {}
_cached = {};

constructor(columns, elements) {
this.columns = columns || this.normalize(elements);
Expand Down Expand Up @@ -73,10 +73,11 @@ export default class ColumnManager {
const setRowSpan = column => {
const rowSpan = rows.length - currentRow;
if (column &&
!column.children && // parent columns are supposed to be one row
!column.children &&
rowSpan > 1 &&
(!column.rowSpan || column.rowSpan < rowSpan)
) {
// parent columns are supposed to be one row
column.rowSpan = rowSpan;
}
};
Expand Down
11 changes: 8 additions & 3 deletions src/Table.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ export default class Table extends React.Component {
rowRef: PropTypes.func,
getBodyWrapper: PropTypes.func,
children: PropTypes.node,
headerAlign: PropTypes.string,
bodyAlign: PropTypes.string,
}

static defaultProps = {
Expand Down Expand Up @@ -67,6 +69,8 @@ export default class Table extends React.Component {
rowRef: () => null,
getBodyWrapper: body => body,
emptyText: () => 'No Data',
headerAlign: 'left',
bodyAlign: 'left',
}

constructor(props) {
Expand Down Expand Up @@ -194,7 +198,7 @@ export default class Table extends React.Component {
}

getHeader(columns, fixed) {
const { showHeader, expandIconAsCell, prefixCls } = this.props;
const { showHeader, headerAlign, expandIconAsCell, prefixCls } = this.props;
const rows = this.getHeaderRows(columns);

if (expandIconAsCell && fixed !== 'right') {
Expand All @@ -213,6 +217,7 @@ export default class Table extends React.Component {
prefixCls={prefixCls}
rows={rows}
rowStyle={trStyle}
headerAlign={headerAlign}
/>
) : null;
}
Expand Down Expand Up @@ -432,7 +437,7 @@ export default class Table extends React.Component {

getTable(options = {}) {
const { columns, fixed } = options;
const { prefixCls, scroll = {}, getBodyWrapper, showHeader } = this.props;
const { prefixCls, scroll = {}, getBodyWrapper, showHeader, bodyAlign } = this.props;
let { useFixedHeader } = this.props;
const bodyStyle = { ...this.props.bodyStyle };
const headStyle = {};
Expand Down Expand Up @@ -475,7 +480,7 @@ export default class Table extends React.Component {
}
}
const tableBody = hasBody ? getBodyWrapper(
<tbody className={`${prefixCls}-tbody`}>
<tbody className={`${prefixCls}-tbody`} style={{ textAlign: bodyAlign }}>
{this.getRows(columns, fixed)}
</tbody>
) : null;
Expand Down
5 changes: 3 additions & 2 deletions src/TableHeader.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@ export default class TableHeader extends React.Component {
prefixCls: PropTypes.string,
rowStyle: PropTypes.object,
rows: PropTypes.array,
headerAlign: PropTypes.string,
}

shouldComponentUpdate(nextProps) {
return !shallowequal(nextProps, this.props);
}

render() {
const { prefixCls, rowStyle, rows } = this.props;
const { prefixCls, rowStyle, rows, headerAlign } = this.props;
return (
<thead className={`${prefixCls}-thead`}>
<thead className={`${prefixCls}-thead`} style={{ textAlign: headerAlign }}>
{
rows.map((row, index) => (
<tr key={index} style={rowStyle}>
Expand Down
5 changes: 5 additions & 0 deletions tests/Table.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -418,4 +418,9 @@ describe('Table', () => {
wrapper.find('.rc-table-row').first().simulate('mouseLeave');
expect(handleRowMouseLeave).toBeCalledWith(data[0], 0, expect.anything());
});

it('renders headerAlign and bodyAlign correctly', () => {
const wrapper = render(createTable({ headerAlign: 'center', bodyAlign: 'right' }));
expect(renderToJson(wrapper)).toMatchSnapshot();
});
});
Loading