Skip to content

Commit ec4864d

Browse files
committed
fix #993
1 parent 92f1449 commit ec4864d

File tree

7 files changed

+150
-2
lines changed

7 files changed

+150
-2
lines changed

docs/row-expand.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
* [expandColumnPosition](#expandColumnPosition)
1919
* [expandColumnRenderer](#expandColumnRenderer)
2020
* [expandHeaderColumnRenderer](#expandHeaderColumnRenderer)
21+
* [parentClassName](#parentClassName)
2122

2223
### <a name="renderer">expandRow.renderer - [Function]</a>
2324

@@ -165,3 +166,24 @@ const expandRow = {
165166
expandColumnPosition: 'right'
166167
};
167168
```
169+
170+
### <a name='parentClassName'>expandRow.parentClassName - [String | Function]</a>
171+
Apply the custom class name on parent row of expanded row. For example:
172+
173+
```js
174+
const expandRow = {
175+
renderer: (row) => ...,
176+
parentClassName: 'foo'
177+
};
178+
```
179+
Below case is more flexible way to custom the class name:
180+
181+
```js
182+
const expandRow = {
183+
renderer: (row) => ...,
184+
parentClassName: (isExpanded, row, rowIndex) => {
185+
if (rowIndex > 2) return 'foo';
186+
return 'bar';
187+
}
188+
};
189+
```
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import React from 'react';
2+
3+
import BootstrapTable from 'react-bootstrap-table-next';
4+
import Code from 'components/common/code-block';
5+
import { productsExpandRowsGenerator } from 'utils/common';
6+
7+
const products = productsExpandRowsGenerator();
8+
9+
const columns = [{
10+
dataField: 'id',
11+
text: 'Product ID'
12+
}, {
13+
dataField: 'name',
14+
text: 'Product Name'
15+
}, {
16+
dataField: 'price',
17+
text: 'Product Price'
18+
}];
19+
20+
const expandRow1 = {
21+
parentClassName: 'parent-expand-foo',
22+
renderer: row => (
23+
<div>
24+
<p>{ `This Expand row is belong to rowKey ${row.id}` }</p>
25+
<p>You can render anything here, also you can add additional data on every row object</p>
26+
<p>expandRow.renderer callback will pass the origin row object to you</p>
27+
</div>
28+
)
29+
};
30+
31+
const expandRow2 = {
32+
parentClassName: (isExpanded, row, rowIndex) => {
33+
if (rowIndex > 2) return 'parent-expand-foo';
34+
return 'parent-expand-bar';
35+
},
36+
renderer: row => (
37+
<div>
38+
<p>{ `This Expand row is belong to rowKey ${row.id}` }</p>
39+
<p>You can render anything here, also you can add additional data on every row object</p>
40+
<p>expandRow.renderer callback will pass the origin row object to you</p>
41+
</div>
42+
)
43+
};
44+
45+
46+
const sourceCode1 = `\
47+
import BootstrapTable from 'react-bootstrap-table-next';
48+
49+
const columns = // omit...
50+
51+
const expandRow = {
52+
parentClassName: 'parent-expand-foo',
53+
renderer: row => (
54+
<div>.....</div>
55+
)
56+
};
57+
58+
<BootstrapTable
59+
keyField='id'
60+
data={ products }
61+
columns={ columns }
62+
expandRow={ expandRow }
63+
/>
64+
`;
65+
66+
const sourceCode2 = `\
67+
import BootstrapTable from 'react-bootstrap-table-next';
68+
69+
const columns = // omit...
70+
71+
const expandRow = {
72+
parentClassName: (isExpanded, row, rowIndex) => {
73+
if (rowIndex > 2) return 'parent-expand-foo';
74+
return 'parent-expand-bar';
75+
},
76+
renderer: row => (
77+
<div>...</div>
78+
)
79+
};
80+
81+
<BootstrapTable
82+
keyField='id'
83+
data={ products }
84+
columns={ columns }
85+
expandRow={ expandRow }
86+
/>
87+
`;
88+
89+
export default () => (
90+
<div>
91+
<BootstrapTable
92+
keyField="id"
93+
data={ products }
94+
columns={ columns }
95+
expandRow={ expandRow1 }
96+
/>
97+
<Code>{ sourceCode1 }</Code>
98+
<BootstrapTable
99+
keyField="id"
100+
data={ products }
101+
columns={ columns }
102+
expandRow={ expandRow2 }
103+
/>
104+
<Code>{ sourceCode2 }</Code>
105+
</div>
106+
);

packages/react-bootstrap-table2-example/stories/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ import ExpandOnlyOne from 'examples/row-expand/expand-only-one';
165165
import CustomExpandColumn from 'examples/row-expand/custom-expand-column';
166166
import ExpandColumnPosition from 'examples/row-expand/expand-column-position';
167167
import ExpandHooks from 'examples/row-expand/expand-hooks';
168+
import ParentRowClassName from 'examples/row-expand/parent-row-classname';
168169

169170
// pagination
170171
import PaginationTable from 'examples/pagination';
@@ -414,7 +415,8 @@ storiesOf('Row Expand', module)
414415
.add('Expand Only One Row at The Same Time', () => <ExpandOnlyOne />)
415416
.add('Custom Expand Indicator', () => <CustomExpandColumn />)
416417
.add('Expand Column Position', () => <ExpandColumnPosition />)
417-
.add('Expand Hooks', () => <ExpandHooks />);
418+
.add('Expand Hooks', () => <ExpandHooks />)
419+
.add('Custom Parent Row ClassName', () => <ParentRowClassName />);
418420

419421
storiesOf('Pagination', module)
420422
.addDecorator(bootstrapStyle())
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.parent-expand-foo {
2+
background-color: coral;
3+
}
4+
5+
.parent-expand-bar {
6+
background-color: aqua;
7+
}

packages/react-bootstrap-table2-example/stories/stylesheet/storybook.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@
1212
@import "sort/index";
1313
@import "search/index";
1414
@import "loading-overlay/index";
15+
@import "row-expand/index";

packages/react-bootstrap-table2/src/bootstrap-table.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,8 @@ BootstrapTable.propTypes = {
189189
expandColumnPosition: PropTypes.oneOf([
190190
Const.INDICATOR_POSITION_LEFT,
191191
Const.INDICATOR_POSITION_RIGHT
192-
])
192+
]),
193+
parentClassName: PropTypes.oneOfType([PropTypes.string, PropTypes.func])
193194
}),
194195
rowStyle: PropTypes.oneOfType([PropTypes.object, PropTypes.func]),
195196
rowEvents: PropTypes.object,

packages/react-bootstrap-table2/src/row-expand/row-consumer.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,32 @@
11
/* eslint react/prop-types: 0 */
22
import React from 'react';
3+
import cs from 'classnames';
34
import ExpandRow from './expand-row';
45
import _ from '../utils';
56
import ExpansionContext from '../contexts/row-expand-context';
67

78
export default (Component) => {
89
const renderWithExpansion = (props, expandRow) => {
10+
let parentClassName = '';
911
const key = props.value;
1012

1113
const expanded = _.contains(expandRow.expanded, key);
1214
const isClosing = _.contains(expandRow.isClosing, key);
1315
const expandable = !expandRow.nonExpandable || !_.contains(expandRow.nonExpandable, key);
16+
if (expanded) {
17+
parentClassName = _.isFunction(expandRow.parentClassName) ?
18+
expandRow.parentClassName(expanded, props.row, props.rowIndex) :
19+
(expandRow.parentClassName || '');
20+
}
21+
1422
return [
1523
<Component
1624
{ ...props }
1725
key={ key }
1826
expanded={ expanded }
1927
expandable={ expandable }
2028
expandRow={ { ...expandRow } }
29+
className={ cs(props.className, parentClassName) }
2130
/>,
2231
expanded || isClosing ? <ExpandRow
2332
key={ `${key}-expanding` }

0 commit comments

Comments
 (0)