1
1
<script lang="tsx">
2
- import { defineComponent , PropType , ref } from ' vue'
2
+ import { defineComponent , PropType , computed } from ' vue'
3
3
import { isHexColor } from ' @/utils/color'
4
4
import { ElTag } from ' element-plus'
5
5
import { DictDataType , getDictOptions } from ' @/utils/dict'
6
+ import { isArray , isString , isNumber } from ' @/utils/is'
6
7
7
8
export default defineComponent ({
8
9
name: ' DictTag' ,
@@ -12,46 +13,75 @@ export default defineComponent({
12
13
required: true
13
14
},
14
15
value: {
15
- type: [String , Number , Boolean ] as PropType < string | number | boolean > ,
16
+ type: [String , Number , Boolean , Array ] ,
16
17
required: true
18
+ },
19
+ // 字符串分隔符 只有当 props.value 传入值为字符串时有效
20
+ sepSymbol: {
21
+ type: String as PropType <string >,
22
+ default: ' ,'
23
+ },
24
+ // 每个tag之间的间隔,默认为5px
25
+ tagSpacing: {
26
+ type: String as PropType <string >,
27
+ default: ' 5px'
17
28
}
18
29
},
19
30
setup(props ) {
20
- const dictData = ref <DictDataType >()
21
- const getDictObj = (dictType : string , value : string ) => {
22
- const dictOptions = getDictOptions (dictType )
23
- dictOptions .forEach ((dict : DictDataType ) => {
24
- if (dict .value === value ) {
25
- if (dict .colorType + ' ' === ' default' ) {
26
- dict .colorType = ' info'
27
- }
28
- dictData .value = dict
29
- }
30
- })
31
- }
31
+ const valueArr: any = computed (() => {
32
+ // 1.是Number类型的情况
33
+ if (isNumber (props .value )) {
34
+ return [String (props .value )]
35
+ }
36
+ // 2.是字符串(进一步判断是否有包含分隔符号 -> props.sepSymbol )
37
+ else if (isString (props .value )) {
38
+ return props .value .split (props .sepSymbol )
39
+ }
40
+ // 3.数组
41
+ else if (isArray (props .value )) {
42
+ return props .value .map (String )
43
+ }
44
+ return []
45
+ })
32
46
const rederDictTag = () => {
33
47
if (! props .type ) {
34
48
return null
35
49
}
36
50
// 解决自定义字典标签值为零时标签不渲染的问题
37
- if (props .value === undefined || props .value === null ) {
51
+ if (props .value === undefined || props .value === null || props . value === ' ' ) {
38
52
return null
39
53
}
40
- getDictObj (props .type , props . value . toString () )
41
- // 添加标签的文字颜色为白色,解决自定义背景颜色时标签文字看不清的问题
54
+ const dictOptions = getDictOptions (props .type )
55
+
42
56
return (
43
- <ElTag
44
- style = { dictData .value ?.cssClass ? ' color: #fff' : ' ' }
45
- type = { dictData .value ?.colorType }
46
- color = {
47
- dictData .value ?.cssClass && isHexColor (dictData .value ?.cssClass )
48
- ? dictData .value ?.cssClass
49
- : ' '
50
- }
51
- disableTransitions = { true }
57
+ <div
58
+ class = " dict-tag"
59
+ style = { {
60
+ display: ' flex' ,
61
+ gap: props .tagSpacing ,
62
+ justifyContent: ' center' ,
63
+ alignItems: ' center'
64
+ }}
52
65
>
53
- { dictData .value ?.label }
54
- </ElTag >
66
+ { dictOptions .map ((dict : DictDataType ) => {
67
+ if (valueArr .value .includes (dict .value )) {
68
+ if (dict .colorType + ' ' === ' primary' || dict .colorType + ' ' === ' default' ) {
69
+ dict .colorType = ' '
70
+ }
71
+ return (
72
+ // 添加标签的文字颜色为白色,解决自定义背景颜色时标签文字看不清的问题
73
+ <ElTag
74
+ style = { dict ?.cssClass ? ' color: #fff' : ' ' }
75
+ type = { dict ?.colorType }
76
+ color = { dict ?.cssClass && isHexColor (dict ?.cssClass ) ? dict ?.cssClass : ' ' }
77
+ disableTransitions = { true }
78
+ >
79
+ { dict ?.label }
80
+ </ElTag >
81
+ )
82
+ }
83
+ })}
84
+ </div >
55
85
)
56
86
}
57
87
return () => rederDictTag ()
0 commit comments