hide snackbar after 5 seconds

This commit is contained in:
Delucse 2020-09-22 11:34:03 +02:00
parent 0f0bfb98c6
commit 6a21410709

View File

@ -21,21 +21,49 @@ const styles = (theme) => ({
class Snackbar extends Component {
constructor(props){
super(props);
this.state = {
open: props.open
};
this.timeout = null;
}
componentDidMount(){
if(this.state.open){
this.autoHideDuration();
}
}
componentWillUnmount(){
if(this.state.open){
clearTimeout(this.timeout);
}
}
onClose = () => {
this.setState({open: false});
}
autoHideDuration = () => {
this.timeout = setTimeout(() => {
this.onClose();
}, 5000);
}
render() {
return (
<MaterialUISnackbar
anchorOrigin={{vertical: 'bottom', horizontal: 'left' }}
open={this.props.open}
onClose={this.props.onClose}
key={Date.now()+this.props.message}
autoHideDuration={5000}
open={this.state.open}
key={this.props.key}
style={{left: '22px', bottom: '40px', width: '300px', zIndex: '100'}}
>
<SnackbarContent
style={{flexWrap: 'nowrap'}}
className={this.props.type === 'error' ? this.props.classes.error : this.props.classes.success}
action={
<IconButton onClick={this.props.onClose} style={{color: 'inherit'}}>
<IconButton onClick={this.onClose} style={{color: 'inherit'}}>
<FontAwesomeIcon icon={faTimes} size="xs"/>
</IconButton>
}