应用台导航页
  • 主页
  • 博客
  • 知识库
  • 工作台
  • 集萃
  • 友链
  • 关于
一个 ts Tag 组件
React

一个 ts Tag 组件

技术
更新于 2023-08-04
— 805 字
返回

一个 TypeScript 版本的 Tag 组件示例:

tsx
interface Props extends React.HTMLAttributes<HTMLSpanElement> {
tagType: "primary" | "secondary";
text: string;
}
function Tag({ tagType, text, ...props }: Props) {
const baseClasses = `
inline-block
font-sans
text-xs
py-1.5
px-3
mb-4
rounded-lg`;
let classes = "";
let backgroundColorClass = "";
let textColorClass = "";
switch (tagType) {
case "primary":
backgroundColorClass = "bg-primary-100 dark:bg-primary-500";
textColorClass = "text-primary-500 dark:text-white";
break;
case "secondary":
backgroundColorClass = "bg-secondary-100 dark:bg-secondary-500";
textColorClass = "text-secondary-500 dark:text-white";
break;
default:
// handle other possible types
break;
}
classes = `${baseClasses} ${backgroundColorClass} ${textColorClass}`;
return (
<span className={classes} {...props}>
{text}
</span>
);
}

声明 Props 接口来指定 Props 类型,通过扩展 React.HTMLAttributes<HTMLSpanElement> 来继承 span HTML 元素的特性。最后,在属性中添加 {...props} 可以保持 JSX 中原有传递给 Tag 时的特性。

dors logoDors

Dors 是花野猫开发为知识工作者打造的数字花园应用,包含的博客、个人记事本、及其他实用功能。

花园

  • 花坛——博客
  • 果园——知识库

工坊——作者开发的实用工具

  • 小记
  • 秒切——一键按秒分割视频
  • 中国重点高校地理位置可视化网站
  • 中国行政区划数据查询平台
  • excel 重命名工具

misc

  • 生活章程
  • 画廊
  • just have fun!

© 2022 - present. All Rights Reserved.滇ICP备2025063395号-1

花野猫打造