perf: dictTag

pull/2/head
xingyu 2023-01-17 15:07:46 +08:00
parent d0ad54b4d7
commit dbfc485bb1
1 changed files with 47 additions and 40 deletions

View File

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