Skip to content

Commit 23cb0bb

Browse files
committed
fix #1054
1 parent 70827ee commit 23cb0bb

File tree

2 files changed

+59
-25
lines changed

2 files changed

+59
-25
lines changed

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,9 @@ const qualityFilter = selectFilter({
115115
// omit...
116116
```
117117

118-
> Note, the selectOptions can be an array also:
118+
> Note, the selectOptions can be an array or a function which return an array:
119+
120+
### Array as options
119121

120122
```js
121123
const selectOptions = [
@@ -133,6 +135,24 @@ const columns = [
133135
})
134136
}];
135137
```
138+
### Function as options
139+
140+
```js
141+
const selectOptions = [
142+
{ value: 0, label: 'good' },
143+
{ value: 1, label: 'Bad' },
144+
{ value: 2, label: 'unknown' }
145+
];
146+
const columns = [
147+
..., {
148+
dataField: 'quality',
149+
text: 'Product Quailty',
150+
formatter: cell => selectOptions.find(opt => opt.value === cell).label,
151+
filter: selectFilter({
152+
options: () => selectOptions
153+
})
154+
}];
155+
```
136156

137157
The benifit is `react-bootstrap-table2` will render the select options by the order of array.
138158

packages/react-bootstrap-table2-filter/src/components/select.js

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* eslint react/require-default-props: 0 */
22
/* eslint no-return-assign: 0 */
33
/* eslint react/no-unused-prop-types: 0 */
4+
/* eslint class-methods-use-this: 0 */
45
import React, { Component } from 'react';
56
import PropTypes from 'prop-types';
67
import { LIKE, EQ } from '../comparison';
@@ -44,7 +45,8 @@ class SelectFilter extends Component {
4445
constructor(props) {
4546
super(props);
4647
this.filter = this.filter.bind(this);
47-
const isSelected = getOptionValue(props.options, this.getDefaultValue()) !== undefined;
48+
this.options = this.getOptions(props);
49+
const isSelected = getOptionValue(this.options, this.getDefaultValue()) !== undefined;
4850
this.state = { isSelected };
4951
}
5052

@@ -69,19 +71,30 @@ class SelectFilter extends Component {
6971

7072
componentDidUpdate(prevProps) {
7173
let needFilter = false;
72-
if (this.props.defaultValue !== prevProps.defaultValue) {
74+
const {
75+
column,
76+
onFilter,
77+
defaultValue
78+
} = this.props;
79+
const nextOptions = this.getOptions(this.props);
80+
if (defaultValue !== prevProps.defaultValue) {
7381
needFilter = true;
74-
} else if (!optionsEquals(this.props.options, prevProps.options)) {
82+
} else if (!optionsEquals(nextOptions, this.options)) {
83+
this.options = nextOptions;
7584
needFilter = true;
7685
}
7786
if (needFilter) {
7887
const value = this.selectInput.value;
7988
if (value) {
80-
this.props.onFilter(this.props.column, FILTER_TYPE.SELECT)(value);
89+
onFilter(column, FILTER_TYPE.SELECT)(value);
8190
}
8291
}
8392
}
8493

94+
getOptions(props) {
95+
return typeof props.options === 'function' ? props.options(props.column) : props.options;
96+
}
97+
8598
getDefaultValue() {
8699
const { filterState, defaultValue } = this.props;
87100
if (filterState && typeof filterState.filterVal !== 'undefined') {
@@ -90,25 +103,6 @@ class SelectFilter extends Component {
90103
return defaultValue;
91104
}
92105

93-
getOptions() {
94-
const optionTags = [];
95-
const { options, placeholder, column, withoutEmptyOption } = this.props;
96-
if (!withoutEmptyOption) {
97-
optionTags.push((
98-
<option key="-1" value="">{ placeholder || `Select ${column.text}...` }</option>
99-
));
100-
}
101-
if (Array.isArray(options)) {
102-
options.forEach(({ value, label }) =>
103-
optionTags.push(<option key={ value } value={ value }>{ label }</option>));
104-
} else {
105-
Object.keys(options).forEach(key =>
106-
optionTags.push(<option key={ key } value={ key }>{ options[key] }</option>)
107-
);
108-
}
109-
return optionTags;
110-
}
111-
112106
cleanFiltered() {
113107
const value = (this.props.defaultValue !== undefined) ? this.props.defaultValue : '';
114108
this.setState(() => ({ isSelected: value !== '' }));
@@ -128,6 +122,26 @@ class SelectFilter extends Component {
128122
this.props.onFilter(this.props.column, FILTER_TYPE.SELECT)(value);
129123
}
130124

125+
renderOptions() {
126+
const optionTags = [];
127+
const { options } = this;
128+
const { placeholder, column, withoutEmptyOption } = this.props;
129+
if (!withoutEmptyOption) {
130+
optionTags.push((
131+
<option key="-1" value="">{ placeholder || `Select ${column.text}...` }</option>
132+
));
133+
}
134+
if (Array.isArray(options)) {
135+
options.forEach(({ value, label }) =>
136+
optionTags.push(<option key={ value } value={ value }>{ label }</option>));
137+
} else {
138+
Object.keys(options).forEach(key =>
139+
optionTags.push(<option key={ key } value={ key }>{ options[key] }</option>)
140+
);
141+
}
142+
return optionTags;
143+
}
144+
131145
render() {
132146
const {
133147
style,
@@ -163,7 +177,7 @@ class SelectFilter extends Component {
163177
onClick={ e => e.stopPropagation() }
164178
defaultValue={ this.getDefaultValue() || '' }
165179
>
166-
{ this.getOptions() }
180+
{ this.renderOptions() }
167181
</select>
168182
</label>
169183
);

0 commit comments

Comments
 (0)