Skip to content

Commit c6ee616

Browse files
authored
Merge pull request #1 from coreui/dev-refactor-kw
fix: CAlert: component refactor
2 parents 5770132 + a64f06e commit c6ee616

File tree

4 files changed

+97
-91
lines changed

4 files changed

+97
-91
lines changed

package.json

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@coreui/react",
3-
"version": "3.0.0-alpha.5",
3+
"version": "3.0.0-alpha.6",
44
"description": "CoreUI React Bootstrap 4 components",
55
"license": "MIT",
66
"author": {
@@ -42,26 +42,26 @@
4242
"link": "npm link && link.sh"
4343
},
4444
"dependencies": {
45-
"@popperjs/core": "^2.2.1",
45+
"@popperjs/core": "^2.3.3",
4646
"classnames": "^2.2.6",
47-
"core-js": "^3.6.4",
47+
"core-js": "^3.6.5",
4848
"lodash.isfunction": "^3.0.9",
4949
"lodash.isobject": "^3.0.2",
5050
"lodash.tonumber": "^4.0.3",
5151
"prop-types": "^15.7.2",
5252
"react-onclickout": "^2.0.8",
5353
"react-perfect-scrollbar": "~1.5.8",
54-
"react-popper": "^2.1.0",
54+
"react-popper": "^2.2.2",
5555
"react-transition-group": "^4.3.0",
56-
"tippy.js": "^6.1.1"
56+
"tippy.js": "^6.2.0"
5757
},
5858
"peerDependencies": {
5959
"@coreui/coreui": "^3.0.0",
60-
"@coreui/icons-react": "^1.0.0-alpha.0",
61-
"react": "^16.12.0",
62-
"react-dom": "^16.12.0",
60+
"@coreui/icons-react": "^1.0.0-alpha.3",
61+
"react": "^16.13.1",
62+
"react-dom": "^16.13.1",
6363
"react-router-dom": "^5.1.2",
64-
"mutationobserver-shim": "^0.3.3"
64+
"mutationobserver-shim": "^0.3.5"
6565
},
6666
"devDependencies": {
6767
"@coreui/coreui-chartjs": "^2.0.0-beta.0",
@@ -72,7 +72,7 @@
7272
"eslint": "^6.8.0",
7373
"eslint-plugin-import": "^2.20.1",
7474
"eslint-plugin-react": "^7.19.0",
75-
"nwb": "^0.24.3",
75+
"nwb": "^0.24.5",
7676
"react-app-polyfill": "^1.0.6",
7777
"sinon": "^5.1.1"
7878
},

src/CAlert.js

+63-61
Original file line numberDiff line numberDiff line change
@@ -1,107 +1,109 @@
1-
import React, {useState} from 'react';
2-
import PropTypes from 'prop-types';
3-
import classNames from 'classnames';
4-
import {tagPropType, mapToCssModules} from './Shared/helper.js';
5-
import CFade from './CFade';
1+
import React, { useState, useEffect, useRef } from 'react'
2+
import PropTypes from 'prop-types'
3+
import classNames from 'classnames'
4+
import { mapToCssModules } from './Shared/helper.js'
5+
import CFade from './CFade'
66

77
//component - CoreUI / CAlert
8-
9-
const CAlert = props=>{
10-
8+
const CAlert = props => {
119
let {
12-
tag: Tag,
1310
children,
1411
className,
1512
cssModule,
16-
custom,
1713
//
18-
toggle,
14+
onShowUpdate,
15+
closeButton,
1916
transition,
20-
closeAriaLabel,
21-
closeClassName,
2217
color,
2318
fade,
2419
show,
25-
iconSlot,
26-
closeProps,
2720
...attributes
28-
} = props;
21+
} = props
2922

3023
//render
31-
3224
const classes = mapToCssModules(classNames(
3325
className,
3426
'alert',
35-
`alert-${color}`,
36-
{ 'alert-dismissible': toggle }
37-
), cssModule);
38-
39-
const closeClasses = mapToCssModules(classNames('close', closeClassName), cssModule);
40-
27+
{
28+
[`alert-${color}`]: color,
29+
'alert-dismissible': closeButton
30+
}
31+
), cssModule)
32+
4133
const alertTransition = {
4234
...CFade.defaultProps,
4335
...transition,
4436
baseClass: fade ? transition.baseClass : '',
45-
timeout: fade ? transition.timeout : 0,
46-
};
37+
timeout: fade ? transition.timeout : 0
38+
}
39+
const [isOpen, setIsOpen] = useState(show)
40+
41+
useEffect(() => {
42+
setIsOpen(show)
43+
}, [show])
4744

48-
const [isOpen, setIsOpen] = useState(true);
45+
let timeout = useRef(null)
4946

50-
if (!custom){
51-
let userToggle = toggle;
52-
toggle = ()=>{
53-
setIsOpen(!isOpen);
54-
if (userToggle)
55-
userToggle();
56-
};
57-
show = isOpen;
58-
}
47+
useEffect(() => {
48+
if (onShowUpdate) {
49+
onShowUpdate(isOpen)
50+
}
51+
clearTimeout(timeout.current)
52+
if (typeof isOpen === 'number' && isOpen > 0) {
53+
timeout.current = setTimeout(() => {
54+
setIsOpen(isOpen - 1)
55+
}, 1000)
56+
}
57+
return () => clearTimeout(timeout)
58+
}, [isOpen])
5959

6060
return (
61-
<CFade {...attributes} {...alertTransition} tag={Tag} className={classes} in={show} role="alert">
62-
{!custom ?
63-
<button type="button" className={closeClasses} aria-label={closeAriaLabel} onClick={toggle} {...closeProps}>
64-
<span aria-hidden="true">{iconSlot}</span>
65-
</button>
66-
: null}
61+
<CFade
62+
{...alertTransition}
63+
tag="div"
64+
className={classes}
65+
in={Boolean(isOpen)}
66+
role="alert"
67+
{...attributes}
68+
>
6769
{children}
70+
{
71+
closeButton ?
72+
<button
73+
type="button"
74+
className="close"
75+
aria-label="Close"
76+
onClick={() => setIsOpen(false)}
77+
>
78+
<span aria-hidden="true">&times;</span>
79+
</button>
80+
: null
81+
}
6882
</CFade>
69-
);
70-
83+
)
7184
}
72-
7385
CAlert.propTypes = {
74-
tag: tagPropType,
7586
children: PropTypes.node,
7687
className: PropTypes.string,
7788
cssModule: PropTypes.object,
78-
custom: PropTypes.bool,
7989
//
8090
innerRef: PropTypes.oneOfType([PropTypes.object, PropTypes.func, PropTypes.string]),
81-
toggle: PropTypes.func,
82-
closeClassName: PropTypes.string,
83-
closeProps: PropTypes.object,
84-
closeAriaLabel: PropTypes.string,
91+
onShowUpdate: PropTypes.func,
92+
closeButton: PropTypes.bool,
8593
color: PropTypes.string,
8694
fade: PropTypes.bool,
87-
show: PropTypes.bool,
95+
show: PropTypes.oneOfType([PropTypes.bool, PropTypes.number]),
8896
transition: PropTypes.shape(CFade.propTypes),
89-
iconSlot: PropTypes.node
90-
};
91-
97+
}
9298
CAlert.defaultProps = {
9399
tag: 'div',
94100
//
95-
color: 'success',
96101
show: true,
97-
closeAriaLabel: 'Close',
98102
fade: true,
99103
transition: {
100104
...CFade.defaultProps,
101105
unmountOnExit: true,
102-
},
103-
iconSlot: <React.Fragment>&times;</React.Fragment>
104-
};
105-
106+
}
107+
}
106108
//export
107-
export default CAlert;
109+
export default CAlert

src/CBadge.js

+23-19
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import React from 'react';
2-
import PropTypes from 'prop-types';
3-
import classNames from 'classnames';
4-
import {tagPropType, mapToCssModules} from './Shared/helper.js';
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'
56

67
//component - CoreUI / CBadge
78

8-
const CBadge = props=>{
9+
const CBadge = props => {
910

1011
let {
1112
tag: Tag,
@@ -16,40 +17,43 @@ const CBadge = props=>{
1617
color,
1718
shape,
1819
...attributes
19-
} = props;
20+
} = props
2021

2122
// render
2223

2324
const classes = mapToCssModules(classNames(
2425
className,
2526
'badge',
26-
'badge-' + color,
27-
shape ? 'badge-'+shape : false
28-
), cssModule);
29-
30-
if (attributes.href && Tag === 'span') {
31-
Tag = 'a';
27+
{
28+
[`badge-${color}`]: color,
29+
[`badge-${shape}`]: shape
30+
}
31+
), cssModule)
32+
33+
if (attributes.to || attributes.href) {
34+
return (
35+
<CLink {...attributes} className={classes} ref={innerRef} />
36+
)
3237
}
3338

3439
return (
35-
<Tag {...attributes} className={classes} ref={innerRef} />
36-
);
40+
<Tag className={classes} {...attributes} ref={innerRef} />
41+
)
3742

3843
}
3944

4045
CBadge.propTypes = {
4146
tag: tagPropType,
42-
children: PropTypes.node,
4347
className: PropTypes.string,
4448
cssModule: PropTypes.object,
4549
//
4650
innerRef: PropTypes.oneOfType([PropTypes.object, PropTypes.func, PropTypes.string]),
4751
color: PropTypes.string,
48-
shape: PropTypes.string
49-
};
52+
shape: PropTypes.oneOf(['', 'pill'])
53+
}
5054

5155
CBadge.defaultProps = {
5256
tag: 'span'
53-
};
57+
}
5458

55-
export default CBadge;
59+
export default CBadge

src/CProgress.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ CProgress.propTypes = {
9797
innerRef: PropTypes.oneOfType([PropTypes.object, PropTypes.func, PropTypes.string]),
9898
barClassName: PropTypes.string,
9999
barProps: PropTypes.object,
100-
height: PropTypes.string,
100+
height: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
101101
bar: PropTypes.bool,
102102
multi: PropTypes.bool,
103103
value: PropTypes.oneOfType([

0 commit comments

Comments
 (0)