1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 | 1x 1x 6x 6x 3x 3x 3x 2x 2x 2x 2x 1x 1x 1x 1x 2x 2x 2x 9x 9x 8x 1x 1x 1x | // @flow import React, { Component } from 'react'; import PropTypes from 'prop-types'; import Snackbar from 'material-ui/Snackbar'; import lightBaseTheme from 'material-ui/styles/baseThemes/lightBaseTheme'; import MuiThemeProvider from 'material-ui/styles/MuiThemeProvider'; import getMuiTheme from 'material-ui/styles/getMuiTheme'; type Props = { action?: Node, autoHideDuration?: number, bodyStyle?: object, className?: string, contentStyle?: object, fireEvent?: () => void, id: string, message: Node, n_clicks?: number, open: boolean, setProps?: (props: {open?: boolean}) => void, style?: object, }; const propTypes = { /** * The label for the action on the snackbar. */ action: PropTypes.node, /** * The number of milliseconds to wait before automatically dismissing. If no value is specified * the snackbar will dismiss normally. If a value is provided the snackbar can still be dismissed * normally. If a snackbar is dismissed before the timer expires, the timer will be cleared. */ autoHideDuration: PropTypes.number, /** * Override the inline-styles of the body element. */ bodyStyle: PropTypes.objectOf(PropTypes.any), /** * The css class name of the root element. */ className: PropTypes.string, /** * Override the inline-styles of the content element. */ contentStyle: PropTypes.objectOf(PropTypes.any), /** * A callback for firing events to dash. */ fireEvent: PropTypes.func, /** * The element's ID. */ id: PropTypes.string.isRequired, /** * The message to be displayed. * (Note: If the message is an element or array, and the Snackbar may re-render while it is * still open, ensure that the same object remains as the message property if you want to avoid * the Snackbar hiding and showing again) */ message: PropTypes.node.isRequired, /** * An integer that represents the number of times that action button has been clicked on. */ n_clicks: PropTypes.number, /** * Controls whether the Snackbar is opened or not. */ open: PropTypes.bool.isRequired, /** * Dash callback to update props on the server */ setProps: PropTypes.func, /** * Override the inline-styles of the root element. */ style: PropTypes.objectOf(PropTypes.any), }; type State = { open: boolean, }; const defaultProps = { action: null, autoHideDuration: 3000, bodyStyle: {}, className: '', contentStyle: {}, fireEvent: () => {}, n_clicks: 0, setProps: () => {}, style: {}, }; export default class SDSnackbar extends Component<Props, State> { constructor(props: Props) { super(props); this.state = {open: props.open}; } componentWillReceiveProps(nextProps: Props): void { Eif (nextProps.open !== null && nextProps.open !== this.props.open) { if (nextProps.open === true) this.handleOpen(); if (nextProps.open === false) this.handleClose(); } } handleOpen = () => { const { setProps } = this.props; Eif (typeof setProps === 'function') setProps({open: true}); this.setState({open: true}); }; handleClose = () => { const { setProps } = this.props; Eif (typeof setProps === 'function') setProps({open: false}); this.setState({open: false}); }; handleActionClick = () => { console.log('handling click event'); Eif (this.props.setProps) this.props.setProps({n_clicks: this.props.n_clicks + 1}); Eif (this.props.fireEvent) this.props.fireEvent({event: 'click'}); }; render() { const { action, autoHideDuration, bodyStyle, className, contentStyle, id, message, style } = this.props; if (this.props.fireEvent) { return ( <div id={id}> <MuiThemeProvider muiTheme={getMuiTheme(lightBaseTheme)}> <Snackbar action={action} autoHideDuration={autoHideDuration} bodyStyle={bodyStyle} className={className} contentStyle={contentStyle} message={message} onActionClick={this.handleActionClick} onRequestClose={this.handleClose} open={this.state.open} style={style} /> </MuiThemeProvider> </div> ); } else { return ( <div id={id}> <MuiThemeProvider muiTheme={getMuiTheme(lightBaseTheme)}> <Snackbar action={action} autoHideDuration={autoHideDuration} bodyStyle={bodyStyle} className={className} contentStyle={contentStyle} message={message} onRequestClose={this.handleClose} open={this.state.open} style={style} /> </MuiThemeProvider> </div> ); } } } SDSnackbar.propTypes = propTypes; SDSnackbar.defaultProps = defaultProps; |