feat(runtime-vapor): support v-bind for event

This commit is contained in:
三咲智子 Kevin Deng 2024-03-14 14:25:54 +08:00
parent b4aa5f98d7
commit b4da5a8da6
No known key found for this signature in database
GPG Key ID: 69992F2250DFD93E
4 changed files with 20 additions and 17 deletions

View File

@ -3,16 +3,15 @@ import { patchStyle } from './modules/style'
import { patchAttr } from './modules/attrs' import { patchAttr } from './modules/attrs'
import { patchDOMProp } from './modules/props' import { patchDOMProp } from './modules/props'
import { patchEvent } from './modules/events' import { patchEvent } from './modules/events'
import { isFunction, isModelListener, isOn, isString } from '@vue/shared' import {
isFunction,
isModelListener,
isNativeOn,
isOn,
isString,
} from '@vue/shared'
import type { RendererOptions } from '@vue/runtime-core' import type { RendererOptions } from '@vue/runtime-core'
const isNativeOn = (key: string) =>
key.charCodeAt(0) === 111 /* o */ &&
key.charCodeAt(1) === 110 /* n */ &&
// lowercase letter
key.charCodeAt(2) > 96 &&
key.charCodeAt(2) < 123
type DOMRendererOptions = RendererOptions<Node, Element> type DOMRendererOptions = RendererOptions<Node, Element>
export const patchProp: DOMRendererOptions['patchProp'] = ( export const patchProp: DOMRendererOptions['patchProp'] = (

View File

@ -8,7 +8,7 @@ import { withKeys, withModifiers } from '@vue/runtime-dom'
import { queuePostRenderEffect } from '../scheduler' import { queuePostRenderEffect } from '../scheduler'
export function addEventListener( export function addEventListener(
el: HTMLElement, el: Element,
event: string, event: string,
handler: (...args: any) => any, handler: (...args: any) => any,
options?: AddEventListenerOptions, options?: AddEventListenerOptions,
@ -23,7 +23,7 @@ interface ModifierOptions {
} }
export function on( export function on(
el: HTMLElement, el: Element,
event: string, event: string,
handlerGetter: () => undefined | ((...args: any[]) => any), handlerGetter: () => undefined | ((...args: any[]) => any),
options: AddEventListenerOptions & options: AddEventListenerOptions &

View File

@ -3,6 +3,7 @@ import {
includeBooleanAttr, includeBooleanAttr,
isArray, isArray,
isFunction, isFunction,
isNativeOn,
isOn, isOn,
isString, isString,
normalizeClass, normalizeClass,
@ -12,6 +13,7 @@ import {
import { warn } from '../warning' import { warn } from '../warning'
import { setStyle } from './style' import { setStyle } from './style'
import { MetadataKind, getMetadata, recordPropMetadata } from '../metadata' import { MetadataKind, getMetadata, recordPropMetadata } from '../metadata'
import { on } from './event'
export function setClass(el: Element, value: any) { export function setClass(el: Element, value: any) {
const prev = recordPropMetadata(el, 'class', (value = normalizeClass(value))) const prev = recordPropMetadata(el, 'class', (value = normalizeClass(value)))
@ -110,6 +112,8 @@ export function setDynamicProp(el: Element, key: string, value: any) {
setClass(el, value) setClass(el, value)
} else if (key === 'style') { } else if (key === 'style') {
setStyle(el as HTMLElement, value) setStyle(el as HTMLElement, value)
} else if (isOn(key)) {
on(el, key[2].toLowerCase() + key.slice(3), () => value, { effect: true })
} else if ( } else if (
key[0] === '.' key[0] === '.'
? ((key = key.slice(1)), true) ? ((key = key.slice(1)), true)
@ -193,13 +197,6 @@ export function setHtml(el: Element, value: any) {
} }
// TODO copied from runtime-dom // TODO copied from runtime-dom
const isNativeOn = (key: string) =>
key.charCodeAt(0) === 111 /* o */ &&
key.charCodeAt(1) === 110 /* n */ &&
// lowercase letter
key.charCodeAt(2) > 96 &&
key.charCodeAt(2) < 123
function shouldSetAsProp( function shouldSetAsProp(
el: Element, el: Element,
key: string, key: string,

View File

@ -18,6 +18,13 @@ export const isOn = (key: string) =>
// uppercase letter // uppercase letter
(key.charCodeAt(2) > 122 || key.charCodeAt(2) < 97) (key.charCodeAt(2) > 122 || key.charCodeAt(2) < 97)
export const isNativeOn = (key: string) =>
key.charCodeAt(0) === 111 /* o */ &&
key.charCodeAt(1) === 110 /* n */ &&
// lowercase letter
key.charCodeAt(2) > 96 &&
key.charCodeAt(2) < 123
export const isModelListener = (key: string) => key.startsWith('onUpdate:') export const isModelListener = (key: string) => key.startsWith('onUpdate:')
export const extend = Object.assign export const extend = Object.assign