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,56 @@ 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
17
18
}
18
19
},
19
20
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 + ' ' === ' primary' || dict .colorType + ' ' === ' default' ) {
26
- dict .colorType = ' '
27
- }
28
- dictData .value = dict
29
- }
30
- })
31
- }
21
+ const valueArr: any = computed (() => {
22
+ // 1.是Number类型的情况
23
+ if (isNumber (props .value )) {
24
+ return [String (props .value )]
25
+ }
26
+ // 2.是字符串(进一步判断是否有',')
27
+ else if (isString (props .value )) {
28
+ return props .value .includes (' ,' ) ? props .value .split (' ,' ) : [String (props .value )]
29
+ }
30
+ // 3.数组
31
+ else if (isArray (props .value )) {
32
+ return props .value .map (String )
33
+ }
34
+ })
32
35
const rederDictTag = () => {
33
36
if (! props .type ) {
34
37
return null
35
38
}
36
39
// 解决自定义字典标签值为零时标签不渲染的问题
37
- if (props .value === undefined || props .value === null ) {
40
+ if (props .value === undefined || props .value === null || props . value === ' ' ) {
38
41
return null
39
42
}
40
- getDictObj (props .type , props . value . toString () )
41
- // 添加标签的文字颜色为白色,解决自定义背景颜色时标签文字看不清的问题
43
+ const dictOptions = getDictOptions (props .type )
44
+
42
45
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 }
52
- >
53
- { dictData .value ?.label }
54
- </ElTag >
46
+ <div class = " dict-tag" >
47
+ { dictOptions .map ((dict : DictDataType ) => {
48
+ if (valueArr .value .includes (dict .value )) {
49
+ if (dict .colorType + ' ' === ' primary' || dict .colorType + ' ' === ' default' ) {
50
+ dict .colorType = ' '
51
+ }
52
+ return (
53
+ // 添加标签的文字颜色为白色,解决自定义背景颜色时标签文字看不清的问题
54
+ <ElTag
55
+ style = { dict ?.cssClass ? ' color: #fff' : ' ' }
56
+ type = { dict ?.colorType }
57
+ color = { dict ?.cssClass && isHexColor (dict ?.cssClass ) ? dict ?.cssClass : ' ' }
58
+ disableTransitions = { true }
59
+ >
60
+ { dict ?.label }
61
+ </ElTag >
62
+ )
63
+ }
64
+ })}
65
+ </div >
55
66
)
56
67
}
57
68
return () => rederDictTag ()
0 commit comments