UNPKG

ra-core

Version:

Core components of react-admin, a frontend Framework for building admin applications on top of REST services, using ES6, React

148 lines 6.65 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (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 () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.Notification = void 0; var React = __importStar(require("react")); var react_1 = require("react"); var __1 = require("..//index.cjs"); /** * Provides a way to show a notification. * @see useNotify * * @example <caption>Basic usage</caption> * <Notification /> * * @param props The component props * @param {string} props.type The notification type. Defaults to 'info'. * @param {number} props.autoHideDuration Duration in milliseconds to wait until hiding a given notification. Defaults to 4000. * @param {boolean} props.multiLine Set it to `true` if the notification message should be shown in more than one line. */ var Notification = function () { var _a = (0, __1.useNotificationContext)(), notifications = _a.notifications, takeNotification = _a.takeNotification; var takeMutation = (0, __1.useTakeUndoableMutation)(); var _b = (0, react_1.useState)(false), open = _b[0], setOpen = _b[1]; var _c = React.useState(undefined), currentNotification = _c[0], setCurrentNotification = _c[1]; var translate = (0, __1.useTranslate)(); (0, react_1.useEffect)(function () { var _a; if (notifications.length && !currentNotification) { // Set a new snack when we don't have an active one var notification = takeNotification(); if (notification) { setCurrentNotification(notification); setOpen(true); } } if (currentNotification) { var beforeunload_1 = function (e) { e.preventDefault(); var confirmationMessage = ''; e.returnValue = confirmationMessage; return confirmationMessage; }; if ((_a = currentNotification === null || currentNotification === void 0 ? void 0 : currentNotification.notificationOptions) === null || _a === void 0 ? void 0 : _a.undoable) { window.addEventListener('beforeunload', beforeunload_1); return function () { window.removeEventListener('beforeunload', beforeunload_1); }; } } }, [notifications, currentNotification, open, takeNotification]); var handleRequestClose = (0, react_1.useCallback)(function () { setOpen(false); }, [setOpen]); var handleExited = (0, react_1.useCallback)(function () { var _a; if (currentNotification && ((_a = currentNotification.notificationOptions) === null || _a === void 0 ? void 0 : _a.undoable)) { var mutation = takeMutation(); if (mutation) { mutation({ isUndo: false }); } else { // FIXME kept for BC: remove in v6 __1.undoableEventEmitter.emit('end', { isUndo: false }); } } setCurrentNotification(undefined); }, [currentNotification, takeMutation]); var handleUndo = (0, react_1.useCallback)(function () { var mutation = takeMutation(); if (mutation) { mutation({ isUndo: true }); } else { // FIXME kept for BC: remove in v6 __1.undoableEventEmitter.emit('end', { isUndo: true }); } setOpen(false); }, [takeMutation]); var _d = currentNotification || {}, message = _d.message, notificationOptions = _d.notificationOptions; var _e = notificationOptions || {}, messageArgs = _e.messageArgs, undoable = _e.undoable; (0, react_1.useEffect)(function () { if (!undoable) return; var timer = setTimeout(function () { handleExited(); }, (notificationOptions === null || notificationOptions === void 0 ? void 0 : notificationOptions.autoHideDuration) || 4000); return function () { return clearTimeout(timer); }; }, [undoable, handleExited, notificationOptions]); if (!currentNotification) return null; return (React.createElement(__1.CloseNotificationContext.Provider, { value: handleRequestClose }, React.createElement("div", { className: "ra-notification", style: { color: 'white', position: 'fixed', bottom: 16, right: 16, zIndex: 1400, display: open ? 'flex' : 'none', paddingLeft: '16px', paddingRight: '16px', borderRadius: '4px', alignItems: 'center', justifyContent: 'center', gap: '16px', backgroundColor: 'rgba(0, 0, 0, 0.8)', } }, React.createElement("p", null, message && typeof message === 'string' ? translate(message, messageArgs) : message), React.createElement("div", { style: { display: 'flex', gap: '8px' } }, undoable ? (React.createElement("button", { onClick: handleUndo, type: "button" }, translate('ra.action.undo'))) : null, React.createElement("button", { onClick: handleExited, type: "button" }, translate('ra.action.close')))))); }; exports.Notification = Notification; //# sourceMappingURL=Notification.js.map