import * as React from 'react'; import { Button, ButtonGroup, FormGroup, FormControl } from 'react-bootstrap'; import styles from './paginationControls.module.scss'; import { If, Then, Else } from 'react-if'; import { observer } from 'mobx-react'; import classNames from 'classnames'; import { EditableSpan } from 'cbioportal-frontend-commons'; export const SHOW_ALL_PAGE_SIZE = -1; export const MAX_DIGITS = 6; export interface IPaginationControlsProps { currentPage?: number; totalItems?: number; itemsPerPage?: number; itemsPerPageOptions?: number[]; // sorted ascending showAllOption?: boolean; showMoreButton?: boolean; textBeforeButtons?: string; textBetweenButtons?: string; firstButtonContent?: string | JSX.Element; previousButtonContent?: string | JSX.Element; nextButtonContent?: string | JSX.Element; lastButtonContent?: string | JSX.Element; showFirstPage?: boolean; showLastPage?: boolean; showItemsPerPageSelector?: boolean; onChangeItemsPerPage?: (itemsPerPage: number) => void; onFirstPageClick?: () => void; onPreviousPageClick?: () => void; onNextPageClick?: () => void; onLastPageClick?: () => void; onChangeCurrentPage?: (newPage: number) => void; className?: string; style?: { [k: string]: string | number }; firstPageDisabled?: boolean; previousPageDisabled?: boolean; nextPageDisabled?: boolean; lastPageDisabled?: boolean; pageNumberEditable?: boolean; groupButtons?: boolean; hidePaginationIfOnePage?: boolean; bsStyle?: 'default' | 'primary' | 'success' | 'info' | 'warning'; } @observer export class PaginationControls extends React.Component< IPaginationControlsProps, {} > { public static defaultProps = { itemsPerPage: SHOW_ALL_PAGE_SIZE, itemsPerPageOptions: [10, 25, 50, 100], showAllOption: true, textBeforeButtons: '', textBetweenButtons: 'text btwn buttons', firstButtonContent: , previousButtonContent: , nextButtonContent: , lastButtonContent: , showItemsPerPageSelector: true, showFirstPage: false, showLastPage: false, className: '', style: {}, previousPageDisabled: false, nextPageDisabled: false, pageNumberEditable: false, showMoreButton: true, groupButtons: true, hidePaginationIfOnePage: true, bsStyle: 'default', }; constructor(props: IPaginationControlsProps) { super(props); this.handleChangeItemsPerPage = this.handleChangeItemsPerPage.bind( this ); this.resetItemsPerPage = this.resetItemsPerPage.bind(this); this.jumpToPage = this.jumpToPage.bind(this); this.handleShowMore = this.handleShowMore.bind(this); this.getSectionBetweenPaginationButtons = this.getSectionBetweenPaginationButtons.bind( this ); } private jumpToPage(p: string) { if (this.props.onChangeCurrentPage) { this.props.onChangeCurrentPage(parseInt(p, 10)); } } handleChangeItemsPerPage(evt: React.FormEvent) { if (this.props.onChangeItemsPerPage) { this.props.onChangeItemsPerPage( parseInt((evt.target as HTMLSelectElement).value, 10) ); } } handleShowMore() { if ( this.props.itemsPerPageOptions && this.props.itemsPerPageOptions.length > 0 && this.props.itemsPerPage && this.props.onChangeItemsPerPage ) { const index = this.props.itemsPerPageOptions.indexOf( this.props.itemsPerPage ); if ( index > -1 && index < this.props.itemsPerPageOptions.length - 1 ) { this.props.onChangeItemsPerPage( this.props.itemsPerPageOptions[index + 1] ); } else { this.props.onChangeItemsPerPage( this.props.itemsPerPage + this.props.itemsPerPageOptions[ this.props.itemsPerPageOptions.length - 1 ] ); } } } resetItemsPerPage() { this.props.onChangeItemsPerPage && this.props.itemsPerPageOptions && this.props.itemsPerPageOptions.length > 0 && this.props.onChangeItemsPerPage(this.props.itemsPerPageOptions[0]); } private getSectionBetweenPaginationButtons() { if (this.props.showMoreButton) { return ( ); } else { return ( {this.props.textBetweenButtons} ); } } private get shouldHidePaginationControls() { return ( this.props.hidePaginationIfOnePage && this.props.itemsPerPageOptions && typeof this.props.totalItems !== 'undefined' && this.props.itemsPerPageOptions[0] >= this.props.totalItems ); } render() { const pageSizeOptionElts = (this.props.itemsPerPageOptions || []).map( (opt: number) => ( ) ); if (this.props.showAllOption) { pageSizeOptionElts.push( ); } let buttons: any = [ , , this.getSectionBetweenPaginationButtons(), , , , ]; if (this.props.groupButtons) { buttons = ( {buttons} ); } else { buttons =
{buttons}
; } if (this.shouldHidePaginationControls) { buttons = null; } return (
{this.props.textBeforeButtons} {buttons} } > {pageSizeOptionElts}
); } } export default PaginationControls;