Advertisement
ArCiGo

TablePaginationActions

Mar 2nd, 2018
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import FirstPageIcon from 'material-ui-icons/FirstPage';
  2. import KeyboardArrowLeft from 'material-ui-icons/KeyboardArrowLeft';
  3. import KeyboardArrowRight from 'material-ui-icons/KeyboardArrowRight';
  4. import LastPageIcon from 'material-ui-icons/LastPage';
  5. import IconButton from 'material-ui/IconButton';
  6. import { StyleRules, Theme, withStyles } from 'material-ui/styles';
  7. import * as PropTypes from 'prop-types';
  8. import * as React from 'react';
  9.  
  10. const styleClasses  = {
  11.   buttonStyle: '',
  12.   root: '',
  13. };
  14.  
  15. const styles = (theme: Theme): StyleRules<keyof typeof styleClasses> => (
  16.   {
  17.     buttonStyle: {
  18.       height: '30px',
  19.       width: '30px'
  20.     },
  21.     root: {
  22.       flexShrink: 0,
  23.       marginLeft: theme.spacing.unit * 2.5
  24.     }
  25.   }
  26. );
  27. interface IProps {
  28.   classes: any;
  29.   count: number;
  30.   page: number;
  31.   rowsPerPage: number;
  32.   onChangePage(event: React.MouseEvent<HTMLElement>, page: number): void;
  33. }
  34. const TablePaginationActions: React.SFC<IProps> = ({ classes, count, page, rowsPerPage, onChangePage }) => {
  35.   const handleFirstPageButtonClick = (event: React.MouseEvent<HTMLButtonElement>) => {
  36.     onChangePage(event, 0);
  37.   };
  38.  
  39.   const handleBackButtonClick = (event: React.MouseEvent<HTMLButtonElement>) => {
  40.     onChangePage(event, page - 1);
  41.   };
  42.  
  43.   const handleNextButtonClick = (event: React.MouseEvent<HTMLButtonElement>) => {
  44.     onChangePage(event, page + 1);
  45.   };
  46.  
  47.   const handleLastPageButtonClick = (event: React.MouseEvent<HTMLButtonElement>) => {
  48.     const $maxPage: number = Math.max(0, Math.ceil(count / rowsPerPage) - 1);
  49.  
  50.     onChangePage(
  51.       event,
  52.       $maxPage
  53.     );
  54.   };
  55.  
  56.   const handlePageButtonClick = (event: React.MouseEvent<HTMLElement>, Page: number) => {
  57.     onChangePage(
  58.       event,
  59.       Page,
  60.     );
  61.   };
  62.  
  63.   let pages: any[] = [];
  64.   const maxPage = Math.max(0, Math.ceil(count / rowsPerPage) - 1);
  65.  
  66.   if (page < 3) {
  67.     pages = [ 0, 1, 2, 3, 4 ];
  68.   } else if (maxPage === 3) {
  69.     pages = [ 0, 1, 2, 3];
  70.   } else if (page > maxPage - 2) {
  71.     pages = [maxPage - 4, maxPage - 3, maxPage - 2, maxPage - 1, maxPage];
  72.   } else {
  73.     pages = [ page - 2, page - 1, page, page + 1, page + 2 ];
  74.   }
  75.  
  76.   return (
  77.     <div className={classes.root}>
  78.       <IconButton
  79.         className={classes.buttonStyle}
  80.         onClick={handleFirstPageButtonClick}
  81.         disabled={page === 0}
  82.         aria-label='First Page'
  83.       >
  84.         <FirstPageIcon />
  85.       </IconButton>
  86.  
  87.       <IconButton
  88.         className={classes.buttonStyle}
  89.         onClick={handleBackButtonClick}
  90.         disabled={page === 0}
  91.         aria-label='Previous Page'
  92.       >
  93.         <KeyboardArrowLeft />
  94.       </IconButton>
  95.  
  96.       {
  97.         pages.map((element, index) => ( count / rowsPerPage > index &&
  98.             <IconButton
  99.               className={classes.buttonStyle}
  100.               key={index}
  101.               onClick={(event) => handlePageButtonClick(event, pages[index])}
  102.               aria-label={`Page${index + 1}`}
  103.               style={ pages[index] === page ?
  104.                 { fontSize: '18px', background: '#158cba', color: 'white' } :
  105.                 { fontSize: '18px' } }
  106.             >
  107.               {pages[index] + 1}
  108.             </IconButton>))
  109.       }
  110.  
  111.       <IconButton
  112.         className={classes.buttonStyle}
  113.         onClick={handleNextButtonClick}
  114.         disabled={page >= Math.ceil(count / rowsPerPage) - 1}
  115.         aria-label='Next Page'
  116.       >
  117.         <KeyboardArrowRight />
  118.       </IconButton>
  119.  
  120.       <IconButton
  121.         className={classes.buttonStyle}
  122.         onClick={handleLastPageButtonClick}
  123.         disabled={page >= Math.ceil(count / rowsPerPage) - 1}
  124.         aria-label='Last Page'
  125.       >
  126.         <LastPageIcon />
  127.       </IconButton>
  128.  
  129.     </div>
  130.   );
  131. };
  132.  
  133. export default withStyles(styles, { withTheme: true })(
  134.   TablePaginationActions,
  135. );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement