Skip to content

Commit 463950d

Browse files
committed
refactor: refactor by cursor
1 parent 8f26e36 commit 463950d

File tree

3 files changed

+159
-237
lines changed

3 files changed

+159
-237
lines changed

src/Options.tsx

Lines changed: 30 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,10 @@ import type { SelectProps } from 'rc-select';
22
import type { OptionProps } from 'rc-select/es/Option';
33
import KEYCODE from 'rc-util/lib/KeyCode';
44
import classNames from 'classnames';
5-
import React from 'react';
5+
import React, { useState } from 'react';
66
import type { PaginationLocale, PaginationProps } from './interface';
77

88
interface InternalSelectProps extends SelectProps {
9-
/**
10-
* form antd v5.5.0, popupMatchSelectWidth default is true
11-
*/
129
popupMatchSelectWidth?: boolean;
1310
}
1411

@@ -31,26 +28,24 @@ interface OptionsProps {
3128

3229
const defaultPageSizeOptions = ['10', '20', '50', '100'];
3330

34-
const Options: React.FC<OptionsProps> = (props) => {
35-
const {
36-
pageSizeOptions = defaultPageSizeOptions,
37-
locale,
38-
changeSize,
39-
pageSize,
40-
goButton,
41-
quickGo,
42-
rootPrefixCls,
43-
selectComponentClass: Select,
44-
selectPrefixCls,
45-
disabled,
46-
buildOptionText,
47-
showSizeChanger,
48-
} = props;
49-
50-
const [goInputText, setGoInputText] = React.useState('');
31+
const Options: React.FC<OptionsProps> = ({
32+
pageSizeOptions = defaultPageSizeOptions,
33+
locale,
34+
changeSize,
35+
pageSize,
36+
goButton,
37+
quickGo,
38+
rootPrefixCls,
39+
selectComponentClass: Select,
40+
selectPrefixCls,
41+
disabled,
42+
buildOptionText,
43+
showSizeChanger,
44+
}) => {
45+
const [goInputText, setGoInputText] = useState('');
5146

5247
const getValidValue = () => {
53-
return !goInputText || Number.isNaN(goInputText)
48+
return !goInputText || isNaN(Number(goInputText))
5449
? undefined
5550
: Number(goInputText);
5651
};
@@ -71,25 +66,20 @@ const Options: React.FC<OptionsProps> = (props) => {
7166
setGoInputText(e.target.value);
7267
};
7368

74-
const handleBlur = (e: React.FocusEvent<HTMLInputElement, Element>) => {
75-
if (goButton || goInputText === '') {
76-
return;
77-
}
69+
const handleBlur = (e: React.FocusEvent<HTMLInputElement>) => {
70+
if (goButton || goInputText === '') return;
7871
setGoInputText('');
7972
if (
8073
e.relatedTarget &&
81-
(e.relatedTarget.className.indexOf(`${rootPrefixCls}-item-link`) >= 0 ||
82-
e.relatedTarget.className.indexOf(`${rootPrefixCls}-item`) >= 0)
83-
) {
74+
(e.relatedTarget.className.includes(`${rootPrefixCls}-item-link`) ||
75+
e.relatedTarget.className.includes(`${rootPrefixCls}-item`))
76+
)
8477
return;
85-
}
8678
quickGo?.(getValidValue());
8779
};
8880

89-
const go = (e: any) => {
90-
if (goInputText === '') {
91-
return;
92-
}
81+
const go = (e: React.KeyboardEvent<HTMLInputElement> | React.MouseEvent) => {
82+
if (goInputText === '') return;
9383
if (e.keyCode === KEYCODE.ENTER || e.type === 'click') {
9484
setGoInputText('');
9585
quickGo?.(getValidValue());
@@ -104,20 +94,14 @@ const Options: React.FC<OptionsProps> = (props) => {
10494
) {
10595
return pageSizeOptions;
10696
}
107-
return pageSizeOptions.concat([pageSize.toString()]).sort((a, b) => {
108-
const numberA = Number.isNaN(Number(a)) ? 0 : Number(a);
109-
const numberB = Number.isNaN(Number(b)) ? 0 : Number(b);
110-
return numberA - numberB;
111-
});
97+
return [...pageSizeOptions, pageSize.toString()].sort(
98+
(a, b) => Number(a) - Number(b),
99+
);
112100
};
113-
// ============== cls ==============
114-
const prefixCls = `${rootPrefixCls}-options`;
115101

116-
// ============== render ==============
102+
const prefixCls = `${rootPrefixCls}-options`;
117103

118-
if (!showSizeChanger && !quickGo) {
119-
return null;
120-
}
104+
if (!showSizeChanger && !quickGo) return null;
121105

122106
let changeSelect: React.ReactNode = null;
123107
let goInput: React.ReactNode = null;
@@ -127,11 +111,7 @@ const Options: React.FC<OptionsProps> = (props) => {
127111
const {
128112
options: showSizeChangerOptions,
129113
className: showSizeChangerClassName,
130-
} =
131-
typeof showSizeChanger === 'object'
132-
? showSizeChanger
133-
: ({} as SelectProps);
134-
// use showSizeChanger.options if existed, otherwise use pageSizeOptions
114+
} = typeof showSizeChanger === 'object' ? showSizeChanger : {};
135115
const options = showSizeChangerOptions
136116
? undefined
137117
: getPageSizeOptions().map((opt, i) => (

src/Pager.tsx

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
/* eslint react/prop-types: 0 */
21
import classNames from 'classnames';
32
import React from 'react';
43
import type { PaginationProps } from './interface';
@@ -9,25 +8,24 @@ export interface PagerProps extends Pick<PaginationProps, 'itemRender'> {
98
active?: boolean;
109
className?: string;
1110
showTitle: boolean;
12-
onClick?: (page: number) => void;
13-
onKeyPress?: (
11+
onClick: (page: number) => void;
12+
onKeyPress: (
1413
e: React.KeyboardEvent<HTMLLIElement>,
1514
onClick: PagerProps['onClick'],
16-
page: PagerProps['page'],
15+
page: number,
1716
) => void;
1817
}
1918

20-
const Pager: React.FC<PagerProps> = (props) => {
21-
const {
22-
rootPrefixCls,
23-
page,
24-
active,
25-
className,
26-
showTitle,
27-
onClick,
28-
onKeyPress,
29-
itemRender,
30-
} = props;
19+
const Pager: React.FC<PagerProps> = ({
20+
rootPrefixCls,
21+
page,
22+
active,
23+
className,
24+
showTitle,
25+
onClick,
26+
onKeyPress,
27+
itemRender,
28+
}) => {
3129
const prefixCls = `${rootPrefixCls}-item`;
3230

3331
const cls = classNames(
@@ -40,9 +38,7 @@ const Pager: React.FC<PagerProps> = (props) => {
4038
className,
4139
);
4240

43-
const handleClick = () => {
44-
onClick(page);
45-
};
41+
const handleClick = () => onClick(page);
4642

4743
const handleKeyPress = (e: React.KeyboardEvent<HTMLLIElement>) => {
4844
onKeyPress(e, onClick, page);

0 commit comments

Comments
 (0)