forked from coreui/coreui-react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCAlert.js
92 lines (80 loc) · 2 KB
/
CAlert.js
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
import React, { useState, useEffect, useRef } from 'react'
import PropTypes from 'prop-types'
import classNames from 'classnames'
import CFade from '../fade/CFade'
import { omitByKeys } from '@coreui/utils/src'
import { CFadeProps } from '../utils/helper.js'
import CButtonClose from '../button/CButtonClose'
//component - CoreUI / CAlert
const CAlert = props => {
let {
children,
className,
innerRef,
//
onShowChange,
closeButton,
color,
fade,
show,
...attributes
} = props
//render
const classes = classNames(
className,
'alert',
{
[`alert-${color}`]: color,
'alert-dismissible': closeButton
}
)
const alertTransition = {
baseClass: fade ? CFade.baseClass : '',
timeout: fade ? CFade.timeout : 0,
unmountOnExit: true
}
const [isOpen, setIsOpen] = useState(show)
useEffect(() => {
setIsOpen(show)
}, [show])
let timeout = useRef()
useEffect(() => {
onShowChange && onShowChange(isOpen)
clearTimeout(timeout.current)
if (typeof isOpen === 'number' && isOpen > 0) {
timeout.current = setTimeout(() => {
setIsOpen(isOpen - 1)
}, 1000)
}
return () => clearTimeout(timeout.current)
}, [isOpen])
const attrs = omitByKeys(attributes, CFadeProps)
return (
<CFade
{...alertTransition}
className={classes}
in={Boolean(isOpen)}
role="alert"
{...attrs}
innerRef={innerRef}
>
{ children }
{ closeButton && <CButtonClose onClick={() => setIsOpen(false)} />}
</CFade>
)
}
CAlert.propTypes = {
children: PropTypes.node,
className: PropTypes.oneOfType([PropTypes.string, PropTypes.object, PropTypes.array]),
innerRef: PropTypes.oneOfType([PropTypes.object, PropTypes.func]),
onShowChange: PropTypes.func,
closeButton: PropTypes.bool,
color: PropTypes.string,
fade: PropTypes.bool,
show: PropTypes.oneOfType([PropTypes.bool, PropTypes.number]),
}
CAlert.defaultProps = {
show: true,
fade: true
}
export default CAlert