|
| 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); |
0 commit comments