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