UNPKG

@grandlinex/react-components

Version:
119 lines (118 loc) 3.85 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Badge = exports.BadgeColorX = void 0; const react_1 = __importStar(require("react")); const react_icons_1 = require("@grandlinex/react-icons"); const util_1 = require("../../../util"); class BadgeColorX { constructor(color) { if (!BadgeColorX.isValidBadge(color)) { throw new Error(`Invalid Badge Color: ${color}`); } this.color = color; } toForm() { if (BadgeColorX.isCustomBadge(this.color)) { return { text: this.color, mode: 'custom', color01: this.getBackground(), color02: this.getText() || '#000000', }; } return { text: this.color, mode: this.color, color01: '#000000', color02: '#ffffff', }; } getColor() { if (BadgeColorX.isCustomBadge(this.color)) { const [bg, fg] = this.color.slice(1, -1).split('&'); return { backgroundColor: bg, color: fg, }; } return { backgroundColor: this.color, }; } getBackground() { return this.getColor().backgroundColor; } getText() { return this.getColor().color; } static isCustomBadge(color) { return /^!#.*&#.*!$/.test(color); } static isValidBadge(color) { if (typeof color !== 'string') { return false; } if (this.bgColors.includes(color)) { return true; } return /^!#.*&#.*!$/.test(color); } getStringColor() { return this.color; } static fromSColor(bg, fg) { return new BadgeColorX(`!#${bg.replace('#', '')}&#${fg.replace('#', '')}!`); } } exports.BadgeColorX = BadgeColorX; BadgeColorX.bgColors = [ 'red', 'black', 'blue', 'yellow', 'green', 'orange', ]; const Badge = (prop) => { const { text, color, icon, close } = prop; const customColor = (0, react_1.useMemo)(() => { if (color && BadgeColorX.isCustomBadge(color)) { return new BadgeColorX(color).getColor(); } return undefined; }, [color]); return (react_1.default.createElement("div", { style: customColor, className: (0, util_1.cnx)(`glx-badge`, [ !!color && !customColor, `glx-badge--${color}`, ]) }, icon ? (0, react_icons_1.getIcon)(icon)({}) : null, text, close ? (react_1.default.createElement("button", { onClick: close, style: { color: customColor?.color, } }, react_1.default.createElement(react_icons_1.IOClose, null))) : null)); }; exports.Badge = Badge;