zhongrj
2025-11-24 276323dce9613867abb3f58a4cc2abbfb2fd0dea
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
import React from 'react';
import update from 'immutability-helper';
import HistoryNav from '../classes/HistoryNav';
import PropTypes from 'prop-types';
 
class Paginated extends React.Component{
  static defaultProps = {
    currentPage: 1
  };
 
  static propTypes = {
      history: PropTypes.object.isRequired, // reference to the history object coming from the route this component is bound to
      currentPage: PropTypes.number
  };
 
  constructor(props){
    super(props);
 
    this.historyNav = new HistoryNav(props.history);
  }
 
  updatePagination(itemsPerPage, totalItems){
    let currentPage = this.props.currentPage;
    const totalPages = this.totalPages(itemsPerPage, totalItems);
 
    if (currentPage > totalPages) currentPage = totalPages;
 
    this.setState({pagination: {
        switchingPages: false,
        itemsPerPage: itemsPerPage,
        totalItems: totalItems
      }
    });
  }
 
  totalPages(itemsPerPage, totalItems){
    let pages = Math.ceil(totalItems / itemsPerPage);
    if (pages < 1) pages = 1;
    return pages;
  }
 
  setPaginationState(props, done){
    this.setState(update(this.state, {
      pagination: {
        $merge: props
      }
    }), done);
  }
 
  handlePageItemsNumChange(delta, needsRefreshCallback){
    let currentPage = this.props.currentPage;
    const pagesBefore = this.totalPages(this.state.pagination.itemsPerPage, this.state.pagination.totalItems),
          pagesAfter = this.totalPages(this.state.pagination.itemsPerPage, this.state.pagination.totalItems + delta);
 
    if (currentPage > pagesAfter) currentPage = pagesAfter;
 
    this.historyNav.changeQS('page', currentPage);
 
    this.setPaginationState({
      totalItems: this.state.pagination.totalItems + delta
    }, () => {
      if (pagesBefore !== pagesAfter) needsRefreshCallback(pagesAfter);
    });
  }
 
  render(){
    throw new Error("Override me");
  }
 
export default Paginated;