Skip to content

Commit 326e433

Browse files
authored
Refactoring: js => ts (#515)
1 parent ba61c1c commit 326e433

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+256
-130
lines changed

src/KeyCode.js renamed to src/KeyCode.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export default {
1+
const KeyCode = {
22
ZERO: 48,
33
NINE: 57,
44

@@ -12,3 +12,5 @@ export default {
1212
ARROW_UP: 38,
1313
ARROW_DOWN: 40,
1414
};
15+
16+
export default KeyCode;

src/Options.jsx renamed to src/Options.tsx

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,28 @@
22
import React from 'react';
33
import KEYCODE from './KeyCode';
44

5-
class Options extends React.Component {
5+
interface Props {
6+
disabled: boolean;
7+
locale: any;
8+
rootPrefixCls: string;
9+
selectPrefixCls: string;
10+
current: number;
11+
pageSize: number;
12+
pageSizeOptions: (string | number)[];
13+
goButton: boolean | string;
14+
changeSize: (size: number) => void;
15+
quickGo: (value: number) => void;
16+
buildOptionText?: (value: string | number) => string;
17+
selectComponentClass: React.ComponentType<any> & {
18+
Option?: React.ComponentType<any>;
19+
};
20+
}
21+
22+
interface State {
23+
goInputText: string;
24+
}
25+
26+
class Options extends React.Component<Props, State> {
627
static defaultProps = {
728
pageSizeOptions: ['10', '20', '50', '100'],
829
};
@@ -11,33 +32,32 @@ class Options extends React.Component {
1132
goInputText: '',
1233
};
1334

14-
getValidValue() {
35+
getValidValue = () => {
1536
const { goInputText } = this.state;
1637
// eslint-disable-next-line no-restricted-globals
17-
return !goInputText || isNaN(goInputText) ? undefined : Number(goInputText);
18-
}
38+
return !goInputText || Number.isNaN(goInputText)
39+
? undefined
40+
: Number(goInputText);
41+
};
1942

20-
buildOptionText = (value) => `${value} ${this.props.locale.items_per_page}`;
43+
buildOptionText = (value: string) =>
44+
`${value} ${this.props.locale.items_per_page}`;
2145

22-
changeSize = (value) => {
46+
changeSize = (value: number) => {
2347
this.props.changeSize(Number(value));
2448
};
2549

26-
handleChange = (e) => {
27-
this.setState({
28-
goInputText: e.target.value,
29-
});
50+
handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
51+
this.setState({ goInputText: e.target.value });
3052
};
3153

32-
handleBlur = (e) => {
54+
handleBlur = (e: React.FocusEvent<HTMLInputElement, Element>) => {
3355
const { goButton, quickGo, rootPrefixCls } = this.props;
3456
const { goInputText } = this.state;
3557
if (goButton || goInputText === '') {
3658
return;
3759
}
38-
this.setState({
39-
goInputText: '',
40-
});
60+
this.setState({ goInputText: '' });
4161
if (
4262
e.relatedTarget &&
4363
(e.relatedTarget.className.indexOf(`${rootPrefixCls}-item-link`) >= 0 ||
@@ -48,15 +68,13 @@ class Options extends React.Component {
4868
quickGo(this.getValidValue());
4969
};
5070

51-
go = (e) => {
71+
go = (e: any) => {
5272
const { goInputText } = this.state;
5373
if (goInputText === '') {
5474
return;
5575
}
5676
if (e.keyCode === KEYCODE.ENTER || e.type === 'click') {
57-
this.setState({
58-
goInputText: '',
59-
});
77+
this.setState({ goInputText: '' });
6078
this.props.quickGo(this.getValidValue());
6179
}
6280
};
@@ -72,9 +90,9 @@ class Options extends React.Component {
7290
}
7391
return pageSizeOptions.concat([pageSize.toString()]).sort((a, b) => {
7492
// eslint-disable-next-line no-restricted-globals
75-
const numberA = isNaN(Number(a)) ? 0 : Number(a);
93+
const numberA = Number.isNaN(Number(a)) ? 0 : Number(a);
7694
// eslint-disable-next-line no-restricted-globals
77-
const numberB = isNaN(Number(b)) ? 0 : Number(b);
95+
const numberB = Number.isNaN(Number(b)) ? 0 : Number(b);
7896
return numberA - numberB;
7997
});
8098
}

src/Pager.jsx

Lines changed: 0 additions & 34 deletions
This file was deleted.

src/Pager.tsx

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/* eslint react/prop-types: 0 */
2+
import classNames from 'classnames';
3+
import React from 'react';
4+
5+
interface Props {
6+
last?: boolean;
7+
locale?: any;
8+
rootPrefixCls: string;
9+
page: number;
10+
active?: boolean;
11+
className?: string;
12+
showTitle: boolean;
13+
onClick?: (page: number) => void;
14+
onKeyPress?: (
15+
e: React.KeyboardEvent<HTMLLIElement>,
16+
onClick: Props['onClick'],
17+
page: Props['page'],
18+
) => void;
19+
itemRender?: (
20+
page: number,
21+
type: 'page' | 'prev' | 'next' | 'jump-prev' | 'jump-next',
22+
element: React.ReactNode,
23+
) => React.ReactNode;
24+
}
25+
26+
const Pager: React.FC<Props> = (props) => {
27+
const {
28+
rootPrefixCls,
29+
page,
30+
active,
31+
className,
32+
showTitle,
33+
onClick,
34+
onKeyPress,
35+
itemRender,
36+
} = props;
37+
const prefixCls = `${rootPrefixCls}-item`;
38+
const cls = classNames(prefixCls, `${prefixCls}-${page}`, {
39+
[`${prefixCls}-active`]: active,
40+
[`${prefixCls}-disabled`]: !page,
41+
[props.className]: className,
42+
});
43+
44+
const handleClick = () => {
45+
onClick(page);
46+
};
47+
48+
const handleKeyPress = (e: React.KeyboardEvent<HTMLLIElement>) => {
49+
onKeyPress(e, onClick, page);
50+
};
51+
52+
return (
53+
<li
54+
title={showTitle ? page.toString() : null}
55+
className={cls}
56+
onClick={handleClick}
57+
onKeyPress={handleKeyPress}
58+
tabIndex={0}
59+
>
60+
{itemRender(page, 'page', <a rel="nofollow">{page}</a>)}
61+
</li>
62+
);
63+
};
64+
65+
export default Pager;

0 commit comments

Comments
 (0)