UNPKG

brightyui

Version:

Brighty UI library

78 lines 5.07 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var tslib_1 = require("tslib"); var jsx_runtime_1 = require("react/jsx-runtime"); var react_1 = require("react"); var Icon_module_scss_1 = tslib_1.__importDefault(require("../../../src/styles/Icon.module.scss")); var ErrorIcon_1 = require("../../images/IconComponents/ErrorIcon"); var cnbuilder_1 = require("cnbuilder"); var EColors_1 = require("../../types/EColors"); var useBool_1 = tslib_1.__importDefault(require("../../hooks/useBool")); var Icon = function (_a) { var currency = _a.currency, type = _a.type, country = _a.country, icon = _a.icon, _b = _a.wrapperColor, wrapperColor = _b === void 0 ? EColors_1.EColors.fill4 : _b, _c = _a.size, size = _c === void 0 ? 'medium' : _c, loading = _a.loading, width = _a.width, height = _a.height, subIcon = _a.subIcon, subIconClassname = _a.subIconClassname, src = _a.src; var _d = (0, react_1.useState)(''), path = _d[0], setPath = _d[1]; var error = (0, useBool_1.default)(); var importIcon = function (path) { return tslib_1.__awaiter(void 0, void 0, void 0, function () { var _path, err_1; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 2, , 3]); return [4 /*yield*/, Promise.resolve("".concat("../../../src/images/".concat(path, ".svg"))).then(function (s) { return tslib_1.__importStar(require(s)); })]; case 1: _path = (_a.sent()).default; setPath(_path); error.onFalse(); return [3 /*break*/, 3]; case 2: err_1 = _a.sent(); console.error(err_1); error.onTrue(); return [3 /*break*/, 3]; case 3: return [2 /*return*/]; } }); }); }; var getRoute = (0, react_1.useCallback)(function () { switch (type) { case 'currency': return importIcon("currencies/".concat(currency)); case 'country': return importIcon("countries/".concat(country)); default: return null; } }, [type, currency, country]); (0, react_1.useEffect)(function () { getRoute(); }, [type, currency, country]); var customStyles = (0, react_1.useMemo)(function () { if (size === 'custom') { return { width: "".concat(width, "px"), height: "".concat(height, "px"), }; } }, [width, height, size]); if (loading) { return ((0, jsx_runtime_1.jsx)("div", { className: (0, cnbuilder_1.cnb)(Icon_module_scss_1.default.root__round, Icon_module_scss_1.default.root__round__loading, Icon_module_scss_1.default["root__size_".concat(size)]), style: customStyles })); } if (!path && type !== 'icon' && type !== 'profile') { return ((0, jsx_runtime_1.jsx)("div", { className: (0, cnbuilder_1.cnb)(Icon_module_scss_1.default.root__round, Icon_module_scss_1.default.root__round__loading, Icon_module_scss_1.default["root__size_".concat(size)]), style: customStyles })); } if ((type === 'currency' && currency) || (type === 'country' && country)) { if (error.value) { return ((0, jsx_runtime_1.jsx)("div", { className: (0, cnbuilder_1.cnb)(Icon_module_scss_1.default.root__round, Icon_module_scss_1.default.root__round__error, Icon_module_scss_1.default["root__size_".concat(size)]), style: customStyles, children: (0, jsx_runtime_1.jsx)(ErrorIcon_1.ErrorIcon, {}) })); } return ((0, jsx_runtime_1.jsx)("div", { className: (0, cnbuilder_1.cnb)(Icon_module_scss_1.default.root__round, Icon_module_scss_1.default["root__size_".concat(size)]), style: customStyles, children: !!path && (0, jsx_runtime_1.jsx)("img", { src: path }) })); } if (type === 'icon') { return ((0, jsx_runtime_1.jsxs)("div", { className: (0, cnbuilder_1.cnb)(Icon_module_scss_1.default.root__round, Icon_module_scss_1.default["root__size_".concat(size)]), style: tslib_1.__assign({ backgroundColor: wrapperColor }, customStyles), children: [icon, subIcon && (0, jsx_runtime_1.jsx)("div", { className: (0, cnbuilder_1.cnb)(Icon_module_scss_1.default.subIcon, subIconClassname), children: subIcon })] })); } if (type === 'profile') { return ((0, jsx_runtime_1.jsxs)("div", { className: (0, cnbuilder_1.cnb)(Icon_module_scss_1.default.root__round, Icon_module_scss_1.default["root__size_".concat(size)]), style: tslib_1.__assign({ backgroundColor: wrapperColor }, customStyles), children: [icon, !!src && (0, jsx_runtime_1.jsx)("img", { src: src, className: Icon_module_scss_1.default.profileAvatar }), subIcon && (0, jsx_runtime_1.jsx)("div", { className: (0, cnbuilder_1.cnb)(Icon_module_scss_1.default.subIcon, subIconClassname), children: subIcon })] })); } return null; }; exports.default = Icon; //# sourceMappingURL=Icon.js.map