Skip to content

Commit 5770132

Browse files
committed
fix: CButton: component refactor
- add CLink component for functionality share - delete 'close', 'textHtml', 'aria-label' props - make 'disabled' prop add disabled HTML attribute
1 parent 61859ed commit 5770132

File tree

1 file changed

+38
-56
lines changed

1 file changed

+38
-56
lines changed

src/CButton.js

+38-56
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,70 @@
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'
66
//component - CoreUI / CButton
77

8-
const CButton = props=>{
8+
const CButton = props => {
99

1010
let {
1111
tag: Tag,
12-
children,
1312
className,
1413
cssModule,
1514
//
1615
innerRef,
16+
onClick,
17+
disabled,
1718
active,
18-
'aria-label': ariaLabel,
1919
block,
20-
close,
2120
color,
2221
size,
23-
textHtml,
2422
pressed,
2523
shape,
2624
variant,
2725
...attributes
28-
} = props;
26+
} = props
2927

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)
3929

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
4731

32+
//render
4833
const classes = mapToCssModules(classNames(
4934
className,
50-
{ close },
51-
close || 'btn',
52-
close || btnColor,
35+
'btn',
36+
variant || color ? `btn${variant ? '-' + variant : ''}-${color}` : false,
5337
size ? `btn-${size}` : false,
5438
block ? 'btn-block' : false,
5539
shape ? `btn-${shape}` : false,
5640
pressed ? 'btn-pressed' : false,
57-
{ 'active': active,
58-
'disabled': props.disabled }
59-
), cssModule);
41+
{ 'active': active && !isLink,
42+
'disabled': disabled && !isLink }
43+
), cssModule)
6044

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>
7745

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+
}
7864
}
7965

8066
CButton.propTypes = {
8167
tag: tagPropType,
82-
children: PropTypes.node,
8368
cssModule: PropTypes.object,
8469
className: PropTypes.string,
8570
//
@@ -89,18 +74,15 @@ CButton.propTypes = {
8974
shape: PropTypes.string,
9075
variant: PropTypes.oneOf(['', 'ghost', 'outline']),
9176
color: PropTypes.string,
92-
close: PropTypes.bool,
9377
disabled: PropTypes.bool,
9478
onClick: PropTypes.func,
9579
size: PropTypes.string,
9680
pressed: PropTypes.bool,
97-
textHtml: PropTypes.string,
98-
'aria-label': PropTypes.string,
99-
};
81+
}
10082

10183
CButton.defaultProps = {
10284
tag: 'button'
103-
};
85+
}
10486

10587
//export
106-
export default CButton;
88+
export default CButton

0 commit comments

Comments
 (0)