UNPKG

@bytedance/mona-client-web

Version:

web for mona

74 lines 3.21 kB
var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; import React from 'react'; import ReactDOM from 'react-dom'; import './index.module.less'; import { getToastIconConfig, ToastType } from './util'; import { hide } from '../util'; export * from './util'; var MONA_WEB_TOAST = 'mona-web-toast'; export var webHideToast = function () { return hide(MONA_WEB_TOAST); }; export function Toast(_a) { var title = _a.title, _b = _a.icon, icon = _b === void 0 ? 'success' : _b; var _c = getToastIconConfig(icon), url = _c.url, className = _c.className; return (React.createElement("div", { className: "mona-web-toast-body" }, url && React.createElement("img", { className: "mona-web-toast-".concat(className, "-img"), src: url, alt: className }), React.createElement("span", { className: "mona-web-toast-text" }, title))); } export function Toastconfirm(config, popType) { if (!(config === null || config === void 0 ? void 0 : config.title)) { return; } var container = document.createElement('div'); container.id = MONA_WEB_TOAST; if (popType === ToastType.loading) { config.icon = 'loading'; } var successCallbackText = { errMsg: 'showToast:ok' }; var errorCallbackText = { errMsg: "showToast:fail params.title should be string, but got ".concat(typeof config.title, " ") }; function render() { var _a, _b, _c, _d; var timer = null; try { webHideToast(); if (timer) { timer = null; } document.body.append(container); ReactDOM.render(React.createElement(Toast, __assign({}, config)), container); if (popType === ToastType.toast) { timer = setTimeout(function () { webHideToast(); }, config.duration || 1500); } (_a = config.success) === null || _a === void 0 ? void 0 : _a.call(config, successCallbackText); (_b = config.complete) === null || _b === void 0 ? void 0 : _b.call(config, successCallbackText); } catch (_e) { (_c = config.fail) === null || _c === void 0 ? void 0 : _c.call(config, errorCallbackText); (_d = config.complete) === null || _d === void 0 ? void 0 : _d.call(config, errorCallbackText); } } render(); } export function webShowToast(config) { var _a, _b; if (!config.title) { var errMsg = 'showToast:fail'; (_a = config === null || config === void 0 ? void 0 : config.fail) === null || _a === void 0 ? void 0 : _a.call(config, { errMsg: errMsg }); (_b = config === null || config === void 0 ? void 0 : config.complete) === null || _b === void 0 ? void 0 : _b.call(config, { errMsg: errMsg }); } else { Toastconfirm(config, ToastType.toast); } } //# sourceMappingURL=index.js.map