From 678a4b35f8bc640239b90311eb2d55cc8a0cafa4 Mon Sep 17 00:00:00 2001 From: ZHAO Jinxiang Date: Tue, 30 Nov 2021 15:16:18 +0800 Subject: [PATCH] feat: add directives type support (#863) --- src/apis/createApp.ts | 16 +++++++++++++--- src/component/directives.ts | 29 +++++++++++++++++++++++++++++ src/component/index.ts | 8 ++++++++ 3 files changed, 50 insertions(+), 3 deletions(-) create mode 100644 src/component/directives.ts diff --git a/src/apis/createApp.ts b/src/apis/createApp.ts index 4e927176..4c86dde9 100644 --- a/src/apis/createApp.ts +++ b/src/apis/createApp.ts @@ -1,5 +1,6 @@ import type Vue from 'vue' import { VueConstructor } from 'vue' +import { Directive } from '../component/directives' import { getVueConstructor } from '../runtimeContext' import { warn } from '../utils' @@ -9,7 +10,8 @@ export interface App { use: VueConstructor['use'] mixin: VueConstructor['mixin'] component: VueConstructor['component'] - directive: VueConstructor['directive'] + directive(name: string): Directive | undefined + directive(name: string, directive: Directive): this mount: Vue['$mount'] unmount: Vue['$destroy'] } @@ -19,12 +21,19 @@ export function createApp(rootComponent: any, rootProps: any = undefined): App { let mountedVM: Vue | undefined = undefined - return { + const app: App = { config: V.config, use: V.use.bind(V), mixin: V.mixin.bind(V), component: V.component.bind(V), - directive: V.directive.bind(V), + directive(name: string, dir?: Directive | undefined): any { + if (dir) { + V.directive(name, dir as any) + return app + } else { + return V.directive(name) + } + }, mount: (el, hydrating) => { if (!mountedVM) { mountedVM = new V({ propsData: rootProps, ...rootComponent }) @@ -51,4 +60,5 @@ export function createApp(rootComponent: any, rootProps: any = undefined): App { } }, } + return app } diff --git a/src/component/directives.ts b/src/component/directives.ts new file mode 100644 index 00000000..44d91da4 --- /dev/null +++ b/src/component/directives.ts @@ -0,0 +1,29 @@ +import type { VNodeDirective, VNode } from 'vue' + +export type DirectiveModifiers = Record + +export interface DirectiveBinding extends Readonly { + readonly modifiers: DirectiveModifiers + readonly value: V + readonly oldValue: V | null +} + +export type DirectiveHook = ( + el: T, + binding: DirectiveBinding, + vnode: VNode, + prevVNode: Prev +) => void + +export interface ObjectDirective { + bind?: DirectiveHook + inserted?: DirectiveHook + update?: DirectiveHook + componentUpdated?: DirectiveHook + unbind?: DirectiveHook +} +export type FunctionDirective = DirectiveHook + +export type Directive = + | ObjectDirective + | FunctionDirective diff --git a/src/component/index.ts b/src/component/index.ts index f7278620..a54967f3 100644 --- a/src/component/index.ts +++ b/src/component/index.ts @@ -18,3 +18,11 @@ export { ExtractPropTypes, ExtractDefaultPropTypes, } from './componentProps' + +export { + DirectiveModifiers, + DirectiveBinding, + DirectiveHook, + ObjectDirective, + FunctionDirective, +} from './directives'