Fork of the excellent esp8266-react - https://github.com/rjwats/esp8266-react
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
2.2 KiB

  1. import React, {Fragment} from 'react';
  2. import PropTypes from 'prop-types';
  3. import { withStyles } from '@material-ui/core/styles';
  4. import Snackbar from '@material-ui/core/Snackbar';
  5. import IconButton from '@material-ui/core/IconButton';
  6. import CloseIcon from '@material-ui/icons/Close';
  7. const styles = theme => ({
  8. close: {
  9. width: theme.spacing.unit * 4,
  10. height: theme.spacing.unit * 4,
  11. },
  12. });
  13. class SnackbarNotification extends React.Component {
  14. constructor(props) {
  15. super(props);
  16. this.raiseNotification=this.raiseNotification.bind(this);
  17. }
  18. static childContextTypes = {
  19. raiseNotification: PropTypes.func.isRequired
  20. }
  21. getChildContext = () => {
  22. return {raiseNotification : this.raiseNotification};
  23. };
  24. state = {
  25. open: false,
  26. message: null
  27. };
  28. raiseNotification = (message) => {
  29. this.setState({ open: true, message:message });
  30. };
  31. handleClose = (event, reason) => {
  32. if (reason === 'clickaway') {
  33. return;
  34. }
  35. this.setState({ open: false });
  36. };
  37. render() {
  38. const { classes } = this.props;
  39. return (
  40. <Fragment>
  41. <Snackbar
  42. anchorOrigin={{
  43. vertical: 'bottom',
  44. horizontal: 'left',
  45. }}
  46. open={this.state.open}
  47. autoHideDuration={6000}
  48. onClose={this.handleClose}
  49. SnackbarContentProps={{
  50. 'aria-describedby': 'message-id',
  51. }}
  52. message={<span id="message-id">{this.state.message}</span>}
  53. action={
  54. <IconButton
  55. aria-label="Close"
  56. color="inherit"
  57. className={classes.close}
  58. onClick={this.handleClose}
  59. >
  60. <CloseIcon />
  61. </IconButton>
  62. }
  63. />
  64. {this.props.children}
  65. </Fragment>
  66. );
  67. }
  68. }
  69. SnackbarNotification.propTypes = {
  70. classes: PropTypes.object.isRequired
  71. };
  72. export default withStyles(styles)(SnackbarNotification);
  73. export function withNotifier(WrappedComponent) {
  74. return class extends React.Component {
  75. static contextTypes = {
  76. raiseNotification: PropTypes.func.isRequired
  77. };
  78. render() {
  79. return <WrappedComponent raiseNotification={this.context.raiseNotification} {...this.props} />;
  80. }
  81. };
  82. }