1
- import React , { forwardRef , HTMLAttributes , useState } from 'react'
1
+ import React , { forwardRef , HTMLAttributes , useEffect , useState } from 'react'
2
2
import PropTypes from 'prop-types'
3
3
import classNames from 'classnames'
4
4
import { CSSTransition } from 'react-transition-group'
@@ -22,13 +22,9 @@ export interface CAlertProps extends HTMLAttributes<HTMLDivElement> {
22
22
*/
23
23
dismissible ?: boolean
24
24
/**
25
- * Method called before the dissmiss animation has started .
25
+ * Callback fired when the component requests to be closed .
26
26
*/
27
- onDismiss ?: ( ) => void
28
- /**
29
- * Method called after the dissmiss animation has completed and the component is removed from the dom.
30
- */
31
- onDismissed ?: ( ) => void
27
+ onClose ?: ( ) => void
32
28
/**
33
29
* Set the alert variant to a solid.
34
30
*/
@@ -48,36 +44,47 @@ export const CAlert = forwardRef<HTMLDivElement, CAlertProps>(
48
44
dismissible,
49
45
variant,
50
46
visible = true ,
51
- onDismiss,
52
- onDismissed,
47
+ onClose,
53
48
...rest
54
49
} ,
55
50
ref ,
56
51
) => {
57
52
const [ _visible , setVisible ] = useState ( visible )
58
53
54
+ useEffect ( ( ) => {
55
+ setVisible ( visible )
56
+ } , [ visible ] )
57
+
59
58
const _className = classNames (
60
59
'alert' ,
61
60
variant === 'solid' ? `bg-${ color } text-white` : `alert-${ color } ` ,
62
61
{
63
62
'alert-dismissible fade' : dismissible ,
64
- show : _visible && dismissible ,
63
+ // show: _visible,
65
64
} ,
66
65
className ,
67
66
)
68
67
68
+ const getTransitionClass = ( state : string ) => {
69
+ return state === 'entered'
8000
span> && 'show'
70
+ }
71
+
69
72
return (
70
- < CSSTransition
71
- in = { _visible }
72
- timeout = { 150 }
73
- onExit = { onDismiss }
74
- onExited = { onDismissed }
75
- unmountOnExit
76
- >
77
- < div className = { _className } role = "alert" { ...rest } ref = { ref } >
78
- { children }
79
- { dismissible && < CCloseButton onClick = { ( ) => setVisible ( false ) } /> }
80
- </ div >
73
+ < CSSTransition in = { _visible } timeout = { 150 } onExit = { onClose } mountOnEnter unmountOnExit >
74
+ { ( state ) => {
75
+ const transitionClass = getTransitionClass ( state )
76
+ return (
77
+ < div
78
+ className = { classNames ( _className , transitionClass ) }
79
+ role = &
8000
quot;alert"
80
+ { ...rest }
81
+ ref = { ref }
82
+ >
83
+ { children }
84
+ { dismissible && < CCloseButton onClick = { ( ) => setVisible ( false ) } /> }
85
+ </ div >
86
+ )
87
+ } }
81
88
</ CSSTransition >
82
89
)
83
90
} ,
@@ -88,8 +95,7 @@ CAlert.propTypes = {
88
95
className : PropTypes . string ,
89
96
color : colorPropType . isRequired ,
90
97
dismissible : PropTypes . bool ,
91
- onDismiss : PropTypes . func ,
92
- onDismissed : PropTypes . func ,
98
+ onClose : PropTypes . func ,
93
99
variant : PropTypes . string ,
94
100
visible : PropTypes . bool ,
95
101
}
0 commit comments