@appbuckets/react-ui-smart-components
Version:
UI Extended Components that work with @appbuckets/react-client and @appbuckets/react-ui
157 lines (150 loc) • 4.94 kB
JavaScript
;
var _tslib = require('../_virtual/_tslib.js');
var React = require('react');
var Yup = require('yup');
var ButterToast = require('butter-toast');
var getToastProps = require('./lib/getToastProps.js');
function _interopDefaultLegacy(e) {
return e && typeof e === 'object' && 'default' in e ? e : { default: e };
}
function _interopNamespace(e) {
if (e && e.__esModule) return e;
var n = Object.create(null);
if (e) {
Object.keys(e).forEach(function (k) {
if (k !== 'default') {
var d = Object.getOwnPropertyDescriptor(e, k);
Object.defineProperty(
n,
k,
d.get
? d
: {
enumerable: true,
get: function () {
return e[k];
},
}
);
}
});
}
n['default'] = e;
return Object.freeze(n);
}
var React__namespace = /*#__PURE__*/ _interopNamespace(React);
var ButterToast__default = /*#__PURE__*/ _interopDefaultLegacy(ButterToast);
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 Yup.ValidationError) {
content.errors.forEach(function (error) {
return _this.show(error, options, overrideProps);
});
return;
}
/** Normalize Content to ToastProps */
var contentProps = getToastProps.getToastProps(content);
/** If content is invalid, return */
if (!contentProps) {
return;
}
/** Build toast props, combining default, computed and overridden */
var toastProps = _tslib.__assign(
_tslib.__assign(_tslib.__assign({}, _this.defaultProps), contentProps),
overrideProps
);
/** Get the component and raise the notification */
var Component = _this.Component;
ButterToast__default['default'].raise(
_tslib.__assign(
_tslib.__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__namespace.createElement(
Component,
_tslib.__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;
})();
module.exports = NotificationManager;