5 changed files with 108 additions and 5 deletions
@ -0,0 +1,4 @@
|
||||
import { withInstall } from '@/utils' |
||||
import dictTag from './src/DictTag.vue' |
||||
|
||||
export const DictTag = withInstall(dictTag) |
@ -0,0 +1,55 @@
|
||||
<script lang="tsx"> |
||||
import { defineComponent, PropType, ref } from 'vue' |
||||
import { isHexColor } from '@/utils/color' |
||||
import { Tag } from 'ant-design-vue' |
||||
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 |
||||
} |
||||
}, |
||||
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 |
||||
} |
||||
// 解决自定义字典标签值为零时标签不渲染的问题 |
||||
if (props.value === undefined) { |
||||
return null |
||||
} |
||||
getDictObj(props.type, props.value.toString()) |
||||
// 添加标签的文字颜色为白色,解决自定义背景颜色时标签文字看不清的问题 |
||||
return ( |
||||
<Tag |
||||
style={dictData.value?.cssClass ? 'color: #108ee9' : ''} |
||||
type={dictData.value?.colorType} |
||||
color={dictData.value?.cssClass && isHexColor(dictData.value?.cssClass) ? dictData.value?.cssClass : ''} |
||||
> |
||||
{dictData.value?.label} |
||||
</Tag> |
||||
) |
||||
} |
||||
return () => rederDictTag() |
||||
} |
||||
}) |
||||
</script> |
@ -0,0 +1,39 @@
|
||||
import { DictTag } from '@/components/DictTag' |
||||
import { Image, Tag } from 'ant-design-vue' |
||||
import dayjs from 'dayjs' |
||||
import { h } from 'vue' |
||||
|
||||
export const useRender = { |
||||
// 渲染图片
|
||||
renderImg: (text) => { |
||||
if (text) { |
||||
return h(Image, { |
||||
src: text, |
||||
height: 80, |
||||
width: 80 |
||||
}) |
||||
} |
||||
}, |
||||
renderTag: (text, color?) => { |
||||
if (!color) { |
||||
return h(Tag, { color }, () => text) |
||||
} else { |
||||
return h('span', text) |
||||
} |
||||
}, |
||||
renderDate: (text, format?) => { |
||||
if (!format) { |
||||
return dayjs(text).format('YYYY-MM-DD HH:mm:ss') |
||||
} else { |
||||
return dayjs(text).format(format) |
||||
} |
||||
}, |
||||
renderDict: (text, type) => { |
||||
if (type) { |
||||
return h(DictTag, { |
||||
type: type, |
||||
value: text |
||||
}) |
||||
} |
||||
} |
||||
} |
Reference in new issue