feat(directives): add support for function directives (#252)

This commit is contained in:
Dmitry Sharshakov
2019-10-16 09:12:26 +03:00
committed by Evan You
parent a72652f6e6
commit 0bac763f5a
4 changed files with 83 additions and 10 deletions

View File

@@ -34,7 +34,7 @@ export type DirectiveHook<T = any> = (
prevVNode: VNode<any, T> | null
) => void
export interface Directive<T = any> {
export interface ObjectDirective<T = any> {
beforeMount?: DirectiveHook<T>
mounted?: DirectiveHook<T>
beforeUpdate?: DirectiveHook<T>
@@ -43,6 +43,10 @@ export interface Directive<T = any> {
unmounted?: DirectiveHook<T>
}
export type FunctionDirective<T = any> = DirectiveHook<T>
export type Directive<T = any> = ObjectDirective<T> | FunctionDirective<T>
type DirectiveModifiers = Record<string, boolean>
const valueCache = new WeakMap<Directive, WeakMap<any, any>>()
@@ -60,8 +64,16 @@ function applyDirective(
valueCacheForDir = new WeakMap<VNode, any>()
valueCache.set(directive, valueCacheForDir)
}
if (isFunction(directive)) {
directive = {
mounted: directive,
updated: directive
} as ObjectDirective
}
for (const key in directive) {
const hook = directive[key as keyof Directive]!
const hook = directive[key as keyof ObjectDirective]!
const hookKey = `vnode` + key[0].toUpperCase() + key.slice(1)
const vnodeHook = (vnode: VNode, prevVNode: VNode | null) => {
let oldValue

View File

@@ -81,6 +81,8 @@ export {
Directive,
DirectiveBinding,
DirectiveHook,
ObjectDirective,
FunctionDirective,
DirectiveArguments
} from './directives'
export { SuspenseBoundary } from './suspense'