From 1b594f909d6b05cc1b67eb1483f31a08eacd0f4a Mon Sep 17 00:00:00 2001 From: sperry94 Date: Thu, 28 Mar 2019 08:09:05 -0600 Subject: [PATCH] converting consecutive snackbars to ts --- .../demos/snackbars/ConsecutiveSnackbars.js | 13 +- .../demos/snackbars/ConsecutiveSnackbars.tsx | 121 ++++++++++++++++++ .../demos/snackbars/CustomizedSnackbars.tsx | 2 +- .../demos/snackbars/DirectionSnackbar.tsx | 2 +- .../demos/snackbars/LongTextSnackbar.tsx | 2 +- .../demos/snackbars/PositionedSnackbar.tsx | 2 +- 6 files changed, 133 insertions(+), 9 deletions(-) create mode 100644 docs/src/pages/demos/snackbars/ConsecutiveSnackbars.tsx diff --git a/docs/src/pages/demos/snackbars/ConsecutiveSnackbars.js b/docs/src/pages/demos/snackbars/ConsecutiveSnackbars.js index 53abfd8207e54a..d953f8ac4d2d8e 100644 --- a/docs/src/pages/demos/snackbars/ConsecutiveSnackbars.js +++ b/docs/src/pages/demos/snackbars/ConsecutiveSnackbars.js @@ -15,10 +15,13 @@ const styles = theme => ({ class ConsecutiveSnackbars extends React.Component { queue = []; - state = { - open: false, - messageInfo: {}, - }; + constructor(props) { + super(props); + + this.state = { + open: false, + }; + } handleClick = message => () => { this.queue.push({ @@ -57,7 +60,7 @@ class ConsecutiveSnackbars extends React.Component { render() { const { classes } = this.props; - const { messageInfo } = this.state; + const { messageInfo = {} } = this.state; return (
diff --git a/docs/src/pages/demos/snackbars/ConsecutiveSnackbars.tsx b/docs/src/pages/demos/snackbars/ConsecutiveSnackbars.tsx new file mode 100644 index 00000000000000..dfd5580f6ad668 --- /dev/null +++ b/docs/src/pages/demos/snackbars/ConsecutiveSnackbars.tsx @@ -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; + +export interface State { + open: boolean; + messageInfo?: SnackbarMessage; +} + +class ConsecutiveSnackbars extends React.Component { + 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 ( +
+ + + {messageInfo.message}} + action={[ + , + + + , + ]} + /> +
+ ); + } +} + +(ConsecutiveSnackbars as React.ComponentClass).propTypes = { + classes: PropTypes.object.isRequired, +} as any; + +export default withStyles(styles)(ConsecutiveSnackbars); diff --git a/docs/src/pages/demos/snackbars/CustomizedSnackbars.tsx b/docs/src/pages/demos/snackbars/CustomizedSnackbars.tsx index 15eddd7a6594b7..97cb04ea8e6094 100644 --- a/docs/src/pages/demos/snackbars/CustomizedSnackbars.tsx +++ b/docs/src/pages/demos/snackbars/CustomizedSnackbars.tsx @@ -47,7 +47,7 @@ const useStyles1 = makeStyles((theme: Theme) => ({ }, })); -interface Props { +export interface Props { className?: string; message?: string; onClose?: () => void; diff --git a/docs/src/pages/demos/snackbars/DirectionSnackbar.tsx b/docs/src/pages/demos/snackbars/DirectionSnackbar.tsx index 9c58dccfe1409a..0557f16e49c899 100644 --- a/docs/src/pages/demos/snackbars/DirectionSnackbar.tsx +++ b/docs/src/pages/demos/snackbars/DirectionSnackbar.tsx @@ -20,7 +20,7 @@ function TransitionDown(props: TransitionProps) { return ; } -interface State { +export interface State { open: boolean; Transition?: React.ComponentType; } diff --git a/docs/src/pages/demos/snackbars/LongTextSnackbar.tsx b/docs/src/pages/demos/snackbars/LongTextSnackbar.tsx index eeeac848087ec9..741f64005d367f 100644 --- a/docs/src/pages/demos/snackbars/LongTextSnackbar.tsx +++ b/docs/src/pages/demos/snackbars/LongTextSnackbar.tsx @@ -17,7 +17,7 @@ const styles = (theme: Theme) => }, }); -type Props = WithStyles; +export type Props = WithStyles; function LongTextSnackbar(props: Props) { const { classes } = props; diff --git a/docs/src/pages/demos/snackbars/PositionedSnackbar.tsx b/docs/src/pages/demos/snackbars/PositionedSnackbar.tsx index 0fa86af3dfe516..ad93e21eb5467d 100644 --- a/docs/src/pages/demos/snackbars/PositionedSnackbar.tsx +++ b/docs/src/pages/demos/snackbars/PositionedSnackbar.tsx @@ -2,7 +2,7 @@ import React from 'react'; import Button from '@material-ui/core/Button'; import Snackbar, { SnackbarOrigin } from '@material-ui/core/Snackbar'; -interface State extends SnackbarOrigin { +export interface State extends SnackbarOrigin { open: boolean; }