Skip to content

Commit

Permalink
feat(ToastContainer): add clear interface
Browse files Browse the repository at this point in the history
  • Loading branch information
tomchentw committed Oct 12, 2014
1 parent f0a7222 commit 29bef4c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
5 changes: 5 additions & 0 deletions client/scripts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,15 @@ var Hello = React.createClass({
this.refs.container.success("hi! Now" + new Date(), "///title\\\\\\");
},

clearAlert () {
this.refs.container.clear();
},

render: function() {
return <div>
<ToastContainer toastMessageClass={ToastMessage.jQuery} ref="container" className="toast-top-right" />
<p onClick={this.addAlert}>Hello {this.props.name}</p>
<button onClick={this.clearAlert}>CLEAR</button>
</div>;
}
});
Expand Down
18 changes: 14 additions & 4 deletions src/ToastContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ module.exports = React.createClass({
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: {
Expand All @@ -44,6 +52,7 @@ module.exports = React.createClass({
getInitialState () {
return {
toasts: [],
toastId: 0,
previousMessage: null
};
},
Expand All @@ -55,13 +64,14 @@ module.exports = React.createClass({
return;
}
}
var key = Date.now();
var key = state.toastId++;
var newToast = update(optionsOverride || {}, {
$merge: {
key,
type,
title,
message,
key,
ref: `toasts__${ key }`,
handleOnClick: this._handleToastOnClick,
handleRemove: this._handleToastRemove
}
Expand All @@ -70,8 +80,8 @@ module.exports = React.createClass({
toastOperation[(props.newestOnTop ? "$unshift" : "$push")] = [newToast];

var newState = update(state, {
previousMessage: { $set: message },
toasts: toastOperation
toasts: toastOperation,
previousMessage: { $set: message }
});
this.setState(newState);
},
Expand Down
4 changes: 2 additions & 2 deletions src/ToastMessage/jQueryMixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ module.exports = {
},

hideToast (override) {
if (this.state.intervalId == null && !override) return;
var {state, props} = this;
if (state.isHiding || (state.intervalId == null && !override)) return;
this.setState({isHiding: true});

var {props} = this;
this._get$Node()[props.hideMethod]({
duration: props.hideDuration,
easing: props.hideEasing,
Expand Down

0 comments on commit 29bef4c

Please sign in to comment.