]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
feat(runtime-dom): support passing initial props to custom element constructor
authorEvan You <yyx990803@gmail.com>
Thu, 22 Jul 2021 22:19:54 +0000 (18:19 -0400)
committerEvan You <yyx990803@gmail.com>
Thu, 22 Jul 2021 22:19:58 +0000 (18:19 -0400)
packages/runtime-dom/__tests__/customElement.spec.ts
packages/runtime-dom/src/apiCustomElement.ts
packages/runtime-dom/src/index.ts

index 1729129b1bb28786cf95cd4c0e03e5193695f33f..1a4437ed360e0b082829589c18f8324e04967a4f 100644 (file)
@@ -17,7 +17,15 @@ describe('defineCustomElement', () => {
 
   describe('mounting/unmount', () => {
     const E = defineCustomElement({
-      render: () => h('div', 'hello')
+      props: {
+        msg: {
+          type: String,
+          default: 'hello'
+        }
+      },
+      render() {
+        return h('div', this.msg)
+      }
     })
     customElements.define('my-element', E)
 
@@ -30,13 +38,13 @@ describe('defineCustomElement', () => {
     })
 
     test('should work w/ manual instantiation', () => {
-      const e = new E()
+      const e = new E({ msg: 'inline' })
       // should lazy init
       expect(e._instance).toBe(null)
       // should initialize on connect
       container.appendChild(e)
       expect(e._instance).toBeTruthy()
-      expect(e.shadowRoot!.innerHTML).toBe(`<div>hello</div>`)
+      expect(e.shadowRoot!.innerHTML).toBe(`<div>inline</div>`)
     })
 
     test('should unmount on remove', async () => {
index 31302e13b0643f206f3c75fe75a660b506787796..fe3f594f69e469bbab37e3fd231ae0c7fa89f566 100644 (file)
@@ -23,8 +23,8 @@ import {
 import { camelize, extend, hyphenate, isArray, toNumber } from '@vue/shared'
 import { hydrate, render } from '.'
 
-type VueElementConstructor<P = {}> = {
-  new (): VueElement & P
+export type VueElementConstructor<P = {}> = {
+  new (initialProps?: Record<string, any>): VueElement & P
 }
 
 // defineCustomElement provides the same type inference as defineComponent
@@ -134,8 +134,8 @@ export function defineCustomElement(
     static get observedAttributes() {
       return attrKeys
     }
-    constructor() {
-      super(Comp, attrKeys, propKeys, hydate)
+    constructor(initialProps?: Record<string, any>) {
+      super(Comp, initialProps, attrKeys, propKeys, hydate)
     }
   }
 
@@ -163,10 +163,6 @@ const BaseClass = (
 ) as typeof HTMLElement
 
 export class VueElement extends BaseClass {
-  /**
-   * @internal
-   */
-  _props: Record<string, any> = {}
   /**
    * @internal
    */
@@ -178,6 +174,7 @@ export class VueElement extends BaseClass {
 
   constructor(
     private _def: ComponentOptions & { styles?: string[] },
+    private _props: Record<string, any> = {},
     private _attrKeys: string[],
     private _propKeys: string[],
     hydrate?: RootHydrateFunction
index 8a3012c20d1c4ddef8a61e5151c3aa0932a45505..f6f0cf322c4f611132d0b7ed7ee3488f8762ecd1 100644 (file)
@@ -198,7 +198,8 @@ function normalizeContainer(
 export {
   defineCustomElement,
   defineSSRCustomElement,
-  VueElement
+  VueElement,
+  VueElementConstructor
 } from './apiCustomElement'
 
 // SFC CSS utilities