Skip to content

Commit 0d0d1a8

Browse files
committed
fix #947
1 parent db612ea commit 0d0d1a8

File tree

5 files changed

+167
-3
lines changed

5 files changed

+167
-3
lines changed

docs/row-selection.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
* [selectionRenderer](#selectionRenderer)
2323
* [selectionHeaderRenderer](#selectionHeaderRenderer)
2424
* [headerColumnStyle](#headerColumnStyle)
25+
* [selectColumnStyle](#selectColumnStyle)
2526

2627
### <a name="mode">selectRow.mode - [String]</a>
2728

@@ -225,6 +226,42 @@ const selectRow = {
225226
};
226227
```
227228

229+
### <a name='selectColumnStyle'>selectRow.selectColumnStyle - [Object | Function]</a>
230+
A way to custome the selection cell. `selectColumnStyle` not only accept a simple style object but also a callback function for more flexible customization:
231+
232+
### Style Object
233+
234+
```js
235+
const selectRow = {
236+
mode: 'checkbox',
237+
selectColumnStyle: { backgroundColor: 'blue' }
238+
};
239+
```
240+
241+
### Callback Function
242+
If a callback function present, you can get below information to custom the selection cell:
243+
244+
* `checked`: Whether current row is seleccted or not
245+
* `disabled`: Whether current row is disabled or not
246+
* `rowIndex`: Current row index
247+
* `rowKey`: Current row key
248+
249+
250+
```js
251+
const selectRow = {
252+
mode: 'checkbox',
253+
selectColumnStyle: ({
254+
checked,
255+
disabled,
256+
rowIndex,
257+
rowKey
258+
}) => (
259+
// ....
260+
return { backgroundColor: 'blue' };
261+
)
262+
};
263+
```
264+
228265
### <a name='onSelect'>selectRow.onSelect - [Function]</a>
229266
This callback function will be called when a row is select/unselect and pass following three arguments:
230267
`row`, `isSelect`, `rowIndex` and `e`.
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/* eslint no-unused-vars: 0 */
2+
import React from 'react';
3+
4+
import BootstrapTable from 'react-bootstrap-table-next';
5+
import Code from 'components/common/code-block';
6+
import { productsGenerator } from 'utils/common';
7+
8+
const products = productsGenerator(2);
9+
10+
const columns = [{
11+
dataField: 'id',
12+
text: 'Product ID'
13+
}, {
14+
dataField: 'name',
15+
text: 'Product Name'
16+
}, {
17+
dataField: 'price',
18+
text: 'Product Price'
19+
}];
20+
21+
const selectRow1 = {
22+
mode: 'checkbox',
23+
clickToSelect: true,
24+
selectColumnStyle: {
25+
backgroundColor: 'grey'
26+
}
27+
};
28+
29+
const sourceCode1 = `\
30+
import BootstrapTable from 'react-bootstrap-table-next';
31+
32+
const columns = ...
33+
34+
const selectRow = {
35+
mode: 'checkbox',
36+
clickToSelect: true,
37+
selectColumnStyle: {
38+
backgroundColor: 'grey'
39+
}
40+
};
41+
42+
<BootstrapTable
43+
keyField='id'
44+
data={ products }
45+
columns={ columns }
46+
selectRow={ selectRow }
47+
/>
48+
`;
49+
50+
const selectRow2 = {
51+
mode: 'checkbox',
52+
clickToSelect: true,
53+
selectColumnStyle: ({
54+
checked,
55+
disabled,
56+
rowIndex,
57+
rowKey
58+
}) => {
59+
if (checked) {
60+
return {
61+
backgroundColor: 'yellow'
62+
};
63+
}
64+
return {
65+
backgroundColor: 'pink'
66+
};
67+
}
68+
};
69+
70+
const sourceCode2 = `\
71+
import BootstrapTable from 'react-bootstrap-table-next';
72+
73+
const columns = ...
74+
75+
const selectRow = {
76+
mode: 'checkbox',
77+
clickToSelect: true,
78+
selectColumnStyle: ({
79+
checked,
80+
disabled,
81+
rowIndex,
82+
rowKey
83+
}) => {
84+
if (checked) {
85+
return {
86+
backgroundColor: 'yellow'
87+
};
88+
}
89+
return {
90+
backgroundColor: 'pink'
91+
};
92+
}
93+
};
94+
95+
<BootstrapTable
96+
keyField='id'
97+
data={ products }
98+
columns={ columns }
99+
selectRow={ selectRow }
100+
/>
101+
`;
102+
103+
export default () => (
104+
<div>
105+
<BootstrapTable keyField="id" data={ products } columns={ columns } selectRow={ selectRow1 } />
106+
<Code>{ sourceCode1 }</Code>
107+
<BootstrapTable keyField="id" data={ products } columns={ columns } selectRow={ selectRow2 } />
108+
<Code>{ sourceCode2 }</Code>
109+
</div>
110+
);

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ import NonSelectableRowsTable from 'examples/row-selection/non-selectable-rows';
150150
import SelectionBgColorTable from 'examples/row-selection/selection-bgcolor';
151151
import SelectionHooks from 'examples/row-selection/selection-hooks';
152152
import HideSelectionColumnTable from 'examples/row-selection/hide-selection-column';
153+
import SelectionColumnStyleTable from 'examples/row-selection/select-column-style';
153154
import SelectionColumnPositionTable from 'examples/row-selection/selection-column-position';
154155

155156
// work on row expand
@@ -396,6 +397,7 @@ storiesOf('Row Selection', module)
396397
.add('Not Selectabled Rows', () => <NonSelectableRowsTable />)
397398
.add('Selection Hooks', () => <SelectionHooks />)
398399
.add('Hide Selection Column', () => <HideSelectionColumnTable />)
400+
.add('Custom Selection Column Style', () => <SelectionColumnStyleTable />)
399401
.add('Selection Column Position', () => <SelectionColumnPositionTable />);
400402

401403
storiesOf('Row Expand', module)

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ BootstrapTable.propTypes = {
169169
selectionRenderer: PropTypes.func,
170170
selectionHeaderRenderer: PropTypes.func,
171171
headerColumnStyle: PropTypes.oneOfType([PropTypes.object, PropTypes.func]),
172+
selectColumnStyle: PropTypes.oneOfType([PropTypes.object, PropTypes.func]),
172173
selectColumnPosition: PropTypes.oneOf([
173174
Const.INDICATOR_POSITION_LEFT,
174175
Const.INDICATOR_POSITION_RIGHT

packages/react-bootstrap-table2/src/row-selection/selection-cell.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import React, { Component } from 'react';
66
import PropTypes from 'prop-types';
77
import Const from '../const';
8+
import _ from '../utils';
89
import { BootstrapContext } from '../contexts/bootstrap';
910

1011
export default class SelectionCell extends Component {
@@ -17,7 +18,8 @@ export default class SelectionCell extends Component {
1718
rowIndex: PropTypes.number,
1819
tabIndex: PropTypes.number,
1920
clickToSelect: PropTypes.bool,
20-
selectionRenderer: PropTypes.func
21+
selectionRenderer: PropTypes.func,
22+
selectColumnStyle: PropTypes.oneOfType([PropTypes.object, PropTypes.func])
2123
}
2224

2325
constructor() {
@@ -31,7 +33,8 @@ export default class SelectionCell extends Component {
3133
this.props.selected !== nextProps.selected ||
3234
this.props.disabled !== nextProps.disabled ||
3335
this.props.rowKey !== nextProps.rowKey ||
34-
this.props.tabIndex !== nextProps.tabIndex;
36+
this.props.tabIndex !== nextProps.tabIndex ||
37+
this.props.selectColumnStyle !== nextProps.selectColumnStyle;
3538

3639
return shouldUpdate;
3740
}
@@ -57,17 +60,28 @@ export default class SelectionCell extends Component {
5760

5861
render() {
5962
const {
63+
rowKey,
6064
mode: inputType,
6165
selected,
6266
disabled,
6367
tabIndex,
6468
rowIndex,
65-
selectionRenderer
69+
selectionRenderer,
70+
selectColumnStyle
6671
} = this.props;
6772

6873
const attrs = {};
6974
if (tabIndex !== -1) attrs.tabIndex = tabIndex;
7075

76+
attrs.style = _.isFunction(selectColumnStyle) ?
77+
selectColumnStyle({
78+
checked: selected,
79+
disabled,
80+
rowIndex,
81+
rowKey
82+
}) :
83+
selectColumnStyle;
84+
7185
return (
7286
<BootstrapContext.Consumer>
7387
{

0 commit comments

Comments
 (0)