/
Notification.js
146 lines (128 loc) · 3.17 KB
/
Notification.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import React from 'react'
import PropTypes from 'prop-types'
import classNames from 'classnames'
import { withStyles } from '@material-ui/core/styles/index'
import IconButton from '@material-ui/core/IconButton/index'
import Snackbar from '@material-ui/core/Snackbar/index'
import SnackbarContent from '@material-ui/core/SnackbarContent/index'
import CheckCircleIcon from '@material-ui/icons/CheckCircle'
import InfoIcon from '@material-ui/icons/Info'
import ErrorIcon from '@material-ui/icons/Error'
import WarningIcon from '@material-ui/icons/Warning'
import CloseIcon from '@material-ui/icons/Close'
import green from '@material-ui/core/colors/green'
import amber from '@material-ui/core/colors/amber'
const variantIcon = {
success: CheckCircleIcon,
warning: WarningIcon,
error: ErrorIcon,
info: InfoIcon
}
const styles1 = theme => ({
message: {
display: 'flex',
alignItems: 'center'
},
success: {
backgroundColor: green[600]
},
error: {
backgroundColor: theme.palette.error.dark
},
info: {
backgroundColor: theme.palette.primary.dark
},
warning: {
backgroundColor: amber[700]
},
icon: {
fontSize: 20
},
iconVariant: {
opacity: 0.9,
marginRight: theme.spacing.unit
}
})
function CustomSnackbarContent( props ) {
const { classes, className, message, onClose, variant, ...other } = props
const Icon = variantIcon[variant]
const contentClasses = classNames(classes[variant], className)
const iconClasses = classNames(classes.icon, classes.iconVariant)
return (
<SnackbarContent
className={contentClasses}
message={
<span className={classes.message}>
<Icon className={iconClasses} />
{message}
</span>
}
action={[
<IconButton
key="close"
aria-label="Close"
color="inherit"
className={classes.close}
onClick={onClose}
>
<CloseIcon className={classes.icon} />
</IconButton>
]}
{...other}
/>
)
}
const { bool, func, node, object, oneOf, oneOfType, string } = PropTypes
CustomSnackbarContent.propTypes = {
classes: object.isRequired,
className: string,
onClose: func,
message: oneOfType([string, node]),
variant: oneOf(['success', 'warning', 'error', 'info'])
}
const StyledCustomSnackbarContent = withStyles(styles1)(CustomSnackbarContent)
const styles2 = theme => ({
margin: {
flexGrow: 0,
margin: theme.spacing.unit,
fontFamily: 'Roboto, Helvetica, Arial, sans-serif'
},
button: {
padding: theme.spacing.unit / 2
}
})
function Notification( props ) {
const { classes } = props
const msg = props.message || 'Please fix the errors shown.'
return (
<Snackbar
anchorOrigin={{
vertical: 'top',
horizontal: 'center'
}}
open={props.open}
autoHideDuration={6000}
onClose={props.handleClose}
>
<StyledCustomSnackbarContent
variant="warning"
className={classes.margin}
message={msg}
onClose={props.handleClose}
/>
</Snackbar>
)
}
Notification.defaultProps = {
open: false,
variant: 'info'
}
Notification.propTypes = {
classes: object.isRequired,
className: string,
open: bool,
handleClose: func,
message: oneOfType([string, node]),
variant: oneOf(['success', 'warning', 'error', 'info'])
}
export default withStyles(styles2)(Notification)