Skip to content

Commit ecea3ef

Browse files
committed
1 parent 8bef7eb commit ecea3ef

File tree

5 files changed

+126
-31
lines changed

5 files changed

+126
-31
lines changed

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

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,18 @@ export default (
1717
class FilterProvider extends React.Component {
1818
static propTypes = {
1919
data: PropTypes.array.isRequired,
20-
columns: PropTypes.array.isRequired
20+
columns: PropTypes.array.isRequired,
21+
listenerForPagination: PropTypes.object
2122
}
2223

2324
constructor(props) {
2425
super(props);
2526
this.currFilters = {};
2627
this.onFilter = this.onFilter.bind(this);
2728
this.onExternalFilter = this.onExternalFilter.bind(this);
29+
this.state = {
30+
data: props.data
31+
};
2832
}
2933

3034
componentDidMount() {
@@ -68,7 +72,12 @@ export default (
6872
filter.props.onFilter(filterVal);
6973
}
7074

71-
this.forceUpdate();
75+
const { listenerForPagination, data } = this.props;
76+
const result = filters(data, this.props.columns, _)(this.currFilters);
77+
if (listenerForPagination) {
78+
listenerForPagination.emit('filterChanged', result.length);
79+
}
80+
this.setState({ data: result });
7281
};
7382
}
7483

@@ -79,13 +88,9 @@ export default (
7988
}
8089

8190
render() {
82-
let { data } = this.props;
83-
if (!isRemoteFiltering()) {
84-
data = filters(data, this.props.columns, _)(this.currFilters);
85-
}
8691
return (
8792
<FilterContext.Provider value={ {
88-
data,
93+
data: this.state.data,
8994
onFilter: this.onFilter,
9095
onExternalFilter: this.onExternalFilter
9196
} }

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ describe('FilterContext', () => {
4545

4646
function shallowContext(
4747
enableRemote = false,
48-
tableColumns = columns
48+
tableColumns = columns,
49+
listenerForPagination,
4950
) {
5051
mockBase.mockReset();
5152
handleFilterChange.mockReset();
@@ -59,6 +60,7 @@ describe('FilterContext', () => {
5960
<FilterContext.Provider
6061
columns={ tableColumns }
6162
data={ data }
63+
listenerForPagination={ listenerForPagination }
6264
>
6365
<FilterContext.Consumer>
6466
{
@@ -252,6 +254,23 @@ describe('FilterContext', () => {
252254
});
253255
});
254256

257+
describe('when props.listenerForPagination is defined', () => {
258+
const filterVal = '3';
259+
const newDataLength = 0;
260+
const listenerForPagination = { emit: jest.fn() };
261+
262+
beforeEach(() => {
263+
wrapper = shallow(shallowContext(false, columns, listenerForPagination));
264+
wrapper.render();
265+
instance = wrapper.instance();
266+
});
267+
268+
it('should call listenerForPagination.emit correctly', () => {
269+
instance.onFilter(columns[1], FILTER_TYPE.TEXT)(filterVal);
270+
expect(listenerForPagination.emit).toHaveBeenCalledWith('filterChanged', newDataLength);
271+
});
272+
});
273+
255274
describe('combination', () => {
256275
beforeEach(() => {
257276
wrapper = shallow(shallowContext());

packages/react-bootstrap-table2-paginator/src/state-context.js

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@
22
/* eslint react/require-default-props: 0 */
33
/* eslint no-lonely-if: 0 */
44
import React from 'react';
5+
import EventEmitter from 'events';
56
import Const from './const';
7+
import { alignPage } from './page';
68

79
const StateContext = React.createContext();
810

911
class StateProvider extends React.Component {
1012
constructor(props) {
1113
super(props);
1214
this.handleChangePage = this.handleChangePage.bind(this);
15+
this.handleDataSizeChange = this.handleDataSizeChange.bind(this);
1316
this.handleChangeSizePerPage = this.handleChangeSizePerPage.bind(this);
1417

1518
let currPage;
@@ -36,7 +39,10 @@ class StateProvider extends React.Component {
3639
}
3740

3841
this.currPage = currPage;
42+
this.dataSize = options.totalSize;
3943
this.currSizePerPage = currSizePerPage;
44+
this.filterListener = new EventEmitter();
45+
this.filterListener.on('filterChanged', this.handleDataSizeChange);
4046
}
4147

4248
componentWillReceiveProps(nextProps) {
@@ -51,7 +57,7 @@ class StateProvider extends React.Component {
5157

5258
getPaginationProps = () => {
5359
const { pagination: { options }, bootstrap4 } = this.props;
54-
const { currPage, currSizePerPage } = this;
60+
const { currPage, currSizePerPage, dataSize } = this;
5561
const withFirstAndLast = typeof options.withFirstAndLast === 'undefined' ?
5662
Const.With_FIRST_AND_LAST : options.withFirstAndLast;
5763
const alwaysShowAllBtns = typeof options.alwaysShowAllBtns === 'undefined' ?
@@ -72,7 +78,7 @@ class StateProvider extends React.Component {
7278
hideSizePerPage,
7379
alwaysShowAllBtns,
7480
withFirstAndLast,
75-
dataSize: options.totalSize,
81+
dataSize,
7682
sizePerPageList: options.sizePerPageList || Const.SIZE_PER_PAGE_LIST,
7783
paginationSize: options.paginationSize || Const.PAGINATION_SIZE,
7884
showTotal: options.showTotal,
@@ -106,6 +112,20 @@ class StateProvider extends React.Component {
106112
return e.result;
107113
};
108114

115+
handleDataSizeChange(newDataSize) {
116+
const { pagination: { options } } = this.props;
117+
const pageStartIndex = typeof options.pageStartIndex === 'undefined' ?
118+
Const.PAGE_START_INDEX : options.pageStartIndex;
119+
this.dataSize = newDataSize;
120+
this.currPage = alignPage(
121+
newDataSize,
122+
this.currPage,
123+
this.currSizePerPage,
124+
pageStartIndex
125+
);
126+
this.forceUpdate();
127+
}
128+
109129
handleChangePage(currPage) {
110130
const { currSizePerPage } = this;
111131
const { pagination: { options } } = this.props;
@@ -153,7 +173,8 @@ class StateProvider extends React.Component {
153173
paginationProps,
154174
paginationTableProps: {
155175
pagination,
156-
setPaginationRemoteEmitter: this.setPaginationRemoteEmitter
176+
setPaginationRemoteEmitter: this.setPaginationRemoteEmitter,
177+
listenerForPagination: this.filterListener
157178
}
158179
} }
159180
>

0 commit comments

Comments
 (0)