mirror of https://github.com/vuejs/core.git
fix(custom-element): support same direct setup function signature in defineCustomElement
close #11116
This commit is contained in:
parent
3e89a0da21
commit
7c8b12620a
|
@ -338,6 +338,23 @@ describe('defineCustomElement', () => {
|
||||||
expect(el.maxAge).toBe(50)
|
expect(el.maxAge).toBe(50)
|
||||||
expect(el.shadowRoot.innerHTML).toBe('max age: 50/type: number')
|
expect(el.shadowRoot.innerHTML).toBe('max age: 50/type: number')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test('support direct setup function syntax with extra options', () => {
|
||||||
|
const E = defineCustomElement(
|
||||||
|
props => {
|
||||||
|
return () => props.text
|
||||||
|
},
|
||||||
|
{
|
||||||
|
props: {
|
||||||
|
text: String,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
)
|
||||||
|
customElements.define('my-el-setup-with-props', E)
|
||||||
|
container.innerHTML = `<my-el-setup-with-props text="hello"></my-el-setup-with-props>`
|
||||||
|
const e = container.childNodes[0] as VueElement
|
||||||
|
expect(e.shadowRoot!.innerHTML).toBe('hello')
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('attrs', () => {
|
describe('attrs', () => {
|
||||||
|
|
|
@ -35,10 +35,16 @@ export type VueElementConstructor<P = {}> = {
|
||||||
|
|
||||||
// overload 1: direct setup function
|
// overload 1: direct setup function
|
||||||
export function defineCustomElement<Props, RawBindings = object>(
|
export function defineCustomElement<Props, RawBindings = object>(
|
||||||
setup: (
|
setup: (props: Props, ctx: SetupContext) => RawBindings | RenderFunction,
|
||||||
props: Readonly<Props>,
|
options?: Pick<ComponentOptions, 'name' | 'inheritAttrs' | 'emits'> & {
|
||||||
ctx: SetupContext,
|
props?: (keyof Props)[]
|
||||||
) => RawBindings | RenderFunction,
|
},
|
||||||
|
): VueElementConstructor<Props>
|
||||||
|
export function defineCustomElement<Props, RawBindings = object>(
|
||||||
|
setup: (props: Props, ctx: SetupContext) => RawBindings | RenderFunction,
|
||||||
|
options?: Pick<ComponentOptions, 'name' | 'inheritAttrs' | 'emits'> & {
|
||||||
|
props?: ComponentObjectPropsOptions<Props>
|
||||||
|
},
|
||||||
): VueElementConstructor<Props>
|
): VueElementConstructor<Props>
|
||||||
|
|
||||||
// overload 2: object format with no props
|
// overload 2: object format with no props
|
||||||
|
@ -143,9 +149,13 @@ export function defineCustomElement<P>(
|
||||||
/*! #__NO_SIDE_EFFECTS__ */
|
/*! #__NO_SIDE_EFFECTS__ */
|
||||||
export function defineCustomElement(
|
export function defineCustomElement(
|
||||||
options: any,
|
options: any,
|
||||||
|
extraOptions?: ComponentOptions,
|
||||||
|
/**
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
hydrate?: RootHydrateFunction,
|
hydrate?: RootHydrateFunction,
|
||||||
): VueElementConstructor {
|
): VueElementConstructor {
|
||||||
const Comp = defineComponent(options) as any
|
const Comp = defineComponent(options, extraOptions) as any
|
||||||
class VueCustomElement extends VueElement {
|
class VueCustomElement extends VueElement {
|
||||||
static def = Comp
|
static def = Comp
|
||||||
constructor(initialProps?: Record<string, any>) {
|
constructor(initialProps?: Record<string, any>) {
|
||||||
|
@ -157,9 +167,12 @@ export function defineCustomElement(
|
||||||
}
|
}
|
||||||
|
|
||||||
/*! #__NO_SIDE_EFFECTS__ */
|
/*! #__NO_SIDE_EFFECTS__ */
|
||||||
export const defineSSRCustomElement = ((options: any) => {
|
export const defineSSRCustomElement = ((
|
||||||
|
options: any,
|
||||||
|
extraOptions?: ComponentOptions,
|
||||||
|
) => {
|
||||||
// @ts-expect-error
|
// @ts-expect-error
|
||||||
return defineCustomElement(options, hydrate)
|
return defineCustomElement(options, extraOptions, hydrate)
|
||||||
}) as typeof defineCustomElement
|
}) as typeof defineCustomElement
|
||||||
|
|
||||||
const BaseClass = (
|
const BaseClass = (
|
||||||
|
|
Loading…
Reference in New Issue