2024-04-08 14:04:08 +08:00
|
|
|
import * as React from 'react';
|
2022-05-26 17:14:05 +08:00
|
|
|
import DoubleLeftOutlined from '@ant-design/icons/DoubleLeftOutlined';
|
|
|
|
import DoubleRightOutlined from '@ant-design/icons/DoubleRightOutlined';
|
|
|
|
import LeftOutlined from '@ant-design/icons/LeftOutlined';
|
|
|
|
import RightOutlined from '@ant-design/icons/RightOutlined';
|
|
|
|
import classNames from 'classnames';
|
2023-02-22 18:18:26 +08:00
|
|
|
import type { PaginationLocale, PaginationProps as RcPaginationProps } from 'rc-pagination';
|
2023-01-12 11:04: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';
|
2024-04-08 14:04:08 +08:00
|
|
|
|
2024-12-11 11:42:51 +08:00
|
|
|
import { devUseWarning } from '../_util/warning';
|
2020-05-24 23:13:32 +08:00
|
|
|
import { ConfigContext } from '../config-provider';
|
2023-05-12 14:53:47 +08:00
|
|
|
import useSize from '../config-provider/hooks/useSize';
|
2020-05-24 23:13:32 +08:00
|
|
|
import useBreakpoint from '../grid/hooks/useBreakpoint';
|
2023-03-21 13:08:43 +08:00
|
|
|
import { useLocale } from '../locale';
|
2024-12-11 11:42:51 +08:00
|
|
|
import type { SelectProps } from '../select';
|
|
|
|
import Select from '../select';
|
2024-04-08 14:04:08 +08:00
|
|
|
import { useToken } from '../theme/internal';
|
2022-03-30 13:54:21 +08:00
|
|
|
import useStyle from './style';
|
2023-11-22 09:43:20 +08:00
|
|
|
import BorderedStyle from './style/bordered';
|
2024-12-11 11:42:51 +08:00
|
|
|
import useShowSizeChanger from './useShowSizeChanger';
|
2016-03-31 17:46:35 +08:00
|
|
|
|
2024-12-11 11:42:51 +08:00
|
|
|
export interface PaginationProps
|
|
|
|
extends Omit<RcPaginationProps, 'showSizeChanger' | 'pageSizeOptions'> {
|
2019-02-14 22:39:11 +08:00
|
|
|
showQuickJumper?: boolean | { goButton?: React.ReactNode };
|
2020-03-10 20:32:34 +08:00
|
|
|
size?: 'default' | 'small';
|
|
|
|
responsive?: boolean;
|
2018-07-27 15:14:20 +08:00
|
|
|
role?: string;
|
2021-07-28 12:18:47 +08:00
|
|
|
totalBoundaryShowSizeChanger?: number;
|
2023-01-20 11:03:50 +08:00
|
|
|
rootClassName?: string;
|
2024-09-22 22:22:09 +08:00
|
|
|
showSizeChanger?: boolean | SelectProps;
|
2024-12-11 11:42:51 +08:00
|
|
|
/** @deprecated Not official support. Will be removed in next major version. */
|
|
|
|
selectComponentClass?: any;
|
|
|
|
/** `string` type will be removed in next major version. */
|
|
|
|
pageSizeOptions?: (string | number)[];
|
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
|
|
|
|
2023-01-20 11:03:50 +08:00
|
|
|
export interface PaginationConfig extends Omit<PaginationProps, 'rootClassName'> {
|
2020-04-27 23:45:14 +08:00
|
|
|
position?: PaginationPosition;
|
2018-05-21 21:52:18 +08:00
|
|
|
}
|
|
|
|
|
2022-11-09 12:28:04 +08:00
|
|
|
export type { PaginationLocale };
|
2017-11-21 15:37:39 +08:00
|
|
|
|
2023-07-11 11:20:11 +08:00
|
|
|
const Pagination: React.FC<PaginationProps> = (props) => {
|
|
|
|
const {
|
2024-06-25 21:27:11 +08:00
|
|
|
align,
|
2023-07-11 11:20:11 +08:00
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
selectPrefixCls: customizeSelectPrefixCls,
|
|
|
|
className,
|
|
|
|
rootClassName,
|
|
|
|
style,
|
|
|
|
size: customizeSize,
|
|
|
|
locale: customLocale,
|
|
|
|
responsive,
|
|
|
|
showSizeChanger,
|
2024-12-11 11:42:51 +08:00
|
|
|
selectComponentClass,
|
|
|
|
pageSizeOptions,
|
2023-07-11 11:20:11 +08:00
|
|
|
...restProps
|
|
|
|
} = props;
|
2022-02-21 16:48:39 +08:00
|
|
|
const { xs } = useBreakpoint(responsive);
|
2023-11-22 09:43:20 +08:00
|
|
|
const [, token] = useToken();
|
2020-03-10 20:32:34 +08:00
|
|
|
|
2022-05-26 17:14:05 +08:00
|
|
|
const { getPrefixCls, direction, pagination = {} } = React.useContext(ConfigContext);
|
2020-05-24 23:13:32 +08:00
|
|
|
const prefixCls = getPrefixCls('pagination', customizePrefixCls);
|
2020-03-10 20:32:34 +08:00
|
|
|
|
2022-03-30 13:54:21 +08:00
|
|
|
// Style
|
2023-12-14 14:58:53 +08:00
|
|
|
const [wrapCSSVar, hashId, cssVarCls] = useStyle(prefixCls);
|
2022-03-30 13:54:21 +08:00
|
|
|
|
2024-12-11 11:42:51 +08:00
|
|
|
// ============================== Size ==============================
|
|
|
|
const mergedSize = useSize(customizeSize);
|
|
|
|
|
|
|
|
const isSmall = mergedSize === 'small' || !!(xs && !mergedSize && responsive);
|
|
|
|
|
|
|
|
// ============================= Locale =============================
|
|
|
|
const [contextLocale] = useLocale('Pagination', enUS);
|
|
|
|
|
|
|
|
const locale = { ...contextLocale, ...customLocale };
|
2022-05-26 17:14:05 +08:00
|
|
|
|
2024-12-11 11:42:51 +08:00
|
|
|
// ========================== Size Changer ==========================
|
|
|
|
// Merge the props showSizeChanger
|
|
|
|
const [propShowSizeChanger, propSizeChangerSelectProps] = useShowSizeChanger(showSizeChanger);
|
|
|
|
const [contextShowSizeChanger, contextSizeChangerSelectProps] = useShowSizeChanger(
|
|
|
|
pagination.showSizeChanger,
|
|
|
|
);
|
|
|
|
|
|
|
|
const mergedShowSizeChanger = propShowSizeChanger ?? contextShowSizeChanger;
|
|
|
|
const mergedShowSizeChangerSelectProps =
|
|
|
|
propSizeChangerSelectProps ?? contextSizeChangerSelectProps;
|
|
|
|
|
|
|
|
const SizeChanger: typeof Select = selectComponentClass || Select;
|
|
|
|
|
|
|
|
// Generate options
|
|
|
|
const mergedPageSizeOptions = React.useMemo(() => {
|
|
|
|
return pageSizeOptions ? pageSizeOptions.map((option) => Number(option)) : undefined;
|
|
|
|
}, [pageSizeOptions]);
|
|
|
|
|
|
|
|
// Render size changer
|
|
|
|
const sizeChangerRender: RcPaginationProps['sizeChangerRender'] = (info) => {
|
|
|
|
const {
|
|
|
|
disabled,
|
|
|
|
size: pageSize,
|
|
|
|
onSizeChange,
|
|
|
|
'aria-label': ariaLabel,
|
|
|
|
className: sizeChangerClassName,
|
|
|
|
options,
|
|
|
|
} = info;
|
|
|
|
|
|
|
|
const { className: propSizeChangerClassName, onChange: propSizeChangerOnChange } =
|
|
|
|
mergedShowSizeChangerSelectProps || {};
|
|
|
|
|
|
|
|
// Origin Select is using Select.Option,
|
|
|
|
// So it make the option value must be string
|
|
|
|
// Just for compatible
|
|
|
|
const selectedValue = options.find(
|
|
|
|
(option) => String(option.value) === String(pageSize),
|
|
|
|
)?.value;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<SizeChanger
|
|
|
|
disabled={disabled}
|
|
|
|
showSearch
|
|
|
|
popupMatchSelectWidth={false}
|
|
|
|
getPopupContainer={(triggerNode) => triggerNode.parentNode}
|
|
|
|
aria-label={ariaLabel}
|
|
|
|
options={options}
|
|
|
|
{...mergedShowSizeChangerSelectProps}
|
|
|
|
value={selectedValue}
|
|
|
|
onChange={(nextSize, option) => {
|
|
|
|
onSizeChange?.(nextSize);
|
|
|
|
propSizeChangerOnChange?.(nextSize, option);
|
|
|
|
}}
|
|
|
|
size={isSmall ? 'small' : 'middle'}
|
|
|
|
className={classNames(sizeChangerClassName, propSizeChangerClassName)}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
const warning = devUseWarning('Pagination');
|
|
|
|
|
|
|
|
warning(
|
|
|
|
!selectComponentClass,
|
|
|
|
'usage',
|
|
|
|
'`selectComponentClass` is not official api which will be removed.',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ============================= Render =============================
|
2023-03-26 23:51:33 +08:00
|
|
|
const iconsProps = React.useMemo<Record<PropertyKey, React.ReactNode>>(() => {
|
2020-06-28 22:41:59 +08:00
|
|
|
const ellipsis = <span className={`${prefixCls}-item-ellipsis`}>•••</span>;
|
2023-03-26 23:51:33 +08:00
|
|
|
const prevIcon = (
|
2020-06-28 22:41:59 +08:00
|
|
|
<button className={`${prefixCls}-item-link`} type="button" tabIndex={-1}>
|
2023-03-26 23:51:33 +08:00
|
|
|
{direction === 'rtl' ? <RightOutlined /> : <LeftOutlined />}
|
2020-06-28 22:41:59 +08:00
|
|
|
</button>
|
2018-08-21 17:13:12 +08:00
|
|
|
);
|
2023-03-26 23:51:33 +08:00
|
|
|
const nextIcon = (
|
2020-06-28 22:41:59 +08:00
|
|
|
<button className={`${prefixCls}-item-link`} type="button" tabIndex={-1}>
|
2023-03-26 23:51:33 +08:00
|
|
|
{direction === 'rtl' ? <LeftOutlined /> : <RightOutlined />}
|
2020-06-28 22:41:59 +08:00
|
|
|
</button>
|
2018-08-21 17:13:12 +08:00
|
|
|
);
|
2023-03-26 23:51:33 +08:00
|
|
|
const jumpPrevIcon = (
|
2024-06-22 21:59:12 +08:00
|
|
|
// biome-ignore lint/a11y/useValidAnchor: it is hard to refactor
|
2018-08-21 17:13:12 +08:00
|
|
|
<a className={`${prefixCls}-item-link`}>
|
2018-08-22 20:01:00 +08:00
|
|
|
<div className={`${prefixCls}-item-container`}>
|
2023-03-26 23:51:33 +08:00
|
|
|
{direction === 'rtl' ? (
|
|
|
|
<DoubleRightOutlined className={`${prefixCls}-item-link-icon`} />
|
|
|
|
) : (
|
|
|
|
<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>
|
|
|
|
);
|
2023-03-26 23:51:33 +08:00
|
|
|
const jumpNextIcon = (
|
2024-06-22 21:59:12 +08:00
|
|
|
// biome-ignore lint/a11y/useValidAnchor: it is hard to refactor
|
2018-08-21 17:13:12 +08:00
|
|
|
<a className={`${prefixCls}-item-link`}>
|
2018-08-22 20:01:00 +08:00
|
|
|
<div className={`${prefixCls}-item-container`}>
|
2023-03-26 23:51:33 +08:00
|
|
|
{direction === 'rtl' ? (
|
|
|
|
<DoubleLeftOutlined className={`${prefixCls}-item-link-icon`} />
|
|
|
|
) : (
|
|
|
|
<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>
|
|
|
|
);
|
2023-02-22 18:18:26 +08:00
|
|
|
return { prevIcon, nextIcon, jumpPrevIcon, jumpNextIcon };
|
2023-03-26 23:51:33 +08:00
|
|
|
}, [direction, prefixCls]);
|
2018-08-21 17:13:12 +08:00
|
|
|
|
2023-02-22 18:18:26 +08:00
|
|
|
const selectPrefixCls = getPrefixCls('select', customizeSelectPrefixCls);
|
2023-06-26 15:32:18 +08:00
|
|
|
|
2023-02-22 18:18:26 +08:00
|
|
|
const extendedClassName = classNames(
|
|
|
|
{
|
2024-06-25 21:27:11 +08:00
|
|
|
[`${prefixCls}-${align}`]: !!align,
|
2023-02-22 18:18:26 +08:00
|
|
|
[`${prefixCls}-mini`]: isSmall,
|
|
|
|
[`${prefixCls}-rtl`]: direction === 'rtl',
|
2023-11-22 09:43:20 +08:00
|
|
|
[`${prefixCls}-bordered`]: token.wireframe,
|
2023-02-22 18:18:26 +08:00
|
|
|
},
|
2023-06-26 15:32:18 +08:00
|
|
|
pagination?.className,
|
2023-02-22 18:18:26 +08:00
|
|
|
className,
|
|
|
|
rootClassName,
|
|
|
|
hashId,
|
2023-12-14 14:58:53 +08:00
|
|
|
cssVarCls,
|
2023-02-22 18:18:26 +08:00
|
|
|
);
|
|
|
|
|
2023-06-26 15:32:18 +08:00
|
|
|
const mergedStyle: React.CSSProperties = { ...pagination?.style, ...style };
|
|
|
|
|
2023-11-22 09:43:20 +08:00
|
|
|
return wrapCSSVar(
|
|
|
|
<>
|
|
|
|
{token.wireframe && <BorderedStyle prefixCls={prefixCls} />}
|
|
|
|
<RcPagination
|
|
|
|
{...iconsProps}
|
|
|
|
{...restProps}
|
|
|
|
style={mergedStyle}
|
|
|
|
prefixCls={prefixCls}
|
|
|
|
selectPrefixCls={selectPrefixCls}
|
|
|
|
className={extendedClassName}
|
|
|
|
locale={locale}
|
2024-12-11 11:42:51 +08:00
|
|
|
pageSizeOptions={mergedPageSizeOptions}
|
2023-11-22 09:43:20 +08:00
|
|
|
showSizeChanger={mergedShowSizeChanger}
|
2024-12-11 11:42:51 +08:00
|
|
|
sizeChangerRender={sizeChangerRender}
|
2023-11-22 09:43:20 +08:00
|
|
|
/>
|
|
|
|
</>,
|
2020-05-24 23:13:32 +08:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2023-01-08 21:30:41 +08:00
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
Pagination.displayName = 'Pagination';
|
|
|
|
}
|
|
|
|
|
2020-05-24 23:13:32 +08:00
|
|
|
export default Pagination;
|