-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
SnackbarItem.js
160 lines (143 loc) · 5.29 KB
/
SnackbarItem.js
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
import React, { Component } from 'react';
import classNames from 'classnames';
import PropTypes from 'prop-types';
import { withStyles } from '@material-ui/core/styles';
import Snackbar from '@material-ui/core/Snackbar';
import Collapse from '@material-ui/core/Collapse';
import SnackbarContent from '@material-ui/core/SnackbarContent';
import { getTransitionDirection, getSnackbarClasses, getCollapseClasses } from './SnackbarItem.util';
import styles from './SnackbarItem.styles';
import { capitalise } from '../utils/constants';
class SnackbarItem extends Component {
state = {
collapsed: true,
};
componentWillUnmount = () => {
clearTimeout(this.timeout);
}
handleClose = key => (event, reason) => {
const { onClose, snack: { onClose: singleOnClose } } = this.props;
if (reason === 'clickaway') return;
if (singleOnClose) singleOnClose(event, reason, key);
onClose(event, reason, key);
};
handleExited = key => (event) => {
const { onExited, snack: { onExited: singleOnExited } } = this.props;
if (singleOnExited) singleOnExited(event, key);
onExited(event, key);
};
handleExitedScreen = () => {
this.timeout = setTimeout(() => {
this.setState(({ collapsed }) => ({ collapsed: !collapsed }));
}, 125);
}
render() {
const {
classes,
action,
ContentProps = {},
hideIconVariant,
preventDuplicate,
iconVariant,
snack,
dense,
...other
} = this.props;
const { action: contentAction, className, ...otherContentProps } = ContentProps;
const {
key,
persist,
variant = 'default',
action: singleAction,
ContentProps: singleContentProps = {},
anchorOrigin,
...singleSnackProps
} = snack;
const icon = iconVariant[variant];
const contentProps = {
...otherContentProps,
...singleContentProps,
action: singleAction || singleContentProps.action || contentAction || action,
};
const ariaDescribedby = contentProps['aria-describedby'] || 'client-snackbar';
let finalAction = contentProps.action;
if (typeof finalAction === 'function') {
finalAction = contentProps.action(key);
}
let snackChildren = snack.children;
if (snackChildren && typeof snackChildren === 'function') {
snackChildren = snackChildren(key);
}
return (
<Collapse
unmountOnExit
timeout={175}
in={this.state.collapsed}
classes={getCollapseClasses(classes, dense)}
onExited={this.handleExited(key)}
>
<Snackbar
TransitionProps={{
direction: getTransitionDirection(anchorOrigin),
onExited: this.handleExitedScreen,
}}
{...other}
{...singleSnackProps}
anchorOrigin={anchorOrigin}
open={snack.open}
classes={getSnackbarClasses(classes)}
onClose={this.handleClose(key)}
>
{snackChildren || (
<SnackbarContent
className={classNames(
classes.base,
classes[`variant${capitalise(variant)}`],
(!hideIconVariant && icon) ? classes.lessPadding : null,
className,
)}
{...contentProps}
aria-describedby={ariaDescribedby}
message={(
<span id={ariaDescribedby} className={classes.message}>
{!hideIconVariant ? icon : null}
{snack.message}
</span>
)}
action={finalAction}
/>
)}
</Snackbar>
</Collapse>
);
}
}
SnackbarItem.propTypes = {
classes: PropTypes.object.isRequired,
snack: PropTypes.shape({
message: PropTypes.oneOfType([
PropTypes.string,
PropTypes.node,
]).isRequired,
variant: PropTypes.oneOf(
['default', 'error', 'success', 'warning', 'info'],
),
key: PropTypes.oneOfType([
PropTypes.string,
PropTypes.number,
]).isRequired,
open: PropTypes.bool.isRequired,
}).isRequired,
iconVariant: PropTypes.shape({
success: PropTypes.any.isRequired,
warning: PropTypes.any.isRequired,
error: PropTypes.any.isRequired,
info: PropTypes.any.isRequired,
}).isRequired,
hideIconVariant: PropTypes.bool.isRequired,
preventDuplicate: PropTypes.bool.isRequired,
dense: PropTypes.bool.isRequired,
onClose: PropTypes.func.isRequired,
onExited: PropTypes.func.isRequired,
};
export default withStyles(styles)(SnackbarItem);