Skip to content

Commit 1a39b2b

Browse files
author
Jovert Lota Palonpon
committed
[UI] Added custom snackbar component
1 parent dda7d1b commit 1a39b2b

File tree

2 files changed

+133
-0
lines changed

2 files changed

+133
-0
lines changed

resources/js/ui/Snackbar.js

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
import React from 'react';
2+
import PropTypes from 'prop-types';
3+
import classNames from 'classnames';
4+
5+
import {
6+
Button,
7+
colors,
8+
IconButton,
9+
Snackbar as MuiSnackbar,
10+
SnackbarContent,
11+
withStyles,
12+
} from '@material-ui/core';
13+
14+
import {
15+
CheckCircle as CheckCircleIcon,
16+
Close as CloseIcon,
17+
Warning as WarningIcon,
18+
Error as ErrorIcon,
19+
Info as InfoIcon,
20+
} from '@material-ui/icons';
21+
22+
const icons = {
23+
success: CheckCircleIcon,
24+
warning: WarningIcon,
25+
error: ErrorIcon,
26+
info: InfoIcon,
27+
};
28+
29+
const Snackbar = props => {
30+
const {
31+
classes,
32+
className,
33+
type,
34+
body,
35+
closed,
36+
action,
37+
actionText,
38+
...other
39+
} = props;
40+
const Icon = icons[type];
41+
42+
return (
43+
<MuiSnackbar
44+
anchorOrigin={{
45+
vertical: 'bottom',
46+
horizontal: 'left',
47+
}}
48+
open
49+
autoHideDuration={5000}
50+
onClose={closed}
51+
>
52+
<SnackbarContent
53+
className={classNames(classes[type], className)}
54+
aria-describedby="client-snackbar"
55+
message={
56+
<span id="client-snackbar" className={classes.message}>
57+
<Icon
58+
className={classNames(
59+
classes.icon,
60+
classes.iconVariant,
61+
)}
62+
/>
63+
{body}
64+
</span>
65+
}
66+
action={[
67+
<Button
68+
key="undo"
69+
color="inherit"
70+
size="small"
71+
onClick={action}
72+
>
73+
{actionText}
74+
</Button>,
75+
76+
<IconButton
77+
key="close"
78+
aria-label="Close"
79+
color="inherit"
80+
className={classes.close}
81+
onClick={closed}
82+
>
83+
<CloseIcon className={classes.icon} />
84+
</IconButton>,
85+
]}
86+
{...other}
87+
/>
88+
</MuiSnackbar>
89+
);
90+
};
91+
92+
const styles = theme => ({
93+
success: {
94+
backgroundColor: colors.green[600],
95+
},
96+
97+
error: {
98+
backgroundColor: theme.palette.error.dark,
99+
},
100+
101+
info: {
102+
backgroundColor: theme.palette.primary.dark,
103+
},
104+
105+
warning: {
106+
backgroundColor: colors.amber[700],
107+
},
108+
109+
icon: {
110+
fontSize: 20,
111+
},
112+
113+
iconVariant: {
114+
opacity: 0.9,
115+
marginRight: theme.spacing.unit,
116+
},
117+
118+
message: {
119+
display: 'flex',
120+
alignItems: 'center',
121+
},
122+
});
123+
124+
Snackbar.propTypes = {
125+
type: PropTypes.oneOf(['success', 'warning', 'error', 'info']).isRequired,
126+
body: PropTypes.string.isRequired,
127+
closed: PropTypes.func.isRequired,
128+
actionText: PropTypes.string,
129+
action: PropTypes.func,
130+
};
131+
132+
export default withStyles(styles)(Snackbar);

resources/js/ui/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
export { default as Snackbar } from './Snackbar';
12
export { default as Table } from './Table';
23
export { default as TableToolbar } from './TableToolbar/TableToolbar';
34
export { default as TablePaginationActions } from './TablePaginationActions';

0 commit comments

Comments
 (0)