Skip to content

Commit 5b1708d

Browse files
authored
Merge pull request #2358 from didi/feat-props-default-value
feat: web&rn properties 默认值与微信拉齐
2 parents 70309ed + afc0543 commit 5b1708d

File tree

3 files changed

+31
-5
lines changed

3 files changed

+31
-5
lines changed

packages/core/src/convertor/wxToWeb.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import {
66
diffAndCloneA,
77
error,
88
hasOwn,
9-
isDev
9+
isDev,
10+
getDefaultValueByType
1011
} from '@mpxjs/utils'
1112
import { implemented } from '../core/implement'
1213

@@ -56,6 +57,12 @@ export default {
5657
return diffAndCloneA(prop.value).clone
5758
}
5859
: prop.value
60+
} else {
61+
// 没有显式设置value时,根据type自动添加默认值,与微信小程序原生行为保持一致
62+
const defaultValue = getDefaultValueByType(prop.type, 'web')
63+
if (defaultValue !== undefined) {
64+
newProp.default = defaultValue
65+
}
5966
}
6067
props[key] = newProp
6168
} else {

packages/core/src/platform/patch/getDefaultOptions.ios.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as ReactNative from 'react-native'
33
import { ReactiveEffect } from '../../observer/effect'
44
import { watch } from '../../observer/watch'
55
import { del, reactive, set } from '../../observer/reactive'
6-
import { hasOwn, isFunction, noop, isObject, isArray, getByPath, collectDataset, hump2dash, dash2hump, callWithErrorHandling, wrapMethodsWithErrorHandling, error, setFocusedNavigation } from '@mpxjs/utils'
6+
import { hasOwn, isFunction, noop, isObject, isArray, getByPath, collectDataset, hump2dash, dash2hump, callWithErrorHandling, wrapMethodsWithErrorHandling, error, setFocusedNavigation, getDefaultValueByType } from '@mpxjs/utils'
77
import MpxProxy from '../../core/proxy'
88
import { BEFOREUPDATE, ONLOAD, UPDATED, ONSHOW, ONHIDE, ONRESIZE, REACTHOOKSEXEC } from '../../core/innerLifecycle'
99
import mergeOptions from '../../core/mergeOptions'
@@ -172,8 +172,8 @@ const instanceProto = {
172172
type: field
173173
}
174174
}
175-
// 处理props默认值
176-
propsData[key] = field.value
175+
// 处理props默认值,没有显式设置value时根据type获取默认值,与微信小程序原生行为保持一致
176+
propsData[key] = hasOwn(field, 'value') ? field.value : getDefaultValueByType(field.type)
177177
}
178178
}
179179
})

packages/utils/src/base.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,24 @@ function hasChanged (value, oldValue) {
135135
return !Object.is(value, oldValue)
136136
}
137137

138+
// 根据类型获取小程序默认值,与微信小程序原生行为保持一致
139+
function getDefaultValueByType (type, mode) {
140+
switch (type) {
141+
case String:
142+
return ''
143+
case Number:
144+
return 0
145+
case Boolean:
146+
return false
147+
case Object:
148+
return null
149+
case Array:
150+
return mode === 'web' ? () => [] : []
151+
default:
152+
return undefined
153+
}
154+
}
155+
138156
export {
139157
hasProto,
140158
noop,
@@ -156,5 +174,6 @@ export {
156174
def,
157175
hasChanged,
158176
forEach,
159-
cached
177+
cached,
178+
getDefaultValueByType
160179
}

0 commit comments

Comments
 (0)