Skip to content

Commit b4da5a8

Browse files
committed
feat(runtime-vapor): support v-bind for event
1 parent b4aa5f9 commit b4da5a8

File tree

4 files changed

+20
-17
lines changed

4 files changed

+20
-17
lines changed

packages/runtime-dom/src/patchProp.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,15 @@ import { patchStyle } from './modules/style'
33
import { patchAttr } from './modules/attrs'
44
import { patchDOMProp } from './modules/props'
55
import { patchEvent } from './modules/events'
6-
import { isFunction, isModelListener, isOn, isString } from '@vue/shared'
6+
import {
7+
isFunction,
8+
isModelListener,
9+
isNativeOn,
10+
isOn,
11+
isString,
12+
} from '@vue/shared'
713
import type { RendererOptions } from '@vue/runtime-core'
814

9-
const isNativeOn = (key: string) =>
10-
key.charCodeAt(0) === 111 /* o */ &&
11-
key.charCodeAt(1) === 110 /* n */ &&
12-
// lowercase letter
13-
key.charCodeAt(2) > 96 &&
14-
key.charCodeAt(2) < 123
15-
1615
type DOMRendererOptions = RendererOptions<Node, Element>
1716

1817
export const patchProp: DOMRendererOptions['patchProp'] = (

packages/runtime-vapor/src/dom/event.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { withKeys, withModifiers } from '@vue/runtime-dom'
88
import { queuePostRenderEffect } from '../scheduler'
99

1010
export function addEventListener(
11-
el: HTMLElement,
11+
el: Element,
1212
event: string,
1313
handler: (...args: any) => any,
1414
options?: AddEventListenerOptions,
@@ -23,7 +23,7 @@ interface ModifierOptions {
2323
}
2424

2525
export function on(
26-
el: HTMLElement,
26+
el: Element,
2727
event: string,
2828
handlerGetter: () => undefined | ((...args: any[]) => any),
2929
options: AddEventListenerOptions &

packages/runtime-vapor/src/dom/prop.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
includeBooleanAttr,
44
isArray,
55
isFunction,
6+
isNativeOn,
67
isOn,
78
isString,
89
normalizeClass,
@@ -12,6 +13,7 @@ import {
1213
import { warn } from '../warning'
1314
import { setStyle } from './style'
1415
import { MetadataKind, getMetadata, recordPropMetadata } from '../metadata'
16+
import { on } from './event'
1517

1618
export function setClass(el: Element, value: any) {
1719
const prev = recordPropMetadata(el, 'class', (value = normalizeClass(value)))
@@ -110,6 +112,8 @@ export function setDynamicProp(el: Element, key: string, value: any) {
110112
setClass(el, value)
111113
} else if (key === 'style') {
112114
setStyle(el as HTMLElement, value)
115+
} else if (isOn(key)) {
116+
on(el, key[2].toLowerCase() + key.slice(3), () => value, { effect: true })
113117
} else if (
114118
key[0] === '.'
115119
? ((key = key.slice(1)), true)
@@ -193,13 +197,6 @@ export function setHtml(el: Element, value: any) {
193197
}
194198

195199
// TODO copied from runtime-dom
196-
const isNativeOn = (key: string) =>
197-
key.charCodeAt(0) === 111 /* o */ &&
198-
key.charCodeAt(1) === 110 /* n */ &&
199-
// lowercase letter
200-
key.charCodeAt(2) > 96 &&
201-
key.charCodeAt(2) < 123
202-
203200
function shouldSetAsProp(
204201
el: Element,
205202
key: string,

packages/shared/src/general.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ export const isOn = (key: string) =>
1818
// uppercase letter
1919
(key.charCodeAt(2) > 122 || key.charCodeAt(2) < 97)
2020

21+
export const isNativeOn = (key: string) =>
22+
key.charCodeAt(0) === 111 /* o */ &&
23+
key.charCodeAt(1) === 110 /* n */ &&
24+
// lowercase letter
25+
key.charCodeAt(2) > 96 &&
26+
key.charCodeAt(2) < 123
27+
2128
export const isModelListener = (key: string) => key.startsWith('onUpdate:')
2229

2330
export const extend = Object.assign

0 commit comments

Comments
 (0)