Skip to content
This repository was archived by the owner on Nov 8, 2022. It is now read-only.

Commit d7e1309

Browse files
authored
chore(ts-exercise): buttons comp (#1028)
* chore(ts-js): OrButton comp convert * chore(ts-js): ArrowLink comp convert * chore(ts-js): ArrowButton comp convert * chore(ts-js): Button comp convert
1 parent d113a48 commit d7e1309

File tree

12 files changed

+136
-203
lines changed

12 files changed

+136
-203
lines changed

src/components/Buttons/ArrowButton.js renamed to src/components/Buttons/ArrowButton.tsx

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,35 @@
55
*/
66

77
import React, { useRef, useState, useEffect } from 'react'
8-
import T from 'prop-types'
9-
import { values } from 'ramda'
108

9+
import type { TSIZE } from '@/spec'
1110
import { ICON } from '@/config'
1211
import { SIZE } from '@/constant'
1312
import { buildLog } from '@/utils'
1413

1514
import { Wrapper, Text, LeftIcon, RightIcon } from './styles/arrow_button'
1615

1716
/* eslint-disable-next-line */
18-
const log = buildLog('c:Buttons:index')
17+
const log = buildLog('c:Buttons:ArrowButton')
1918

20-
const ArrowButton = ({
21-
children,
22-
onClick,
23-
size,
24-
direction,
25-
dimWhenIdle,
26-
arrowStyle,
27-
disabled,
19+
type TProps = {
20+
children?: React.ReactNode
21+
onClick?: () => void
22+
size?: TSIZE
23+
direction: 'left' | 'right'
24+
dimWhenIdle?: boolean
25+
disabled?: boolean
26+
arrowStyle?: string
27+
}
28+
29+
const ArrowButton: React.FC<TProps> = ({
30+
children = '下一步',
31+
onClick = log,
32+
size = SIZE.SMALL,
33+
direction = 'right',
34+
dimWhenIdle = false,
35+
arrowStyle = 'default',
36+
disabled = false,
2837
}) => {
2938
const iconSrc =
3039
arrowStyle === 'default'
@@ -88,24 +97,4 @@ const ArrowButton = ({
8897
)
8998
}
9099

91-
ArrowButton.propTypes = {
92-
children: T.oneOfType([T.string, T.node]),
93-
arrowStyle: T.oneOf(['default', 'simple']),
94-
size: T.oneOf(values(SIZE)),
95-
direction: T.oneOf(['left', 'right']),
96-
dimWhenIdle: T.bool,
97-
onClick: T.func,
98-
disabled: T.bool,
99-
}
100-
101-
ArrowButton.defaultProps = {
102-
children: '下一步',
103-
arrowStyle: 'default',
104-
size: SIZE.SMALL,
105-
direction: 'right',
106-
dimWhenIdle: false,
107-
onClick: log,
108-
disabled: false,
109-
}
110-
111100
export default React.memo(ArrowButton)

src/components/Buttons/ArrowLink.js renamed to src/components/Buttons/ArrowLink.tsx

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,35 @@
55
*/
66

77
import React from 'react'
8-
import T from 'prop-types'
9-
import { values } from 'ramda'
108

9+
import type { TSIZE } from '@/spec'
1110
import { ICON_CMD } from '@/config'
1211
import { SIZE } from '@/constant'
1312
import { buildLog } from '@/utils'
1413

1514
import { Wrapper, Text, RightIcon } from './styles/arrow_link'
1615

1716
/* eslint-disable-next-line */
18-
const log = buildLog('c:Buttons:index')
17+
const log = buildLog('c:Buttons:ArrowLink')
18+
19+
type TProps = {
20+
className: string
21+
children?: React.ReactNode
22+
size?: TSIZE
23+
href: string
24+
target: '_blank' | ''
25+
color?: string
26+
hoverColor?: string
27+
}
1928

20-
const ArrowLink = ({
21-
className,
22-
children,
23-
size,
24-
href,
25-
target,
26-
color,
27-
hoverColor,
29+
const ArrowLink: React.FC<TProps> = ({
30+
className = '',
31+
children = '下一步',
32+
size = SIZE.SMALL,
33+
href = '',
34+
target = '_blank',
35+
color = '',
36+
hoverColor = '',
2837
}) => {
2938
return (
3039
<Wrapper
@@ -46,24 +55,4 @@ const ArrowLink = ({
4655
)
4756
}
4857

49-
ArrowLink.propTypes = {
50-
className: T.string,
51-
children: T.oneOfType([T.string, T.node]),
52-
size: T.oneOf(values(SIZE)),
53-
href: T.string,
54-
color: T.string,
55-
hoverColor: T.string,
56-
target: T.oneOf(['_blank', '']),
57-
}
58-
59-
ArrowLink.defaultProps = {
60-
className: '',
61-
children: '下一步',
62-
size: SIZE.SMALL,
63-
href: '',
64-
color: '',
65-
hoverColor: '',
66-
target: '_blank',
67-
}
68-
6958
export default React.memo(ArrowLink)

src/components/Buttons/Button.js renamed to src/components/Buttons/Button.tsx

Lines changed: 30 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import React, { useEffect, useState } from 'react'
2-
import T from 'prop-types'
32

3+
import type { TSIZE_TSM } from '@/spec'
44
import { SIZE } from '@/constant'
5+
import { buildLog } from '@/utils'
56

67
import {
78
Wrapper,
@@ -12,21 +13,37 @@ import {
1213
LoadingText,
1314
} from './styles/button'
1415

15-
const clearTimerIfNeed = (timerId) => {
16+
/* eslint-disable-next-line */
17+
const log = buildLog('c:Buttons:Button')
18+
19+
const clearTimerIfNeed = (timerId: number): void => {
1620
if (timerId) clearTimeout(timerId)
1721
}
1822

19-
const Button = ({
20-
children,
21-
ghost,
22-
type,
23-
onClick,
24-
size,
25-
className,
26-
loading,
27-
loadingText,
28-
noBorder,
29-
disabled,
23+
type TProps = {
24+
children?: React.ReactNode
25+
className?: string
26+
ghost?: boolean
27+
type: 'primary' | 'red' | 'ghost'
28+
size?: TSIZE_TSM
29+
onClick?: () => void
30+
loading?: boolean | null
31+
loadingText?: string
32+
noBorder?: boolean
33+
disabled?: boolean
34+
}
35+
36+
const Button: React.FC<TProps> = ({
37+
children = 'button',
38+
ghost = false,
39+
type = 'primary',
40+
onClick = log,
41+
size = SIZE.MEDIUM,
42+
className = '',
43+
loading = null,
44+
loadingText = '发布中',
45+
noBorder = false,
46+
disabled = false,
3047
}) => {
3148
const [loadingWidth, setLoadingWidth] = useState(0) // 0 || 20 || 65 || 90 || 100
3249

@@ -97,31 +114,4 @@ const Button = ({
97114
}
98115
}
99116

100-
Button.propTypes = {
101-
children: T.oneOfType([T.string, T.node]),
102-
ghost: T.bool,
103-
type: T.oneOf(['primary', 'red', 'ghost']),
104-
size: T.oneOf([SIZE.TINY, SIZE.SMALL, SIZE.MEDIUM]),
105-
onClick: T.func,
106-
className: T.string,
107-
loading: T.oneOfType([T.bool, T.instanceOf(null)]),
108-
loadingText: T.string,
109-
noBorder: T.bool,
110-
disabled: T.bool,
111-
}
112-
113-
Button.defaultProps = {
114-
children: 'Button',
115-
ghost: false,
116-
type: 'primary',
117-
size: SIZE.MEDIUM,
118-
// eslint-disable-next-line no-console
119-
onClick: console.log,
120-
className: '',
121-
loading: null,
122-
loadingText: '发布中..',
123-
noBorder: false,
124-
disabled: false,
125-
}
126-
127117
export default React.memo(Button)

src/components/Buttons/OrButton/HorizontalButton.tsx

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from 'react'
22

3-
import type { TSIZE } from '@/spec'
3+
import type { TProps as TButtonProps } from './index'
44
import { SIZE } from '@/constant'
55

66
import {
@@ -10,21 +10,22 @@ import {
1010
RightButton,
1111
} from '../styles/or_button/horizontal_button'
1212

13-
type TProps = {
14-
activeKey: string
15-
size?: TSIZE
16-
onClick: (key: string) => void
17-
group: {
18-
key: string
19-
title: string
20-
}[]
21-
}
13+
type TProps = Omit<TButtonProps, 'direction'>
2214

2315
const HorizontalButton: React.FC<TProps> = ({
2416
onClick,
25-
size = SIZE.SMALL as TSIZE,
17+
size = SIZE.SMALL,
2618
activeKey,
27-
group,
19+
group = [
20+
{
21+
key: 'hello',
22+
title: 'hello',
23+
},
24+
{
25+
key: 'cps',
26+
title: 'cps',
27+
},
28+
],
2829
}) => {
2930
return (
3031
<Wrapper size={size}>
Lines changed: 19 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from 'react'
2-
import T from 'prop-types'
32

3+
import type { TProps as TButtonProps } from './index'
44
import { SIZE } from '@/constant'
55

66
import {
@@ -10,9 +10,25 @@ import {
1010
BottomButton,
1111
} from '../styles/or_button/vertical_button'
1212

13-
const VerticalButton = ({ onClick, size, activeKey, group }) => {
13+
type TProps = Omit<TButtonProps, 'direction'>
14+
15+
const VerticalButton: React.FC<TProps> = ({
16+
onClick,
17+
size = SIZE.SMALL,
18+
activeKey,
19+
group = [
20+
{
21+
key: 'hello',
22+
title: 'hello',
23+
},
24+
{
25+
key: 'cps',
26+
title: 'cps',
27+
},
28+
],
29+
}) => {
1430
return (
15-
<Wrapper size={size}>
31+
<Wrapper>
1632
<UpButton
1733
size={size}
1834
active={group[0].key === activeKey}
@@ -32,33 +48,4 @@ const VerticalButton = ({ onClick, size, activeKey, group }) => {
3248
)
3349
}
3450

35-
VerticalButton.propTypes = {
36-
size: T.oneOf([SIZE.MEDIUM, SIZE.SMALL]),
37-
onClick: T.func,
38-
activeKey: T.string,
39-
group: T.arrayOf(
40-
T.shape({
41-
key: T.string,
42-
title: T.string,
43-
}),
44-
),
45-
}
46-
47-
VerticalButton.defaultProps = {
48-
size: SIZE.MEDIUM,
49-
// eslint-disable-next-line no-console
50-
onClick: console.log,
51-
activeKey: 'hello',
52-
group: [
53-
{
54-
key: 'hello',
55-
title: 'hello',
56-
},
57-
{
58-
key: 'cps',
59-
title: 'cps',
60-
},
61-
],
62-
}
63-
6451
export default VerticalButton

0 commit comments

Comments
 (0)