-
Notifications
You must be signed in to change notification settings - Fork 82
/
Toast.js
73 lines (63 loc) · 2.29 KB
/
Toast.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
import { useState, useEffect } from 'react';
import PropTypes from 'prop-types';
import './Toast.css';
const Toast = props => {
const { toastList, position, autoDelete, autoDeleteTime } = props;
const [list, setList] = useState(toastList);
useEffect(() => {
setList([...toastList]);
}, [toastList]);
useEffect(() => {
const interval = setInterval(() => {
if (autoDelete && toastList.length && list.length) {
deleteToast(toastList[0].id);
}
}, autoDeleteTime);
return () => {
clearInterval(interval);
}
// eslint-disable-next-line
}, [toastList, autoDelete, autoDeleteTime, list]);
const deleteToast = id => {
const listItemIndex = list.findIndex(e => e.id === id);
const toastListItem = toastList.findIndex(e => e.id === id);
list.splice(listItemIndex, 1);
toastList.splice(toastListItem, 1);
setList([...list]);
}
return (
<>
<div className={`notification-container ${position}`}>
{
list.map((toast, i) =>
<div
key={i}
className={`notification toast ${position}`}
style={{ backgroundColor: toast.backgroundColor }}
>
<button onClick={() => deleteToast(toast.id)}>
X
</button>
<div className="notification-image">
<img src={toast.icon} alt="" />
</div>
<div>
<p className="notification-title">{toast.title}</p>
<p className="notification-message">
{toast.description}
</p>
</div>
</div>
)
}
</div>
</>
);
}
Toast.propTypes = {
toastList: PropTypes.array.isRequired,
position: PropTypes.string.isRequired,
autoDelete: PropTypes.bool,
autoDeleteTime: PropTypes.number
}
export default Toast;