Skip to content

Commit 7cbf684

Browse files
committed
fix(runtime-core): fix kebab-case props update
fix #955
1 parent 27b5c71 commit 7cbf684

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

packages/runtime-core/__tests__/componentProps.spec.ts

+15-8
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ describe('component props', () => {
2020
let proxy: any
2121

2222
const Comp = defineComponent({
23-
props: ['foo'],
23+
props: ['fooBar'],
2424
render() {
2525
props = this.$props
2626
attrs = this.$attrs
@@ -29,18 +29,25 @@ describe('component props', () => {
2929
})
3030

3131
const root = nodeOps.createElement('div')
32-
render(h(Comp, { foo: 1, bar: 2 }), root)
33-
expect(proxy.foo).toBe(1)
34-
expect(props).toEqual({ foo: 1 })
32+
render(h(Comp, { fooBar: 1, bar: 2 }), root)
33+
expect(proxy.fooBar).toBe(1)
34+
expect(props).toEqual({ fooBar: 1 })
3535
expect(attrs).toEqual({ bar: 2 })
3636

37-
render(h(Comp, { foo: 2, bar: 3, baz: 4 }), root)
38-
expect(proxy.foo).toBe(2)
39-
expect(props).toEqual({ foo: 2 })
37+
// test passing kebab-case and resolving to camelCase
38+
render(h(Comp, { 'foo-bar': 2, bar: 3, baz: 4 }), root)
39+
expect(proxy.fooBar).toBe(2)
40+
expect(props).toEqual({ fooBar: 2 })
41+
expect(attrs).toEqual({ bar: 3, baz: 4 })
42+
43+
// test updating kebab-case should not delete it (#955)
44+
render(h(Comp, { 'foo-bar': 3, bar: 3, baz: 4 }), root)
45+
expect(proxy.fooBar).toBe(3)
46+
expect(props).toEqual({ fooBar: 3 })
4047
expect(attrs).toEqual({ bar: 3, baz: 4 })
4148

4249
render(h(Comp, { qux: 5 }), root)
43-
expect(proxy.foo).toBeUndefined()
50+
expect(proxy.fooBar).toBeUndefined()
4451
expect(props).toEqual({})
4552
expect(attrs).toEqual({ qux: 5 })
4653
})

packages/runtime-core/src/componentProps.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,15 @@ export function updateProps(
177177
setFullProps(instance, rawProps, props, attrs)
178178
// in case of dynamic props, check if we need to delete keys from
179179
// the props object
180+
let kebabKey: string
180181
for (const key in rawCurrentProps) {
181-
if (!rawProps || !hasOwn(rawProps, key)) {
182+
if (
183+
!rawProps ||
184+
(!hasOwn(rawProps, key) &&
185+
// it's possible the original props was passed in as kebab-case
186+
// and converted to camelCase (#955)
187+
((kebabKey = hyphenate(key)) === key || !hasOwn(rawProps, kebabKey)))
188+
) {
182189
delete props[key]
183190
}
184191
}

0 commit comments

Comments
 (0)