@perfma/heaven
Version:
An UI Framework base [Ant Design V4](https://ant.design/components/overview-cn/) for React.
131 lines (121 loc) • 3.76 kB
JavaScript
import * as React from 'react';
import Notification from 'rmc-notification';
import Icon from '../icon';
import './toast.less';
var SHORT = 1.5;
var _config = {
duration: SHORT,
mask: true
};
var messageInstance;
var messageNeedHide;
var prefixCls = 'heaven-toast';
function getMessageInstance(mask, callback) {
Notification.newInstance({
prefixCls: prefixCls,
style: {},
transitionName: 'heaven-fade',
className: mask ? "".concat(prefixCls, "-mask") : "".concat(prefixCls, "-nomask")
}, function (notification) {
return callback && callback(notification);
});
}
function notice(content, type) {
var duration = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : _config.duration;
var _onClose = arguments.length > 3 ? arguments[3] : undefined;
var mask = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : _config.mask;
var iconTypes = {
info: 'InfoTwoTone',
success: 'CheckTwoTone',
fail: 'CloseTwoTone',
warning: 'ExclamationTwoTone',
question: 'QuestionTwoTone'
};
var iconType = iconTypes[type];
messageNeedHide = false;
getMessageInstance(mask, function (notification) {
if (!notification) {
return;
}
if (messageInstance) {
messageInstance.destroy();
messageInstance = null;
}
if (messageNeedHide) {
notification.destroy();
messageNeedHide = false;
return;
}
messageInstance = notification;
notification.notice({
duration: duration,
style: {},
content: iconType ? /*#__PURE__*/React.createElement("div", {
className: "".concat(prefixCls, "-text ").concat(prefixCls, "-text-icon"),
role: "alert",
"aria-live": "assertive"
}, /*#__PURE__*/React.createElement(Icon, {
type: iconType,
style: {
fontSize: 24,
marginBottom: 12
}
}), /*#__PURE__*/React.createElement("div", {
className: "".concat(prefixCls, "-text-info")
}, content)) : /*#__PURE__*/React.createElement("div", {
className: "".concat(prefixCls, "-text"),
role: "alert",
"aria-live": "assertive"
}, content),
closable: true,
onClose: function onClose() {
if (_onClose) {
_onClose();
}
notification.destroy();
notification = null;
messageInstance = null;
}
});
});
}
export default {
SHORT: SHORT,
LONG: 8,
show: function show(content, duration, mask) {
return notice(content, '', duration, function () {}, mask);
},
info: function info(content, duration, onClose, mask) {
return notice(content, 'info', duration, onClose, mask);
},
success: function success(content, duration, onClose, mask) {
return notice(content, 'success', duration, onClose, mask);
},
fail: function fail(content, duration, onClose, mask) {
return notice(content, 'fail', duration, onClose, mask);
},
warning: function warning(content, duration, onClose, mask) {
return notice(content, 'warning', duration, onClose, mask);
},
question: function question(content, duration, onClose, mask) {
return notice(content, 'question', duration, onClose, mask);
},
hide: function hide() {
if (messageInstance) {
messageInstance.destroy();
messageInstance = null;
} else {
messageNeedHide = true;
}
},
config: function config() {
var conf = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var _conf$duration = conf.duration,
duration = _conf$duration === void 0 ? SHORT : _conf$duration,
mask = conf.mask;
_config.duration = duration;
if (mask === false) {
_config.mask = false;
}
}
};