Skip to content

Commit f7d31d5

Browse files
committed
# Conflicts: # src/components/DictTag/src/DictTag.vue
2 parents 53cef8b + b5eb9e7 commit f7d31d5

File tree

1 file changed

+58
-28
lines changed

1 file changed

+58
-28
lines changed

src/components/DictTag/src/DictTag.vue

Lines changed: 58 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
<script lang="tsx">
2-
import { defineComponent, PropType, ref } from 'vue'
2+
import { defineComponent, PropType, computed } from 'vue'
33
import { isHexColor } from '@/utils/color'
44
import { ElTag } from 'element-plus'
55
import { DictDataType, getDictOptions } from '@/utils/dict'
6+
import { isArray, isString, isNumber } from '@/utils/is'
67
78
export default defineComponent({
89
name: 'DictTag',
@@ -12,46 +13,75 @@ export default defineComponent({
1213
required: true
1314
},
1415
value: {
15-
type: [String, Number, Boolean] as PropType<string | number | boolean>,
16+
type: [String, Number, Boolean, Array],
1617
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'
1728
}
1829
},
1930
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+
})
3246
const rederDictTag = () => {
3347
if (!props.type) {
3448
return null
3549
}
3650
// 解决自定义字典标签值为零时标签不渲染的问题
37-
if (props.value === undefined || props.value === null) {
51+
if (props.value === undefined || props.value === null || props.value === '') {
3852
return null
3953
}
40-
getDictObj(props.type, props.value.toString())
41-
// 添加标签的文字颜色为白色,解决自定义背景颜色时标签文字看不清的问题
54+
const dictOptions = getDictOptions(props.type)
55+
4256
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+
}}
5265
>
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>
5585
)
5686
}
5787
return () => rederDictTag()

0 commit comments

Comments
 (0)