-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathbutton.tsx
97 lines (84 loc) · 1.84 KB
/
button.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import React, { FC } from 'react'
import classNames from 'classnames'
const isString = (children: React.ReactNode) => {
if (typeof children === 'string') {
return <span>{children}</span>
}
return children
}
export type ButtonType =
| 'default'
| 'primary'
| 'info'
| 'warning'
| 'danger'
| 'dashed'
| 'link'
| 'text'
export type ButtonSize = 'lg' | 'md' | 'sm'
export type ButtonHTMLTypes = 'submit' | 'button' | 'reset'
interface BaseButtonProps {
type?: ButtonType
size?: ButtonSize
disabled?: boolean
block?: boolean
className?: string
href?: string
icon?: React.ReactNode
children?: React.ReactNode
}
type NativeButtonProps = {
htmlType?: ButtonHTMLTypes
target?: string
onClick?: React.MouseEventHandler<HTMLElement>
} & BaseButtonProps &
Omit<React.ButtonHTMLAttributes<HTMLElement>, 'type'>
type AnchorButtonProps = {
href?: string
onClick?: React.MouseEventHandler<HTMLElement>
} & BaseButtonProps &
Omit<React.AnchorHTMLAttributes<HTMLElement>, 'type'>
export type ButtonProps = Partial<NativeButtonProps & AnchorButtonProps>
const Button: FC<ButtonProps> = ({
type,
htmlType,
size,
disabled,
block,
className,
href,
children,
...restProps
}) => {
const classes = classNames('mk-btn', className, {
[`mk-btn-${type}`]: type,
[`mk-btn-${size}`]: size,
'mk-btn-block': block,
})
if (type === 'link' && href) {
return (
<a className={classes} href={href} {...restProps}>
{children}
</a>
)
}
const kids = isString(children)
return (
<button
type={htmlType}
className={classes}
disabled={disabled}
{...restProps}
>
{kids}
</button>
)
}
Button.defaultProps = {
disabled: false,
type: 'default',
size: 'md',
block: false,
htmlType: 'button' as ButtonProps['htmlType'],
}
export default Button