//@ts-nocheck import { computed, defineComponent, isProxy, toRaw } from "vue"; import renders from "./itemRenders"; import { checkXItem, EVENT_TYPE, TIPS_TYPE } from "../tools/validate"; import { xU } from "../ventoseUtils"; import { diff } from "jsondiffpatch"; import { State_UI } from "../State_UI"; const { MutatingProps } = xU; const domClass = { tipsError: "ant-form-item-explain ant-form-item-explain-error" }; const devHelper = {}; const WILL_DELETE = [ "onValidateForm", "_$updateUI", "once", "itemTips", "rules", "labelVNodeRender", "slots", "validate", /* value 用updateValue处理,该值会触发render */ "value" ]; /* itemWrapperClass */ export const xItem = defineComponent({ name: "XItem", props: { /* 绑定的值 */ modelValue: { type: [Object, String, Number, Boolean], default: undefined }, configs: { type: Object, default() { return {}; } } }, emits: ["update:modelValue"], setup(props, { attrs, slots, emit, expose }) { let Cpt_isShowXItem: any = true; let Cpt_isDisabled: any = false; /*isShow*/ if (xU.isFunction(props.configs.isShow)) { Cpt_isShowXItem = computed(props.configs.isShow); } else if (xU.isBoolean(props.configs.isShow)) { Cpt_isShowXItem = computed(() => !!props.configs.isShow); } else { /* isShow 可能不存在 */ Cpt_isShowXItem = computed(() => { if (xU.isUndefined(props.configs.isShow)) { props.configs.isShow = true; } return !!props.configs.isShow; }); } /*disabled*/ if (xU.isFunction(props.configs.disabled)) { Cpt_isDisabled = computed(props.configs.disabled); } else if (xU.isBoolean(props.configs.disabled)) { Cpt_isDisabled = computed(() => !!props.configs.disabled); } /*readonly 在configs中,各个render自行实现*/ return { Cpt_isShowXItem, Cpt_isDisabled }; }, data(vm) { const { $props, $attrs } = vm; const triggerValidate = xU.debounce(function (eventType: string) { const { configs } = vm.$props; /*validate的定义 搜索 MutatingProps_configs_validate */ if (configs.validate) { configs.validate({ eventType: eventType, value: vm.properties.value }); } }, 500); const { listeners, propsWillDeleteFromConfigs } = (() => { const { configs } = $props; /* 后面的属性覆盖前面的属性 */ /* propsWillDeleteFromConfigsSet jsx 避免 listener 与 properties 实践名称重复 */ const propsSet = new Set(); /* 需要一个事件分发,拦截所有事件,再根据配置信息 */ const listeners = { /* 主要的触发方式 */ "onUpdate:value": (val: any) => { /* 使用configs.value的形式,一般是configs与组件是一对一的关系,configs需要是reactive的 */ if (configs.value !== undefined) { if (configs.value === val) { return; } else { configs.value = val; } } /* 双向绑定 */ vm.$emit("update:modelValue", val); /* @ts-ignore */ if (xU.isFunction(listeners.onAfterValueEmit)) { /* @ts-ignore */ listeners.onAfterValueEmit(val); } /* TODO: rule检测*/ triggerValidate(EVENT_TYPE.update); }, onValidateForm: () => { triggerValidate(EVENT_TYPE.validateForm); }, onChange: () => { triggerValidate(EVENT_TYPE.change); }, onInput: () => { triggerValidate(EVENT_TYPE.input); }, onBlur: () => { triggerValidate(EVENT_TYPE.blur); }, onFocus: () => { triggerValidate(EVENT_TYPE.focus); } }; function makeEventHandlerSupportMultiple( prop: unknown, xItemInnerEventHandler: unknown ) { propsSet.add(prop); if ( typeof listeners[prop] === "object" && xU.isArray(listeners[prop].handlerArray) ) { listeners[prop].handlerArray.push(xItemInnerEventHandler); } else { listeners[prop] = (...args: any) => { xU.each(listeners[prop].handlerArray, listener => { listener?.apply(configs, args); }); }; listeners[prop].handlerArray = [xItemInnerEventHandler]; } } /* listenners默认都是EventHandler */ xU.each(listeners, (value, prop) => makeEventHandlerSupportMultiple(prop, value) ); xU.each(configs, (value, prop) => { /* FIX: 监听函数单独出来。listener不知道在哪里被覆盖了,inputPassword 被 pop 包裹,childListener被修改了,UI库??*/ if (xU.isListener(prop)) { makeEventHandlerSupportMultiple(prop, value); } }); xU.each($attrs, (value, prop) => { /* FIX: 监听函数单独出来。listener不知道在哪里被覆盖了,inputPassword 被 pop 包裹,childListener被修改了,UI库??*/ if (xU.isListener(prop)) { makeEventHandlerSupportMultiple(prop, value); } }); return { listeners, propsWillDeleteFromConfigs: [...propsSet] }; })(); return { /* 因修改configs属性,触发的UI刷新 */ rerenderCount: 0, /* validateInfo */ isRequired: false, /* validateInfo */ properties: null, itemSlots: {}, listeners, propsWillDeleteFromConfigs }; }, computed: { CurrentXItem() { if (xU.isObject(this.configs.itemType)) { if (isProxy(this.configs.itemType)) { return toRaw(this.configs.itemType); } return this.configs.itemType; } /* String */ return renders[this.configs.itemType] || renders.Input; }, itemTypeName() { if (xU.isString(this.configs.itemType)) { return String(this.configs.itemType); } return ""; }, isChecking() { return Boolean(this.configs.checking); }, /* 组件唯一标识 */ FormItemId() { return `xItem_${this._.uid}`; }, /* 提示信息的类型及提示信息 */ itemTips() { const _itemTips = { type: "", msg: "" }; if (this.configs?.itemTips?.type) { return { type: this.configs.itemTips.type, msg: xU.isFunction(this.configs.itemTips.msg) ? this.configs.itemTips.msg() : this.configs.itemTips.msg }; } else { this.configs.itemTips = _itemTips; return _itemTips; } }, itemWrapperClass() { return [ this.configs.itemWrapperClass, /*flex 一般与从简在同一行*/ "ant-form-item ant-form-item-with-help x-item flex", this.itemTips.type === TIPS_TYPE.error ? "ant-form-item-has-error" : "" ].join(" "); }, /* VNode */ tipsVNode() { if (this.isChecking) { return (