UNPKG

@trendmicro/react-toasts

Version:
219 lines (164 loc) 10.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _react = _interopRequireWildcard(require("react")); var _context = require("./context"); function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; } function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; } function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); } function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance"); } function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); } function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } } function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } var uniqueId = function () { var id = 0; return function () { return id++; }; }(); var noop = function noop() {}; var ToastProvider = /*#__PURE__*/ function (_Component) { _inherits(ToastProvider, _Component); function ToastProvider() { var _getPrototypeOf2; var _this; _classCallCheck(this, ToastProvider); for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } _this = _possibleConstructorReturn(this, (_getPrototypeOf2 = _getPrototypeOf(ToastProvider)).call.apply(_getPrototypeOf2, [this].concat(args))); _defineProperty(_assertThisInitialized(_this), "hasToast", function (id) { if (_this.state.toasts.length === 0) { return false; } return _this.state.toasts.filter(function (toast) { return toast.id === id; }).length > 0; }); _defineProperty(_assertThisInitialized(_this), "addToast", function (content) { var meta = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; var callback = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : noop; var id = uniqueId(); if (_this.hasToast(id)) { return; } var toast = { id: id, content: content, meta: _objectSpread({}, meta) }; _this.setState(function (state) { var toasts = [].concat(_toConsumableArray(state.toasts), [toast]); return { toasts: toasts }; }, function () { typeof callback === 'function' && callback(id); }); /* eslint-disable-next-line consistent-return */ return id; }); _defineProperty(_assertThisInitialized(_this), "removeToast", function (id) { var callback = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : noop; if (!_this.hasToast(id)) { return; } _this.setState(function (state) { var toasts = state.toasts.filter(function (toast) { return toast.id !== id; }); return { toasts: toasts }; }, function () { typeof callback === 'function' && callback(id); }); }); _defineProperty(_assertThisInitialized(_this), "removeAllToasts", function () { var callback = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : noop; if (_this.state.toasts.length === 0) { return; } _this.setState(function (state) { return { toasts: [] }; }, function () { typeof callback === 'function' && callback(); }); }); _defineProperty(_assertThisInitialized(_this), "updateToast", function (id) { var updater = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : noop; var callback = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : noop; if (!_this.hasToast(id)) { return; } _this.setState(function (state) { var prevToasts = state.toasts; var i = prevToasts.findIndex(function (toast) { return toast.id === id; }); var updatedToast = _objectSpread({}, prevToasts[i]); if (typeof updater === 'function') { var _updater = _objectSpread({}, updater(updatedToast)), _updater$id = _updater.id, _id = _updater$id === void 0 ? updatedToast.id : _updater$id, _updater$content = _updater.content, content = _updater$content === void 0 ? updatedToast.content : _updater$content, _updater$meta = _updater.meta, meta = _updater$meta === void 0 ? updatedToast.meta : _updater$meta; updatedToast = { id: _id, content: content, meta: _objectSpread({}, meta) }; } var toasts = [].concat(_toConsumableArray(prevToasts.slice(0, i)), [updatedToast], _toConsumableArray(prevToasts.slice(i + 1))); return { toasts: toasts }; }, function () { typeof callback === 'function' && callback(id); }); }); _defineProperty(_assertThisInitialized(_this), "state", { toasts: [], hasToast: _this.hasToast, addToast: _this.addToast, removeToast: _this.removeToast, removeAllToasts: _this.removeAllToasts, updateToast: _this.updateToast }); return _this; } _createClass(ToastProvider, [{ key: "render", value: function render() { var _this$props = this.props, _this$props$context = _this$props.context, Context = _this$props$context === void 0 ? _context.ToastContext : _this$props$context, children = _this$props.children; return _react["default"].createElement(Context.Provider, { value: this.state }, typeof children === 'function' ? children(this.state) : children); } }]); return ToastProvider; }(_react.Component); var _default = ToastProvider; exports["default"] = _default;