forked from react-bootstrap-table/react-bootstrap-table2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstate-context.js
200 lines (176 loc) · 6.67 KB
/
state-context.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/* eslint react/prop-types: 0 */
/* eslint react/require-default-props: 0 */
/* eslint no-lonely-if: 0 */
/* eslint camelcase: 0 */
import React from 'react';
import EventEmitter from 'events';
import Const from './const';
import { alignPage } from './page';
const StateContext = React.createContext();
class StateProvider extends React.Component {
constructor(props) {
super(props);
this.handleChangePage = this.handleChangePage.bind(this);
this.handleDataSizeChange = this.handleDataSizeChange.bind(this);
this.handleChangeSizePerPage = this.handleChangeSizePerPage.bind(this);
let currPage;
let currSizePerPage;
const { options } = props.pagination;
const sizePerPageList = options.sizePerPageList || Const.SIZE_PER_PAGE_LIST;
// initialize current page
if (typeof options.page !== 'undefined') {
currPage = options.page;
} else if (typeof options.pageStartIndex !== 'undefined') {
currPage = options.pageStartIndex;
} else {
currPage = Const.PAGE_START_INDEX;
}
// initialize current sizePerPage
if (typeof options.sizePerPage !== 'undefined') {
currSizePerPage = options.sizePerPage;
} else if (typeof sizePerPageList[0] === 'object') {
currSizePerPage = sizePerPageList[0].value;
} else {
currSizePerPage = sizePerPageList[0];
}
this.currPage = currPage;
this.dataSize = options.totalSize;
this.currSizePerPage = currSizePerPage;
this.dataChangeListener = new EventEmitter();
this.dataChangeListener.on('filterChanged', this.handleDataSizeChange);
}
getPaginationProps = () => {
const { pagination: { options }, bootstrap4, tableId } = this.props;
const { currPage, currSizePerPage, dataSize } = this;
const withFirstAndLast = typeof options.withFirstAndLast === 'undefined' ?
Const.With_FIRST_AND_LAST : options.withFirstAndLast;
const alwaysShowAllBtns = typeof options.alwaysShowAllBtns === 'undefined' ?
Const.SHOW_ALL_PAGE_BTNS : options.alwaysShowAllBtns;
const hideSizePerPage = typeof options.hideSizePerPage === 'undefined' ?
Const.HIDE_SIZE_PER_PAGE : options.hideSizePerPage;
const hidePageListOnlyOnePage = typeof options.hidePageListOnlyOnePage === 'undefined' ?
Const.HIDE_PAGE_LIST_ONLY_ONE_PAGE : options.hidePageListOnlyOnePage;
const pageStartIndex = typeof options.pageStartIndex === 'undefined' ?
Const.PAGE_START_INDEX : options.pageStartIndex;
return {
...options,
bootstrap4,
tableId,
page: currPage,
sizePerPage: currSizePerPage,
pageStartIndex,
hidePageListOnlyOnePage,
hideSizePerPage,
alwaysShowAllBtns,
withFirstAndLast,
dataSize,
sizePerPageList: options.sizePerPageList || Const.SIZE_PER_PAGE_LIST,
paginationSize: options.paginationSize || Const.PAGINATION_SIZE,
showTotal: options.showTotal,
pageListRenderer: options.pageListRenderer,
pageButtonRenderer: options.pageButtonRenderer,
sizePerPageRenderer: options.sizePerPageRenderer,
paginationTotalRenderer: options.paginationTotalRenderer,
sizePerPageOptionRenderer: options.sizePerPageOptionRenderer,
firstPageText: options.firstPageText || Const.FIRST_PAGE_TEXT,
prePageText: options.prePageText || Const.PRE_PAGE_TEXT,
nextPageText: options.nextPageText || Const.NEXT_PAGE_TEXT,
lastPageText: options.lastPageText || Const.LAST_PAGE_TEXT,
prePageTitle: options.prePageTitle || Const.PRE_PAGE_TITLE,
nextPageTitle: options.nextPageTitle || Const.NEXT_PAGE_TITLE,
firstPageTitle: options.firstPageTitle || Const.FIRST_PAGE_TITLE,
lastPageTitle: options.lastPageTitle || Const.LAST_PAGE_TITLE,
onPageChange: this.handleChangePage,
onSizePerPageChange: this.handleChangeSizePerPage
};
}
setPaginationRemoteEmitter = (remoteEmitter) => {
this.remoteEmitter = remoteEmitter;
}
getPaginationRemoteEmitter = () => this.remoteEmitter || this.props.remoteEmitter;
UNSAFE_componentWillReceiveProps(nextProps) {
const { custom } = nextProps.pagination.options;
// user should align the page when the page is not fit to the data size when remote enable
if (this.isRemotePagination() || custom) {
if (typeof nextProps.pagination.options.page !== 'undefined') {
this.currPage = nextProps.pagination.options.page;
}
if (typeof nextProps.pagination.options.sizePerPage !== 'undefined') {
this.currSizePerPage = nextProps.pagination.options.sizePerPage;
}
if (typeof nextProps.pagination.options.totalSize !== 'undefined') {
this.dataSize = nextProps.pagination.options.totalSize;
}
}
}
isRemotePagination = () => {
const e = {};
this.remoteEmitter.emit('isRemotePagination', e);
return e.result;
};
handleDataSizeChange(newDataSize) {
const { pagination: { options } } = this.props;
const pageStartIndex = typeof options.pageStartIndex === 'undefined' ?
Const.PAGE_START_INDEX : options.pageStartIndex;
this.currPage = alignPage(
newDataSize,
this.dataSize,
this.currPage,
this.currSizePerPage,
pageStartIndex
);
this.dataSize = newDataSize;
this.forceUpdate();
}
handleChangePage(currPage) {
const { currSizePerPage } = this;
const { pagination: { options } } = this.props;
if (options.onPageChange) {
options.onPageChange(currPage, currSizePerPage);
}
this.currPage = currPage;
if (this.isRemotePagination()) {
this.getPaginationRemoteEmitter().emit('paginationChange', currPage, currSizePerPage);
return;
}
this.forceUpdate();
}
handleChangeSizePerPage(currSizePerPage, currPage) {
const { pagination: { options } } = this.props;
if (options.onSizePerPageChange) {
options.onSizePerPageChange(currSizePerPage, currPage);
}
this.currPage = currPage;
this.currSizePerPage = currSizePerPage;
if (this.isRemotePagination()) {
this.getPaginationRemoteEmitter().emit('paginationChange', currPage, currSizePerPage);
return;
}
this.forceUpdate();
}
render() {
const paginationProps = this.getPaginationProps();
const pagination = {
...this.props.pagination,
options: paginationProps
};
return (
<StateContext.Provider
value={ {
paginationProps,
paginationTableProps: {
pagination,
setPaginationRemoteEmitter: this.setPaginationRemoteEmitter,
dataChangeListener: this.dataChangeListener
}
} }
>
{ this.props.children }
</StateContext.Provider>
);
}
}
export default () => ({
Provider: StateProvider,
Consumer: StateContext.Consumer
});