Skip to content

Commit 251a98d

Browse files
committed
feat: DictTag组件兼容传入多个字典值展示多个标签的情况
1 parent 46da7e2 commit 251a98d

File tree

1 file changed

+40
-29
lines changed

1 file changed

+40
-29
lines changed

src/components/DictTag/src/DictTag.vue

Lines changed: 40 additions & 29 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,56 @@ 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
1718
}
1819
},
1920
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+
})
3235
const rederDictTag = () => {
3336
if (!props.type) {
3437
return null
3538
}
3639
// 解决自定义字典标签值为零时标签不渲染的问题
37-
if (props.value === undefined || props.value === null) {
40+
if (props.value === undefined || props.value === null || props.value === '') {
3841
return null
3942
}
40-
getDictObj(props.type, props.value.toString())
41-
// 添加标签的文字颜色为白色,解决自定义背景颜色时标签文字看不清的问题
43+
const dictOptions = getDictOptions(props.type)
44+
4245
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>
5566
)
5667
}
5768
return () => rederDictTag()

0 commit comments

Comments
 (0)