2019-09-06 15:19:22 +00:00
|
|
|
import {
|
|
|
|
ComputedOptions,
|
|
|
|
MethodOptions,
|
|
|
|
ComponentOptionsWithoutProps,
|
|
|
|
ComponentOptionsWithArrayProps,
|
2019-10-08 13:26:09 +00:00
|
|
|
ComponentOptionsWithObjectProps
|
2019-10-02 14:03:43 +00:00
|
|
|
} from './apiOptions'
|
2019-10-21 17:44:01 +00:00
|
|
|
import { SetupContext, RenderFunction } from './component'
|
2019-10-02 14:03:43 +00:00
|
|
|
import { ComponentPublicInstance } from './componentProxy'
|
2019-09-06 15:19:22 +00:00
|
|
|
import { ExtractPropTypes } from './componentProps'
|
|
|
|
import { isFunction } from '@vue/shared'
|
2019-10-31 16:43:05 +00:00
|
|
|
import { Ref } from '@vue/reactivity'
|
|
|
|
|
|
|
|
interface BaseProps {
|
|
|
|
[key: string]: any
|
|
|
|
key?: string | number
|
|
|
|
ref?: string | Ref | Function
|
|
|
|
}
|
2019-09-06 15:19:22 +00:00
|
|
|
|
|
|
|
// overload 1: direct setup function
|
|
|
|
// (uses user defined props interface)
|
2019-10-31 16:43:05 +00:00
|
|
|
// __isConstructor: true is a type-only differentiator to avoid returned
|
|
|
|
// constructor type from being matched as an options object in h()
|
2019-10-18 18:58:15 +00:00
|
|
|
export function createComponent<Props, RawBindings = object>(
|
2019-10-21 17:44:01 +00:00
|
|
|
setup: (props: Props, ctx: SetupContext) => RawBindings | RenderFunction
|
2019-10-18 18:58:15 +00:00
|
|
|
): {
|
2019-10-31 16:43:05 +00:00
|
|
|
__isConstructor: true
|
|
|
|
new (): ComponentPublicInstance<
|
|
|
|
Props,
|
|
|
|
RawBindings,
|
|
|
|
{},
|
|
|
|
{},
|
|
|
|
{},
|
|
|
|
// public props
|
|
|
|
BaseProps & Props
|
|
|
|
>
|
2019-10-18 18:58:15 +00:00
|
|
|
}
|
2019-09-06 15:19:22 +00:00
|
|
|
|
|
|
|
// overload 2: object format with no props
|
|
|
|
// (uses user defined props interface)
|
|
|
|
// return type is for Vetur and TSX support
|
|
|
|
export function createComponent<
|
|
|
|
Props,
|
|
|
|
RawBindings,
|
|
|
|
D,
|
|
|
|
C extends ComputedOptions = {},
|
|
|
|
M extends MethodOptions = {}
|
|
|
|
>(
|
|
|
|
options: ComponentOptionsWithoutProps<Props, RawBindings, D, C, M>
|
|
|
|
): {
|
2019-10-31 16:43:05 +00:00
|
|
|
__isConstructor: true
|
|
|
|
new (): ComponentPublicInstance<
|
|
|
|
Props,
|
|
|
|
RawBindings,
|
|
|
|
D,
|
|
|
|
C,
|
|
|
|
M,
|
|
|
|
BaseProps & Props
|
|
|
|
>
|
2019-09-06 15:19:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// overload 3: object format with array props declaration
|
2019-10-31 16:43:05 +00:00
|
|
|
// props inferred as { [key in PropNames]?: any }
|
2019-09-06 15:19:22 +00:00
|
|
|
// return type is for Vetur and TSX support
|
|
|
|
export function createComponent<
|
|
|
|
PropNames extends string,
|
|
|
|
RawBindings,
|
|
|
|
D,
|
|
|
|
C extends ComputedOptions = {},
|
|
|
|
M extends MethodOptions = {}
|
|
|
|
>(
|
|
|
|
options: ComponentOptionsWithArrayProps<PropNames, RawBindings, D, C, M>
|
|
|
|
): {
|
2019-10-31 16:43:05 +00:00
|
|
|
__isConstructor: true
|
|
|
|
// array props technically doesn't place any contraints on props in TSX
|
|
|
|
new (): ComponentPublicInstance<BaseProps, RawBindings, D, C, M>
|
2019-09-06 15:19:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// overload 4: object format with object props declaration
|
|
|
|
// see `ExtractPropTypes` in ./componentProps.ts
|
|
|
|
export function createComponent<
|
|
|
|
PropsOptions,
|
|
|
|
RawBindings,
|
|
|
|
D,
|
|
|
|
C extends ComputedOptions = {},
|
|
|
|
M extends MethodOptions = {}
|
|
|
|
>(
|
2019-10-08 13:26:09 +00:00
|
|
|
options: ComponentOptionsWithObjectProps<PropsOptions, RawBindings, D, C, M>
|
2019-09-06 15:19:22 +00:00
|
|
|
): {
|
2019-10-31 16:43:05 +00:00
|
|
|
__isConstructor: true
|
2019-09-06 15:19:22 +00:00
|
|
|
// for Vetur and TSX support
|
2019-09-06 16:58:31 +00:00
|
|
|
new (): ComponentPublicInstance<
|
2019-10-31 16:43:05 +00:00
|
|
|
ExtractPropTypes<PropsOptions, false>,
|
2019-09-06 15:19:22 +00:00
|
|
|
RawBindings,
|
|
|
|
D,
|
|
|
|
C,
|
|
|
|
M,
|
2019-10-31 16:43:05 +00:00
|
|
|
BaseProps & ExtractPropTypes<PropsOptions, false>
|
2019-09-06 15:19:22 +00:00
|
|
|
>
|
|
|
|
}
|
|
|
|
|
|
|
|
// implementation, close to no-op
|
2019-10-22 15:26:48 +00:00
|
|
|
export function createComponent(options: unknown) {
|
2019-10-05 14:09:34 +00:00
|
|
|
return isFunction(options) ? { setup: options } : options
|
2019-09-06 15:19:22 +00:00
|
|
|
}
|