From 2ce3249f6b9599e9aa2f7b9649efb8ceda1ccdb1 Mon Sep 17 00:00:00 2001 From: afc163 Date: Mon, 23 Sep 2024 12:33:56 +0800 Subject: [PATCH] Refactor pagination for readability and maintainability --- For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/react-component/pagination?shareId=XXXX-XXXX-XXXX-XXXX). --- src/{ => components}/Options.tsx | 2 +- src/{ => components}/Pager.tsx | 3 +- src/{ => components}/Pagination.tsx | 4 +- src/components/Simple.tsx | 71 +++++++++++++++++++++++++++++ src/index.ts | 2 +- 5 files changed, 76 insertions(+), 6 deletions(-) rename src/{ => components}/Options.tsx (98%) rename src/{ => components}/Pager.tsx (94%) rename src/{ => components}/Pagination.tsx (99%) create mode 100644 src/components/Simple.tsx diff --git a/src/Options.tsx b/src/components/Options.tsx similarity index 98% rename from src/Options.tsx rename to src/components/Options.tsx index 6973b568..3ee26900 100644 --- a/src/Options.tsx +++ b/src/components/Options.tsx @@ -3,7 +3,7 @@ import type { OptionProps } from 'rc-select/es/Option'; import KEYCODE from 'rc-util/lib/KeyCode'; import classNames from 'classnames'; import React from 'react'; -import type { PaginationLocale, PaginationProps } from './interface'; +import type { PaginationLocale, PaginationProps } from '../interface'; interface InternalSelectProps extends SelectProps { /** diff --git a/src/Pager.tsx b/src/components/Pager.tsx similarity index 94% rename from src/Pager.tsx rename to src/components/Pager.tsx index 2d357787..978e36e6 100644 --- a/src/Pager.tsx +++ b/src/components/Pager.tsx @@ -1,7 +1,6 @@ -/* eslint react/prop-types: 0 */ import classNames from 'classnames'; import React from 'react'; -import type { PaginationProps } from './interface'; +import type { PaginationProps } from '../interface'; export interface PagerProps extends Pick { rootPrefixCls: string; diff --git a/src/Pagination.tsx b/src/components/Pagination.tsx similarity index 99% rename from src/Pagination.tsx rename to src/components/Pagination.tsx index fd37018b..d802d6eb 100644 --- a/src/Pagination.tsx +++ b/src/components/Pagination.tsx @@ -4,8 +4,8 @@ import KeyCode from 'rc-util/lib/KeyCode'; import pickAttrs from 'rc-util/lib/pickAttrs'; import warning from 'rc-util/lib/warning'; import React, { useEffect } from 'react'; -import type { PaginationProps } from './interface'; -import zhCN from './locale/zh_CN'; +import type { PaginationProps } from '../interface'; +import zhCN from '../locale/zh_CN'; import Options from './Options'; import type { PagerProps } from './Pager'; import Pager from './Pager'; diff --git a/src/components/Simple.tsx b/src/components/Simple.tsx new file mode 100644 index 00000000..b8a678d2 --- /dev/null +++ b/src/components/Simple.tsx @@ -0,0 +1,71 @@ +import React from 'react'; +import type { PaginationProps } from '../interface'; + +const Simple: React.FC = ({ + locale, + prefixCls, + current, + allPages, + disabled, + internalInputVal, + handleKeyDown, + handleKeyUp, + handleBlur, + handleGoTO, + goButton, + showTitle, +}) => { + const isReadOnly = typeof simple === 'object' ? simple.readOnly : !simple; + let gotoButton: any = goButton; + + if (goButton) { + if (typeof goButton === 'boolean') { + gotoButton = ( + + ); + } else { + gotoButton = ( + + {goButton} + + ); + } + + gotoButton = ( +
  • + {gotoButton} +
  • + ); + } + + return ( +
  • + {isReadOnly ? ( + internalInputVal + ) : ( + + )} + / + {allPages} +
  • + ); +}; + +export default Simple; diff --git a/src/index.ts b/src/index.ts index ac30e303..2618c94a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,2 +1,2 @@ -export { default } from './Pagination'; +export { default } from './components/Pagination'; export type { PaginationLocale, PaginationProps } from './interface';