fix(runtime-core): should resolve value instead of delete for dynamic props with options

This commit is contained in:
Evan You 2020-04-14 16:17:35 -04:00
parent 0869443d01
commit c80b857eb5
2 changed files with 37 additions and 21 deletions

View File

@ -175,9 +175,17 @@ describe('component props', () => {
expect(proxy.foo).toBe(2) expect(proxy.foo).toBe(2)
expect(proxy.bar).toEqual({ a: 1 }) expect(proxy.bar).toEqual({ a: 1 })
render(h(Comp, { foo: undefined, bar: { b: 2 } }), root) render(h(Comp, { bar: { b: 2 } }), root)
expect(proxy.foo).toBe(1) expect(proxy.foo).toBe(1)
expect(proxy.bar).toEqual({ b: 2 }) expect(proxy.bar).toEqual({ b: 2 })
render(h(Comp, { foo: 3, bar: { b: 3 } }), root)
expect(proxy.foo).toBe(3)
expect(proxy.bar).toEqual({ b: 3 })
render(h(Comp, { bar: { b: 4 } }), root)
expect(proxy.foo).toBe(1)
expect(proxy.bar).toEqual({ b: 4 })
}) })
test('optimized props updates', async () => { test('optimized props updates', async () => {

View File

@ -186,7 +186,16 @@ export function updateProps(
// and converted to camelCase (#955) // and converted to camelCase (#955)
((kebabKey = hyphenate(key)) === key || !hasOwn(rawProps, kebabKey))) ((kebabKey = hyphenate(key)) === key || !hasOwn(rawProps, kebabKey)))
) { ) {
delete props[key] if (options) {
props[key] = resolvePropValue(
options,
rawProps || EMPTY_OBJ,
key,
undefined
)
} else {
delete props[key]
}
} }
} }
for (const key in attrs) { for (const key in attrs) {
@ -250,25 +259,24 @@ function resolvePropValue(
key: string, key: string,
value: unknown value: unknown
) { ) {
let opt = options[key] const opt = options[key]
if (opt == null) { if (opt != null) {
return value const hasDefault = hasOwn(opt, 'default')
} // default values
const hasDefault = hasOwn(opt, 'default') if (hasDefault && value === undefined) {
// default values const defaultValue = opt.default
if (hasDefault && value === undefined) { value = isFunction(defaultValue) ? defaultValue() : defaultValue
const defaultValue = opt.default }
value = isFunction(defaultValue) ? defaultValue() : defaultValue // boolean casting
} if (opt[BooleanFlags.shouldCast]) {
// boolean casting if (!hasOwn(props, key) && !hasDefault) {
if (opt[BooleanFlags.shouldCast]) { value = false
if (!hasOwn(props, key) && !hasDefault) { } else if (
value = false opt[BooleanFlags.shouldCastTrue] &&
} else if ( (value === '' || value === hyphenate(key))
opt[BooleanFlags.shouldCastTrue] && ) {
(value === '' || value === hyphenate(key)) value = true
) { }
value = true
} }
} }
return value return value