import * as React from 'react'; import { themr, ThemedComponentClass } from '@friendsofreactjs/react-css-themr'; import { classNames } from '@shopify/react-utilities/styles'; import Button from '../Button'; import Pager from './Pager'; import PageSize from './PageSize'; // import Options from './Options'; import { default as KEYCODE } from './KeyCode'; import { default as LOCALE } from './locale/en_US'; import { polyfill } from 'react-lifecycles-compat'; import { PAGINATION } from '../ThemeIdentifiers'; import * as baseTheme from './Pagination.scss'; import Label from '../Label'; import Icon from '../Icon'; import Select from '../Select'; import VisuallyHidden from '../VisuallyHidden'; const MAX_PAGER_BUTTONS = 7; interface IProps { disabled?: boolean; prefixCls?: string; className?: string; current: number; defaultCurrent?: number; total: number; pageSize: number; defaultPageSize?: number; onChange?(page: number, pageSize: number): void; hideOnSinglePage?: boolean; showSizeChanger?: boolean; showLessItems?: boolean; onShowSizeChange?(current: any, size: any): void; selectComponentClass?(): void; selectPrefixCls?: string; showPrevNextJumpers?: boolean; showQuickJumper?: any; showTitle?: boolean; pageSizeOptions?: any; // string[]; showTotal?(param1: any, param2: any): any; locale?: any; style?: any; itemRender?(param1: any, param2: any, param3: any): any; prevIcon?: any; nextIcon?: any; jumpPrevIcon?: any; jumpNextIcon?: any; simple?: any; theme?: any; lazyLoading?: any; pageSizeList?: number[]; fixedSize?: boolean; simplePagination?: boolean; showJumpToPage?: boolean; } interface IState { current: number; currentInputValue: number; pageSize: number; pageSizeList: number[]; initialPageSize: number; } export const DefaultProps = { disabled: false, defaultCurrent: 1, current: 1, total: 0, pageSize: 10, defaultPageSize: 10, onChange: noop, onShowSizeChange: noop, className: '', selectPrefixCls: 'rc-select', prefixCls: 'rc-pagination', selectComponentClass: noop, hideOnSinglePage: false, showPrevNextJumpers: true, showQuickJumper: { goButton: true }, showSizeChanger: true, showLessItems: true, showTitle: true, locale: LOCALE, style: {}, itemRender: defaultItemRender, pageSizeOptions: [], showTotal: noop, pageSizeList: [], simple: false, lazyLoading: false, fixedSize: true, simplePagination: false, showJumpToPage: false }; function noop() {} function isInteger(value: number) { return ( typeof value === 'number' && isFinite(value) && Math.floor(value) === value ); } function defaultItemRender(page: any, type: any, element: any) { return element; } function calculatePage(p: any, state: any, props: IProps) { let pageSize = p; if (typeof pageSize === 'undefined') { pageSize = state.pageSize; } return Math.floor((props.total - 1) / pageSize) + 1; } class Pagination extends React.PureComponent { paginationNode: any; static defaultProps = DefaultProps; constructor(props: IProps) { super(props); const hasOnChange = props.onChange !== noop; const hasCurrent = 'current' in props; if (hasCurrent && !hasOnChange) { console.warn( 'Warning: You provided a `current` prop to a Pagination component without an `onChange` handler. This will render a read-only component.' ); // eslint-disable-line } let current = props.defaultCurrent || 1; if ('current' in props) { current = props.current; } const pageSizeList: number[] = props.pageSizeList && props.pageSizeList.length > 0 ? props.pageSizeList : [10, 50, 100]; let pageSize = pageSizeList[0]; pageSize = props.defaultPageSize || pageSize || 10; if ('pageSize' in props) { pageSize = props.pageSize; } current = Math.min(current, calculatePage(pageSize, undefined, props)); this.state = { current, pageSize, pageSizeList, currentInputValue: current, initialPageSize: pageSize, }; } componentDidUpdate(prevProps: IProps, prevState: IState) { // When current page change, fix focused style of prev item // A hacky solution of https://github.com/ant-design/ant-design/issues/8948 const { theme } = this.props; if (prevState.current !== this.state.current && this.paginationNode) { const lastCurrentNode = this.paginationNode.querySelector( theme[`rc-pagination-item-${prevState.current}`] ); if (lastCurrentNode && document.activeElement === lastCurrentNode) { lastCurrentNode.blur(); } } } componentWillReceiveProps(newProps: IProps) { const { current, pageSize } = newProps; const stateObj: any = {}; if (this.props.current !== current) { stateObj['current'] = current; } if (this.props.pageSize !== pageSize) { stateObj['pageSize'] = pageSize; } this.setState(stateObj); } getJumpPrevPage = () => { return Math.max(1, this.state.current - (this.props.showLessItems ? 3 : 5)); } getJumpNextPage = () => { return Math.min( calculatePage(undefined, this.state, this.props), this.state.current + (this.props.showLessItems ? 3 : 5) ); } /** * computed icon node that need to be rendered. * @param {React.ReactNode | React.PureComponentType} icon received icon. * @returns {React.ReactNode} */ getItemIcon = (icon: any, iconType: string, disableProp?: boolean) => { const { theme } = this.props; let iconButton; if (iconType === 'prev') { iconButton = ( ); } else { gotoButton = ( {goButton} ); } gotoButton = (
  • {gotoButton}
  • ); } const className1 = classNames( theme['rc-pagination'], theme['rc-pagination-simple'], className ); const className2 = classNames( !this.hasPrev() && theme['rc-pagination-disabled'], !this.hasPrev() && theme['rc-pagination-prev'] ); const className3 = classNames( !this.hasNext() && theme['rc-pagination-disabled'], !this.hasPrev() && theme['rc-pagination-next'] ); return ( ); } getPagerRange = () => { const { fixedSize, showLessItems } = this.props; const numPages = calculatePage(undefined, this.state, this.props); const pageBufferSize = showLessItems ? 1 : 2; const { current } = this.state; let left = Math.max(1, current - pageBufferSize); let right = Math.min(current + pageBufferSize, numPages); const maxPageLimit = showLessItems ? MAX_PAGER_BUTTONS : MAX_PAGER_BUTTONS + 2; if (numPages <= maxPageLimit) { left = 1; right = numPages; return [left, right]; } if (current - 1 <= pageBufferSize) { right = 1 + pageBufferSize * 2; } if (numPages - current <= pageBufferSize) { left = numPages - pageBufferSize * 2; } const minButtons = fixedSize ? 3 : 1; if (fixedSize && current < 5) { left = minButtons; right = left + 2; } else if (fixedSize && numPages - current < 4) { right = numPages - 2; left = right - 2; } return [left, right]; } addPrevNextNavigation = (pagerList: any) => { const { showLessItems, locale, showTitle, itemRender = defaultItemRender, jumpNextIcon, jumpPrevIcon, theme, } = this.props; const numPages = calculatePage(undefined, this.state, this.props); const pageBufferSize = showLessItems ? 1 : 2; const { current } = this.state; let jumpPrev; let jumpNext; const prevItemTitle = showLessItems ? locale.prev_3 : locale.prev_5; const nextItemTitle = showLessItems ? locale.next_3 : locale.next_5; const className4 = classNames( theme['rc-pagination-jump-prev'], jumpPrevIcon && theme['rc-pagination-jump-prev-custom-icon'] ); jumpPrev = (
  • {itemRender( this.getJumpPrevPage(), 'jump-prev', this.getItemIcon(jumpPrevIcon, 'jump-prev') )}
  • ); const className5 = classNames( theme['rc-pagination-jump-next'], jumpNextIcon && theme['rc-pagination-jump-next-custom-icon'] ); jumpNext = (
  • {itemRender( this.getJumpNextPage(), 'jump-next', this.getItemIcon(jumpNextIcon, 'jump-next') )}
  • ); if (current - 1 >= pageBufferSize * 2 && current !== 1 + 2) { pagerList[0] = React.cloneElement(pagerList[0], { className: theme['rc-pagination-item-after-jump-prev'], }); pagerList.unshift(jumpPrev); } if (numPages - current >= pageBufferSize * 2 && current !== numPages - 2) { pagerList[pagerList.length - 1] = React.cloneElement( pagerList[pagerList.length - 1], { className: theme['rc-pagination-item-before-jump-next'], } ); pagerList.push(jumpNext); } } addFirstLastNavigation = (pagerList: any) => { const { locale, showTitle, itemRender = defaultItemRender, theme, } = this.props; const numPages = calculatePage(undefined, this.state, this.props); const { current } = this.state; let firstPager; let lastPager; let active = false; active = current === 1; firstPager = ( ); active = current === numPages; lastPager = ( ); const [left, right] = this.getPagerRange(); if (left !== 1) { pagerList.unshift(firstPager); } if (right !== numPages) { pagerList.push(lastPager); } } addSpacer = (pagerList: any, adjacent = true) => { const { locale, showTitle, itemRender = defaultItemRender, theme, } = this.props; const numPages = calculatePage(undefined, this.state, this.props); const { current } = this.state; if ( Math.abs( parseInt(pagerList[1].key, 10) - parseInt(pagerList[0].key, 10) ) > 2 ) { const className = classNames(theme['rc-pagination-spacer']); const classNameIcon = classNames(theme['rc-pagination-spacer-icon']); const firstDotsPager = (
  • ); pagerList.splice(1, 0, [firstDotsPager]); } else if ( Math.abs( parseInt(pagerList[1].key, 10) - parseInt(pagerList[0].key, 10) ) === 2 && adjacent ) { const active = current === 2; const adjacent = ( ); pagerList.splice(1, 0, adjacent); } if ( Math.abs( parseInt(pagerList[pagerList.length - 1].key, 10) - parseInt(pagerList[pagerList.length - 2].key, 10) ) > 2 ) { const className = classNames(theme['rc-pagination-spacer']); const classNameIcon = classNames(theme['rc-pagination-spacer-icon']); const lastDotsPager = (
  • ); pagerList.splice(pagerList.length - 1, 0, [lastDotsPager]); } else if ( Math.abs( parseInt(pagerList[pagerList.length - 1].key, 10) - parseInt(pagerList[pagerList.length - 2].key, 10) ) === 2 && adjacent ) { const active = current === numPages - 1; const adjacent = ( ); pagerList.splice(pagerList.length - 1, 0, [adjacent]); } } addPageButtons = (pagerList: any) => { const { locale, showTitle, itemRender = defaultItemRender, theme, } = this.props; const { current } = this.state; const [left, right] = this.getPagerRange(); for (let i = left; i <= right; i++) { const active = current === i; pagerList.push( ); } } renderSimplePagination = () => { const { simplePagination, pageSize, theme } = this.props; const { initialPageSize } = this.state; const numPages = calculatePage(undefined, this.state, this.props); if (simplePagination && numPages > 1) { return (
  • ); } if (simplePagination && numPages === 1 && initialPageSize !== pageSize) { return (
  • ); } return null; } renderJumpToPage = () => { const { theme, showLessItems, } = this.props; const { current = 1, pageSize } = this.state; const numPages = calculatePage(undefined, this.state, this.props); const maxPageLimit = showLessItems ? MAX_PAGER_BUTTONS : MAX_PAGER_BUTTONS + 2; if (numPages <= maxPageLimit) { return null; } const options: any = [...Array(numPages).keys()].map(x => ({ label: x + 1, value: x + 1 })); const onChange = (x: any) => { this.setState({ current: parseInt(x, 10) }, () => { if (this.props.onChange) { this.props.onChange(parseInt(x, 10) || 1, pageSize); } }); }; return (
    Jump to page: