mirror of
				https://github.com/YunaiV/ruoyi-vue-pro.git
				synced 2025-10-31 02:28:03 +08:00 
			
		
		
		
	perf: dictTag
This commit is contained in:
		| @ -1,46 +1,53 @@ | |||||||
| <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' | ||||||
|   type: { |  | ||||||
|     type: String as PropType<string>, | export default defineComponent({ | ||||||
|     required: true |   name: 'DictTag', | ||||||
|  |   props: { | ||||||
|  |     type: { | ||||||
|  |       type: String as PropType<string>, | ||||||
|  |       required: true | ||||||
|  |     }, | ||||||
|  |     value: { | ||||||
|  |       type: [String, Number, Boolean] as PropType<string | number | boolean>, | ||||||
|  |       required: true | ||||||
|  |     } | ||||||
|   }, |   }, | ||||||
|   value: { |   setup(props) { | ||||||
|     type: [String, Number, Boolean] as PropType<string | number | boolean>, |     const dictData = ref<DictDataType>() | ||||||
|     required: true |     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> | </script> | ||||||
| <template> |  | ||||||
|   <ElTag |  | ||||||
|     :disable-transitions="true" |  | ||||||
|     :key="dictData?.value + ''" |  | ||||||
|     :type="dictData?.colorType" |  | ||||||
|     :color="dictData?.cssClass && isHexColor(dictData?.cssClass) ? dictData?.cssClass : ''" |  | ||||||
|   > |  | ||||||
|     {{ dictData?.label }} |  | ||||||
|   </ElTag> |  | ||||||
| </template> |  | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user
	 xingyu
					xingyu