perf: dictTag

This commit is contained in:
xingyu 2023-01-17 15:07:46 +08:00
parent d0ad54b4d7
commit dbfc485bb1

View File

@ -1,46 +1,53 @@
<script setup lang="ts">
import { onMounted, onUpdated, PropType, ref } from 'vue'
import { getDictOptions, DictDataType } from '@/utils/dict'
<script lang="tsx">
import { defineComponent, PropType, ref } from 'vue'
import { isHexColor } from '@/utils/color'
import { ElTag } from 'element-plus'
const props = defineProps({
type: {
type: String as PropType<string>,
required: true
import { DictDataType, getDictOptions } from '@/utils/dict'
export default defineComponent({
name: 'DictTag',
props: {
type: {
type: String as PropType<string>,
required: true
},
value: {
type: [String, Number, Boolean] as PropType<string | number | boolean>,
required: true
}
},
value: {
type: [String, Number, Boolean] as PropType<string | number | boolean>,
required: true
setup(props) {
const dictData = ref<DictDataType>()
const getDictObj = (dictType: string, value: string) => {
const dictOptions = getDictOptions(dictType)
dictOptions.forEach((dict: DictDataType) => {
if (dict.value === value) {
if (dict.colorType + '' === 'primary' || dict.colorType + '' === 'default') {
dict.colorType = ''
}
dictData.value = dict
}
})
}
const rederDictTag = () => {
if (!props.type) {
return null
}
getDictObj(props.type, props.value.toString())
return (
<ElTag
type={dictData.value?.colorType}
color={
dictData.value?.cssClass && isHexColor(dictData.value?.cssClass)
? dictData.value?.cssClass
: ''
}
>
{dictData.value?.label}
</ElTag>
)
}
return () => rederDictTag()
}
})
const dictData = ref<DictDataType>()
const getDictObj = (dictType: string, value: string) => {
const dictOptions = getDictOptions(dictType)
dictOptions.forEach((dict: DictDataType) => {
if (dict.value === value) {
if (dict.colorType + '' === 'primary' || dict.colorType + '' === 'default') {
dict.colorType = ''
}
dictData.value = dict
}
})
}
onMounted(() => {
return getDictObj(props.type, props.value?.toString())
})
onUpdated(() => {
getDictObj(props.type, props.value?.toString())
})
</script>
<template>
<ElTag
:disable-transitions="true"
:key="dictData?.value + ''"
:type="dictData?.colorType"
:color="dictData?.cssClass && isHexColor(dictData?.cssClass) ? dictData?.cssClass : ''"
>
{{ dictData?.label }}
</ElTag>
</template>