@@ -5,135 +5,152 @@ import React, { Component } from 'react';
55import PropTypes from 'prop-types' ;
66
77import Const from './const' ;
8+ import Pagination from './pagination' ;
89import { getByCurrPage } from './page' ;
910
10- const wrapperFactory = baseElement =>
11- class PaginationWrapper extends Component {
12- static propTypes = {
13- store : PropTypes . object . isRequired
11+ class PaginationWrapper extends Component {
12+ static propTypes = {
13+ store : PropTypes . object . isRequired ,
14+ baseElement : PropTypes . func . isRequired
15+ }
16+
17+ constructor ( props ) {
18+ super ( props ) ;
19+ this . handleChangePage = this . handleChangePage . bind ( this ) ;
20+ this . handleChangeSizePerPage = this . handleChangeSizePerPage . bind ( this ) ;
21+
22+ let currPage ;
23+ let currSizePerPage ;
24+ const { options } = props . pagination ;
25+ const sizePerPageList = options . sizePerPageList || Const . SIZE_PER_PAGE_LIST ;
26+
27+ // initialize current page
28+ if ( typeof options . page !== 'undefined' ) {
29+ currPage = options . page ;
30+ } else if ( typeof options . pageStartIndex !== 'undefined' ) {
31+ currPage = options . pageStartIndex ;
32+ } else {
33+ currPage = Const . PAGE_START_INDEX ;
1434 }
1535
16- constructor ( props ) {
17- super ( props ) ;
18- this . handleChangePage = this . handleChangePage . bind ( this ) ;
19- this . handleChangeSizePerPage = this . handleChangeSizePerPage . bind ( this ) ;
20-
21- let currPage ;
22- let currSizePerPage ;
23- const options = props . pagination . options || { } ;
24- const sizePerPageList = options . sizePerPageList || Const . SIZE_PER_PAGE_LIST ;
25-
26- // initialize current page
27- if ( typeof options . page !== 'undefined' ) {
28- currPage = options . page ;
29- } else if ( typeof options . pageStartIndex !== 'undefined' ) {
30- currPage = options . pageStartIndex ;
31- } else {
32- currPage = Const . PAGE_START_INDEX ;
33- }
34-
35- // initialize current sizePerPage
36- if ( typeof options . sizePerPage !== 'undefined' ) {
37- currSizePerPage = options . sizePerPage ;
38- } else if ( typeof sizePerPageList [ 0 ] === 'object' ) {
39- currSizePerPage = sizePerPageList [ 0 ] . value ;
40- } else {
41- currSizePerPage = sizePerPageList [ 0 ] ;
42- }
43-
44- this . state = { currPage, currSizePerPage } ;
36+ // initialize current sizePerPage
37+ if ( typeof options . sizePerPage !== 'undefined' ) {
38+ currSizePerPage = options . sizePerPage ;
39+ } else if ( typeof sizePerPageList [ 0 ] === 'object' ) {
40+ currSizePerPage = sizePerPageList [ 0 ] . value ;
41+ } else {
42+ currSizePerPage = sizePerPageList [ 0 ] ;
4543 }
4644
47- isRemote ( ) {
48- const { remote } = this . props ;
49- return remote === true || ( typeof remote === 'object' && remote . pagination ) ;
50- }
45+ this . state = { currPage, currSizePerPage } ;
46+ }
5147
52- handleChangePage ( currPage ) {
53- const { currSizePerPage } = this . state ;
54- const { pagination : { options } , onRemotePageChange } = this . props ;
55- if ( options . onPageChange ) {
56- options . onPageChange ( currPage , currSizePerPage ) ;
57- }
58- if ( this . isRemote ( ) ) {
59- onRemotePageChange ( currPage , currSizePerPage ) ;
60- return ;
61- }
62- this . setState ( ( ) => {
63- return {
64- currPage
65- } ;
66- } ) ;
48+ componentWillReceiveProps ( nextProps ) {
49+ let needNewState = false ;
50+ let { currPage, currSizePerPage } = this . state ;
51+ const { page, sizePerPage } = nextProps . pagination . options ;
52+ if ( typeof page !== 'undefined' ) {
53+ currPage = page ;
54+ needNewState = true ;
6755 }
68-
69- handleChangeSizePerPage ( currSizePerPage , currPage ) {
70- const { pagination : { options } , onRemotePageChange } = this . props ;
71- if ( options . onSizePerPageChange ) {
72- options . onSizePerPageChange ( currSizePerPage , currPage ) ;
73- }
74- if ( this . isRemote ( ) ) {
75- onRemotePageChange ( currPage , currSizePerPage ) ;
76- return ;
77- }
78- this . setState ( ( ) => {
79- return {
80- currPage,
81- currSizePerPage
82- } ;
83- } ) ;
56+ if ( typeof sizePerPage !== 'undefined' ) {
57+ currSizePerPage = sizePerPage ;
58+ needNewState = true ;
8459 }
8560
86- render ( ) {
87- const { pagination : { Pagination, options } , store } = this . props ;
88- const { currPage, currSizePerPage } = this . state ;
89- const withFirstAndLast = typeof options . withFirstAndLast === 'undefined' ?
90- Const . With_FIRST_AND_LAST : options . withFirstAndLast ;
91- const alwaysShowAllBtns = typeof options . alwaysShowAllBtns === 'undefined' ?
92- Const . SHOW_ALL_PAGE_BTNS : options . alwaysShowAllBtns ;
93- const hideSizePerPage = typeof options . hideSizePerPage === 'undefined' ?
94- Const . HIDE_SIZE_PER_PAGE : options . hideSizePerPage ;
95- const hidePageListOnlyOnePage = typeof options . hidePageListOnlyOnePage === 'undefined' ?
96- Const . HIDE_PAGE_LIST_ONLY_ONE_PAGE : options . hidePageListOnlyOnePage ;
97- const pageStartIndex = typeof options . pageStartIndex === 'undefined' ?
98- Const . PAGE_START_INDEX : options . pageStartIndex ;
99-
100- const data = this . isRemote ( ) ?
101- this . props . data :
102- getByCurrPage ( store ) ( currPage , currSizePerPage , pageStartIndex ) ;
103-
104- const base = baseElement ( {
105- ...this . props ,
106- key : 'table' ,
107- data
108- } ) ;
109-
110- return [
111- base ,
112- < Pagination
113- key = "pagination"
114- dataSize = { options . totalSize || store . data . length }
115- currPage = { currPage }
116- currSizePerPage = { currSizePerPage }
117- onPageChange = { this . handleChangePage }
118- onSizePerPageChange = { this . handleChangeSizePerPage }
119- sizePerPageList = { options . sizePerPageList || Const . SIZE_PER_PAGE_LIST }
120- paginationSize = { options . paginationSize || Const . PAGINATION_SIZE }
121- pageStartIndex = { pageStartIndex }
122- withFirstAndLast = { withFirstAndLast }
123- alwaysShowAllBtns = { alwaysShowAllBtns }
124- hideSizePerPage = { hideSizePerPage }
125- hidePageListOnlyOnePage = { hidePageListOnlyOnePage }
126- firstPageText = { options . firstPageText || Const . FIRST_PAGE_TEXT }
127- prePageText = { options . prePageText || Const . PRE_PAGE_TEXT }
128- nextPageText = { options . nextPageText || Const . NEXT_PAGE_TEXT }
129- lastPageText = { options . lastPageText || Const . LAST_PAGE_TEXT }
130- prePageTitle = { options . prePageTitle || Const . PRE_PAGE_TITLE }
131- nextPageTitle = { options . nextPageTitle || Const . NEXT_PAGE_TITLE }
132- firstPageTitle = { options . firstPageTitle || Const . FIRST_PAGE_TITLE }
133- lastPageTitle = { options . lastPageTitle || Const . LAST_PAGE_TITLE }
134- />
135- ] ;
136- }
137- } ;
61+ if ( needNewState ) this . setState ( ( ) => ( { currPage, currSizePerPage } ) ) ;
62+ }
63+
64+ isRemote ( ) {
65+ const { remote } = this . props ;
66+ return remote === true || ( typeof remote === 'object' && remote . pagination ) ;
67+ }
13868
139- export default wrapperFactory ;
69+ handleChangePage ( currPage ) {
70+ const { currSizePerPage } = this . state ;
71+ const { pagination : { options } , onRemotePageChange } = this . props ;
72+ if ( options . onPageChange ) {
73+ options . onPageChange ( currPage , currSizePerPage ) ;
74+ }
75+ if ( this . isRemote ( ) ) {
76+ onRemotePageChange ( currPage , currSizePerPage ) ;
77+ return ;
78+ }
79+ this . setState ( ( ) => {
80+ return {
81+ currPage
82+ } ;
83+ } ) ;
84+ }
85+
86+ handleChangeSizePerPage ( currSizePerPage , currPage ) {
87+ const { pagination : { options } , onRemotePageChange } = this . props ;
88+ if ( options . onSizePerPageChange ) {
89+ options . onSizePerPageChange ( currSizePerPage , currPage ) ;
90+ }
91+ if ( this . isRemote ( ) ) {
92+ onRemotePageChange ( currPage , currSizePerPage ) ;
93+ return ;
94+ }
95+ this . setState ( ( ) => {
96+ return {
97+ currPage,
98+ currSizePerPage
99+ } ;
100+ } ) ;
101+ }
102+
103+ render ( ) {
104+ const { pagination : { options } , store, baseElement } = this . props ;
105+ const { currPage, currSizePerPage } = this . state ;
106+ const withFirstAndLast = typeof options . withFirstAndLast === 'undefined' ?
107+ Const . With_FIRST_AND_LAST : options . withFirstAndLast ;
108+ const alwaysShowAllBtns = typeof options . alwaysShowAllBtns === 'undefined' ?
109+ Const . SHOW_ALL_PAGE_BTNS : options . alwaysShowAllBtns ;
110+ const hideSizePerPage = typeof options . hideSizePerPage === 'undefined' ?
111+ Const . HIDE_SIZE_PER_PAGE : options . hideSizePerPage ;
112+ const hidePageListOnlyOnePage = typeof options . hidePageListOnlyOnePage === 'undefined' ?
113+ Const . HIDE_PAGE_LIST_ONLY_ONE_PAGE : options . hidePageListOnlyOnePage ;
114+ const pageStartIndex = typeof options . pageStartIndex === 'undefined' ?
115+ Const . PAGE_START_INDEX : options . pageStartIndex ;
116+
117+ const data = this . isRemote ( ) ?
118+ this . props . data :
119+ getByCurrPage ( store ) ( currPage , currSizePerPage , pageStartIndex ) ;
120+
121+ const base = baseElement ( {
122+ ...this . props ,
123+ key : 'table' ,
124+ data
125+ } ) ;
126+
127+ return [
128+ base ,
129+ < Pagination
130+ key = "pagination"
131+ dataSize = { options . totalSize || store . data . length }
132+ currPage = { currPage }
133+ currSizePerPage = { currSizePerPage }
134+ onPageChange = { this . handleChangePage }
135+ onSizePerPageChange = { this . handleChangeSizePerPage }
136+ sizePerPageList = { options . sizePerPageList || Const . SIZE_PER_PAGE_LIST }
137+ paginationSize = { options . paginationSize || Const . PAGINATION_SIZE }
138+ pageStartIndex = { pageStartIndex }
139+ withFirstAndLast = { withFirstAndLast }
140+ alwaysShowAllBtns = { alwaysShowAllBtns }
141+ hideSizePerPage = { hideSizePerPage }
142+ hidePageListOnlyOnePage = { hidePageListOnlyOnePage }
143+ firstPageText = { options . firstPageText || Const . FIRST_PAGE_TEXT }
144+ prePageText = { options . prePageText || Const . PRE_PAGE_TEXT }
145+ nextPageText = { options . nextPageText || Const . NEXT_PAGE_TEXT }
146+ lastPageText = { options . lastPageText || Const . LAST_PAGE_TEXT }
147+ prePageTitle = { options . prePageTitle || Const . PRE_PAGE_TITLE }
148+ nextPageTitle = { options . nextPageTitle || Const . NEXT_PAGE_TITLE }
149+ firstPageTitle = { options . firstPageTitle || Const . FIRST_PAGE_TITLE }
150+ lastPageTitle = { options . lastPageTitle || Const . LAST_PAGE_TITLE }
151+ />
152+ ] ;
153+ }
154+ }
155+
156+ export default PaginationWrapper ;
0 commit comments