-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
converting consecutive snackbars to ts
- Loading branch information
Showing
6 changed files
with
133 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
docs/src/pages/demos/snackbars/ConsecutiveSnackbars.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
import React, { SyntheticEvent } from 'react'; | ||
import PropTypes, { number } from 'prop-types'; | ||
import { createStyles, withStyles, WithStyles, Theme } from '@material-ui/core/styles'; | ||
import Button from '@material-ui/core/Button'; | ||
import Snackbar from '@material-ui/core/Snackbar'; | ||
import IconButton from '@material-ui/core/IconButton'; | ||
import CloseIcon from '@material-ui/icons/Close'; | ||
import { string } from 'parsimmon'; | ||
|
||
const styles = (theme: Theme) => | ||
createStyles({ | ||
close: { | ||
padding: theme.spacing(0.5), | ||
}, | ||
}); | ||
|
||
export interface SnackbarMessage { | ||
message: string; | ||
key: number; | ||
} | ||
|
||
export type Props = WithStyles<typeof styles>; | ||
|
||
export interface State { | ||
open: boolean; | ||
messageInfo?: SnackbarMessage; | ||
} | ||
|
||
class ConsecutiveSnackbars extends React.Component<Props, State> { | ||
queue: SnackbarMessage[] = []; | ||
|
||
constructor(props: Props) { | ||
super(props); | ||
|
||
this.state = { | ||
open: false, | ||
}; | ||
} | ||
|
||
handleClick = (message: string) => () => { | ||
this.queue.push({ | ||
message, | ||
key: new Date().getTime(), | ||
}); | ||
|
||
if (this.state.open) { | ||
// immediately begin dismissing current message | ||
// to start showing new one | ||
this.setState({ open: false }); | ||
} else { | ||
this.processQueue(); | ||
} | ||
}; | ||
|
||
processQueue = () => { | ||
if (this.queue.length > 0) { | ||
this.setState({ | ||
messageInfo: this.queue.shift(), | ||
open: true, | ||
}); | ||
} | ||
}; | ||
|
||
handleClose = (event: SyntheticEvent | MouseEvent, reason?: string) => { | ||
if (reason === 'clickaway') { | ||
return; | ||
} | ||
this.setState({ open: false }); | ||
}; | ||
|
||
handleExited = () => { | ||
this.processQueue(); | ||
}; | ||
|
||
render() { | ||
const { classes } = this.props; | ||
const { messageInfo = {} as SnackbarMessage } = this.state; | ||
|
||
return ( | ||
<div> | ||
<Button onClick={this.handleClick('message a')}>Show message A</Button> | ||
<Button onClick={this.handleClick('message b')}>Show message B</Button> | ||
<Snackbar | ||
key={messageInfo.key} | ||
anchorOrigin={{ | ||
vertical: 'bottom', | ||
horizontal: 'left', | ||
}} | ||
open={this.state.open} | ||
autoHideDuration={6000} | ||
onClose={this.handleClose} | ||
onExited={this.handleExited} | ||
ContentProps={{ | ||
'aria-describedby': 'message-id', | ||
}} | ||
message={<span id="message-id">{messageInfo.message}</span>} | ||
action={[ | ||
<Button key="undo" color="secondary" size="small" onClick={this.handleClose}> | ||
UNDO | ||
</Button>, | ||
<IconButton | ||
key="close" | ||
aria-label="Close" | ||
color="inherit" | ||
className={classes.close} | ||
onClick={this.handleClose} | ||
> | ||
<CloseIcon /> | ||
</IconButton>, | ||
]} | ||
/> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
(ConsecutiveSnackbars as React.ComponentClass<Props>).propTypes = { | ||
classes: PropTypes.object.isRequired, | ||
} as any; | ||
|
||
export default withStyles(styles)(ConsecutiveSnackbars); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters