Skip to content

Commit 3747c36

Browse files
authored
Merge pull request #973 from react-bootstrap-table/develop
20190610 release
2 parents 60eb47d + 0d0d1a8 commit 3747c36

File tree

17 files changed

+506
-31
lines changed

17 files changed

+506
-31
lines changed

docs/row-selection.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@
1616
* [clickToEdit](#clickToEdit)
1717
* [onSelect](#onSelect)
1818
* [onSelectAll](#onSelectAll)
19+
* [selectColumnPosition](#selectColumnPosition)
1920
* [hideSelectColumn](#hideSelectColumn)
2021
* [hideSelectAll](#hideSelectAll)
2122
* [selectionRenderer](#selectionRenderer)
2223
* [selectionHeaderRenderer](#selectionHeaderRenderer)
2324
* [headerColumnStyle](#headerColumnStyle)
25+
* [selectColumnStyle](#selectColumnStyle)
2426

2527
### <a name="mode">selectRow.mode - [String]</a>
2628

@@ -224,6 +226,42 @@ const selectRow = {
224226
};
225227
```
226228

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+
227265
### <a name='onSelect'>selectRow.onSelect - [Function]</a>
228266
This callback function will be called when a row is select/unselect and pass following three arguments:
229267
`row`, `isSelect`, `rowIndex` and `e`.
@@ -275,6 +313,16 @@ const selectRow = {
275313
};
276314
```
277315

316+
### <a name='selectColumnPosition'>selectRow.selectColumnPosition - [String]</a>
317+
Default is `left`. You can give this as `right` for rendering selection column in the right side.
318+
319+
```js
320+
const selectRow = {
321+
mode: 'checkbox',
322+
selectColumnPosition: 'right'
323+
};
324+
```
325+
278326
### <a name='hideSelectColumn'>selectRow.hideSelectColumn - [Bool]</a>
279327
Default is `false`, if you don't want to have a selection column, give this prop as `true`
280328

packages/react-bootstrap-table2-editor/README.md

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,10 @@ const columns = [
8989
In the following, we go though all the predefined editors:
9090

9191
### Dropdown Editor
92-
Dropdown editor give a select menu to choose a data from a list, the `editor.options` is required property for dropdown editor.
92+
Dropdown editor give a select menu to choose a data from a list. When use dropdown editor, either `editor.options` or `editor.getOptions` should be required prop.
93+
94+
#### editor.options
95+
This is most simple case for assign the dropdown options data directly.
9396

9497
```js
9598
import { Type } from 'react-bootstrap-table2-editor';
@@ -119,6 +122,46 @@ const columns = [
119122
}];
120123
```
121124

125+
#### editor.getOptions
126+
It is much flexible which accept a function and you can assign the dropdown options dynamically.
127+
128+
There are two case for `getOptions`:
129+
130+
* *Synchronous*: Just return the options array in `getOptions` callback function
131+
* *Asynchronous*: Call `setOptions` function argument when you get the options from remote.
132+
133+
134+
```js
135+
// Synchronous
136+
137+
const columns = [
138+
..., {
139+
dataField: 'type',
140+
text: 'Job Type',
141+
editor: {
142+
type: Type.SELECT,
143+
getOptions: () => [.....]
144+
}
145+
}];
146+
147+
// Asynchronous
148+
149+
const columns = [
150+
..., {
151+
dataField: 'type',
152+
text: 'Job Type',
153+
editor: {
154+
type: Type.SELECT,
155+
getOptions: (setOptions) => {
156+
setTimeout(() => setOptions([...]), 1500);
157+
}
158+
}
159+
}];
160+
161+
```
162+
163+
[here](https://react-bootstrap-table.github.io/react-bootstrap-table2/storybook/index.html?selectedKind=Cell%20Editing&selectedStory=Dropdown%20Editor%20with%20Dynamic%20Options) is an online example.
164+
122165
### Date Editor
123166
Date editor is use `<input type="date">`, the configuration is very simple:
124167

packages/react-bootstrap-table2-editor/src/dropdown-editor.js

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,32 @@ import cs from 'classnames';
44
import PropTypes from 'prop-types';
55

66
class DropDownEditor extends Component {
7+
constructor(props) {
8+
super(props);
9+
let options = props.options;
10+
if (props.getOptions) {
11+
options = props.getOptions(this.setOptions.bind(this)) || [];
12+
}
13+
this.state = { options };
14+
}
15+
716
componentDidMount() {
817
const { defaultValue, didMount } = this.props;
918
this.select.value = defaultValue;
1019
this.select.focus();
1120
if (didMount) didMount();
1221
}
1322

23+
setOptions(options) {
24+
this.setState({ options });
25+
}
26+
1427
getValue() {
1528
return this.select.value;
1629
}
1730

1831
render() {
19-
const { defaultValue, didMount, className, options, ...rest } = this.props;
32+
const { defaultValue, didMount, getOptions, className, ...rest } = this.props;
2033
const editorClass = cs('form-control editor edit-select', className);
2134

2235
const attr = {
@@ -31,7 +44,7 @@ class DropDownEditor extends Component {
3144
defaultValue={ defaultValue }
3245
>
3346
{
34-
options.map(({ label, value }) => (
47+
this.state.options.map(({ label, value }) => (
3548
<option key={ value } value={ value }>{ label }</option>
3649
))
3750
}
@@ -52,13 +65,16 @@ DropDownEditor.propTypes = {
5265
label: PropTypes.string,
5366
value: PropTypes.any
5467
}))
55-
]).isRequired,
56-
didMount: PropTypes.func
68+
]),
69+
didMount: PropTypes.func,
70+
getOptions: PropTypes.func
5771
};
5872
DropDownEditor.defaultProps = {
5973
className: '',
6074
defaultValue: '',
6175
style: {},
62-
didMount: undefined
76+
options: [],
77+
didMount: undefined,
78+
getOptions: undefined
6379
};
6480
export default DropDownEditor;
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
/* eslint react/prefer-stateless-function: 0 */
2+
import React from 'react';
3+
4+
import BootstrapTable from 'react-bootstrap-table-next';
5+
import cellEditFactory, { Type } from 'react-bootstrap-table2-editor';
6+
import Code from 'components/common/code-block';
7+
import { jobsGenerator } from 'utils/common';
8+
9+
const jobs = jobsGenerator().map(j => ({
10+
...j,
11+
type2: j.type
12+
}));
13+
14+
const columns = [{
15+
dataField: 'id',
16+
text: 'Job ID'
17+
}, {
18+
dataField: 'name',
19+
text: 'Job Name'
20+
}, {
21+
dataField: 'owner',
22+
text: 'Job Owner'
23+
}, {
24+
dataField: 'type',
25+
text: 'Job Type1',
26+
editor: {
27+
type: Type.SELECT,
28+
getOptions: () => [{
29+
value: 'A',
30+
label: 'A'
31+
}, {
32+
value: 'B',
33+
label: 'B'
34+
}, {
35+
value: 'C',
36+
label: 'C'
37+
}, {
38+
value: 'D',
39+
label: 'D'
40+
}, {
41+
value: 'E',
42+
label: 'E'
43+
}]
44+
}
45+
}, {
46+
dataField: 'type2',
47+
text: 'Job Type2',
48+
editor: {
49+
type: Type.SELECT,
50+
getOptions: (setOptions) => {
51+
setTimeout(() => {
52+
setOptions([{
53+
value: 'A',
54+
label: 'A'
55+
}, {
56+
value: 'B',
57+
label: 'B'
58+
}, {
59+
value: 'C',
60+
label: 'C'
61+
}, {
62+
value: 'D',
63+
label: 'D'
64+
}, {
65+
value: 'E',
66+
label: 'E'
67+
}]);
68+
}, 2000);
69+
}
70+
}
71+
}];
72+
73+
const sourceCode = `\
74+
import BootstrapTable from 'react-bootstrap-table-next';
75+
import cellEditFactory, { Type } from 'react-bootstrap-table2-editor';
76+
77+
const columns = [{
78+
dataField: 'id',
79+
text: 'Job ID'
80+
}, {
81+
dataField: 'name',
82+
text: 'Job Name'
83+
}, {
84+
dataField: 'owner',
85+
text: 'Job Owner'
86+
}, {
87+
dataField: 'type',
88+
text: 'Job Type1',
89+
editor: {
90+
type: Type.SELECT,
91+
getOptions: () => [{
92+
value: 'A',
93+
label: 'A'
94+
}, {
95+
value: 'B',
96+
label: 'B'
97+
}, {
98+
value: 'C',
99+
label: 'C'
100+
}, {
101+
value: 'D',
102+
label: 'D'
103+
}, {
104+
value: 'E',
105+
label: 'E'
106+
}]
107+
}
108+
}, {
109+
dataField: 'type2',
110+
text: 'Job Type2',
111+
editor: {
112+
type: Type.SELECT,
113+
getOptions: (setOptions) => {
114+
setTimeout(() => {
115+
setOptions([{
116+
value: 'A',
117+
label: 'A'
118+
}, {
119+
value: 'B',
120+
label: 'B'
121+
}, {
122+
value: 'C',
123+
label: 'C'
124+
}, {
125+
value: 'D',
126+
label: 'D'
127+
}, {
128+
value: 'E',
129+
label: 'E'
130+
}]);
131+
}, 2000);
132+
}
133+
}
134+
}];
135+
136+
<BootstrapTable
137+
keyField="id"
138+
data={ jobs }
139+
columns={ columns }
140+
cellEdit={ cellEditFactory({ mode: 'click', blurToSave: true }) }
141+
/>
142+
`;
143+
144+
export default () => (
145+
<div>
146+
<h3>Dropdown Editor with Dynamic Options</h3>
147+
<BootstrapTable
148+
keyField="id"
149+
data={ jobs }
150+
columns={ columns }
151+
cellEdit={ cellEditFactory({ mode: 'click', blurToSave: true }) }
152+
/>
153+
<Code>{ sourceCode }</Code>
154+
</div>
155+
);

0 commit comments

Comments
 (0)