feat(runtime-dom): support passing initial props to custom element constructor
This commit is contained in:
		
							parent
							
								
									7a7e1d8e9f
								
							
						
					
					
						commit
						5b76843b69
					
				@ -17,7 +17,15 @@ describe('defineCustomElement', () => {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
  describe('mounting/unmount', () => {
 | 
					  describe('mounting/unmount', () => {
 | 
				
			||||||
    const E = defineCustomElement({
 | 
					    const E = defineCustomElement({
 | 
				
			||||||
      render: () => h('div', 'hello')
 | 
					      props: {
 | 
				
			||||||
 | 
					        msg: {
 | 
				
			||||||
 | 
					          type: String,
 | 
				
			||||||
 | 
					          default: 'hello'
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					      },
 | 
				
			||||||
 | 
					      render() {
 | 
				
			||||||
 | 
					        return h('div', this.msg)
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
    })
 | 
					    })
 | 
				
			||||||
    customElements.define('my-element', E)
 | 
					    customElements.define('my-element', E)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -30,13 +38,13 @@ describe('defineCustomElement', () => {
 | 
				
			|||||||
    })
 | 
					    })
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    test('should work w/ manual instantiation', () => {
 | 
					    test('should work w/ manual instantiation', () => {
 | 
				
			||||||
      const e = new E()
 | 
					      const e = new E({ msg: 'inline' })
 | 
				
			||||||
      // should lazy init
 | 
					      // should lazy init
 | 
				
			||||||
      expect(e._instance).toBe(null)
 | 
					      expect(e._instance).toBe(null)
 | 
				
			||||||
      // should initialize on connect
 | 
					      // should initialize on connect
 | 
				
			||||||
      container.appendChild(e)
 | 
					      container.appendChild(e)
 | 
				
			||||||
      expect(e._instance).toBeTruthy()
 | 
					      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 () => {
 | 
					    test('should unmount on remove', async () => {
 | 
				
			||||||
 | 
				
			|||||||
@ -23,8 +23,8 @@ import {
 | 
				
			|||||||
import { camelize, extend, hyphenate, isArray, toNumber } from '@vue/shared'
 | 
					import { camelize, extend, hyphenate, isArray, toNumber } from '@vue/shared'
 | 
				
			||||||
import { hydrate, render } from '.'
 | 
					import { hydrate, render } from '.'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type VueElementConstructor<P = {}> = {
 | 
					export type VueElementConstructor<P = {}> = {
 | 
				
			||||||
  new (): VueElement & P
 | 
					  new (initialProps?: Record<string, any>): VueElement & P
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// defineCustomElement provides the same type inference as defineComponent
 | 
					// defineCustomElement provides the same type inference as defineComponent
 | 
				
			||||||
@ -134,8 +134,8 @@ export function defineCustomElement(
 | 
				
			|||||||
    static get observedAttributes() {
 | 
					    static get observedAttributes() {
 | 
				
			||||||
      return attrKeys
 | 
					      return attrKeys
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    constructor() {
 | 
					    constructor(initialProps?: Record<string, any>) {
 | 
				
			||||||
      super(Comp, attrKeys, propKeys, hydate)
 | 
					      super(Comp, initialProps, attrKeys, propKeys, hydate)
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -163,10 +163,6 @@ const BaseClass = (
 | 
				
			|||||||
) as typeof HTMLElement
 | 
					) as typeof HTMLElement
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export class VueElement extends BaseClass {
 | 
					export class VueElement extends BaseClass {
 | 
				
			||||||
  /**
 | 
					 | 
				
			||||||
   * @internal
 | 
					 | 
				
			||||||
   */
 | 
					 | 
				
			||||||
  _props: Record<string, any> = {}
 | 
					 | 
				
			||||||
  /**
 | 
					  /**
 | 
				
			||||||
   * @internal
 | 
					   * @internal
 | 
				
			||||||
   */
 | 
					   */
 | 
				
			||||||
@ -178,6 +174,7 @@ export class VueElement extends BaseClass {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
  constructor(
 | 
					  constructor(
 | 
				
			||||||
    private _def: ComponentOptions & { styles?: string[] },
 | 
					    private _def: ComponentOptions & { styles?: string[] },
 | 
				
			||||||
 | 
					    private _props: Record<string, any> = {},
 | 
				
			||||||
    private _attrKeys: string[],
 | 
					    private _attrKeys: string[],
 | 
				
			||||||
    private _propKeys: string[],
 | 
					    private _propKeys: string[],
 | 
				
			||||||
    hydrate?: RootHydrateFunction
 | 
					    hydrate?: RootHydrateFunction
 | 
				
			||||||
 | 
				
			|||||||
@ -198,7 +198,8 @@ function normalizeContainer(
 | 
				
			|||||||
export {
 | 
					export {
 | 
				
			||||||
  defineCustomElement,
 | 
					  defineCustomElement,
 | 
				
			||||||
  defineSSRCustomElement,
 | 
					  defineSSRCustomElement,
 | 
				
			||||||
  VueElement
 | 
					  VueElement,
 | 
				
			||||||
 | 
					  VueElementConstructor
 | 
				
			||||||
} from './apiCustomElement'
 | 
					} from './apiCustomElement'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// SFC CSS utilities
 | 
					// SFC CSS utilities
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user