vxe-pc-ui
Version:
A vue based PC component library
90 lines (89 loc) • 2.83 kB
JavaScript
import { defineComponent, ref, h, reactive, createCommentVNode } from 'vue';
import XEUtils from 'xe-utils';
import { getConfig, createEvent, useSize } from '../../ui';
import { getSlotVNs } from '../../ui/src/vn';
export default defineComponent({
name: 'VxeTag',
props: {
status: String,
title: [String, Number],
icon: String,
content: [String, Number],
size: {
type: String,
default: () => getConfig().tag.size || getConfig().size
}
},
emits: [
'click'
],
setup(props, context) {
const { slots, emit } = context;
const xID = XEUtils.uniqueId();
const { computeSize } = useSize(props);
const refElem = ref();
const reactData = reactive({});
const refMaps = {
refElem
};
const computeMaps = {};
const $xeTag = {
xID,
props,
context,
reactData,
getRefMaps: () => refMaps,
getComputeMaps: () => computeMaps
};
const dispatchEvent = (type, params, evnt) => {
emit(type, createEvent(evnt, { $tag: $xeTag }, params));
};
const tagMethods = {
dispatchEvent
};
const tagPrivateMethods = {};
const clickEvent = (evnt) => {
dispatchEvent('click', {}, evnt);
};
Object.assign($xeTag, tagMethods, tagPrivateMethods);
const renderContent = () => {
const { icon, content } = props;
const defaultSlot = slots.default;
const iconSlot = slots.icon;
return [
iconSlot || icon
? h('span', {
class: 'vxe-tag--icon'
}, iconSlot
? getSlotVNs(iconSlot({}))
: [
h('i', {
class: icon
})
])
: createCommentVNode(),
h('span', {
class: 'vxe-tag--content'
}, defaultSlot ? defaultSlot({}) : XEUtils.toValueString(content))
];
};
const renderVN = () => {
const { status, title } = props;
const vSize = computeSize.value;
return h('span', {
ref: refElem,
class: ['vxe-tag', {
[`size--${vSize}`]: vSize,
[`theme--${status}`]: status
}],
title,
onClick: clickEvent
}, renderContent());
};
$xeTag.renderVN = renderVN;
return $xeTag;
},
render() {
return this.renderVN();
}
});