Pagination.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. "use client";
  2. var __rest = this && this.__rest || function (s, e) {
  3. var t = {};
  4. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
  5. if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
  6. if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
  7. }
  8. return t;
  9. };
  10. import * as React from 'react';
  11. import DoubleLeftOutlined from "@ant-design/icons/es/icons/DoubleLeftOutlined";
  12. import DoubleRightOutlined from "@ant-design/icons/es/icons/DoubleRightOutlined";
  13. import LeftOutlined from "@ant-design/icons/es/icons/LeftOutlined";
  14. import RightOutlined from "@ant-design/icons/es/icons/RightOutlined";
  15. import classNames from 'classnames';
  16. import RcPagination from 'rc-pagination';
  17. import enUS from "rc-pagination/es/locale/en_US";
  18. import { devUseWarning } from '../_util/warning';
  19. import { useComponentConfig } from '../config-provider/context';
  20. import useSize from '../config-provider/hooks/useSize';
  21. import useBreakpoint from '../grid/hooks/useBreakpoint';
  22. import { useLocale } from '../locale';
  23. import Select from '../select';
  24. import { useToken } from '../theme/internal';
  25. import useStyle from './style';
  26. import BorderedStyle from './style/bordered';
  27. import useShowSizeChanger from './useShowSizeChanger';
  28. const Pagination = props => {
  29. const {
  30. align,
  31. prefixCls: customizePrefixCls,
  32. selectPrefixCls: customizeSelectPrefixCls,
  33. className,
  34. rootClassName,
  35. style,
  36. size: customizeSize,
  37. locale: customLocale,
  38. responsive,
  39. showSizeChanger,
  40. selectComponentClass,
  41. pageSizeOptions
  42. } = props,
  43. restProps = __rest(props, ["align", "prefixCls", "selectPrefixCls", "className", "rootClassName", "style", "size", "locale", "responsive", "showSizeChanger", "selectComponentClass", "pageSizeOptions"]);
  44. const {
  45. xs
  46. } = useBreakpoint(responsive);
  47. const [, token] = useToken();
  48. const {
  49. getPrefixCls,
  50. direction,
  51. showSizeChanger: contextShowSizeChangerConfig,
  52. className: contextClassName,
  53. style: contextStyle
  54. } = useComponentConfig('pagination');
  55. const prefixCls = getPrefixCls('pagination', customizePrefixCls);
  56. // Style
  57. const [wrapCSSVar, hashId, cssVarCls] = useStyle(prefixCls);
  58. // ============================== Size ==============================
  59. const mergedSize = useSize(customizeSize);
  60. const isSmall = mergedSize === 'small' || !!(xs && !mergedSize && responsive);
  61. // ============================= Locale =============================
  62. const [contextLocale] = useLocale('Pagination', enUS);
  63. const locale = Object.assign(Object.assign({}, contextLocale), customLocale);
  64. // ========================== Size Changer ==========================
  65. // Merge the props showSizeChanger
  66. const [propShowSizeChanger, propSizeChangerSelectProps] = useShowSizeChanger(showSizeChanger);
  67. const [contextShowSizeChanger, contextSizeChangerSelectProps] = useShowSizeChanger(contextShowSizeChangerConfig);
  68. const mergedShowSizeChanger = propShowSizeChanger !== null && propShowSizeChanger !== void 0 ? propShowSizeChanger : contextShowSizeChanger;
  69. const mergedShowSizeChangerSelectProps = propSizeChangerSelectProps !== null && propSizeChangerSelectProps !== void 0 ? propSizeChangerSelectProps : contextSizeChangerSelectProps;
  70. const SizeChanger = selectComponentClass || Select;
  71. // Generate options
  72. const mergedPageSizeOptions = React.useMemo(() => {
  73. return pageSizeOptions ? pageSizeOptions.map(option => Number(option)) : undefined;
  74. }, [pageSizeOptions]);
  75. // Render size changer
  76. const sizeChangerRender = info => {
  77. var _a;
  78. const {
  79. disabled,
  80. size: pageSize,
  81. onSizeChange,
  82. 'aria-label': ariaLabel,
  83. className: sizeChangerClassName,
  84. options
  85. } = info;
  86. const {
  87. className: propSizeChangerClassName,
  88. onChange: propSizeChangerOnChange
  89. } = mergedShowSizeChangerSelectProps || {};
  90. // Origin Select is using Select.Option,
  91. // So it make the option value must be string
  92. // Just for compatible
  93. const selectedValue = (_a = options.find(option => String(option.value) === String(pageSize))) === null || _a === void 0 ? void 0 : _a.value;
  94. return /*#__PURE__*/React.createElement(SizeChanger, Object.assign({
  95. disabled: disabled,
  96. showSearch: true,
  97. popupMatchSelectWidth: false,
  98. getPopupContainer: triggerNode => triggerNode.parentNode,
  99. "aria-label": ariaLabel,
  100. options: options
  101. }, mergedShowSizeChangerSelectProps, {
  102. value: selectedValue,
  103. onChange: (nextSize, option) => {
  104. onSizeChange === null || onSizeChange === void 0 ? void 0 : onSizeChange(nextSize);
  105. propSizeChangerOnChange === null || propSizeChangerOnChange === void 0 ? void 0 : propSizeChangerOnChange(nextSize, option);
  106. },
  107. size: isSmall ? 'small' : 'middle',
  108. className: classNames(sizeChangerClassName, propSizeChangerClassName)
  109. }));
  110. };
  111. if (process.env.NODE_ENV !== 'production') {
  112. const warning = devUseWarning('Pagination');
  113. process.env.NODE_ENV !== "production" ? warning(!selectComponentClass, 'usage', '`selectComponentClass` is not official api which will be removed.') : void 0;
  114. }
  115. // ============================= Render =============================
  116. const iconsProps = React.useMemo(() => {
  117. const ellipsis = /*#__PURE__*/React.createElement("span", {
  118. className: `${prefixCls}-item-ellipsis`
  119. }, "\u2022\u2022\u2022");
  120. const prevIcon = /*#__PURE__*/React.createElement("button", {
  121. className: `${prefixCls}-item-link`,
  122. type: "button",
  123. tabIndex: -1
  124. }, direction === 'rtl' ? /*#__PURE__*/React.createElement(RightOutlined, null) : /*#__PURE__*/React.createElement(LeftOutlined, null));
  125. const nextIcon = /*#__PURE__*/React.createElement("button", {
  126. className: `${prefixCls}-item-link`,
  127. type: "button",
  128. tabIndex: -1
  129. }, direction === 'rtl' ? /*#__PURE__*/React.createElement(LeftOutlined, null) : /*#__PURE__*/React.createElement(RightOutlined, null));
  130. const jumpPrevIcon =
  131. /*#__PURE__*/
  132. // biome-ignore lint/a11y/useValidAnchor: it is hard to refactor
  133. React.createElement("a", {
  134. className: `${prefixCls}-item-link`
  135. }, /*#__PURE__*/React.createElement("div", {
  136. className: `${prefixCls}-item-container`
  137. }, direction === 'rtl' ? (/*#__PURE__*/React.createElement(DoubleRightOutlined, {
  138. className: `${prefixCls}-item-link-icon`
  139. })) : (/*#__PURE__*/React.createElement(DoubleLeftOutlined, {
  140. className: `${prefixCls}-item-link-icon`
  141. })), ellipsis));
  142. const jumpNextIcon =
  143. /*#__PURE__*/
  144. // biome-ignore lint/a11y/useValidAnchor: it is hard to refactor
  145. React.createElement("a", {
  146. className: `${prefixCls}-item-link`
  147. }, /*#__PURE__*/React.createElement("div", {
  148. className: `${prefixCls}-item-container`
  149. }, direction === 'rtl' ? (/*#__PURE__*/React.createElement(DoubleLeftOutlined, {
  150. className: `${prefixCls}-item-link-icon`
  151. })) : (/*#__PURE__*/React.createElement(DoubleRightOutlined, {
  152. className: `${prefixCls}-item-link-icon`
  153. })), ellipsis));
  154. return {
  155. prevIcon,
  156. nextIcon,
  157. jumpPrevIcon,
  158. jumpNextIcon
  159. };
  160. }, [direction, prefixCls]);
  161. const selectPrefixCls = getPrefixCls('select', customizeSelectPrefixCls);
  162. const extendedClassName = classNames({
  163. [`${prefixCls}-${align}`]: !!align,
  164. [`${prefixCls}-mini`]: isSmall,
  165. [`${prefixCls}-rtl`]: direction === 'rtl',
  166. [`${prefixCls}-bordered`]: token.wireframe
  167. }, contextClassName, className, rootClassName, hashId, cssVarCls);
  168. const mergedStyle = Object.assign(Object.assign({}, contextStyle), style);
  169. return wrapCSSVar(/*#__PURE__*/React.createElement(React.Fragment, null, token.wireframe && /*#__PURE__*/React.createElement(BorderedStyle, {
  170. prefixCls: prefixCls
  171. }), /*#__PURE__*/React.createElement(RcPagination, Object.assign({}, iconsProps, restProps, {
  172. style: mergedStyle,
  173. prefixCls: prefixCls,
  174. selectPrefixCls: selectPrefixCls,
  175. className: extendedClassName,
  176. locale: locale,
  177. pageSizeOptions: mergedPageSizeOptions,
  178. showSizeChanger: mergedShowSizeChanger,
  179. sizeChangerRender: sizeChangerRender
  180. }))));
  181. };
  182. if (process.env.NODE_ENV !== 'production') {
  183. Pagination.displayName = 'Pagination';
  184. }
  185. export default Pagination;