react-toast-notifications
Advanced tools
Comparing version 2.3.0 to 2.4.0
@@ -89,4 +89,2 @@ 'use strict'; | ||
}, _this.clearTimer = function () { | ||
if (!_this.props.autoDismiss) return; | ||
if (_this.timeout) _this.timeout.clear(); | ||
@@ -110,2 +108,11 @@ }, _this.onMouseEnter = function () { | ||
}, { | ||
key: 'componentDidUpdate', | ||
value: function componentDidUpdate(prevProps) { | ||
if (prevProps.autoDismiss !== this.props.autoDismiss) { | ||
var startOrClear = this.props.autoDismiss ? this.startTimer : this.clearTimer; | ||
startOrClear(); | ||
} | ||
} | ||
}, { | ||
key: 'componentWillUnmount', | ||
@@ -112,0 +119,0 @@ value: function componentWillUnmount() { |
@@ -32,2 +32,4 @@ 'use strict'; | ||
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
@@ -66,7 +68,21 @@ | ||
return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = ToastProvider.__proto__ || Object.getPrototypeOf(ToastProvider)).call.apply(_ref, [this].concat(args))), _this), _this.state = { toasts: [] }, _this.add = function (content) { | ||
return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = ToastProvider.__proto__ || Object.getPrototypeOf(ToastProvider)).call.apply(_ref, [this].concat(args))), _this), _this.state = { toasts: [] }, _this.has = function (id) { | ||
if (!_this.state.toasts.length) { | ||
return false; | ||
} | ||
return Boolean(_this.state.toasts.filter(function (t) { | ||
return t.id === id; | ||
}).length); | ||
}, _this.onDismiss = function (id) { | ||
var cb = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : _utils.NOOP; | ||
return function () { | ||
cb(id); | ||
_this.remove(id); | ||
}; | ||
}, _this.add = function (content) { | ||
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var cb = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : _utils.NOOP; | ||
var id = (0, _utils.generateUEID)(); | ||
var id = options.id || (0, _utils.generateUEID)(); | ||
var callback = function callback() { | ||
@@ -76,10 +92,17 @@ return cb(id); | ||
// bail if a toast exists with this ID | ||
if (_this.has(id)) { | ||
return; | ||
} | ||
// update the toast stack | ||
_this.setState(function (state) { | ||
var toasts = state.toasts.slice(0); | ||
var toast = Object.assign({}, { content: content, id: id }, options); | ||
var newToast = _extends({ content: content, id: id }, options); | ||
var toasts = [].concat(_toConsumableArray(state.toasts), [newToast]); | ||
toasts.push(toast); | ||
return { toasts: toasts }; | ||
}, callback); | ||
// consumer may want to do something with the generated ID (and not use the callback) | ||
return id; | ||
}, _this.remove = function (id) { | ||
@@ -92,2 +115,7 @@ var cb = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : _utils.NOOP; | ||
// bail if NO toasts exists with this ID | ||
if (!_this.has(id)) { | ||
return; | ||
} | ||
_this.setState(function (state) { | ||
@@ -99,11 +127,43 @@ var toasts = state.toasts.filter(function (t) { | ||
}, callback); | ||
}, _this.onDismiss = function (id) { | ||
var cb = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : _utils.NOOP; | ||
return function () { | ||
cb(id); | ||
_this.remove(id); | ||
}, _this.removeAll = function () { | ||
if (!_this.state.toasts.length) { | ||
return; | ||
} | ||
_this.state.toasts.forEach(function (t) { | ||
return _this.remove(t.id); | ||
}); | ||
}, _this.update = function (id) { | ||
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var cb = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : _utils.NOOP; | ||
var callback = function callback() { | ||
return cb(id); | ||
}; | ||
// bail if NO toasts exists with this ID | ||
if (!_this.has(id)) { | ||
return; | ||
} | ||
// update the toast stack | ||
_this.setState(function (state) { | ||
var old = state.toasts; | ||
var i = old.findIndex(function (t) { | ||
return t.id === id; | ||
}); | ||
var updatedToast = _extends({}, old[i], options); | ||
var toasts = [].concat(_toConsumableArray(old.slice(0, i)), [updatedToast], _toConsumableArray(old.slice(i + 1))); | ||
return { toasts: toasts }; | ||
}, callback); | ||
}, _temp), _possibleConstructorReturn(_this, _ret); | ||
} | ||
// Internal Helpers | ||
// ------------------------------ | ||
// Public API | ||
// ------------------------------ | ||
_createClass(ToastProvider, [{ | ||
@@ -127,3 +187,5 @@ key: 'render', | ||
var add = this.add, | ||
remove = this.remove; | ||
remove = this.remove, | ||
removeAll = this.removeAll, | ||
update = this.update; | ||
@@ -137,3 +199,3 @@ var toasts = Object.freeze(this.state.toasts); | ||
Provider, | ||
{ value: { add: add, remove: remove, toasts: toasts } }, | ||
{ value: { add: add, remove: remove, removeAll: removeAll, update: update, toasts: toasts } }, | ||
children, | ||
@@ -234,4 +296,6 @@ portalTarget ? (0, _reactDom.createPortal)(_react2.default.createElement( | ||
removeToast: ctx.remove, | ||
removeAllToasts: ctx.removeAll, | ||
updateToast: ctx.update, | ||
toastStack: ctx.toasts | ||
}; | ||
}; |
{ | ||
"name": "react-toast-notifications", | ||
"version": "2.3.0", | ||
"version": "2.4.0", | ||
"description": "A configurable, composable, toast notification system for react.", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -66,3 +66,3 @@ <img align="right" src="https://user-images.githubusercontent.com/2730833/41197727-5e0b4d2e-6cab-11e8-9d0d-873d1f8ebced.png" alt="react-toast-notifications" /> | ||
| children | Required. The content of the toast notification. | | ||
| autoDismiss `boolean` | Default: `false`. Whether or not to dismiss the toast automatically after a timeout. | | ||
| autoDismiss `boolean` | Default: `false`. Whether or not to dismiss the toast automatically after a timeout. Inherited from `ToastProvider` if not provided. | | ||
| autoDismissTimeout `number` | Inherited from `ToastProvider`. | | ||
@@ -79,3 +79,3 @@ | onDismiss: `Id => void` | Passed in dynamically. | | ||
```jsx | ||
const { addToast, removeToast, toastStack } = useToasts(); | ||
const { addToast, removeToast, removeAllToasts, updateToast, toastStack } = useToasts(); | ||
``` | ||
@@ -94,2 +94,10 @@ | ||
The `removeAllToasts` method has no arguments. | ||
The `updateToast` method has three arguments: | ||
1. The first is the `ID` of the toast to update. | ||
1. The second is the `Options` object, which differs slightly from the add method because it accepts a `content` property. | ||
1. The third is an optional callback, which is passed the updated toast `ID`. | ||
The `toastStack` is an array of objects representing the current toasts, e.g. | ||
@@ -96,0 +104,0 @@ |
46554
882
145