1
+ import { ButtonHTMLAttributes , PropsWithChildren } from 'react'
2
+ import { Link , LinkProps } from 'react-router-dom'
1
3
import { Progressing } from '@Common/Progressing'
2
4
import { Tooltip } from '@Common/Tooltip'
3
5
import { ComponentSizeType } from '@Shared/constants'
4
- import { ButtonProps , ButtonStyleType , ButtonVariantType } from './types'
6
+ import { ButtonComponentType , ButtonProps , ButtonStyleType , ButtonVariantType } from './types'
5
7
import { BUTTON_SIZE_TO_ICON_CLASS_NAME_MAP , BUTTON_SIZE_TO_LOADER_SIZE_MAP } from './constants'
6
8
import { getButtonDerivedClass } from './utils'
7
9
import './button.scss'
8
10
11
+ const ButtonElement = ( {
12
+ component = ButtonComponentType . button ,
13
+ linkProps,
14
+ buttonProps,
15
+ onClick,
16
+ ...props
17
+ } : PropsWithChildren <
18
+ Omit <
19
+ ButtonProps ,
20
+ | 'text'
21
+ | 'variant'
22
+ | 'size'
23
+ | 'style'
24
+ | 'startIcon'
25
+ | 'endIcon'
26
+ | 'showTooltip'
27
+ | 'tooltipProps'
28
+ | 'dataTestId'
29
+ | 'isLoading'
30
+ > & {
31
+ className : string
32
+ 'data-testid' : ButtonProps [ 'dataTestId' ]
33
+ }
34
+ > ) => {
35
+ if ( component === ButtonComponentType . link ) {
36
+ return (
37
+ < Link
38
+ { ...linkProps }
39
+ { ...props }
40
+ // Added the specific class to ensure that the link override is applied
41
+ className = { `${ props . className } button__link ${ props . disabled ? 'dc__disable-click' : '' } ` }
42
+ onClick = { onClick as LinkProps [ 'onClick' ] }
43
+ />
44
+ )
45
+ }
46
+
47
+ return (
48
+ < button
49
+ { ...buttonProps }
50
+ { ...props }
51
+ // eslint-disable-next-line react/button-has-type
52
+ type = { buttonProps ?. type || 'button' }
53
+ onClick = { onClick as ButtonHTMLAttributes < HTMLButtonElement > [ 'onClick' ] }
54
+ />
55
+ )
56
+ }
57
+
9
58
/**
10
59
* Generic component for Button.
11
60
* Should be used in combination of variant, size and style.
@@ -55,6 +104,15 @@ import './button.scss'
55
104
* <Button text="Hello World" showTippy tippyContent="Tippy content" />
56
105
* ```
57
106
*
107
+ * @example With onClick
108
+ * ```tsx
109
+ * <Button text="Hello World" onClick={noop} />
110
+ * ```
111
+ *
112
+ * @example Link component
113
+ * ```tsx
114
+ * <Button component={ButtonComponentType.link} linkProps={{ to: '#' }} />
115
+ * ```
58
116
*/
59
117
const Button = ( {
60
118
dataTestId,
@@ -68,7 +126,6 @@ const Button = ({
68
126
isLoading = false ,
69
127
showTooltip = false ,
70
128
tooltipProps = { } ,
71
- type = 'button' ,
72
129
...props
73
130
} : ButtonProps ) => {
74
131
const isDisabled = disabled || isLoading
@@ -77,19 +134,17 @@ const Button = ({
77
134
return (
78
135
< Tooltip { ...tooltipProps } alwaysShowTippyOnHover = { showTooltip && ! ! tooltipProps ?. content } >
79
136
< div >
80
- < button
137
+ < ButtonElement
81
138
{ ...props }
82
139
disabled = { isDisabled }
83
- // eslint-disable-next-line react/button-has-type
84
- type = { type }
85
140
className = { `br-4 flex cursor dc__mnw-100 dc__tab-focus dc__position-rel dc__capitalize ${ getButtonDerivedClass ( { size, variant, style, isLoading } ) } ${ isDisabled ? 'dc__disabled' : '' } ` }
86
141
data-testid = { dataTestId }
87
142
>
88
143
{ startIcon && < span className = { iconClass } > { startIcon } </ span > }
89
144
< span className = "dc__mxw-150 dc__align-left dc__truncate" > { text } </ span >
90
145
{ endIcon && < span className = { iconClass } > { endIcon } </ span > }
91
146
{ isLoading && < Progressing size = { BUTTON_SIZE_TO_LOADER_SIZE_MAP [ size ] } /> }
92
- </ button >
147
+ </ ButtonElement >
93
148
</ div >
94
149
</ Tooltip >
95
150
)
0 commit comments