feat(directives): add support for function directives (#252)
This commit is contained in:
committed by
Evan You
parent
a72652f6e6
commit
0bac763f5a
@@ -1,4 +1,9 @@
|
||||
import { Directive, VNode, DirectiveBinding, warn } from '@vue/runtime-core'
|
||||
import {
|
||||
ObjectDirective,
|
||||
VNode,
|
||||
DirectiveBinding,
|
||||
warn
|
||||
} from '@vue/runtime-core'
|
||||
import { addEventListener } from '../modules/events'
|
||||
import { isArray, isObject } from '@vue/shared'
|
||||
|
||||
@@ -30,7 +35,7 @@ function toNumber(val: string): number | string {
|
||||
|
||||
// We are exporting the v-model runtime directly as vnode hooks so that it can
|
||||
// be tree-shaken in case v-model is never used.
|
||||
export const vModelText: Directive<HTMLInputElement | HTMLTextAreaElement> = {
|
||||
export const vModelText: ObjectDirective<HTMLInputElement | HTMLTextAreaElement> = {
|
||||
beforeMount(el, { value, modifiers: { lazy, trim, number } }, vnode) {
|
||||
el.value = value
|
||||
const assign = getModelAssigner(vnode)
|
||||
@@ -72,7 +77,7 @@ export const vModelText: Directive<HTMLInputElement | HTMLTextAreaElement> = {
|
||||
}
|
||||
}
|
||||
|
||||
export const vModelCheckbox: Directive<HTMLInputElement> = {
|
||||
export const vModelCheckbox: ObjectDirective<HTMLInputElement> = {
|
||||
beforeMount(el, binding, vnode) {
|
||||
setChecked(el, binding, vnode)
|
||||
const assign = getModelAssigner(vnode)
|
||||
@@ -111,7 +116,7 @@ function setChecked(
|
||||
: !!value
|
||||
}
|
||||
|
||||
export const vModelRadio: Directive<HTMLInputElement> = {
|
||||
export const vModelRadio: ObjectDirective<HTMLInputElement> = {
|
||||
beforeMount(el, { value }, vnode) {
|
||||
el.checked = looseEqual(value, vnode.props!.value)
|
||||
const assign = getModelAssigner(vnode)
|
||||
@@ -124,7 +129,7 @@ export const vModelRadio: Directive<HTMLInputElement> = {
|
||||
}
|
||||
}
|
||||
|
||||
export const vModelSelect: Directive<HTMLSelectElement> = {
|
||||
export const vModelSelect: ObjectDirective<HTMLSelectElement> = {
|
||||
// use mounted & updated because <select> relies on its children <option>s.
|
||||
mounted(el, { value }, vnode) {
|
||||
setSelected(el, value)
|
||||
@@ -214,7 +219,7 @@ function getValue(el: HTMLOptionElement | HTMLInputElement) {
|
||||
return '_value' in el ? (el as any)._value : el.value
|
||||
}
|
||||
|
||||
export const vModelDynamic: Directive<
|
||||
export const vModelDynamic: ObjectDirective<
|
||||
HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement
|
||||
> = {
|
||||
beforeMount(el, binding, vnode) {
|
||||
@@ -236,9 +241,9 @@ function callModelHook(
|
||||
binding: DirectiveBinding,
|
||||
vnode: VNode,
|
||||
prevVNode: VNode | null,
|
||||
hook: keyof Directive
|
||||
hook: keyof ObjectDirective
|
||||
) {
|
||||
let modelToUse: Directive
|
||||
let modelToUse: ObjectDirective
|
||||
switch (el.tagName) {
|
||||
case 'SELECT':
|
||||
modelToUse = vModelSelect
|
||||
|
||||
Reference in New Issue
Block a user