Skip to content

Commit 617fbf4

Browse files
authored
chore: code optimization (#540)
* chore: code optimization * type: update type
1 parent a513003 commit 617fbf4

File tree

5 files changed

+38
-27
lines changed

5 files changed

+38
-27
lines changed

docs/examples/lessPages.tsx

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,17 @@ const doublePath = [
2020
'1c9.1-11.7 9.1-27.9 0-39.5z',
2121
];
2222

23-
const getSvgIcon = (path, reverse, type) => {
23+
const getSvgIcon = (
24+
path: string | string[],
25+
reverse: boolean,
26+
type: string,
27+
) => {
2428
const paths = Array.isArray(path) ? path : [path];
25-
const renderPaths = paths.map((p, i) => {
26-
return <path key={i} d={p} />;
27-
});
29+
const renderPaths = paths.map<React.ReactNode>((p, i) => (
30+
<path key={i} d={p} />
31+
));
2832
return (
29-
<i
30-
className={`custom-icon-${type}`}
31-
style={{
32-
fontSize: '16px',
33-
}}
34-
>
33+
<i className={`custom-icon-${type}`} style={{ fontSize: '16px' }}>
3534
<svg
3635
viewBox="0 0 1024 1024"
3736
width="1em"
@@ -58,11 +57,9 @@ class App extends React.Component {
5857
current: 3,
5958
useIcon: true,
6059
};
61-
onChange = (page) => {
60+
onChange = (page: number) => {
6261
console.log(page);
63-
this.setState({
64-
current: page,
65-
});
62+
this.setState({ current: page });
6663
};
6764
toggleCustomIcon = () => {
6865
this.setState({

src/Options.tsx

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ interface OptionsProps {
2929

3030
const defaultPageSizeOptions = ['10', '20', '50', '100'];
3131

32-
function Options(props: OptionsProps) {
32+
const Options: React.FC<OptionsProps> = (props) => {
3333
const {
3434
pageSizeOptions = defaultPageSizeOptions,
3535
locale,
@@ -113,12 +113,12 @@ function Options(props: OptionsProps) {
113113
return null;
114114
}
115115

116-
let changeSelect = null;
117-
let goInput = null;
118-
let gotoButton = null;
116+
let changeSelect: React.ReactNode = null;
117+
let goInput: React.ReactNode = null;
118+
let gotoButton: React.ReactNode = null;
119119

120120
if (changeSize && Select) {
121-
const options = getPageSizeOptions().map((opt, i) => (
121+
const options = getPageSizeOptions().map<React.ReactNode>((opt, i) => (
122122
<Select.Option key={i} value={opt.toString()}>
123123
{mergeBuildOptionText(opt)}
124124
</Select.Option>
@@ -187,6 +187,10 @@ function Options(props: OptionsProps) {
187187
{goInput}
188188
</li>
189189
);
190+
};
191+
192+
if (process.env.NODE_ENV !== 'production') {
193+
Options.displayName = 'Options';
190194
}
191195

192196
export default Options;

src/Pager.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,8 @@ const Pager: React.FC<PagerProps> = (props) => {
6363
) : null;
6464
};
6565

66+
if (process.env.NODE_ENV !== 'production') {
67+
Pager.displayName = 'Pager';
68+
}
69+
6670
export default Pager;

src/Pagination.tsx

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ function calculatePage(p: number | undefined, pageSize: number, total: number) {
3333
return Math.floor((total - 1) / _pageSize) + 1;
3434
}
3535

36-
function Pagination(props: PaginationProps) {
36+
const Pagination: React.FC<PaginationProps> = (props) => {
3737
const {
3838
// cls
3939
prefixCls = 'rc-pagination',
@@ -117,7 +117,7 @@ function Pagination(props: PaginationProps) {
117117
/>
118118
);
119119
if (typeof icon === 'function') {
120-
iconNode = React.createElement(icon, { ...props });
120+
iconNode = React.createElement<PaginationProps>(icon, { ...props });
121121
}
122122
return iconNode as React.ReactNode;
123123
}
@@ -275,8 +275,8 @@ function Pagination(props: PaginationProps) {
275275
'prev',
276276
getItemIcon(prevIcon, 'prev page'),
277277
);
278-
return React.isValidElement(prevButton)
279-
? React.cloneElement<any>(prevButton, { disabled: !hasPrev })
278+
return React.isValidElement<HTMLButtonElement>(prevButton)
279+
? React.cloneElement(prevButton, { disabled: !hasPrev })
280280
: prevButton;
281281
}
282282

@@ -286,8 +286,8 @@ function Pagination(props: PaginationProps) {
286286
'next',
287287
getItemIcon(nextIcon, 'next page'),
288288
);
289-
return React.isValidElement(nextButton)
290-
? React.cloneElement<any>(nextButton, { disabled: !hasNext })
289+
return React.isValidElement<HTMLButtonElement>(nextButton)
290+
? React.cloneElement(nextButton, { disabled: !hasNext })
291291
: nextButton;
292292
}
293293

@@ -323,7 +323,8 @@ function Pagination(props: PaginationProps) {
323323
return null;
324324
}
325325

326-
const pagerList: React.ReactElement[] = [];
326+
const pagerList: React.ReactElement<PagerProps>[] = [];
327+
327328
const pagerProps: PagerProps = {
328329
rootPrefixCls: prefixCls,
329330
onClick: handleChange,
@@ -468,7 +469,7 @@ function Pagination(props: PaginationProps) {
468469
}
469470

470471
if (current - 1 >= pageBufferSize * 2 && current !== 1 + 2) {
471-
pagerList[0] = React.cloneElement(pagerList[0], {
472+
pagerList[0] = React.cloneElement<PagerProps>(pagerList[0], {
472473
className: classNames(
473474
`${prefixCls}-item-after-jump-prev`,
474475
pagerList[0].props.className,
@@ -574,6 +575,10 @@ function Pagination(props: PaginationProps) {
574575
/>
575576
</ul>
576577
);
578+
};
579+
580+
if (process.env.NODE_ENV !== 'production') {
581+
Pagination.displayName = 'Pagination';
577582
}
578583

579584
export default Pagination;

src/interface.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ export interface PaginationProps extends Partial<PaginationData> {
6060
) => React.ReactNode;
6161
showTotal?: (total: number, range: [number, number]) => React.ReactNode;
6262
}
63+
6364
export interface PaginationState {
6465
current: number;
6566
currentInputValue: number;

0 commit comments

Comments
 (0)