1
- import React from 'react' ;
2
- import PropTypes from 'prop-types' ;
3
- import classNames from 'classnames' ;
4
- import { tagPropType , mapToCssModules } from './Shared/helper.js' ;
5
-
1
+ import React from 'react'
2
+ import PropTypes from 'prop-types'
3
+ import classNames from 'classnames'
4
+ import { tagPropType , mapToCssModules } from './Shared/helper.js'
5
+ import CLink from './CLink'
6
6
//component - CoreUI / CButton
7
7
8
- const CButton = props => {
8
+ const CButton = props => {
9
9
10
10
let {
11
11
tag : Tag ,
12
- children,
13
12
className,
14
13
cssModule,
15
14
//
16
15
innerRef,
16
+ onClick,
17
+ disabled,
17
18
active,
18
- 'aria-label' : ariaLabel ,
19
19
block,
20
- close,
21
20
color,
22
21
size,
23
- textHtml,
24
22
pressed,
25
23
shape,
26
24
variant,
27
25
...attributes
28
- } = props ;
26
+ } = props
29
27
30
- const onClick = e => {
31
- if ( props . disabled ) {
32
- e . preventDefault ( ) ;
33
- return ;
34
- }
35
- if ( props . onClick ) {
36
- props . onClick ( e ) ;
37
- }
38
- }
28
+ const click = e => onClick && onClick ( e )
39
29
40
- //render
41
-
42
- if ( close && typeof children === 'undefined' ) {
43
- children = < span aria-hidden > ×</ span > ;
44
- }
45
-
46
- const btnColor = `btn${ variant ? '-' + variant : '' } -${ color } ` ;
30
+ const isLink = attributes . to || attributes . href
47
31
32
+ //render
48
33
const classes = mapToCssModules ( classNames (
49
34
className ,
50
- { close } ,
51
- close || 'btn' ,
52
- close || btnColor ,
35
+ 'btn' ,
36
+ variant || color ? `btn${ variant ? '-' + variant : '' } -${ color } ` : false ,
53
37
size ? `btn-${ size } ` : false ,
54
38
block ? 'btn-block' : false ,
55
39
shape ? `btn-${ shape } ` : false ,
56
40
pressed ? 'btn-pressed' : false ,
57
- { 'active' : active ,
58
- 'disabled' : props . disabled }
59
- ) , cssModule ) ;
41
+ { 'active' : active && ! isLink ,
42
+ 'disabled' : disabled && ! isLink }
43
+ ) , cssModule )
60
44
61
- if ( attributes . href && Tag === 'button' ) {
62
- Tag = 'a' ;
63
- }
64
-
65
- const defaultAriaLabel = close ? 'Close' : null ;
66
-
67
- return < Tag
68
- type = { ( Tag === 'button' && attributes . onClick ) ? 'button' : undefined }
69
- { ...attributes }
70
- className = { classes }
71
- onClick = { onClick }
72
- aria-label = { ariaLabel || defaultAriaLabel }
73
- ref = { innerRef }
74
- >
75
- { textHtml ? textHtml : children }
76
- </ Tag >
77
45
46
+ if ( isLink ) {
47
+ return < CLink
48
+ { ...attributes }
49
+ active = { active }
50
+ className = { classes }
51
+ onClick = { click }
52
+ innerRef = { innerRef }
53
+ />
54
+ } else {
55
+ return < Tag
56
+ className = { classes }
57
+ type = "button"
58
+ disabled = { disabled }
59
+ { ...attributes }
60
+ onClick = { click }
61
+ ref = { innerRef }
62
+ />
63
+ }
78
64
}
79
65
80
66
CButton . propTypes = {
81
67
tag : tagPropType ,
82
- children : PropTypes . node ,
83
68
cssModule : PropTypes . object ,
84
69
className : PropTypes . string ,
85
70
//
@@ -89,18 +74,15 @@ CButton.propTypes = {
89
74
shape : PropTypes . string ,
90
75
variant : PropTypes . oneOf ( [ '' , 'ghost' , 'outline' ] ) ,
91
76
color : PropTypes . string ,
92
- close : PropTypes . bool ,
93
77
disabled : PropTypes . bool ,
94
78
onClick : PropTypes . func ,
95
79
size : PropTypes . string ,
96
80
pressed : PropTypes . bool ,
97
- textHtml : PropTypes . string ,
98
- 'aria-label' : PropTypes . string ,
99
- } ;
81
+ }
100
82
101
83
CButton . defaultProps = {
102
84
tag : 'button'
103
- } ;
85
+ }
104
86
105
87
//export
106
- export default CButton ;
88
+ export default CButton
0 commit comments