StatusTag.vue 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <template>
  2. <el-tag
  3. :type="elTagType"
  4. :size="size"
  5. :effect="effect"
  6. :class="{ 'adm-tag--primary': resolvedType === 'primary' }"
  7. disable-transitions
  8. >{{ resolvedText }}</el-tag>
  9. </template>
  10. <script>
  11. /**
  12. * StatusTag —— 统一状态标签
  13. *
  14. * 设计目的:后台 129 个页面直接使用 <el-tag> 且状态→文案/颜色的映射散落各处
  15. * (有 levelTagType() 函数式,也有 row.x ? 'success' : 'info' 内联三元式),
  16. * 导致同一状态在不同页面颜色/文案不一致。本组件把映射收敛到 options。
  17. *
  18. * 用法一(映射式,推荐):
  19. * const STATUS_OPTIONS = [
  20. * { value: 0, label: '待审核', type: 'warning' },
  21. * { value: 1, label: '已通过', type: 'success' }
  22. * ]
  23. * <StatusTag :value="row.status" :options="STATUS_OPTIONS" />
  24. *
  25. * 用法二(简单式,等价原 el-tag):
  26. * <StatusTag :type="row.enabled ? 'success' : 'info'" :text="row.enabled ? '是' : '否'" />
  27. *
  28. * type 取值:primary / success / info / warning / danger
  29. */
  30. export default {
  31. name: 'StatusTag',
  32. props: {
  33. /** 当前状态值(映射式必填) */
  34. value: { type: [String, Number, Boolean], default: '' },
  35. /** 状态映射表:[{ value, label, type }] */
  36. options: { type: Array, default: null },
  37. /** 直接指定文案(优先于 options 的 label) */
  38. text: { type: String, default: '' },
  39. /** 直接指定类型:primary/success/info/warning/danger */
  40. type: { type: String, default: '' },
  41. /** 尺寸:medium / small / mini */
  42. size: { type: String, default: 'small' },
  43. /** 主题:light / dark / plain */
  44. effect: { type: String, default: 'light' },
  45. /** 命中不到映射时的兜底文案 */
  46. fallback: { type: String, default: '-' }
  47. },
  48. computed: {
  49. matched() {
  50. if (!this.options || !this.options.length) return null
  51. return this.options.find(item => item.value === this.value) || null
  52. },
  53. resolvedText() {
  54. if (this.text) return this.text
  55. if (this.matched && this.matched.label !== undefined) return this.matched.label
  56. if (this.value === '' || this.value === null || this.value === undefined) return this.fallback
  57. return this.value
  58. },
  59. resolvedType() {
  60. if (this.type) return this.type
  61. if (this.matched && this.matched.type) return this.matched.type
  62. return 'info'
  63. },
  64. // Element UI 的 el-tag 默认态即品牌蓝,primary 交由自定义 class 精确控制配色
  65. elTagType() {
  66. return this.resolvedType === 'primary' ? 'info' : this.resolvedType
  67. }
  68. }
  69. }
  70. </script>
  71. <style scoped>
  72. /* 品牌蓝状态标签:与 admin-tokens 的 Ant Design 5 令牌保持一致 */
  73. .adm-tag--primary {
  74. background: #e6f4ff;
  75. border-color: #91caff;
  76. color: var(--adm-primary);
  77. }
  78. </style>