Skip to content

Commit c406fd0

Browse files
committed
feat(runtime-vapor): support defineModel
1 parent ef6986f commit c406fd0

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

packages/runtime-core/src/component.ts

+7
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,7 @@ export interface GenericComponentInstance {
366366
* @internal
367367
*/
368368
refs: Data
369+
emit: EmitFn
369370
/**
370371
* used for keeping track of .once event handlers on components
371372
* @internal
@@ -377,6 +378,10 @@ export interface GenericComponentInstance {
377378
* @internal
378379
*/
379380
propsDefaults: Data | null
381+
/**
382+
* @internal
383+
*/
384+
getRawProps: () => Record<string, any> | null
380385

381386
// exposed properties via expose()
382387
exposed: Record<string, any> | null
@@ -730,6 +735,7 @@ export function createComponentInstance(
730735

731736
// props default value
732737
propsDefaults: null,
738+
getRawProps: null!,
733739

734740
// inheritAttrs
735741
inheritAttrs: type.inheritAttrs,
@@ -777,6 +783,7 @@ export function createComponentInstance(
777783
}
778784
instance.root = parent ? parent.root : instance
779785
instance.emit = emit.bind(null, instance)
786+
instance.getRawProps = () => instance.vnode.props
780787

781788
// apply custom element special handling
782789
if (vnode.ce) {

packages/runtime-core/src/helpers/useModel.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { type Ref, customRef, ref } from '@vue/reactivity'
22
import { EMPTY_OBJ, camelize, hasChanged, hyphenate } from '@vue/shared'
33
import type { DefineModelOptions, ModelRef } from '../apiSetupHelpers'
4-
import { getCurrentInstance } from '../component'
4+
import { getCurrentGenericInstance } from '../component'
55
import { warn } from '../warning'
66
import type { NormalizedProps } from '../componentProps'
77
import { watchSyncEffect } from '../apiWatch'
@@ -23,14 +23,14 @@ export function useModel(
2323
name: string,
2424
options: DefineModelOptions = EMPTY_OBJ,
2525
): Ref {
26-
const i = getCurrentInstance()!
26+
const i = getCurrentGenericInstance()!
2727
if (__DEV__ && !i) {
2828
warn(`useModel() called without active instance.`)
2929
return ref() as any
3030
}
3131

3232
const camelizedName = camelize(name)
33-
if (__DEV__ && !(i.propsOptions[0] as NormalizedProps)[camelizedName]) {
33+
if (__DEV__ && !(i.propsOptions![0] as NormalizedProps)[camelizedName]) {
3434
warn(`useModel() called with prop "${name}" which is not declared.`)
3535
return ref() as any
3636
}
@@ -65,7 +65,7 @@ export function useModel(
6565
) {
6666
return
6767
}
68-
const rawProps = i.vnode!.props
68+
const rawProps = i.getRawProps()
6969
if (
7070
!(
7171
rawProps &&

packages/runtime-vapor/src/component.ts

+13
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ import {
5050
import {
5151
type DynamicPropsSource,
5252
type RawProps,
53+
getKeysFromRawProps,
5354
getPropsProxyHandlers,
5455
hasFallthroughAttrs,
5556
normalizePropsOptions,
@@ -284,6 +285,7 @@ export class VaporComponentInstance implements GenericComponentInstance {
284285
props: Record<string, any>
285286
attrs: Record<string, any>
286287
propsDefaults: Record<string, any> | null
288+
getRawProps: () => Record<string, any>
287289

288290
slots: StaticSlots
289291

@@ -393,6 +395,17 @@ export class VaporComponentInstance implements GenericComponentInstance {
393395
} else {
394396
this.props = this.attrs = EMPTY_OBJ
395397
}
398+
this.getRawProps = () => {
399+
const keys = getKeysFromRawProps(this.rawProps)
400+
return keys.reduce(
401+
(props, key) => {
402+
// TODO
403+
props[key] = null
404+
return props
405+
},
406+
{} as Record<string, any>,
407+
)
408+
}
396409

397410
// init slots
398411
this.rawSlots = rawSlots || EMPTY_OBJ

0 commit comments

Comments
 (0)