WordClockESP/interface/src/components/SnackbarNotification.js

94 lines
2.2 KiB
JavaScript
Raw Normal View History

2018-03-03 22:41:57 +00:00
import React, {Fragment} from 'react';
import PropTypes from 'prop-types';
2018-05-18 22:29:14 +00:00
import { withStyles } from '@material-ui/core/styles';
import Snackbar from '@material-ui/core/Snackbar';
import IconButton from '@material-ui/core/IconButton';
import CloseIcon from '@material-ui/icons/Close';
const styles = theme => ({
close: {
2019-04-27 21:44:46 +00:00
padding: theme.spacing.unit / 2,
},
});
class SnackbarNotification extends React.Component {
2018-03-03 22:41:57 +00:00
constructor(props) {
super(props);
this.raiseNotification=this.raiseNotification.bind(this);
}
static childContextTypes = {
raiseNotification: PropTypes.func.isRequired
}
getChildContext = () => {
return {raiseNotification : this.raiseNotification};
};
state = {
open: false,
message: null
};
raiseNotification = (message) => {
this.setState({ open: true, message:message });
};
handleClose = (event, reason) => {
if (reason === 'clickaway') {
return;
}
this.setState({ open: false });
};
render() {
const { classes } = this.props;
return (
2018-03-03 22:41:57 +00:00
<Fragment>
<Snackbar
anchorOrigin={{
vertical: 'bottom',
horizontal: 'left',
}}
open={this.state.open}
autoHideDuration={6000}
onClose={this.handleClose}
SnackbarContentProps={{
'aria-describedby': 'message-id',
}}
message={<span id="message-id">{this.state.message}</span>}
action={
<IconButton
aria-label="Close"
color="inherit"
className={classes.close}
onClick={this.handleClose}
>
<CloseIcon />
</IconButton>
2018-03-03 22:41:57 +00:00
}
/>
{this.props.children}
</Fragment>
);
}
}
SnackbarNotification.propTypes = {
2018-03-03 22:41:57 +00:00
classes: PropTypes.object.isRequired
};
export default withStyles(styles)(SnackbarNotification);
2018-03-03 22:41:57 +00:00
export function withNotifier(WrappedComponent) {
return class extends React.Component {
static contextTypes = {
raiseNotification: PropTypes.func.isRequired
};
render() {
return <WrappedComponent raiseNotification={this.context.raiseNotification} {...this.props} />;
}
};
}