2017-11-17 14:38:54 +08:00
|
|
|
import * as React from 'react';
|
2016-03-31 17:46:35 +08:00
|
|
|
import RcPagination from 'rc-pagination';
|
2017-09-26 23:12:47 +08:00
|
|
|
import enUS from 'rc-pagination/lib/locale/en_US';
|
2017-03-17 15:23:25 +08:00
|
|
|
import classNames from 'classnames';
|
2020-03-02 12:09:38 +08:00
|
|
|
import LeftOutlined from '@ant-design/icons/LeftOutlined';
|
|
|
|
import RightOutlined from '@ant-design/icons/RightOutlined';
|
|
|
|
import DoubleLeftOutlined from '@ant-design/icons/DoubleLeftOutlined';
|
|
|
|
import DoubleRightOutlined from '@ant-design/icons/DoubleRightOutlined';
|
2019-08-13 14:07:17 +08:00
|
|
|
|
2016-03-31 17:46:35 +08:00
|
|
|
import MiniSelect from './MiniSelect';
|
2018-12-05 19:12:18 +08:00
|
|
|
import Select from '../select';
|
|
|
|
import LocaleReceiver from '../locale-provider/LocaleReceiver';
|
2020-05-24 23:13:32 +08:00
|
|
|
import { ConfigContext } from '../config-provider';
|
|
|
|
import useBreakpoint from '../grid/hooks/useBreakpoint';
|
2016-03-31 17:46:35 +08:00
|
|
|
|
2016-08-15 12:00:05 +08:00
|
|
|
export interface PaginationProps {
|
2017-11-21 13:48:37 +08:00
|
|
|
total?: number;
|
2017-03-17 15:23:25 +08:00
|
|
|
defaultCurrent?: number;
|
2019-05-10 15:38:35 +08:00
|
|
|
disabled?: boolean;
|
2017-03-17 15:23:25 +08:00
|
|
|
current?: number;
|
2016-08-15 12:00:05 +08:00
|
|
|
defaultPageSize?: number;
|
|
|
|
pageSize?: number;
|
2017-11-21 13:48:37 +08:00
|
|
|
onChange?: (page: number, pageSize?: number) => void;
|
2017-12-29 20:14:53 +08:00
|
|
|
hideOnSinglePage?: boolean;
|
2016-08-15 12:00:05 +08:00
|
|
|
showSizeChanger?: boolean;
|
2017-03-17 15:23:25 +08:00
|
|
|
pageSizeOptions?: string[];
|
2016-08-15 12:00:05 +08:00
|
|
|
onShowSizeChange?: (current: number, size: number) => void;
|
2019-02-14 22:39:11 +08:00
|
|
|
showQuickJumper?: boolean | { goButton?: React.ReactNode };
|
2020-04-11 16:36:12 +08:00
|
|
|
showTitle?: boolean;
|
2017-08-02 13:03:42 +08:00
|
|
|
showTotal?: (total: number, range: [number, number]) => React.ReactNode;
|
2020-03-10 20:32:34 +08:00
|
|
|
size?: 'default' | 'small';
|
|
|
|
responsive?: boolean;
|
2017-03-06 15:48:03 +08:00
|
|
|
simple?: boolean;
|
2016-08-15 12:00:05 +08:00
|
|
|
style?: React.CSSProperties;
|
|
|
|
locale?: Object;
|
2017-03-17 15:23:25 +08:00
|
|
|
className?: string;
|
2016-09-14 16:18:33 +08:00
|
|
|
prefixCls?: string;
|
|
|
|
selectPrefixCls?: string;
|
2018-12-07 20:02:01 +08:00
|
|
|
itemRender?: (
|
|
|
|
page: number,
|
|
|
|
type: 'page' | 'prev' | 'next' | 'jump-prev' | 'jump-next',
|
2019-03-15 13:20:56 +08:00
|
|
|
originalElement: React.ReactElement<HTMLElement>,
|
2018-12-07 20:02:01 +08:00
|
|
|
) => React.ReactNode;
|
2018-07-27 15:14:20 +08:00
|
|
|
role?: string;
|
2019-04-08 21:43:06 +08:00
|
|
|
showLessItems?: boolean;
|
2016-08-15 12:00:05 +08:00
|
|
|
}
|
|
|
|
|
2020-04-27 23:45:14 +08:00
|
|
|
export type PaginationPosition = 'top' | 'bottom' | 'both';
|
2020-03-27 15:48:42 +08:00
|
|
|
|
2018-05-21 21:52:18 +08:00
|
|
|
export interface PaginationConfig extends PaginationProps {
|
2020-04-27 23:45:14 +08:00
|
|
|
position?: PaginationPosition;
|
2018-05-21 21:52:18 +08:00
|
|
|
}
|
|
|
|
|
2017-11-21 15:37:39 +08:00
|
|
|
export type PaginationLocale = any;
|
|
|
|
|
2020-05-24 23:13:32 +08:00
|
|
|
const Pagination: React.FC<PaginationProps> = ({
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
selectPrefixCls: customizeSelectPrefixCls,
|
|
|
|
className,
|
|
|
|
size,
|
|
|
|
locale: customLocale,
|
|
|
|
...restProps
|
|
|
|
}) => {
|
|
|
|
const { xs } = useBreakpoint();
|
2020-03-10 20:32:34 +08:00
|
|
|
|
2020-05-24 23:13:32 +08:00
|
|
|
const { getPrefixCls, direction } = React.useContext(ConfigContext);
|
|
|
|
const prefixCls = getPrefixCls('pagination', customizePrefixCls);
|
2020-03-10 20:32:34 +08:00
|
|
|
|
2020-05-24 23:13:32 +08:00
|
|
|
const getIconsProps = () => {
|
2020-06-28 22:41:59 +08:00
|
|
|
const ellipsis = <span className={`${prefixCls}-item-ellipsis`}>•••</span>;
|
2020-01-02 19:10:16 +08:00
|
|
|
let prevIcon = (
|
2020-06-28 22:41:59 +08:00
|
|
|
<button className={`${prefixCls}-item-link`} type="button" tabIndex={-1}>
|
2019-11-28 12:34:33 +08:00
|
|
|
<LeftOutlined />
|
2020-06-28 22:41:59 +08:00
|
|
|
</button>
|
2018-08-21 17:13:12 +08:00
|
|
|
);
|
2020-01-02 19:10:16 +08:00
|
|
|
let nextIcon = (
|
2020-06-28 22:41:59 +08:00
|
|
|
<button className={`${prefixCls}-item-link`} type="button" tabIndex={-1}>
|
2019-11-28 12:34:33 +08:00
|
|
|
<RightOutlined />
|
2020-06-28 22:41:59 +08:00
|
|
|
</button>
|
2018-08-21 17:13:12 +08:00
|
|
|
);
|
2020-01-02 19:10:16 +08:00
|
|
|
let jumpPrevIcon = (
|
2018-08-21 17:13:12 +08:00
|
|
|
<a className={`${prefixCls}-item-link`}>
|
2018-08-22 20:01:00 +08:00
|
|
|
{/* You can use transition effects in the container :) */}
|
|
|
|
<div className={`${prefixCls}-item-container`}>
|
2019-11-28 12:34:33 +08:00
|
|
|
<DoubleLeftOutlined className={`${prefixCls}-item-link-icon`} />
|
2020-06-28 22:41:59 +08:00
|
|
|
{ellipsis}
|
2018-08-22 20:01:00 +08:00
|
|
|
</div>
|
2018-08-21 17:13:12 +08:00
|
|
|
</a>
|
|
|
|
);
|
2020-01-02 19:10:16 +08:00
|
|
|
let jumpNextIcon = (
|
2018-08-21 17:13:12 +08:00
|
|
|
<a className={`${prefixCls}-item-link`}>
|
2018-08-22 20:01:00 +08:00
|
|
|
{/* You can use transition effects in the container :) */}
|
|
|
|
<div className={`${prefixCls}-item-container`}>
|
2019-11-28 12:34:33 +08:00
|
|
|
<DoubleRightOutlined className={`${prefixCls}-item-link-icon`} />
|
2020-06-28 22:41:59 +08:00
|
|
|
{ellipsis}
|
2018-08-22 20:01:00 +08:00
|
|
|
</div>
|
2018-08-21 17:13:12 +08:00
|
|
|
</a>
|
|
|
|
);
|
2020-01-02 19:10:16 +08:00
|
|
|
// change arrows direction in right-to-left direction
|
|
|
|
if (direction === 'rtl') {
|
2020-06-28 22:41:59 +08:00
|
|
|
[prevIcon, nextIcon] = [nextIcon, prevIcon];
|
|
|
|
[jumpPrevIcon, jumpNextIcon] = [jumpNextIcon, jumpPrevIcon];
|
2020-01-02 19:10:16 +08:00
|
|
|
}
|
2018-08-21 17:13:12 +08:00
|
|
|
return {
|
|
|
|
prevIcon,
|
|
|
|
nextIcon,
|
|
|
|
jumpPrevIcon,
|
|
|
|
jumpNextIcon,
|
|
|
|
};
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2018-08-21 17:13:12 +08:00
|
|
|
|
2020-05-24 23:13:32 +08:00
|
|
|
const renderPagination = (contextLocale: PaginationLocale) => {
|
2018-06-21 12:00:09 +08:00
|
|
|
const locale = { ...contextLocale, ...customLocale };
|
2020-05-24 23:13:32 +08:00
|
|
|
const isSmall = size === 'small' || !!(xs && !size && restProps.responsive);
|
|
|
|
const selectPrefixCls = getPrefixCls('select', customizeSelectPrefixCls);
|
2020-09-06 13:07:39 +08:00
|
|
|
const extendedClassName = classNames(
|
|
|
|
{
|
|
|
|
mini: isSmall,
|
|
|
|
[`${prefixCls}-rtl`]: direction === 'rtl',
|
|
|
|
},
|
|
|
|
className,
|
|
|
|
);
|
2018-12-05 19:12:18 +08:00
|
|
|
|
2020-05-24 23:13:32 +08:00
|
|
|
return (
|
|
|
|
<RcPagination
|
|
|
|
{...restProps}
|
|
|
|
prefixCls={prefixCls}
|
|
|
|
selectPrefixCls={selectPrefixCls}
|
|
|
|
{...getIconsProps()}
|
|
|
|
className={extendedClassName}
|
|
|
|
selectComponentClass={isSmall ? MiniSelect : Select}
|
|
|
|
locale={locale}
|
|
|
|
/>
|
2016-03-31 17:46:35 +08:00
|
|
|
);
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2017-03-17 15:23:25 +08:00
|
|
|
|
2020-05-24 23:13:32 +08:00
|
|
|
return (
|
|
|
|
<LocaleReceiver componentName="Pagination" defaultLocale={enUS}>
|
|
|
|
{renderPagination}
|
|
|
|
</LocaleReceiver>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Pagination;
|