Tag 標籤
用於標記和選擇的標籤。支援六種語義類型、三檔尺寸、描邊態、圓角態與可關閉態。
契約源:
lh-ui-core/specs/tag.json· 黃金用例:lh-ui-core/cases/tag.json(6 條,已翻譯為 hypium 測試)
類型
六種類型決定底色、文字色與描邊色。
預設主要成功
警告危險資訊
HTML 模擬預覽(鴻蒙 / ArkUI 外觀),非真機渲染
typescript
LhTag({ text: '主要', type: LhTagType.Primary })描邊與圓角
plain 為 true 時切換為語義淺底 + 語義文字 + 語義描邊;round 為 true 時 圓角取高度的一半(與端側 radius 分數換算一致)。
描邊描邊描邊
圓角圓角描邊
HTML 模擬預覽(鴻蒙 / ArkUI 外觀),非真機渲染
尺寸與關閉
三檔尺寸映射到 tag.height / paddingHorizontal / fontSize 令牌;closable 為 true 時在右側繪製關閉符,點選觸發 onClose。
小中大
可關閉✕圓角可關閉✕
HTML 模擬預覽(鴻蒙 / ArkUI 外觀),非真機渲染
API
Props
| 屬性 | 說明 | 型別 | 預設值 |
|---|---|---|---|
text | 標籤文字內容(spec 的 default 插槽) | string | '' |
type | 語義類型,決定底色 / 文字色 / 描邊色 | LhTagType | Default |
size | 標籤尺寸,映射到尺寸令牌 | LhTagSize | Medium |
plain | 是否描邊態(語義淺底 + 語義文字 + 語義描邊) | boolean | false |
round | 是否圓角態(圓角 = 高度一半) | boolean | false |
closable | 是否顯示關閉符;點選觸發 onClose | boolean | false |
onClose | 點選關閉符時觸發 | () => void | 空函式 |
LhTagType
| 值 | 實心底 | 實心文字 | plain 底 / 文字 / 描邊 |
|---|---|---|---|
Default | color.background.default | color.text.primary | —(預設即中性) |
Primary | color.primary.default | color.onPrimary | color.primary.bg / .default / .default |
Success | color.success.default | color.onPrimary | color.success.bg / .default / .default |
Warning | color.warning.default | color.onPrimary | color.warning.bg / .default / .default |
Danger | color.danger.default | color.onPrimary | color.danger.bg / .default / .default |
Info | color.info.default | color.onPrimary | color.info.bg / .default / .default |
預設類型描邊取 color.border.base;語義類型實心態描邊透明。
LhTagSize
| 值 | height | paddingHorizontal | fontSize |
|---|---|---|---|
Small | tag.height.small(20vp) | tag.paddingHorizontal.small(8vp) | tag.fontSize.small(12) |
Medium | tag.height.medium(24vp) | tag.paddingHorizontal.medium(12vp) | tag.fontSize.medium(13) |
Large | tag.height.large(28vp) | tag.paddingHorizontal.large(16vp) | tag.fontSize.large(14) |
令牌對照
| 令牌 | 用途 |
|---|---|
tag.height.{small,medium,large} | 三檔高度(round 態圓角取其一半) |
tag.paddingHorizontal.{small,medium,large} | 三檔橫向內邊距 |
tag.fontSize.{small,medium,large} | 三檔字級 |
tag.borderRadius / tag.borderWidth | 基礎圓角與描邊寬度 |
color.background.default / color.text.primary / color.border.base | 預設類型三色 |
color.<sem>.default / color.onPrimary | 語義類型實心底 / 文字 |
color.<sem>.bg | plain 態淺底 |
spacing.xs | 關閉符與文字間距 |
黃金用例對照
cases/tag.json 的 6 條斷言與 hypium 測試的對應關係(entry/src/test/Tag.test.ets):
| case id | 斷言 | 本地測試 |
|---|---|---|
default-type-renders-text | 預設類型中性底/文字 | default_type_uses_neutral_tokens |
primary-type-uses-primary-tokens | 主色類型消費主色令牌 | primary_type_uses_primary_tokens |
plain-uses-bg-tokens | 描邊態消費語義淺底令牌 | plain_uses_bg_tokens |
small-size-uses-small-tokens | 小號消費 small 尺寸令牌 | small_size_uses_small_tokens |
round-makes-radius-half-height | 圓角態取高度一半 | round_makes_radius_half_height |
closable-tap-emits-onclose | 點選關閉符觸發 onClose | closable_close_gap_uses_spacing_xs(間距令牌側證) |
另有暗色專項:dark_colors_come_from_dark_tokens / dimensions_are_identical_in_dark_mode。 執行:DevEco Studio 右鍵 entry/src/test → Run Tests。

