Skip to content

Commit

Permalink
Refactor pagination for readability and maintainability
Browse files Browse the repository at this point in the history
  • Loading branch information
afc163 committed Sep 23, 2024
1 parent 8f26e36 commit 2ce3249
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/Options.tsx → src/components/Options.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
/**
Expand Down
3 changes: 1 addition & 2 deletions src/Pager.tsx → src/components/Pager.tsx
Original file line number Diff line number Diff line change
@@ -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<PaginationProps, 'itemRender'> {
rootPrefixCls: string;
Expand Down
4 changes: 2 additions & 2 deletions src/Pagination.tsx → src/components/Pagination.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
71 changes: 71 additions & 0 deletions src/components/Simple.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import React from 'react';
import type { PaginationProps } from '../interface';

const Simple: React.FC<PaginationProps> = ({
locale,
prefixCls,
current,
allPages,
disabled,
internalInputVal,
handleKeyDown,
handleKeyUp,
handleBlur,
handleGoTO,
goButton,
showTitle,
}) => {
const isReadOnly = typeof simple === 'object' ? simple.readOnly : !simple;
let gotoButton: any = goButton;

Check warning

Code scanning / CodeQL

Useless assignment to local variable Warning

The initial value of gotoButton is unused, since it is always overwritten.

if (goButton) {
if (typeof goButton === 'boolean') {
gotoButton = (
<button type="button" onClick={handleGoTO} onKeyUp={handleGoTO}>
{locale.jump_to_confirm}
</button>
);
} else {
gotoButton = (
<span onClick={handleGoTO} onKeyUp={handleGoTO}>
{goButton}
</span>
);
}

gotoButton = (
<li
title={showTitle ? `${locale.jump_to}${current}/${allPages}` : null}
className={`${prefixCls}-simple-pager`}
>
{gotoButton}
</li>
);

Check warning

Code scanning / CodeQL

Useless assignment to local variable Warning

The value assigned to gotoButton here is unused.
}

return (
<li
title={showTitle ? `${current}/${allPages}` : null}
className={`${prefixCls}-simple-pager`}
>
{isReadOnly ? (
internalInputVal
) : (
<input
type="text"
value={internalInputVal}
disabled={disabled}
onKeyDown={handleKeyDown}
onKeyUp={handleKeyUp}
onChange={handleKeyUp}
onBlur={handleBlur}
size={3}
/>
)}
<span className={`${prefixCls}-slash`}>/</span>
{allPages}
</li>
);
};

export default Simple;
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { default } from './Pagination';
export { default } from './components/Pagination';
export type { PaginationLocale, PaginationProps } from './interface';

0 comments on commit 2ce3249

Please sign in to comment.