Skip to content

Commit 63689ed

Browse files
committed
fix(custom-element): reflect prop default value on custom element
close #9006 close #10537
1 parent bcb9a70 commit 63689ed

File tree

4 files changed

+37
-10
lines changed

4 files changed

+37
-10
lines changed

Diff for: packages/runtime-core/src/component.ts

+9
Original file line numberDiff line numberDiff line change
@@ -1247,4 +1247,13 @@ export interface ComponentCustomElementInterface {
12471247
* @internal
12481248
*/
12491249
_removeChildStyle(type: ConcreteComponent): void
1250+
/**
1251+
* @internal
1252+
*/
1253+
_setProp(
1254+
key: string,
1255+
val: any,
1256+
shouldReflect?: boolean,
1257+
shouldUpdate?: boolean,
1258+
): void
12501259
}

Diff for: packages/runtime-core/src/componentProps.ts

+4
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,10 @@ function resolvePropValue(
480480
} else {
481481
value = defaultValue
482482
}
483+
// #9006 reflect default value on custom element
484+
if (instance.ce) {
485+
instance.ce._setProp(key, value)
486+
}
483487
}
484488
// boolean casting
485489
if (opt[BooleanFlags.shouldCast]) {

Diff for: packages/runtime-dom/__tests__/customElement.spec.ts

+20-1
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ describe('defineCustomElement', () => {
338338
expect(el.shadowRoot!.innerHTML).toMatchInlineSnapshot('"<div>foo</div>"')
339339
})
340340

341-
// # 5793
341+
// #5793
342342
test('set number value in dom property', () => {
343343
const E = defineCustomElement({
344344
props: {
@@ -357,6 +357,25 @@ describe('defineCustomElement', () => {
357357
expect(el.shadowRoot.innerHTML).toBe('max age: 50/type: number')
358358
})
359359

360+
// #9006
361+
test('should reflect default value', () => {
362+
const E = defineCustomElement({
363+
props: {
364+
value: {
365+
type: String,
366+
default: 'hi',
367+
},
368+
},
369+
render() {
370+
return this.value
371+
},
372+
})
373+
customElements.define('my-el-default-val', E)
374+
container.innerHTML = `<my-el-default-val></my-el-default-val>`
375+
const e = container.childNodes[0] as any
376+
expect(e.value).toBe('hi')
377+
})
378+
360379
test('support direct setup function syntax with extra options', () => {
361380
const E = defineCustomElement(
362381
props => {

Diff for: packages/runtime-dom/src/apiCustomElement.ts

+4-9
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ export class VueElement
395395
// check if there are props set pre-upgrade or connect
396396
for (const key of Object.keys(this)) {
397397
if (key[0] !== '_' && declaredPropKeys.includes(key)) {
398-
this._setProp(key, this[key as keyof this], true, false)
398+
this._setProp(key, this[key as keyof this])
399399
}
400400
}
401401

@@ -406,7 +406,7 @@ export class VueElement
406406
return this._getProp(key)
407407
},
408408
set(val) {
409-
this._setProp(key, val)
409+
this._setProp(key, val, true, true)
410410
},
411411
})
412412
}
@@ -435,7 +435,7 @@ export class VueElement
435435
if (this._numberProps && this._numberProps[camelKey]) {
436436
value = toNumber(value)
437437
}
438-
this._setProp(camelKey, value, false)
438+
this._setProp(camelKey, value, false, true)
439439
}
440440

441441
/**
@@ -448,12 +448,7 @@ export class VueElement
448448
/**
449449
* @internal
450450
*/
451-
protected _setProp(
452-
key: string,
453-
val: any,
454-
shouldReflect = true,
455-
shouldUpdate = true,
456-
) {
451+
_setProp(key: string, val: any, shouldReflect = true, shouldUpdate = false) {
457452
if (val !== this._props[key]) {
458453
this._props[key] = val
459454
if (shouldUpdate && this._instance) {

0 commit comments

Comments
 (0)