Skip to content

Commit

Permalink
fix(custom-element): support same direct setup function signature in …
Browse files Browse the repository at this point in the history
…defineCustomElement

close #11116
  • Loading branch information
yyx990803 committed Jun 14, 2024
1 parent 3e89a0d commit 7c8b126
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
17 changes: 17 additions & 0 deletions packages/runtime-dom/__tests__/customElement.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,23 @@ describe('defineCustomElement', () => {
expect(el.maxAge).toBe(50)
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', () => {
Expand Down
27 changes: 20 additions & 7 deletions packages/runtime-dom/src/apiCustomElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,16 @@ export type VueElementConstructor<P = {}> = {

// overload 1: direct setup function
export function defineCustomElement<Props, RawBindings = object>(
setup: (
props: Readonly<Props>,
ctx: SetupContext,
) => RawBindings | RenderFunction,
setup: (props: Props, ctx: SetupContext) => RawBindings | RenderFunction,
options?: Pick<ComponentOptions, 'name' | 'inheritAttrs' | 'emits'> & {
props?: (keyof Props)[]
},
): VueElementConstructor<Props>
export function defineCustomElement<Props, RawBindings = object>(
setup: (props: Props, ctx: SetupContext) => RawBindings | RenderFunction,
options?: Pick<ComponentOptions, 'name' | 'inheritAttrs' | 'emits'> & {
props?: ComponentObjectPropsOptions<Props>

Check failure on line 46 in packages/runtime-dom/src/apiCustomElement.ts

View workflow job for this annotation

GitHub Actions / lint-and-test-dts

Cannot find name 'ComponentObjectPropsOptions'. Did you mean 'ComponentPropsOptions'?

Check failure on line 46 in packages/runtime-dom/src/apiCustomElement.ts

View workflow job for this annotation

GitHub Actions / lint-and-test-dts

Parameter 'options' of exported function has or is using private name 'ComponentObjectPropsOptions'.
},
): VueElementConstructor<Props>

// overload 2: object format with no props
Expand Down Expand Up @@ -143,9 +149,13 @@ export function defineCustomElement<P>(
/*! #__NO_SIDE_EFFECTS__ */
export function defineCustomElement(
options: any,
extraOptions?: ComponentOptions,
/**
* @internal
*/
hydrate?: RootHydrateFunction,
): VueElementConstructor {
const Comp = defineComponent(options) as any
const Comp = defineComponent(options, extraOptions) as any
class VueCustomElement extends VueElement {
static def = Comp
constructor(initialProps?: Record<string, any>) {
Expand All @@ -157,9 +167,12 @@ export function defineCustomElement(
}

/*! #__NO_SIDE_EFFECTS__ */
export const defineSSRCustomElement = ((options: any) => {
export const defineSSRCustomElement = ((
options: any,
extraOptions?: ComponentOptions,
) => {
// @ts-expect-error
return defineCustomElement(options, hydrate)
return defineCustomElement(options, extraOptions, hydrate)
}) as typeof defineCustomElement

const BaseClass = (
Expand Down

0 comments on commit 7c8b126

Please sign in to comment.