-
Notifications
You must be signed in to change notification settings - Fork 0
/
Alert.js
104 lines (91 loc) · 2.82 KB
/
Alert.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
93
94
95
96
97
98
99
100
101
102
103
104
import { useState } from 'react';
import {
CheckCircleIcon,
XCircleIcon,
InformationCircleIcon,
XIcon
} from '@heroicons/react/solid';
function Alert(props) {
const {
afterDismissed = () => {},
children = null,
className: customClassName,
isDismissable = false,
type = 'info'
} = props;
const [isDismissed, setIsDismissed] = useState(false);
const getClassName = () => {
let className = 'rounded-md p-4';
switch (type) {
case 'success':
className += ' bg-green-50 text-green-800';
break;
case 'danger':
className += ' bg-red-50 text-red-800';
break;
case 'info':
default:
className += ' bg-indigo-50 text-indigo-800';
break;
}
if (customClassName) {
className += ` ${customClassName}`;
}
if (isDismissed) {
className += ' hidden';
}
return className;
};
const renderIcon = () => {
switch (type) {
case 'success':
return <CheckCircleIcon className="h-5 w-5 text-green-400" aria-hidden="true" />;
case 'danger':
return <XCircleIcon className="h-5 w-5 text-red-400" aria-hidden="true" />;
case 'info':
default:
return <InformationCircleIcon className="h5 w-5 text-indigo-400" aria-hidden="true" />;
}
};
const renderCloseIcon = () => {
let colorClassName;
switch (type) {
case 'success':
colorClassName = 'text-green-400';
break;
case 'danger':
colorClassName = 'text-red-400';
break;
case 'info':
default:
colorClassName = '';
}
return <XIcon className={ `h-4 w-4 ${colorClassName}` } />;
};
return (
<div className={ getClassName() }>
<div className="flex">
<div className="flex-shrink-0">
{ renderIcon() }
</div>
<div className="flex-1 ml-3">
<div className="text-sm font-medium">
{ children }
</div>
</div>
{ isDismissable &&
<div
className="flex-shrink-0 flex items-center cursor-pointer"
onClick={ () => {
setIsDismissed(true);
afterDismissed();
} }
>
{ renderCloseIcon() }
</div>
}
</div>
</div>
);
}
export default Alert;