UNPKG

@etsoo/react

Version:

TypeScript ReactJs UI Independent Framework

83 lines (82 loc) 2.79 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.NotifierReact = exports.NotificationReact = void 0; const react_1 = __importDefault(require("react")); const notificationbase_1 = require("@etsoo/notificationbase"); const State_1 = require("../states/State"); /** * React notification */ class NotificationReact extends notificationbase_1.Notification { } exports.NotificationReact = NotificationReact; /** * Notifier for React */ class NotifierReact extends notificationbase_1.NotificationContainer { /** * Singleton instance */ static get instance() { return NotifierReact._instance; } /** * Update notifier * @param notifier Notifier */ static updateInstance(notifier) { NotifierReact._instance = notifier; } /** * Constructor */ constructor() { super((notification, dismiss) => { // Debug if (this.debug) { console.debug("NotifierReact.updateCallback", notification, dismiss, this.loadingCount); } // Make sure the state update is set if (this.stateUpdate) this.stateUpdate({ notification, dismiss }); }); } /** * Create state provider * @param className Style class name * @returns Provider */ createProvider(className, debug) { // Custom creator const creator = (state, update, props) => { // Hold the current state update this.stateUpdate = update; // Aligns collection const aligns = []; for (const align in state) { // Notifications under the align const notifications = state[align]; // UI collections const ui = notifications.map((notification) => notification.render(props, className ? className + "-item" : className)); // Add to the collection aligns.push(this.createContainer(Number(align), ui)); } // Debug if (debug) { console.debug("NotifierReact.createProvider", className, state, aligns); } // Generate the component return react_1.default.createElement("div", { className }, aligns); }; // Create state const { provider } = State_1.State.create((state, _action) => { // Collection update is done with NotificationContainer return { ...state }; }, this.notifications, {}, creator); return provider; } } exports.NotifierReact = NotifierReact;