Skip to content

Commit 4e16397

Browse files
committed
refactor: refactor by cursor
1 parent 463950d commit 4e16397

File tree

1 file changed

+42
-53
lines changed

1 file changed

+42
-53
lines changed

src/Pagination.tsx

Lines changed: 42 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -118,20 +118,21 @@ const Pagination: React.FC<PaginationProps> = (props) => {
118118
return iconNode as React.ReactNode;
119119
};
120120

121-
const getValidValue = (e: any): number => {
121+
const getValidValue = (e: React.ChangeEvent<HTMLInputElement>): number => {
122122
const inputValue = e.target.value;
123123
const allPages = calculatePage(undefined, pageSize, total);
124-
let value: number;
124+
125125
if (inputValue === '') {
126-
value = inputValue;
127-
} else if (Number.isNaN(Number(inputValue))) {
128-
value = internalInputVal;
129-
} else if (inputValue >= allPages) {
130-
value = allPages;
131-
} else {
132-
value = Number(inputValue);
126+
return 1; // 返回1作为默认值
133127
}
134-
return value;
128+
129+
const parsedValue = Number(inputValue);
130+
131+
if (isNaN(parsedValue)) {
132+
return internalInputVal; // 返回内部值
133+
}
134+
135+
return Math.min(Math.max(parsedValue, 1), allPages); // 限制在有效范围内
135136
};
136137

137138
const isValid = (page: number) =>
@@ -140,12 +141,7 @@ const Pagination: React.FC<PaginationProps> = (props) => {
140141
const handleChange = (page: number) => {
141142
if (isValid(page) && !disabled) {
142143
const currentPage = calculatePage(undefined, pageSize, total);
143-
let newPage = page;
144-
if (page > currentPage) {
145-
newPage = currentPage;
146-
} else if (page < 1) {
147-
newPage = 1;
148-
}
144+
const newPage = Math.max(1, Math.min(page, currentPage));
149145

150146
if (newPage !== internalInputVal) {
151147
setInternalInputVal(newPage);
@@ -178,19 +174,16 @@ const Pagination: React.FC<PaginationProps> = (props) => {
178174
setInternalInputVal(value);
179175
}
180176

181-
switch ((event as React.KeyboardEvent<HTMLInputElement>).keyCode) {
182-
case KeyCode.ENTER:
183-
handleChange(value);
184-
break;
185-
case KeyCode.UP:
186-
handleChange(value - 1);
187-
break;
188-
case KeyCode.DOWN:
189-
handleChange(value + 1);
190-
break;
191-
default:
192-
break;
193-
}
177+
const keyActions: { [key: number]: () => void } = {
178+
[KeyCode.ENTER]: () => handleChange(value),
179+
[KeyCode.UP]: () => handleChange(value - 1),
180+
[KeyCode.DOWN]: () => handleChange(value + 1),
181+
};
182+
183+
(
184+
keyActions[(event as React.KeyboardEvent<HTMLInputElement>).keyCode] ||
185+
(() => {})
186+
)();
194187
};
195188

196189
const handleBlur = (event: React.FocusEvent<HTMLInputElement, Element>) => {
@@ -307,30 +300,26 @@ const Pagination: React.FC<PaginationProps> = (props) => {
307300
let simplePager: React.ReactNode = null;
308301

309302
if (simple) {
310-
if (goButton) {
311-
if (typeof goButton === 'boolean') {
312-
gotoButton = (
313-
<button type="button" onClick={handleGoTO} onKeyUp={handleGoTO}>
314-
{locale.jump_to_confirm}
315-
</button>
316-
);
317-
} else {
318-
gotoButton = (
319-
<span onClick={handleGoTO} onKeyUp={handleGoTO}>
320-
{goButton}
321-
</span>
322-
);
323-
}
324-
325-
gotoButton = (
326-
<li
327-
title={showTitle ? `${locale.jump_to}${current}/${allPages}` : null}
328-
className={`${prefixCls}-simple-pager`}
329-
>
330-
{gotoButton}
331-
</li>
332-
);
333-
}
303+
gotoButton = goButton ? (
304+
typeof goButton === 'boolean' ? (
305+
<button type="button" onClick={handleGoTO} onKeyUp={handleGoTO}>
306+
{locale.jump_to_confirm}
307+
</button>
308+
) : (
309+
<span onClick={handleGoTO} onKeyUp={handleGoTO}>
310+
{goButton}
311+
</span>
312+
)
313+
) : null;
314+
315+
gotoButton = (
316+
<li
317+
title={showTitle ? `${locale.jump_to}${current}/${allPages}` : null}
318+
className={`${prefixCls}-simple-pager`}
319+
>
320+
{gotoButton}
321+
</li>
322+
);
334323

335324
simplePager = (
336325
<li

0 commit comments

Comments
 (0)