UNPKG

react-native-app-notification

Version:
161 lines (160 loc) 8.31 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; var __spreadArrays = (this && this.__spreadArrays) || function () { for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length; for (var r = Array(s), k = 0, i = 0; i < il; i++) for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++) r[k] = a[j]; return r; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.AppNotification = void 0; var react_1 = __importStar(require("react")); var AppNotificationUI_1 = __importDefault(require("./AppNotificationUI")); var animations_1 = require("./AppNotificationUI/animations"); var AppNotificationWrapper_1 = __importDefault(require("./AppNotificationWrapper")); var styled_1 = require("./styled"); var AppNotification = /** @class */ (function (_super) { __extends(AppNotification, _super); function AppNotification(props) { var _a; var _this = _super.call(this, props) || this; _this.state = { notificationQueue: [], }; _this.clearNotifications = function () { return _this.setState({ notificationQueue: [] }); }; _this.animateOutNotification = function (id) { var notificationQueue = _this.state.notificationQueue; var newQueue = notificationQueue.map(function (notification) { return notification.id === id ? __assign(__assign({}, notification), { animateOut: true }) : notification; }); _this.setState({ notificationQueue: newQueue }); setTimeout(function () { return _this.removeNotification(id); }, AppNotification.DEFAULT_DURATION); }; _this.removeNotification = function (id) { var notificationQueue = _this.state.notificationQueue; var newQueue = notificationQueue.filter(function (notification) { return notification.id !== id; }); _this.setState({ notificationQueue: newQueue }); }; _this._onPress = function (id, callback) { callback(); _this.removeNotification(id); }; _this.renderNotification = function (notification) { return (react_1.default.createElement(AppNotificationWrapper_1.default, __assign({ key: notification.id }, _this.props, notification), _this.props.renderNotification ? (_this.props.renderNotification(__assign(__assign({}, notification), { close: function () { return _this.removeNotification(notification.id); } }))) : (react_1.default.createElement(AppNotificationUI_1.default, __assign({}, _this.props, notification, { onPress: notification.panEnabled ? undefined : notification.onPress }))))); }; _this.id = (_a = props.id) !== null && _a !== void 0 ? _a : AppNotification._defaultId; AppNotification.refs.set(_this.id, _this); return _this; } AppNotification.prototype.componentWillUnmount = function () { AppNotification.refs.delete(this.id); }; AppNotification.prototype.showNotification = function (_a) { var _this = this; var duration = _a.duration, styles = _a.styles, notificationOptions = __rest(_a, ["duration", "styles"]); var notificationQueue = this.state.notificationQueue; var _b = this.props, defaultDuration = _b.duration, maxAmount = _b.maxAmount; var id = Math.random().toString(); var newNotificationQueue = __spreadArrays(notificationQueue, [ __assign(__assign(__assign({}, notificationOptions), styles), { onPress: typeof notificationOptions.onPress === 'function' ? function () { return _this._onPress(id, notificationOptions.onPress); } : null, id: id }), ]); this.setState({ notificationQueue: maxAmount === undefined ? newNotificationQueue : newNotificationQueue.slice(newNotificationQueue.length - maxAmount), }); setTimeout(function () { return _this.animateOutNotification(id); }, duration || defaultDuration || AppNotification.DEFAULT_DURATION); }; AppNotification.prototype.render = function () { var _a = this.props, contentContainerStyle = _a.contentContainerStyle, alignBottom = _a.alignBottom, bottomOffset = _a.bottomOffset, topOffset = _a.topOffset; var notificationQueue = this.state.notificationQueue; return (react_1.default.createElement(styled_1.AppNotificationContainer, { style: contentContainerStyle, alignBottom: alignBottom, bottomOffset: bottomOffset, topOffset: topOffset }, notificationQueue.map(this.renderNotification))); }; AppNotification.DEFAULT_DURATION = 5000; AppNotification.refs = new Map(); AppNotification._defaultId = 'AppNotificationDefaultIdentifier'; AppNotification.AnimationWrappers = { Shrink: animations_1.Shrink, SlideUpFadeIn: animations_1.SlideUpFadeIn, FadeIn: animations_1.FadeIn, FadeOut: animations_1.FadeOut, }; AppNotification.clear = function (id) { var _a; if (id) (_a = AppNotification.getRef(id)) === null || _a === void 0 ? void 0 : _a.clearNotifications(); else AppNotification.refs.forEach(function (ref) { return ref.clearNotifications(); }); }; AppNotification.getRef = function (id) { var _a; if (id === void 0) { id = AppNotification._defaultId; } return (_a = AppNotification.refs.get(id)) !== null && _a !== void 0 ? _a : AppNotification.refs.values().next().value; }; AppNotification.show = function (options) { var ref = AppNotification.getRef(options.id); if (!ref) return console.warn('notificationRef is undefined'); return ref.showNotification(options); }; return AppNotification; }(react_1.Component)); exports.AppNotification = AppNotification; exports.default = AppNotification;