UNPKG

@appbuckets/react-ui-smart-components

Version:

UI Extended Components that work with @appbuckets/react-client and @appbuckets/react-ui

119 lines (116 loc) 4 kB
import { __assign } from '../_virtual/_tslib.js'; import * as React from 'react'; import { ValidationError } from 'yup'; import ButterToast from 'butter-toast'; import { getToastProps } from './lib/getToastProps.js'; var NotificationManager = /** @class */ (function () { /* -------- * Constructor Function * -------- */ function NotificationManager(namespace, Component, defaultProps) { var _this = this; /* -------- * Main Show Function * -------- */ this.show = function (content, options, overrideProps) { /** Cycle through the array */ if (Array.isArray(content)) { content.forEach(function (singleContent) { return _this.show(singleContent, options, overrideProps); }); return; } /** Cycle through YupValidation errors */ if (content instanceof ValidationError) { content.errors.forEach(function (error) { return _this.show(error, options, overrideProps); }); return; } /** Normalize Content to ToastProps */ var contentProps = getToastProps(content); /** If content is invalid, return */ if (!contentProps) { return; } /** Build toast props, combining default, computed and overridden */ var toastProps = __assign( __assign(__assign({}, _this.defaultProps), contentProps), overrideProps ); /** Get the component and raise the notification */ var Component = _this.Component; ButterToast.raise( __assign( __assign({ namespace: _this.namespace, timeout: 6000 }, options), { content: function (props) { var dismiss = props.dismiss, onClick = props.onClick; var handleDismiss = typeof dismiss === 'function' && toastProps.dismissible ? function () { return dismiss(); } : undefined; var handleClick = typeof onClick === 'function' || typeof toastProps.onClick === 'function' ? function (e, componentProps) { if (typeof onClick === 'function') { onClick(e); } if (typeof toastProps.onClick === 'function') { toastProps.onClick(e, componentProps); } } : undefined; return React.createElement( Component, __assign({}, toastProps, { dismiss: handleDismiss, onClick: handleClick, }) ); }, } ) ); }; this.custom = function (content, options, props) { return _this.show(content, options, props); }; this.default = function (props, options) { return _this.show(props, options); }; this.error = function (props, options) { return _this.show(props, options, { danger: true, icon: 'times-circle' }); }; this.info = function (props, options) { return _this.show(props, options, { info: true, icon: 'info-circle' }); }; this.primary = function (props, options) { return _this.show(props, options, { primary: true }); }; this.secondary = function (props, options) { return _this.show(props, options, { secondary: true }); }; this.success = function (props, options) { return _this.show(props, options, { success: true, icon: 'check-circle', }); }; this.warning = function (props, options) { return _this.show(props, options, { warning: true, icon: 'exclamation-circle', }); }; this.namespace = namespace; this.Component = Component; this.defaultProps = defaultProps; } return NotificationManager; })(); export { NotificationManager as default };