|
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 Slot from './Shared/Slot'; |
6 |
| -import CToastHeader from './CToastHeader'; |
7 |
| -import CToastBody from './CToastBody'; |
8 |
| -import CButtonClose from './CButtonClose'; |
9 |
| -import CFade from './CFade'; |
10 |
| -import style from './CToast.module.css'; |
| 1 | +import React, { useState, useEffect } 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' |
| 6 | +import style from './CToast.module.css' |
11 | 7 |
|
12 |
| -//component - CoreUI / CToast |
| 8 | +export const Context = React.createContext({}) |
13 | 9 |
|
14 |
| -const CToast = props=>{ |
| 10 | +//component - CoreUI / CToast |
| 11 | +const CToast = props => { |
15 | 12 |
|
16 | 13 | const {
|
17 |
| - tag: Tag, |
18 | 14 | className,
|
19 | 15 | cssModule,
|
20 |
| - custom, |
| 16 | + children, |
21 | 17 | //
|
22 | 18 | innerRef,
|
23 | 19 | show,
|
24 |
| - header, |
25 |
| - headerSlot, |
26 | 20 | autohide,
|
27 |
| - closeButton, |
28 | 21 | fade,
|
29 |
| - transition, |
| 22 | + onStateChange, |
30 | 23 | ...attributes
|
31 |
| - } = props; |
32 |
| - |
33 |
| - const [shown, setShown] = useState(show); |
34 |
| - |
35 |
| - const transitionPar = { |
36 |
| - ...CFade.defaultProps, |
37 |
| - ...transition, |
38 |
| - baseClass: fade ? transition.baseClass : '', |
39 |
| - timeout: fade ? transition.timeout : 0, |
40 |
| - }; |
41 |
| - |
42 |
| - let timeout; |
43 |
| - |
44 |
| - const setAutohide = ()=>{ |
45 |
| - timeout = setTimeout(()=>{ |
46 |
| - setShown(false); |
47 |
| - }, autohide); |
| 24 | + } = props |
| 25 | + |
| 26 | + const [state, setState] = useState(show) |
| 27 | + const [timer, setTimer] = useState() |
| 28 | + |
| 29 | + useEffect(() => { |
| 30 | + setState(show) |
| 31 | + }, [show]) |
| 32 | + |
| 33 | + useEffect(() => { |
| 34 | + if (state === true && autohide) { |
| 35 | + setAutohide() |
| 36 | + } |
| 37 | + onStateChange && onStateChange(state) |
| 38 | + return () => clearTimeout(timer) |
| 39 | + }, [state]) |
| 40 | + |
| 41 | + const setAutohide = () => { |
| 42 | + clearTimeout(timer) |
| 43 | + setTimer(setTimeout(() => { |
| 44 | + startAutohide() |
| 45 | + }, autohide)) |
48 | 46 | }
|
49 | 47 |
|
50 |
| - const onMouseOver = ()=>{ |
51 |
| - clearTimeout(timeout); |
| 48 | + const onMouseOver = () => { |
| 49 | + if (state !== 'closing') { |
| 50 | + state !== true && setState(true) |
| 51 | + clearTimeout(timer) |
| 52 | + } |
52 | 53 | }
|
53 |
| - const onMouseOut = ()=>{ |
54 |
| - if (autohide) |
55 |
| - setAutohide(); |
| 54 | + |
| 55 | + const onMouseOut = () => { |
| 56 | + if (autohide && state !== 'closing') { |
| 57 | + setAutohide() |
| 58 | + } |
56 | 59 | }
|
57 |
| - const onClick = ()=>{ |
58 |
| - setShown(false); |
| 60 | + |
| 61 | + const startAutohide = () => { |
| 62 | + setState('hiding') |
| 63 | + clearTimeout(timer) |
| 64 | + setTimer(setTimeout(() => { |
| 65 | + setState(false) |
| 66 | + }, 2000)) |
59 | 67 | }
|
60 | 68 |
|
61 |
| - if (autohide){ |
62 |
| - setAutohide(); |
| 69 | + const close = () => { |
| 70 | + setState(fade ? 'closing' : false) |
| 71 | + clearTimeout(timer) |
| 72 | + fade && setTimer(setTimeout(() => { |
| 73 | + setState(false) |
| 74 | + }, 500)) |
63 | 75 | }
|
64 | 76 |
|
65 | 77 | // render
|
66 |
| - |
67 | 78 | const classes = mapToCssModules(classNames(
|
68 |
| - className, |
69 |
| - 'toast', |
70 |
| - 'mytoast', |
71 |
| - ), Object.assign(style, cssModule)); |
72 |
| - |
73 |
| - if (!shown) |
74 |
| - return null; |
75 |
| - |
76 |
| - if (!custom) |
77 |
| - return ( |
78 |
| - <CFade |
79 |
| - {...attributes} |
80 |
| - {...transitionPar} |
81 |
| - tag={Tag} |
82 |
| - className={classes} |
83 |
| - in={true} |
84 |
| - role="alert" |
85 |
| - innerRef={innerRef} |
86 |
| - > |
87 |
| - <Tag |
88 |
| - {...attributes} |
| 79 | + 'toast', className |
| 80 | + ), cssModule) |
| 81 | + |
| 82 | + const fadeClasses = classNames( |
| 83 | + fade && style[`${state === 'hiding' ? 'slowfade' : 'fade' }`] |
| 84 | + ) |
| 85 | + |
| 86 | + return ( |
| 87 | + <Context.Provider value={{ close }}> |
| 88 | + { |
| 89 | + state && <CFade |
| 90 | + tag={'div'} |
| 91 | + className={classes} |
| 92 | + role="alert" |
89 | 93 | aria-live="assertive"
|
90 | 94 | aria-atomic="true"
|
| 95 | + {...attributes} |
| 96 | + in={state === true} |
91 | 97 | onMouseOver={onMouseOver}
|
92 | 98 | onMouseOut={onMouseOut}
|
93 |
| - className={classes} |
| 99 | + baseClass={fadeClasses} |
| 100 | + innerRef={innerRef} |
94 | 101 | >
|
95 |
| - <CToastHeader> |
96 |
| - { |
97 |
| - headerSlot || header? |
98 |
| - <Slot content={headerSlot}> |
99 |
| - <strong className="mr-auto">{header}</strong> |
100 |
| - </Slot>:'' |
101 |
| - } |
102 |
| - { |
103 |
| - closeButton? |
104 |
| - <CButtonClose className='ml-2 mb-1' onClick={onClick}/>:'' |
105 |
| - } |
106 |
| - </CToastHeader> |
107 |
| - <CToastBody> |
108 |
| - {attributes.children} |
109 |
| - </CToastBody> |
110 |
| - </Tag> |
111 |
| - </CFade> |
112 |
| - ); |
113 |
| - |
114 |
| - return ( |
115 |
| - <Tag {...attributes} onMouseOver={onMouseOver} onMouseOut={onMouseOut} className={classes} ref={innerRef} /> |
116 |
| - ); |
117 |
| - |
| 102 | + {children} |
| 103 | + </CFade> |
| 104 | + } |
| 105 | + </Context.Provider> |
| 106 | + ) |
118 | 107 | }
|
119 | 108 |
|
120 | 109 | CToast.propTypes = {
|
121 |
| - tag: tagPropType, |
122 | 110 | className: PropTypes.string,
|
123 | 111 | cssModule: PropTypes.object,
|
124 |
| - custom: PropTypes.bool, |
| 112 | + children: PropTypes.node, |
125 | 113 | //
|
126 |
| - headerSlot: PropTypes.node, |
127 | 114 | innerRef: PropTypes.oneOfType([PropTypes.func, PropTypes.string, PropTypes.object]),
|
128 | 115 | show: PropTypes.bool,
|
129 |
| - header: PropTypes.string, |
130 | 116 | autohide: PropTypes.oneOfType([PropTypes.number, PropTypes.bool]),
|
131 |
| - closeButton: PropTypes.bool, |
132 | 117 | fade: PropTypes.bool,
|
133 |
| - transition: PropTypes.shape(CFade.propTypes), |
134 |
| - role: PropTypes.string |
| 118 | + onStateChange: PropTypes.func |
135 | 119 | };
|
136 | 120 |
|
137 | 121 | CToast.defaultProps = {
|
138 |
| - custom: true, |
139 |
| - tag: 'div', |
140 |
| - autohide: 1500, |
141 |
| - closeButton: true, |
142 |
| - fade: true, |
143 |
| - transition: { |
144 |
| - ...CFade.defaultProps, |
145 |
| - unmountOnExit: true, |
146 |
| - }, |
147 |
| - role: 'alert', |
| 122 | + fade: true |
148 | 123 | };
|
149 | 124 |
|
150 |
| -export default CToast; |
| 125 | +export default CToast |
0 commit comments