fix: 修复 tags 无法解析颜色的问题

This commit is contained in:
Montia37
2025-09-08 10:34:47 +08:00
parent 5dc65c9cba
commit 4d4da8d3b8
2 changed files with 36 additions and 18 deletions

View File

@@ -36,25 +36,44 @@ type ColorType =
| "mint" | "mint"
| "sky"; | "sky";
// 默认颜色列表,将在组件内部使用 // 完整的颜色列表,用于标签颜色匹配
const defaultColorList: ColorType[] = [ const allColors: ColorType[] = [
"blue", "ruby",
"red", "gray",
"green", "gold",
"bronze",
"brown",
"yellow", "yellow",
"purple", "amber",
"cyan",
"orange", "orange",
"tomato",
"red",
"crimson",
"pink", "pink",
"plum",
"purple",
"violet",
"iris",
"indigo",
"blue",
"cyan",
"teal",
"jade",
"green",
"grass",
"lime",
"mint",
"sky",
]; ];
// 解析带颜色的标签 // 解析带颜色的标签
const parseTagWithColor = (tag: string, availableColors: ColorType[]) => { const parseTagWithColor = (tag: string) => {
const colorMatch = tag.match(/<(\w+)>$/); const colorMatch = tag.match(/<(\w+)>$/);
if (colorMatch) { if (colorMatch) {
const color = colorMatch[1].toLowerCase(); const color = colorMatch[1].toLowerCase();
const text = tag.replace(/<\w+>$/, ""); const text = tag.replace(/<\w+>$/, "");
// 检查颜色是否在支持的颜色列表中 // 检查颜色是否在完整的颜色列表中
if (availableColors.includes(color as ColorType)) { if (allColors.includes(color as ColorType)) {
return { text, color: color as ColorType }; return { text, color: color as ColorType };
} }
} }
@@ -66,11 +85,8 @@ const Tag = React.forwardRef<HTMLDivElement, TagProps>(
// 在组件内部使用 useConfigItem 钩子 // 在组件内部使用 useConfigItem 钩子
const tagDefaultColorList = useConfigItem("tagDefaultColorList"); const tagDefaultColorList = useConfigItem("tagDefaultColorList");
// 解析配置的颜色列表 // 解析配置的颜色列表,仅用于循环排序
const colorList = React.useMemo(() => { const colorList = React.useMemo(() => {
if (!tagDefaultColorList) {
return defaultColorList;
}
return tagDefaultColorList return tagDefaultColorList
.split(",") .split(",")
.map((color: string) => color.trim()) as ColorType[]; .map((color: string) => color.trim()) as ColorType[];
@@ -82,7 +98,7 @@ const Tag = React.forwardRef<HTMLDivElement, TagProps>(
className={cn("flex flex-wrap gap-1", className)} className={cn("flex flex-wrap gap-1", className)}
{...props}> {...props}>
{tags.map((tag, index) => { {tags.map((tag, index) => {
const { text, color } = parseTagWithColor(tag, colorList); const { text, color } = parseTagWithColor(tag);
const badgeColor = color || colorList[index % colorList.length]; const badgeColor = color || colorList[index % colorList.length];
return ( return (

View File

@@ -1,6 +1,7 @@
import { useContext } from "react"; import { useContext } from "react";
import type { ConfigOptions } from "./default"; import type { ConfigOptions } from "./default";
import { ConfigContext } from "./ConfigContext"; import { ConfigContext } from "./ConfigContext";
import { DEFAULT_CONFIG } from "./default";
/** /**
* 使用全局配置 Hook用于获取当前应用配置 * 使用全局配置 Hook用于获取当前应用配置
@@ -13,13 +14,14 @@ export function useAppConfig(): ConfigOptions {
/** /**
* 使用特定配置项 Hook直接获取某个配置项的值 * 使用特定配置项 Hook直接获取某个配置项的值
* @param key 配置项键名 * @param key 配置项键名
* @returns 配置项的值 * @returns 配置项的值,如果为 undefined 则返回默认配置中的值
*/ */
export function useConfigItem<K extends keyof ConfigOptions>( export function useConfigItem<K extends keyof ConfigOptions>(
key: K key: K
): ConfigOptions[K] { ): NonNullable<ConfigOptions[K]> {
const config = useContext(ConfigContext); const config = useContext(ConfigContext);
return config[key]; // 如果配置项为 undefined则回退到默认配置
return (config[key] ?? DEFAULT_CONFIG[key]) as NonNullable<ConfigOptions[K]>;
} }
// 导出配置类型 // 导出配置类型