UNPKG

yanzhen-design-vue

Version:

56 lines (55 loc) 1.71 kB
import { computed, useAttrs, unref, mergeProps, createCommentVNode } from "vue"; import { useModelValue } from "@yanzhen-libs/utils-vue"; import { mergeWith } from "@yanzhen-libs/utils"; import { providePageConfigure, getComponentsInstance, isComponent, createSchemaElement } from "@yanzhen-lowcode/core"; import { omit } from "lodash-es"; function useWrapper(props, emit) { const valueRef = useModelValue(props, null, emit); const tag = computed(() => props.tag); const model = computed(() => props.model ?? {}); const propsRef = computed(() => omit(props, "tag", "model")); const attrs = useAttrs(); const _props = computed(() => { const schema = unref(propsRef); return mergeProps(unref(attrs), { class: [schema == null ? void 0 : schema.class], style: schema == null ? void 0 : schema.style }); }); function handleEmit(type, key, value) { let flag = true; switch (true) { case (props.prop && type === "update:model" && key === props.prop): valueRef.value = value; flag = false; break; } if (flag && emit && !type.startsWith("update:")) { emit(type, { model: unref(model), type, key, value }); } } providePageConfigure(model, { listener: handleEmit }); const Wrapper = computed(() => { const type = unref(tag); const props2 = unref(propsRef); const comp = type && getComponentsInstance(type); if (isComponent(comp)) { const typeProps = mergeWith({ type }, props2); return createSchemaElement(typeProps, unref(_props)); } return createCommentVNode("v-if", true); }); return { Wrapper }; } export { useWrapper };