Skip to content

Commit 1f73806

Browse files
committed
fix #1221
1 parent 9c66a20 commit 1f73806

File tree

5 files changed

+51
-15
lines changed

5 files changed

+51
-15
lines changed

packages/react-bootstrap-table2-example/examples/column-filter/clear-all-filters.js

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
import React from 'react';
22
import BootstrapTable from 'react-bootstrap-table-next';
3-
import filterFactory, { textFilter, dateFilter } from 'react-bootstrap-table2-filter';
3+
import filterFactory, { textFilter, dateFilter, selectFilter } from 'react-bootstrap-table2-filter';
44
import Code from 'components/common/code-block';
55
import { stockGenerator } from 'utils/common';
66

77
const products = stockGenerator(8);
88

9+
const selectOptions = {
10+
0: 'good',
11+
1: 'Bad',
12+
2: 'unknown'
13+
};
14+
915
let nameFilter;
1016
let priceFilter;
17+
let qualityFilter;
1118
let stockDateFilter;
1219

1320
const columns = [{
@@ -19,15 +26,28 @@ const columns = [{
1926
filter: textFilter({
2027
getFilter: (filter) => {
2128
nameFilter = filter;
22-
}
29+
},
30+
onFilter: filterVal => console.log(`Filter product name ${filterVal}`)
31+
})
32+
}, {
33+
dataField: 'quality',
34+
text: 'Product Quailty',
35+
formatter: cell => selectOptions[cell],
36+
filter: selectFilter({
37+
options: selectOptions,
38+
getFilter: (filter) => {
39+
qualityFilter = filter;
40+
},
41+
onFilter: filterVal => console.log(`Filter quality ${filterVal}`)
2342
})
2443
}, {
2544
dataField: 'price',
2645
text: 'Price',
2746
filter: textFilter({
2847
getFilter: (filter) => {
2948
priceFilter = filter;
30-
}
49+
},
50+
onFilter: filterVal => console.log(`Filter Price: ${filterVal}`)
3151
})
3252
}, {
3353
dataField: 'inStockDate',
@@ -36,13 +56,15 @@ const columns = [{
3656
filter: dateFilter({
3757
getFilter: (filter) => {
3858
stockDateFilter = filter;
39-
}
59+
},
60+
onFilter: filterVal => console.log(`Filter date: ${filterVal}`)
4061
})
4162
}];
4263

4364
const handleClick = () => {
4465
nameFilter('');
4566
priceFilter('');
67+
qualityFilter('');
4668
stockDateFilter();
4769
};
4870

packages/react-bootstrap-table2-example/src/utils/common.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ export const stockGenerator = (quantity = 5) =>
7777
name: `Stock Name ${index}`,
7878
price: Math.floor((Math.random() * 2) + 1),
7979
visible: Math.random() > 0.5,
80+
quality: index % 3,
8081
inStockDate:
8182
new Date(startDate.getTime() + Math.random() * (endDate.getTime() - startDate.getTime()))
8283
}));

packages/react-bootstrap-table2-filter/src/context.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export default (
2525
constructor(props) {
2626
super(props);
2727
this.currFilters = {};
28+
this.clearFilters = {};
2829
this.onFilter = this.onFilter.bind(this);
2930
this.doFilter = this.doFilter.bind(this);
3031
this.onExternalFilter = this.onExternalFilter.bind(this);
@@ -42,6 +43,7 @@ export default (
4243
return (filterVal) => {
4344
// watch out here if migration to context API, #334
4445
const currFilters = Object.assign({}, this.currFilters);
46+
this.clearFilters = {};
4547
const { dataField, filter } = column;
4648

4749
const needClearFilters =
@@ -51,6 +53,7 @@ export default (
5153

5254
if (needClearFilters) {
5355
delete currFilters[dataField];
56+
this.clearFilters = { [dataField]: { clear: true, filterVal } };
5457
} else {
5558
// select default comparator is EQ, others are LIKE
5659
const {
@@ -93,7 +96,7 @@ export default (
9396

9497
doFilter(props, ignoreEmitDataChange = false) {
9598
const { dataChangeListener, data, columns } = props;
96-
const result = filters(data, columns, _)(this.currFilters);
99+
const result = filters(data, columns, _)(this.currFilters, this.clearFilters);
97100
this.data = result;
98101
if (dataChangeListener && !ignoreEmitDataChange) {
99102
this.isEmitDataChange = true;

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

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -229,14 +229,13 @@ export const filterFactory = _ => (filterType) => {
229229
return filterFn;
230230
};
231231

232-
export const filters = (data, columns, _) => (currFilters) => {
232+
export const filters = (data, columns, _) => (currFilters, clearFilters = {}) => {
233233
const factory = filterFactory(_);
234+
const filterState = { ...clearFilters, ...currFilters };
234235
let result = data;
235236
let filterFn;
236-
Object.keys(currFilters).forEach((dataField) => {
237+
Object.keys(filterState).forEach((dataField) => {
237238
let currentResult;
238-
const filterObj = currFilters[dataField];
239-
filterFn = factory(filterObj.filterType);
240239
let filterValue;
241240
let customFilter;
242241
for (let i = 0; i < columns.length; i += 1) {
@@ -248,13 +247,23 @@ export const filters = (data, columns, _) => (currFilters) => {
248247
break;
249248
}
250249
}
251-
if (customFilter) {
252-
currentResult = customFilter(filterObj.filterVal, result);
253-
}
254-
if (typeof currentResult === 'undefined') {
255-
result = filterFn(result, dataField, filterObj, filterValue);
250+
251+
if (clearFilters[dataField] && customFilter) {
252+
currentResult = customFilter(clearFilters[dataField].filterVal, result);
253+
if (typeof currentResult !== 'undefined') {
254+
result = currentResult;
255+
}
256256
} else {
257-
result = currentResult;
257+
const filterObj = filterState[dataField];
258+
filterFn = factory(filterObj.filterType);
259+
if (customFilter) {
260+
currentResult = customFilter(filterObj.filterVal, result);
261+
}
262+
if (typeof currentResult === 'undefined') {
263+
result = filterFn(result, dataField, filterObj, filterValue);
264+
} else {
265+
result = currentResult;
266+
}
258267
}
259268
});
260269
return result;

packages/react-bootstrap-table2-filter/test/context.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ describe('FilterContext', () => {
171171
filterVals.forEach((filterVal) => {
172172
instance.onFilter(columns[1], FILTER_TYPE.TEXT)(filterVal);
173173
expect(Object.keys(instance.currFilters)).toHaveLength(0);
174+
expect(Object.keys(instance.clearFilters)).toHaveLength(1);
174175
});
175176
});
176177
});

0 commit comments

Comments
 (0)