From 9ba90c8b3a03e736396428cdfb26f31dd64244a1 Mon Sep 17 00:00:00 2001 From: fxy060608 Date: Mon, 17 Oct 2022 17:18:03 +0800 Subject: [PATCH] fix(mp-weixin): uni://form-field (question/155373) --- packages/shims-vue-runtime.d.ts | 1 + packages/uni-mp-alipay/dist/uni.mp.esm.js | 21 ++++++-- packages/uni-mp-baidu/dist/uni.mp.esm.js | 40 ++++++++++++++-- packages/uni-mp-core/src/index.ts | 2 +- .../src/runtime/componentOptions.ts | 4 +- .../uni-mp-core/src/runtime/componentProps.ts | 48 +++++++++++++++++-- packages/uni-mp-kuaishou/dist/uni.mp.esm.js | 40 ++++++++++++++-- packages/uni-mp-lark/dist/uni.mp.esm.js | 43 +++++++++++++++-- packages/uni-mp-qq/dist/uni.mp.esm.js | 40 ++++++++++++++-- packages/uni-mp-toutiao/dist/uni.mp.esm.js | 43 +++++++++++++++-- .../src/runtime/componentLifetimes.ts | 11 ++++- packages/uni-mp-weixin/dist/uni.mp.esm.js | 40 ++++++++++++++-- .../uni-mp-weixin/src/runtime/lifetimes.ts | 5 ++ .../uni-quickapp-webview/dist/uni.mp.esm.js | 43 +++++++++++++++-- packages/uni-stat/lib/uni.plugin.js | 2 +- packages/uni-stat/src/plugin/index.ts | 12 +++-- 16 files changed, 347 insertions(+), 48 deletions(-) diff --git a/packages/shims-vue-runtime.d.ts b/packages/shims-vue-runtime.d.ts index dd307c337..b37448045 100644 --- a/packages/shims-vue-runtime.d.ts +++ b/packages/shims-vue-runtime.d.ts @@ -5,6 +5,7 @@ declare module '@vue/runtime-core' { route: string $scope: { $getAppWebview?: () => PlusWebviewWebviewObject + setData(data: Record, callback?: () => void): void } // 目前 H5,APP 平台 getCurrentPages 中获取的 page 对象调整为 vm 对象 $getAppWebview?: () => PlusWebviewWebviewObject diff --git a/packages/uni-mp-alipay/dist/uni.mp.esm.js b/packages/uni-mp-alipay/dist/uni.mp.esm.js index 4bc669339..7a1fcef38 100644 --- a/packages/uni-mp-alipay/dist/uni.mp.esm.js +++ b/packages/uni-mp-alipay/dist/uni.mp.esm.js @@ -339,7 +339,7 @@ const builtInProps = [ // 小程序不能直接定义 $slots 的 props,所以通过 vueSlots 转换到 $slots 'uS', ]; -function initDefaultProps(isBehavior = false) { +function initDefaultProps(options, isBehavior = false) { const properties = {}; if (!isBehavior) { // 均不指定类型,避免微信小程序 property received type-uncompatible value 警告 @@ -365,6 +365,19 @@ function initDefaultProps(isBehavior = false) { }, }; } + if (options.behaviors) { + // wx://form-field + if (options.behaviors.includes('my://form-field')) { + properties.name = { + type: null, + value: '', + }; + properties.value = { + type: null, + value: '', + }; + } + } return properties; } function initVirtualHostProps(options) { @@ -392,7 +405,7 @@ function initProps(mpComponentOptions) { if (!mpComponentOptions.properties) { mpComponentOptions.properties = {}; } - extend(mpComponentOptions.properties, initDefaultProps(), initVirtualHostProps(mpComponentOptions.options)); + extend(mpComponentOptions.properties, initDefaultProps(mpComponentOptions), initVirtualHostProps(mpComponentOptions.options)); } function findPropsData(properties, isPage) { return ((isPage @@ -453,14 +466,14 @@ function initBehaviors(vueOptions) { if (behavior === 'uni://form-field') { if (isArray(vueProps)) { vueProps.push('name'); - vueProps.push('value'); + vueProps.push('modelValue'); } else { vueProps.name = { type: String, default: '', }; - vueProps.value = { + vueProps.modelValue = { type: [String, Number, Boolean, Array, Object, Date], default: '', }; diff --git a/packages/uni-mp-baidu/dist/uni.mp.esm.js b/packages/uni-mp-baidu/dist/uni.mp.esm.js index ec6c09830..3fb881c0c 100644 --- a/packages/uni-mp-baidu/dist/uni.mp.esm.js +++ b/packages/uni-mp-baidu/dist/uni.mp.esm.js @@ -437,7 +437,7 @@ const builtInProps = [ // 小程序不能直接定义 $slots 的 props,所以通过 vueSlots 转换到 $slots 'uS', ]; -function initDefaultProps(isBehavior = false) { +function initDefaultProps(options, isBehavior = false) { const properties = {}; if (!isBehavior) { // 均不指定类型,避免微信小程序 property received type-uncompatible value 警告 @@ -463,6 +463,19 @@ function initDefaultProps(isBehavior = false) { }, }; } + if (options.behaviors) { + // wx://form-field + if (options.behaviors.includes('swan://form-field')) { + properties.name = { + type: null, + value: '', + }; + properties.value = { + type: null, + value: '', + }; + } + } return properties; } function initVirtualHostProps(options) { @@ -478,7 +491,7 @@ function initProps(mpComponentOptions) { if (!mpComponentOptions.properties) { mpComponentOptions.properties = {}; } - extend(mpComponentOptions.properties, initDefaultProps(), initVirtualHostProps(mpComponentOptions.options)); + extend(mpComponentOptions.properties, initDefaultProps(mpComponentOptions), initVirtualHostProps(mpComponentOptions.options)); } const PROP_TYPES = [String, Number, Boolean, Object, Array, null]; function parsePropType(type, defaultValue) { @@ -557,6 +570,22 @@ function findPagePropsData(properties) { }); } return propsData; +} +function initFormField(vm) { + // 同步 form-field 的 name,value 值 + const vueOptions = vm.$options; + if (isArray(vueOptions.behaviors) && + vueOptions.behaviors.includes('uni://form-field')) { + vm.$watch('modelValue', () => { + vm.$scope && + vm.$scope.setData({ + name: vm.name, + value: vm.modelValue, + }); + }, { + immediate: true, + }); + } } function initData(_) { @@ -630,14 +659,14 @@ function initBehaviors(vueOptions) { if (behavior === 'uni://form-field') { if (isArray(vueProps)) { vueProps.push('name'); - vueProps.push('value'); + vueProps.push('modelValue'); } else { vueProps.name = { type: String, default: '', }; - vueProps.value = { + vueProps.modelValue = { type: [String, Number, Boolean, Array, Object, Date], default: '', }; @@ -888,6 +917,9 @@ function initLifetimes({ mocks, isPage, initRelation, vueOptions, }) { initComponentInstance(instance, options); }, }); + if (!isMiniProgramPage) { + initFormField(this.$vm); + } }, ready() { // 当组件 props 默认值为 true,初始化时传入 false 会导致 created,ready 触发, 但 attached 不触发 diff --git a/packages/uni-mp-core/src/index.ts b/packages/uni-mp-core/src/index.ts index 9026a71d3..173515aa7 100644 --- a/packages/uni-mp-core/src/index.ts +++ b/packages/uni-mp-core/src/index.ts @@ -23,7 +23,7 @@ export { initBehaviors, updateComponentProps, } from './runtime/componentOptions' -export { initProps } from './runtime/componentProps' +export { initProps, initFormField } from './runtime/componentProps' export { initHooks, initUnknownHooks, diff --git a/packages/uni-mp-core/src/runtime/componentOptions.ts b/packages/uni-mp-core/src/runtime/componentOptions.ts index a28c67a4f..33f42b4fc 100644 --- a/packages/uni-mp-core/src/runtime/componentOptions.ts +++ b/packages/uni-mp-core/src/runtime/componentOptions.ts @@ -111,13 +111,13 @@ export function initBehaviors(vueOptions: ComponentOptions): string[] { if (behavior === 'uni://form-field') { if (isArray(vueProps)) { vueProps.push('name') - vueProps.push('value') + vueProps.push('modelValue') } else { vueProps.name = { type: String, default: '', } - vueProps.value = { + vueProps.modelValue = { type: [String, Number, Boolean, Array, Object, Date], default: '', } diff --git a/packages/uni-mp-core/src/runtime/componentProps.ts b/packages/uni-mp-core/src/runtime/componentProps.ts index 1c08b4a10..221860500 100644 --- a/packages/uni-mp-core/src/runtime/componentProps.ts +++ b/packages/uni-mp-core/src/runtime/componentProps.ts @@ -1,4 +1,7 @@ -import { ComponentPropsOptions } from '@vue/runtime-core' +import { + ComponentPropsOptions, + ComponentPublicInstance, +} from '@vue/runtime-core' import { extend, isArray, isFunction, isPlainObject } from '@vue/shared' import type { MPComponentOptions, MPComponentInstance } from './component' // @ts-ignore @@ -24,7 +27,10 @@ const builtInProps = [ 'uS', ] -function initDefaultProps(isBehavior: boolean = false) { +function initDefaultProps( + options: MPComponentOptions, + isBehavior: boolean = false +) { const properties: Component.PropertyOption = {} if (!isBehavior) { // 均不指定类型,避免微信小程序 property received type-uncompatible value 警告 @@ -50,6 +56,19 @@ function initDefaultProps(isBehavior: boolean = false) { }, } } + if (options.behaviors) { + // wx://form-field + if (options.behaviors.includes('__GLOBAL__://form-field')) { + properties.name = { + type: null, + value: '', + } + properties.value = { + type: null, + value: '', + } + } + } return properties } @@ -81,7 +100,7 @@ export function initProps(mpComponentOptions: MPComponentOptions) { } extend( mpComponentOptions.properties, - initDefaultProps(), + initDefaultProps(mpComponentOptions), initVirtualHostProps(mpComponentOptions.options) ) } @@ -176,3 +195,26 @@ function findPagePropsData(properties: Record) { } return propsData } + +export function initFormField(vm: ComponentPublicInstance) { + // 同步 form-field 的 name,value 值 + const vueOptions = vm.$options + if ( + isArray(vueOptions.behaviors) && + vueOptions.behaviors.includes('uni://form-field') + ) { + vm.$watch( + 'modelValue', + () => { + vm.$scope && + vm.$scope.setData({ + name: (vm as any).name, + value: (vm as any).modelValue, + }) + }, + { + immediate: true, + } + ) + } +} diff --git a/packages/uni-mp-kuaishou/dist/uni.mp.esm.js b/packages/uni-mp-kuaishou/dist/uni.mp.esm.js index e97846374..c1e74c7fc 100644 --- a/packages/uni-mp-kuaishou/dist/uni.mp.esm.js +++ b/packages/uni-mp-kuaishou/dist/uni.mp.esm.js @@ -437,7 +437,7 @@ const builtInProps = [ // 小程序不能直接定义 $slots 的 props,所以通过 vueSlots 转换到 $slots 'uS', ]; -function initDefaultProps(isBehavior = false) { +function initDefaultProps(options, isBehavior = false) { const properties = {}; if (!isBehavior) { // 均不指定类型,避免微信小程序 property received type-uncompatible value 警告 @@ -463,6 +463,19 @@ function initDefaultProps(isBehavior = false) { }, }; } + if (options.behaviors) { + // wx://form-field + if (options.behaviors.includes('ks://form-field')) { + properties.name = { + type: null, + value: '', + }; + properties.value = { + type: null, + value: '', + }; + } + } return properties; } function initVirtualHostProps(options) { @@ -478,7 +491,7 @@ function initProps(mpComponentOptions) { if (!mpComponentOptions.properties) { mpComponentOptions.properties = {}; } - extend(mpComponentOptions.properties, initDefaultProps(), initVirtualHostProps(mpComponentOptions.options)); + extend(mpComponentOptions.properties, initDefaultProps(mpComponentOptions), initVirtualHostProps(mpComponentOptions.options)); } const PROP_TYPES = [String, Number, Boolean, Object, Array, null]; function parsePropType(type, defaultValue) { @@ -546,6 +559,22 @@ function findPagePropsData(properties) { }); } return propsData; +} +function initFormField(vm) { + // 同步 form-field 的 name,value 值 + const vueOptions = vm.$options; + if (isArray(vueOptions.behaviors) && + vueOptions.behaviors.includes('uni://form-field')) { + vm.$watch('modelValue', () => { + vm.$scope && + vm.$scope.setData({ + name: vm.name, + value: vm.modelValue, + }); + }, { + immediate: true, + }); + } } function initData(_) { @@ -615,14 +644,14 @@ function initBehaviors(vueOptions) { if (behavior === 'uni://form-field') { if (isArray(vueProps)) { vueProps.push('name'); - vueProps.push('value'); + vueProps.push('modelValue'); } else { vueProps.name = { type: String, default: '', }; - vueProps.value = { + vueProps.modelValue = { type: [String, Number, Boolean, Array, Object, Date], default: '', }; @@ -854,6 +883,9 @@ function initLifetimes({ mocks, isPage, initRelation, vueOptions, }) { initComponentInstance(instance, options); }, }); + if (!isMiniProgramPage) { + initFormField(this.$vm); + } }, ready() { // 当组件 props 默认值为 true,初始化时传入 false 会导致 created,ready 触发, 但 attached 不触发 diff --git a/packages/uni-mp-lark/dist/uni.mp.esm.js b/packages/uni-mp-lark/dist/uni.mp.esm.js index 3bf5110c1..2b4cdc65c 100644 --- a/packages/uni-mp-lark/dist/uni.mp.esm.js +++ b/packages/uni-mp-lark/dist/uni.mp.esm.js @@ -400,7 +400,7 @@ const builtInProps = [ // 小程序不能直接定义 $slots 的 props,所以通过 vueSlots 转换到 $slots 'uS', ]; -function initDefaultProps(isBehavior = false) { +function initDefaultProps(options, isBehavior = false) { const properties = {}; if (!isBehavior) { // 均不指定类型,避免微信小程序 property received type-uncompatible value 警告 @@ -426,6 +426,19 @@ function initDefaultProps(isBehavior = false) { }, }; } + if (options.behaviors) { + // wx://form-field + if (options.behaviors.includes('tt://form-field')) { + properties.name = { + type: null, + value: '', + }; + properties.value = { + type: null, + value: '', + }; + } + } return properties; } function initVirtualHostProps(options) { @@ -441,7 +454,7 @@ function initProps(mpComponentOptions) { if (!mpComponentOptions.properties) { mpComponentOptions.properties = {}; } - extend(mpComponentOptions.properties, initDefaultProps(), initVirtualHostProps(mpComponentOptions.options)); + extend(mpComponentOptions.properties, initDefaultProps(mpComponentOptions), initVirtualHostProps(mpComponentOptions.options)); } const PROP_TYPES = [String, Number, Boolean, Object, Array, null]; function parsePropType(type, defaultValue) { @@ -509,6 +522,22 @@ function findPagePropsData(properties) { }); } return propsData; +} +function initFormField(vm) { + // 同步 form-field 的 name,value 值 + const vueOptions = vm.$options; + if (isArray(vueOptions.behaviors) && + vueOptions.behaviors.includes('uni://form-field')) { + vm.$watch('modelValue', () => { + vm.$scope && + vm.$scope.setData({ + name: vm.name, + value: vm.modelValue, + }); + }, { + immediate: true, + }); + } } function initData(_) { @@ -578,14 +607,14 @@ function initBehaviors(vueOptions) { if (behavior === 'uni://form-field') { if (isArray(vueProps)) { vueProps.push('name'); - vueProps.push('value'); + vueProps.push('modelValue'); } else { vueProps.name = { type: String, default: '', }; - vueProps.value = { + vueProps.modelValue = { type: [String, Number, Boolean, Array, Object, Date], default: '', }; @@ -844,9 +873,10 @@ function initLifetimes$1({ mocks, isPage, initRelation, vueOptions, }) { if (mpType === 'page' && !mpInstance.route && mpInstance.__route__) { mpInstance.route = mpInstance.__route__; } + const props = findPropsData(properties, mpType === 'page'); this.$vm = $createComponent({ type: vueOptions, - props: findPropsData(properties, mpType === 'page'), + props, }, { mpType, mpInstance, @@ -858,6 +888,9 @@ function initLifetimes$1({ mocks, isPage, initRelation, vueOptions, }) { initComponentInstance(instance, options); }, }); + if (mpType === 'component') { + initFormField(this.$vm); + } // 处理父子关系 initRelation(this, relationOptions); } diff --git a/packages/uni-mp-qq/dist/uni.mp.esm.js b/packages/uni-mp-qq/dist/uni.mp.esm.js index 8afea47f7..e786f713c 100644 --- a/packages/uni-mp-qq/dist/uni.mp.esm.js +++ b/packages/uni-mp-qq/dist/uni.mp.esm.js @@ -386,7 +386,7 @@ const builtInProps = [ // 小程序不能直接定义 $slots 的 props,所以通过 vueSlots 转换到 $slots 'uS', ]; -function initDefaultProps(isBehavior = false) { +function initDefaultProps(options, isBehavior = false) { const properties = {}; if (!isBehavior) { // 均不指定类型,避免微信小程序 property received type-uncompatible value 警告 @@ -412,6 +412,19 @@ function initDefaultProps(isBehavior = false) { }, }; } + if (options.behaviors) { + // wx://form-field + if (options.behaviors.includes('qq://form-field')) { + properties.name = { + type: null, + value: '', + }; + properties.value = { + type: null, + value: '', + }; + } + } return properties; } function initVirtualHostProps(options) { @@ -427,7 +440,7 @@ function initProps(mpComponentOptions) { if (!mpComponentOptions.properties) { mpComponentOptions.properties = {}; } - extend(mpComponentOptions.properties, initDefaultProps(), initVirtualHostProps(mpComponentOptions.options)); + extend(mpComponentOptions.properties, initDefaultProps(mpComponentOptions), initVirtualHostProps(mpComponentOptions.options)); } const PROP_TYPES = [String, Number, Boolean, Object, Array, null]; function parsePropType(type, defaultValue) { @@ -495,6 +508,22 @@ function findPagePropsData(properties) { }); } return propsData; +} +function initFormField(vm) { + // 同步 form-field 的 name,value 值 + const vueOptions = vm.$options; + if (isArray(vueOptions.behaviors) && + vueOptions.behaviors.includes('uni://form-field')) { + vm.$watch('modelValue', () => { + vm.$scope && + vm.$scope.setData({ + name: vm.name, + value: vm.modelValue, + }); + }, { + immediate: true, + }); + } } function initData(_) { @@ -567,14 +596,14 @@ function initBehaviors(vueOptions) { if (behavior === 'uni://form-field') { if (isArray(vueProps)) { vueProps.push('name'); - vueProps.push('value'); + vueProps.push('modelValue'); } else { vueProps.name = { type: String, default: '', }; - vueProps.value = { + vueProps.modelValue = { type: [String, Number, Boolean, Array, Object, Date], default: '', }; @@ -767,6 +796,9 @@ function initLifetimes({ mocks, isPage, initRelation, vueOptions, }) { initComponentInstance(instance, options); }, }); + if (!isMiniProgramPage) { + initFormField(this.$vm); + } }, ready() { // 当组件 props 默认值为 true,初始化时传入 false 会导致 created,ready 触发, 但 attached 不触发 diff --git a/packages/uni-mp-toutiao/dist/uni.mp.esm.js b/packages/uni-mp-toutiao/dist/uni.mp.esm.js index 579af824d..0dbf7ad1f 100644 --- a/packages/uni-mp-toutiao/dist/uni.mp.esm.js +++ b/packages/uni-mp-toutiao/dist/uni.mp.esm.js @@ -400,7 +400,7 @@ const builtInProps = [ // 小程序不能直接定义 $slots 的 props,所以通过 vueSlots 转换到 $slots 'uS', ]; -function initDefaultProps(isBehavior = false) { +function initDefaultProps(options, isBehavior = false) { const properties = {}; if (!isBehavior) { // 均不指定类型,避免微信小程序 property received type-uncompatible value 警告 @@ -426,6 +426,19 @@ function initDefaultProps(isBehavior = false) { }, }; } + if (options.behaviors) { + // wx://form-field + if (options.behaviors.includes('tt://form-field')) { + properties.name = { + type: null, + value: '', + }; + properties.value = { + type: null, + value: '', + }; + } + } return properties; } function initVirtualHostProps(options) { @@ -441,7 +454,7 @@ function initProps(mpComponentOptions) { if (!mpComponentOptions.properties) { mpComponentOptions.properties = {}; } - extend(mpComponentOptions.properties, initDefaultProps(), initVirtualHostProps(mpComponentOptions.options)); + extend(mpComponentOptions.properties, initDefaultProps(mpComponentOptions), initVirtualHostProps(mpComponentOptions.options)); } const PROP_TYPES = [String, Number, Boolean, Object, Array, null]; function parsePropType(type, defaultValue) { @@ -509,6 +522,22 @@ function findPagePropsData(properties) { }); } return propsData; +} +function initFormField(vm) { + // 同步 form-field 的 name,value 值 + const vueOptions = vm.$options; + if (isArray(vueOptions.behaviors) && + vueOptions.behaviors.includes('uni://form-field')) { + vm.$watch('modelValue', () => { + vm.$scope && + vm.$scope.setData({ + name: vm.name, + value: vm.modelValue, + }); + }, { + immediate: true, + }); + } } function initData(_) { @@ -582,14 +611,14 @@ function initBehaviors(vueOptions) { if (behavior === 'uni://form-field') { if (isArray(vueProps)) { vueProps.push('name'); - vueProps.push('value'); + vueProps.push('modelValue'); } else { vueProps.name = { type: String, default: '', }; - vueProps.value = { + vueProps.modelValue = { type: [String, Number, Boolean, Array, Object, Date], default: '', }; @@ -853,9 +882,10 @@ function initLifetimes$1({ mocks, isPage, initRelation, vueOptions, }) { if (mpType === 'page' && !mpInstance.route && mpInstance.__route__) { mpInstance.route = mpInstance.__route__; } + const props = findPropsData(properties, mpType === 'page'); this.$vm = $createComponent({ type: vueOptions, - props: findPropsData(properties, mpType === 'page'), + props, }, { mpType, mpInstance, @@ -867,6 +897,9 @@ function initLifetimes$1({ mocks, isPage, initRelation, vueOptions, }) { initComponentInstance(instance, options); }, }); + if (mpType === 'component') { + initFormField(this.$vm); + } // 处理父子关系 initRelation(this, relationOptions); } diff --git a/packages/uni-mp-toutiao/src/runtime/componentLifetimes.ts b/packages/uni-mp-toutiao/src/runtime/componentLifetimes.ts index ad50b9f31..2c1c25ded 100644 --- a/packages/uni-mp-toutiao/src/runtime/componentLifetimes.ts +++ b/packages/uni-mp-toutiao/src/runtime/componentLifetimes.ts @@ -1,4 +1,4 @@ -import { ComponentInternalInstance, ComponentPublicInstance } from 'vue' +import type { ComponentInternalInstance, ComponentPublicInstance } from 'vue' // @ts-ignore import { pruneComponentPropsCache } from 'vue' import { @@ -8,6 +8,7 @@ import { CreateLifetimesOptions, initSetRef, findPropsData, + initFormField, } from '@dcloudio/uni-mp-core' import { @@ -47,10 +48,12 @@ export function initLifetimes({ mpInstance.route = mpInstance.__route__ } + const props = findPropsData(properties, mpType === 'page') + this.$vm = $createComponent( { type: vueOptions, - props: findPropsData(properties, mpType === 'page'), + props, }, { mpType, @@ -68,6 +71,10 @@ export function initLifetimes({ } ) as ComponentPublicInstance + if (mpType === 'component') { + initFormField(this.$vm) + } + // 处理父子关系 initRelation(this, relationOptions) } diff --git a/packages/uni-mp-weixin/dist/uni.mp.esm.js b/packages/uni-mp-weixin/dist/uni.mp.esm.js index edae8948a..0217fb3f9 100644 --- a/packages/uni-mp-weixin/dist/uni.mp.esm.js +++ b/packages/uni-mp-weixin/dist/uni.mp.esm.js @@ -386,7 +386,7 @@ const builtInProps = [ // 小程序不能直接定义 $slots 的 props,所以通过 vueSlots 转换到 $slots 'uS', ]; -function initDefaultProps(isBehavior = false) { +function initDefaultProps(options, isBehavior = false) { const properties = {}; if (!isBehavior) { // 均不指定类型,避免微信小程序 property received type-uncompatible value 警告 @@ -412,6 +412,19 @@ function initDefaultProps(isBehavior = false) { }, }; } + if (options.behaviors) { + // wx://form-field + if (options.behaviors.includes('wx://form-field')) { + properties.name = { + type: null, + value: '', + }; + properties.value = { + type: null, + value: '', + }; + } + } return properties; } function initVirtualHostProps(options) { @@ -439,7 +452,7 @@ function initProps(mpComponentOptions) { if (!mpComponentOptions.properties) { mpComponentOptions.properties = {}; } - extend(mpComponentOptions.properties, initDefaultProps(), initVirtualHostProps(mpComponentOptions.options)); + extend(mpComponentOptions.properties, initDefaultProps(mpComponentOptions), initVirtualHostProps(mpComponentOptions.options)); } const PROP_TYPES = [String, Number, Boolean, Object, Array, null]; function parsePropType(type, defaultValue) { @@ -507,6 +520,22 @@ function findPagePropsData(properties) { }); } return propsData; +} +function initFormField(vm) { + // 同步 form-field 的 name,value 值 + const vueOptions = vm.$options; + if (isArray(vueOptions.behaviors) && + vueOptions.behaviors.includes('uni://form-field')) { + vm.$watch('modelValue', () => { + vm.$scope && + vm.$scope.setData({ + name: vm.name, + value: vm.modelValue, + }); + }, { + immediate: true, + }); + } } function initData(_) { @@ -579,14 +608,14 @@ function initBehaviors(vueOptions) { if (behavior === 'uni://form-field') { if (isArray(vueProps)) { vueProps.push('name'); - vueProps.push('value'); + vueProps.push('modelValue'); } else { vueProps.name = { type: String, default: '', }; - vueProps.value = { + vueProps.modelValue = { type: [String, Number, Boolean, Array, Object, Date], default: '', }; @@ -779,6 +808,9 @@ function initLifetimes({ mocks, isPage, initRelation, vueOptions, }) { initComponentInstance(instance, options); }, }); + if (!isMiniProgramPage) { + initFormField(this.$vm); + } }, ready() { // 当组件 props 默认值为 true,初始化时传入 false 会导致 created,ready 触发, 但 attached 不触发 diff --git a/packages/uni-mp-weixin/src/runtime/lifetimes.ts b/packages/uni-mp-weixin/src/runtime/lifetimes.ts index 2cbe2db70..3ab827ee0 100644 --- a/packages/uni-mp-weixin/src/runtime/lifetimes.ts +++ b/packages/uni-mp-weixin/src/runtime/lifetimes.ts @@ -8,6 +8,7 @@ import { CreateComponentOptions, CreateLifetimesOptions, findPropsData, + initFormField, } from '@dcloudio/uni-mp-core' import { @@ -55,6 +56,7 @@ export function initLifetimes({ propsData = this.options as Record } } + this.$vm = $createComponent( { type: vueOptions, @@ -75,6 +77,9 @@ export function initLifetimes({ }, } ) as ComponentPublicInstance + if (!isMiniProgramPage) { + initFormField(this.$vm) + } }, ready(this: MPComponentInstance) { // 当组件 props 默认值为 true,初始化时传入 false 会导致 created,ready 触发, 但 attached 不触发 diff --git a/packages/uni-quickapp-webview/dist/uni.mp.esm.js b/packages/uni-quickapp-webview/dist/uni.mp.esm.js index d742ea8d1..2f06c2823 100644 --- a/packages/uni-quickapp-webview/dist/uni.mp.esm.js +++ b/packages/uni-quickapp-webview/dist/uni.mp.esm.js @@ -378,7 +378,7 @@ const builtInProps = [ // 小程序不能直接定义 $slots 的 props,所以通过 vueSlots 转换到 $slots 'uS', ]; -function initDefaultProps(isBehavior = false) { +function initDefaultProps(options, isBehavior = false) { const properties = {}; if (!isBehavior) { // 均不指定类型,避免微信小程序 property received type-uncompatible value 警告 @@ -404,6 +404,19 @@ function initDefaultProps(isBehavior = false) { }, }; } + if (options.behaviors) { + // wx://form-field + if (options.behaviors.includes('qa://form-field')) { + properties.name = { + type: null, + value: '', + }; + properties.value = { + type: null, + value: '', + }; + } + } return properties; } function initVirtualHostProps(options) { @@ -419,7 +432,7 @@ function initProps(mpComponentOptions) { if (!mpComponentOptions.properties) { mpComponentOptions.properties = {}; } - extend(mpComponentOptions.properties, initDefaultProps(), initVirtualHostProps(mpComponentOptions.options)); + extend(mpComponentOptions.properties, initDefaultProps(mpComponentOptions), initVirtualHostProps(mpComponentOptions.options)); } const PROP_TYPES = [String, Number, Boolean, Object, Array, null]; function parsePropType(type, defaultValue) { @@ -487,6 +500,22 @@ function findPagePropsData(properties) { }); } return propsData; +} +function initFormField(vm) { + // 同步 form-field 的 name,value 值 + const vueOptions = vm.$options; + if (isArray(vueOptions.behaviors) && + vueOptions.behaviors.includes('uni://form-field')) { + vm.$watch('modelValue', () => { + vm.$scope && + vm.$scope.setData({ + name: vm.name, + value: vm.modelValue, + }); + }, { + immediate: true, + }); + } } function initData(_) { @@ -556,14 +585,14 @@ function initBehaviors(vueOptions) { if (behavior === 'uni://form-field') { if (isArray(vueProps)) { vueProps.push('name'); - vueProps.push('value'); + vueProps.push('modelValue'); } else { vueProps.name = { type: String, default: '', }; - vueProps.value = { + vueProps.modelValue = { type: [String, Number, Boolean, Array, Object, Date], default: '', }; @@ -818,9 +847,10 @@ function initLifetimes$1({ mocks, isPage, initRelation, vueOptions, }) { if (mpType === 'page' && !mpInstance.route && mpInstance.__route__) { mpInstance.route = mpInstance.__route__; } + const props = findPropsData(properties, mpType === 'page'); this.$vm = $createComponent({ type: vueOptions, - props: findPropsData(properties, mpType === 'page'), + props, }, { mpType, mpInstance, @@ -832,6 +862,9 @@ function initLifetimes$1({ mocks, isPage, initRelation, vueOptions, }) { initComponentInstance(instance, options); }, }); + if (mpType === 'component') { + initFormField(this.$vm); + } // 处理父子关系 initRelation(this, relationOptions); } diff --git a/packages/uni-stat/lib/uni.plugin.js b/packages/uni-stat/lib/uni.plugin.js index bceda0f8e..771b18e61 100644 --- a/packages/uni-stat/lib/uni.plugin.js +++ b/packages/uni-stat/lib/uni.plugin.js @@ -81,7 +81,7 @@ var index = () => [ else { uniStatLog(`已开启 uni统计${statVersion}.0 版本`); if (statVersion === '2') { - uniStatDeviceLog('1【重要】因 HBuilderX 3.4.9 版本起,uni统计2.0 调整了安卓端 deviceId 获取方式,导致 uni统计2.0 App-Android平台部分统计数据不准确。如使用了HBuilderX 3.4.9 - 3.6.4版本且开通了uni统计2.0的应用,需要使用HBuilderX3.6.7及以上版本重新发布应用并升级 uniAdmin 云函数解决,详见:https://ask.dcloud.net.cn/article/40097'); + uniStatDeviceLog('【重要】因 HBuilderX 3.4.9 版本起,uni统计2.0 调整了安卓端 deviceId 获取方式,导致 uni统计2.0 App-Android平台部分统计数据不准确。如使用了HBuilderX 3.4.9 - 3.6.4版本且开通了uni统计2.0的应用,需要使用HBuilderX3.6.7及以上版本重新发布应用并升级 uniAdmin 云函数解决,详见:https://ask.dcloud.net.cn/article/40097'); } } } diff --git a/packages/uni-stat/src/plugin/index.ts b/packages/uni-stat/src/plugin/index.ts index 7cb3c023f..d8e9a3a77 100644 --- a/packages/uni-stat/src/plugin/index.ts +++ b/packages/uni-stat/src/plugin/index.ts @@ -77,8 +77,10 @@ export default () => [ uniStatLog(M['stat.warn.version']) } else { uniStatLog(`已开启 uni统计${statVersion}.0 版本`) - if(statVersion === '2') { - uniStatDeviceLog('【重要】因 HBuilderX 3.4.9 版本起,uni统计2.0 调整了安卓端 deviceId 获取方式,导致 uni统计2.0 App-Android平台部分统计数据不准确。如使用了HBuilderX 3.4.9 - 3.6.4版本且开通了uni统计2.0的应用,需要使用HBuilderX3.6.7及以上版本重新发布应用并升级 uniAdmin 云函数解决,详见:https://ask.dcloud.net.cn/article/40097') + if (statVersion === '2') { + uniStatDeviceLog( + '【重要】因 HBuilderX 3.4.9 版本起,uni统计2.0 调整了安卓端 deviceId 获取方式,导致 uni统计2.0 App-Android平台部分统计数据不准确。如使用了HBuilderX 3.4.9 - 3.6.4版本且开通了uni统计2.0的应用,需要使用HBuilderX3.6.7及以上版本重新发布应用并升级 uniAdmin 云函数解决,详见:https://ask.dcloud.net.cn/article/40097' + ) } } } @@ -89,8 +91,10 @@ export default () => [ uniStatLog( M['stat.warn.tip'].replace('{version}', `${statVersion}.0`) ) - if(statVersion === '2') { - uniStatDeviceLog('【重要】因 HBuilderX 3.4.9 版本起,uni统计2.0 调整了安卓端 deviceId 获取方式,导致 uni统计2.0 App-Android平台部分统计数据不准确。如使用了HBuilderX 3.4.9 - 3.6.4版本且开通了uni统计2.0的应用,需要使用HBuilderX3.6.7及以上版本重新发布应用并升级 uniAdmin 云函数解决,详见:https://ask.dcloud.net.cn/article/40097') + if (statVersion === '2') { + uniStatDeviceLog( + '【重要】因 HBuilderX 3.4.9 版本起,uni统计2.0 调整了安卓端 deviceId 获取方式,导致 uni统计2.0 App-Android平台部分统计数据不准确。如使用了HBuilderX 3.4.9 - 3.6.4版本且开通了uni统计2.0的应用,需要使用HBuilderX3.6.7及以上版本重新发布应用并升级 uniAdmin 云函数解决,详见:https://ask.dcloud.net.cn/article/40097' + ) } } } -- GitLab