Skip to content

Commit

Permalink
fix(runtime-core): should resolve value instead of delete for dynamic…
Browse files Browse the repository at this point in the history
… props with options
  • Loading branch information
yyx990803 committed Apr 14, 2020
1 parent 0869443 commit c80b857
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 21 deletions.
10 changes: 9 additions & 1 deletion packages/runtime-core/__tests__/componentProps.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,17 @@ describe('component props', () => {
expect(proxy.foo).toBe(2)
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.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 () => {
Expand Down
48 changes: 28 additions & 20 deletions packages/runtime-core/src/componentProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,16 @@ export function updateProps(
// and converted to camelCase (#955)
((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) {
Expand Down Expand Up @@ -250,25 +259,24 @@ function resolvePropValue(
key: string,
value: unknown
) {
let opt = options[key]
if (opt == null) {
return value
}
const hasDefault = hasOwn(opt, 'default')
// default values
if (hasDefault && value === undefined) {
const defaultValue = opt.default
value = isFunction(defaultValue) ? defaultValue() : defaultValue
}
// boolean casting
if (opt[BooleanFlags.shouldCast]) {
if (!hasOwn(props, key) && !hasDefault) {
value = false
} else if (
opt[BooleanFlags.shouldCastTrue] &&
(value === '' || value === hyphenate(key))
) {
value = true
const opt = options[key]
if (opt != null) {
const hasDefault = hasOwn(opt, 'default')
// default values
if (hasDefault && value === undefined) {
const defaultValue = opt.default
value = isFunction(defaultValue) ? defaultValue() : defaultValue
}
// boolean casting
if (opt[BooleanFlags.shouldCast]) {
if (!hasOwn(props, key) && !hasDefault) {
value = false
} else if (
opt[BooleanFlags.shouldCastTrue] &&
(value === '' || value === hyphenate(key))
) {
value = true
}
}
}
return value
Expand Down

0 comments on commit c80b857

Please sign in to comment.