@@ -118,20 +118,21 @@ const Pagination: React.FC<PaginationProps> = (props) => {
118
118
return iconNode as React . ReactNode ;
119
119
} ;
120
120
121
- const getValidValue = ( e : any ) : number => {
121
+ const getValidValue = ( e : React . ChangeEvent < HTMLInputElement > ) : number => {
122
122
const inputValue = e . target . value ;
123
123
const allPages = calculatePage ( undefined , pageSize , total ) ;
124
- let value : number ;
124
+
125
125
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作为默认值
133
127
}
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 ) ; // 限制在有效范围内
135
136
} ;
136
137
137
138
const isValid = ( page : number ) =>
@@ -140,12 +141,7 @@ const Pagination: React.FC<PaginationProps> = (props) => {
140
141
const handleChange = ( page : number ) => {
141
142
if ( isValid ( page ) && ! disabled ) {
142
143
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 ) ) ;
149
145
150
146
if ( newPage !== internalInputVal ) {
151
147
setInternalInputVal ( newPage ) ;
@@ -178,19 +174,16 @@ const Pagination: React.FC<PaginationProps> = (props) => {
178
174
setInternalInputVal ( value ) ;
179
175
}
180
176
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
+ ) ( ) ;
194
187
} ;
195
188
196
189
const handleBlur = ( event : React . FocusEvent < HTMLInputElement , Element > ) => {
@@ -307,30 +300,26 @@ const Pagination: React.FC<PaginationProps> = (props) => {
307
300
let simplePager : React . ReactNode = null ;
308
301
309
302
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
+ ) ;
334
323
335
324
simplePager = (
336
325
< li
0 commit comments