-
Notifications
You must be signed in to change notification settings - Fork 109
Expand file tree
/
Copy pathToastContainer.js
More file actions
121 lines (106 loc) · 2.79 KB
/
Copy pathToastContainer.js
File metadata and controls
121 lines (106 loc) · 2.79 KB
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
/** @jsx React.DOM */
var React = require("react/addons");
var {update} = React.addons;
var ToastMessage = require("./ToastMessage");
function noop () {}
module.exports = React.createClass({
displayName: "ToastContainer",
error (message, title, optionsOverride) {
this._notify(this.props.toastType.error, message, title, optionsOverride);
},
info (message, title, optionsOverride) {
this._notify(this.props.toastType.info, message, title, optionsOverride);
},
success (message, title, optionsOverride) {
this._notify(this.props.toastType.success, message, title, optionsOverride);
},
warning (message, title, optionsOverride) {
this._notify(this.props.toastType.warning, message, title, optionsOverride);
},
clear () {
var {refs} = this,
key;
for (key in refs) {
refs[key].hideToast(false);
}
},
getDefaultProps () {
return {
toastType: {
error: "error",
info: "info",
success: "success",
warning: "warning"
},
id: "toast-container",
toastMessageClass: ToastMessage,
preventDuplicates: false,
newestOnTop: true,
onClick: noop
};
},
getInitialState () {
return {
toasts: [],
toastId: 0,
previousMessage: null
};
},
render () {
var {props, state} = this;
return this.transferPropsTo(
<div aria-live="polite" role="alert">
{state.toasts.map((toast) => {
return props.toastMessageClass(toast);
})}
</div>
);
},
_notify (type, message, title, optionsOverride) {
var {props, state} = this;
if (props.preventDuplicates) {
if (state.previousMessage === message) {
return;
}
}
var key = state.toastId++;
var newToast = update(optionsOverride || {}, {
$merge: {
type,
title,
message,
key,
ref: `toasts__${ key }`,
handleOnClick: this._handle_toast_on_click,
handleRemove: this._handle_toast_remove
}
});
var toastOperation = {};
toastOperation[`${ props.newestOnTop ? "$unshift" : "$push" }`] = [newToast];
var newState = update(state, {
toasts: toastOperation,
previousMessage: { $set: message }
});
this.setState(newState);
},
_handle_toast_on_click (event) {
this.props.onClick(event);
if (event.defaultPrevented) {
return;
}
event.preventDefault();
event.stopPropagation();
},
_handle_toast_remove (key) {
var {state} = this;
state.toasts[`${ this.props.newestOnTop ? "reduceRight" : "reduce" }`]((found, toast, index) => {
if (found || toast.key !== key) {
return false;
}
this.setState(update(state, {
toasts: { $splice: [[index, 1]] }
}));
return true;
}, false);
}
});