-
Notifications
You must be signed in to change notification settings - Fork 1
/
snackbar.jsx
60 lines (48 loc) · 1.62 KB
/
snackbar.jsx
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
import React from "react";
import ReactDom from "react-dom";
import _Snackbar from "react-toolbox/lib/snackbar";
let SnackbarList = [];
export class Snackbar extends React.Component {
constructor(props) {
super(props);
this.state = {
active: false
};
this.hide = this.hide.bind(this);
this.onAction = () => {
this.props.onAction(this.hide);
};
SnackbarList.push(this);
}
hide() {
this.setState({ active: false });
setTimeout(this.destroy.bind(this), 1000);
}
destroy() {
if (SnackbarList.indexOf(this) >= 0) {
delete SnackbarList[SnackbarList.indexOf(this)];
}
ReactDom.unmountComponentAtNode(this.props.container);
document.body.removeChild(this.props.container);
}
componentDidMount() {
this.setState({ active: true });
}
render() {
return <_Snackbar action={this.props.action} active={this.state.active} icon="warning" label={this.props.content} timeout={this.props.timeout} onClick={this.onAction} onTimeout={this.hide} type={this.props.type} />;
}
static Show(_config) {
let container = document.createElement("div");
document.body.appendChild(container);
let config = Object.assign({
type: "cancel",
icon: "warning",
onAction: function (hide) { hide(); }
}, _config);
ReactDom.render(<Snackbar {...config} container={container} />, container);
}
static Hide() {
SnackbarList.forEach(snackbar => snackbar.hide());
}
}
export default Snackbar;