Skip to content

add rowStyle and expandedRowStyle Properties #149

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 5 commits 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
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,12 @@ React.render(<Table columns={columns} data={data} />, mountNode);
<td></td>
<td>get row's className</td>
</tr>
<tr>
<td>rowStyle</td>
<td>Function(record, index, indent):object</td>
<td></td>
<td>get row's style</td>
</tr>
<tr>
<td>rowRef</td>
<td>Function(record, index, indent):string</td>
Expand Down Expand Up @@ -164,6 +170,12 @@ React.render(<Table columns={columns} data={data} />, mountNode);
<td></td>
<td>get expanded row's className</td>
</tr>
<tr>
<td>expandedRowStyle</td>
<td>Function(recode, index, indent):object</td>
<td></td>
<td>get expanded row's style</td>
</tr>
<tr>
<td>data</td>
<td>Object[]</td>
Expand Down
Empty file added examples/style.html
Empty file.
46 changes: 46 additions & 0 deletions examples/style.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/* eslint-disable no-console,func-names,react/no-multi-comp */
import React from 'react';
import ReactDOM from 'react-dom';
import Table from 'rc-table';
import 'rc-table/assets/index.less';

const columns = [
{ title: 'title1', dataIndex: 'a',
className: 'a',
key: 'a', width: 100 },
{ id: '123', title: 'title2', dataIndex: 'b',
className: 'b',
key: 'b', width: 100 },
{ title: 'title3', dataIndex: 'c',
className: 'c',
key: 'c', width: 200 },
{
title: 'Operations', dataIndex: '',
className: 'd',
key: 'd', render() {
return <a href="#">Operations</a>;
},
},
];

const data = [
{ a: '123', key: '1' },
{ a: 'cdd', b: 'edd', key: '2' },
{ a: '1333', c: 'eee', d: 2, key: '3' },
];

ReactDOM.render(
<div>
<h2>rowStyle</h2>
<Table
columns={columns}
indentSize={30}
rowStyle={(record, i) => i === 0 ? { backgroundColor: '#eee' } : null}
expandedRowRender={record => <p>extra: {record.a}</p>}
expandedRowStyle={(record, i) => i === 0 ? { backgroundColor: '#f3f3f3' } : null}
data={data}
className="table"
/>
</div>,
document.getElementById('__react-content')
);
15 changes: 13 additions & 2 deletions src/Table.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ export default class Table extends React.Component {
style: PropTypes.object,
rowKey: PropTypes.oneOfType([PropTypes.string, PropTypes.func]),
rowClassName: PropTypes.func,
rowStyle: PropTypes.func,
expandedRowClassName: PropTypes.func,
expandedRowStyle: PropTypes.func,
childrenColumnName: PropTypes.string,
onExpand: PropTypes.func,
onExpandedRowsChange: PropTypes.func,
Expand Down Expand Up @@ -52,7 +54,9 @@ export default class Table extends React.Component {
defaultExpandedRowKeys: [],
rowKey: 'key',
rowClassName: () => '',
rowStyle: () => {},
expandedRowClassName: () => '',
expandedRowStyle: () => {},
onExpand() {},
onExpandedRowsChange() {},
onRowClick() {},
Expand Down Expand Up @@ -252,7 +256,7 @@ export default class Table extends React.Component {
return rows.filter(row => row.length > 0);
}

getExpandedRow(key, content, visible, className, fixed) {
getExpandedRow(key, content, visible, className, style, fixed) {
const { prefixCls, expandIconAsCell } = this.props;
let colCount;
if (fixed === 'left') {
Expand Down Expand Up @@ -282,6 +286,7 @@ export default class Table extends React.Component {
columns={columns}
visible={visible}
className={className}
style={style}
key={`${key}-extra-row`}
rowKey={`${key}-extra-row`}
prefixCls={`${prefixCls}-expanded-row`}
Expand All @@ -301,8 +306,10 @@ export default class Table extends React.Component {
expandedRowRender,
expandRowByClick,
rowClassName,
rowStyle,
rowRef,
expandedRowClassName,
expandedRowStyle,
onRowClick,
onRowDoubleClick,
onRowContextMenu,
Expand All @@ -329,6 +336,7 @@ export default class Table extends React.Component {
expandedRowContent = expandedRowRender(record, i, indent);
}
const className = rowClassName(record, i, indent);
const style = rowStyle(record, i, indent);

const onHoverProps = {};
if (this.columnManager.isAnyColumnsFixed()) {
Expand All @@ -354,6 +362,7 @@ export default class Table extends React.Component {
indentSize={props.indentSize}
needIndentSpaced={needIndentSpaced}
className={className}
style={style}
record={record}
expandIconAsCell={expandIconAsCell}
onDestroy={this.onRowDestroy}
Expand Down Expand Up @@ -384,8 +393,10 @@ export default class Table extends React.Component {
const subVisible = visible && isRowExpanded;

if (expandedRowContent && isRowExpanded) {
const expRowClassName = expandedRowClassName(record, i, indent);
const expRowStyle = expandedRowStyle(record, i, indent);
rst.push(this.getExpandedRow(
key, expandedRowContent, subVisible, expandedRowClassName(record, i, indent), fixed
key, expandedRowContent, subVisible, expRowClassName, expRowStyle, fixed
));
}
if (childrenColumn) {
Expand Down
3 changes: 2 additions & 1 deletion src/TableRow.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export default class TableRow extends React.Component {
onExpand: PropTypes.func,
needIndentSpaced: PropTypes.bool,
className: PropTypes.string,
style: PropTypes.object,
indent: PropTypes.number,
indentSize: PropTypes.number,
expandIconAsCell: PropTypes.bool,
Expand Down Expand Up @@ -193,7 +194,7 @@ export default class TableRow extends React.Component {
);
}
const height = this.props.height || this.state.height;
const style = { height };
const style = { ...this.props.style, height };
if (!visible) {
style.display = 'none';
}
Expand Down
6 changes: 6 additions & 0 deletions tests/__snapshots__/Table.expandRow.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ exports[`Table.expand controlled by expandedRowKeys 1`] = `
]
}
expandedRowRender={[Function]}
expandedRowStyle={[Function]}
getBodyWrapper={[Function]}
indentSize={15}
onExpand={[Function]}
Expand All @@ -55,6 +56,7 @@ exports[`Table.expand controlled by expandedRowKeys 1`] = `
rowClassName={[Function]}
rowKey="key"
rowRef={[Function]}
rowStyle={[Function]}
scroll={Object {}}
showHeader={true}
style={Object {}}
Expand Down Expand Up @@ -571,6 +573,7 @@ exports[`Table.expand controlled by expandedRowKeys 2`] = `
]
}
expandedRowRender={[Function]}
expandedRowStyle={[Function]}
getBodyWrapper={[Function]}
indentSize={15}
onExpand={[Function]}
Expand All @@ -584,6 +587,7 @@ exports[`Table.expand controlled by expandedRowKeys 2`] = `
rowClassName={[Function]}
rowKey="key"
rowRef={[Function]}
rowStyle={[Function]}
scroll={Object {}}
showHeader={true}
style={Object {}}
Expand Down Expand Up @@ -1188,6 +1192,7 @@ exports[`Table.expand expand row by click 1`] = `
expandIconColumnIndex={0}
expandedRowClassName={[Function]}
expandedRowRender={[Function]}
expandedRowStyle={[Function]}
getBodyWrapper={[Function]}
indentSize={15}
onExpand={[Function]}
Expand All @@ -1201,6 +1206,7 @@ exports[`Table.expand expand row by click 1`] = `
rowClassName={[Function]}
rowKey="key"
rowRef={[Function]}
rowStyle={[Function]}
scroll={Object {}}
showHeader={true}
style={Object {}}
Expand Down