Advertisement
Guest User

Untitled

a guest
Dec 16th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import PropTypes from "prop-types";
  2. import React from "react";
  3.  
  4. import times from "lodash.times";
  5.  
  6. import PreviousPages from "../PreviousPages";
  7. import NextPages from "../NextPages";
  8. import Page from "../Page";
  9.  
  10. import styles from "./Pagination.scss";
  11.  
  12. class Pagination extends React.Component {
  13.   constructor(props) {
  14.     super(props);
  15.  
  16.     this.previousPages = this.previousPages.bind(this);
  17.     this.nextPages = this.nextPages.bind(this);
  18.   }
  19.  
  20.   previousPages() {
  21.     const {
  22.       page,
  23.       window,
  24.       ...other
  25.     } = this.props;
  26.  
  27.     const linkAmount = page - window <= 1 ? new Array(page).length - 1 : window;
  28.  
  29.     return (
  30.       times(linkAmount, (count) => (
  31.         <Page {...other} key={count} page={page - count - 1} />
  32.       )).reverse()
  33.     );
  34.   }
  35.  
  36.   nextPages() {
  37.     const {
  38.       page,
  39.       window,
  40.       totalPages,
  41.       ...other
  42.     } = this.props;
  43.  
  44.     const linkAmount = page + window >= totalPages ? totalPages - page : window;
  45.  
  46.     return (
  47.       times(linkAmount, (count) => (
  48.         <Page {...other} key={count} page={page + count + 1} />
  49.       ))
  50.     );
  51.   }
  52.  
  53.   render() {
  54.     const {
  55.       page,
  56.       totalPages,
  57.       ...other
  58.     } = this.props;
  59.  
  60.     if (totalPages < 1) { return false; }
  61.  
  62.     return (
  63.       <div className={styles["guc-pagination"]}>
  64.         <PreviousPages {...this.props} />
  65.         <div className={styles["pages"]}>
  66.           {this.previousPages()}
  67.  
  68.           <Page {...other} key={page}
  69.             current={page === page}
  70.             page={page} variants={["-current"]} />
  71.  
  72.           {this.nextPages()}
  73.         </div>
  74.         <NextPages {...this.props} />
  75.       </div>
  76.     );
  77.   }
  78. }
  79.  
  80. Pagination.defaultProps = {
  81.   page: 1,
  82.   totalPages: 1,
  83.   window: 2
  84. };
  85.  
  86. Pagination.propTypes = {
  87.   totalPages: PropTypes.number,
  88.   page: PropTypes.number,
  89.   window: PropTypes.number
  90. };
  91.  
  92. export default Pagination;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement