1
- import { extend , isArray } from '@vue/shared'
1
+ import { camelize , extend , isArray } from '@vue/shared'
2
2
import type { CodegenContext } from '../generate'
3
3
import type { CreateComponentIRNode , IRProp } from '../ir'
4
4
import {
@@ -13,6 +13,8 @@ import { genExpression } from './expression'
13
13
import { genPropKey } from './prop'
14
14
import { createSimpleExpression } from '@vue/compiler-dom'
15
15
import { genEventHandler } from './event'
16
+ import { genDirectiveModifiers } from './directive'
17
+ import { genModelHandler } from './modelValue'
16
18
17
19
// TODO: generate component slots
18
20
export function genCreateComponent (
@@ -79,8 +81,34 @@ export function genCreateComponent(
79
81
...( prop . handler
80
82
? genEventHandler ( context , prop . values [ 0 ] )
81
83
: [ '() => (' , ...genExpression ( prop . values [ 0 ] , context ) , ')' ] ) ,
84
+ ...( prop . model
85
+ ? [ ...genModel ( prop , context ) , ...genModifiers ( prop , context ) ]
86
+ : [ ] ) ,
82
87
]
83
88
} ) ,
84
89
)
90
+
91
+ function genModel ( prop : IRProp , context : CodegenContext ) {
92
+ const name = prop . key . isStatic
93
+ ? [ JSON . stringify ( `onUpdate:${ camelize ( prop . key . content ) } ` ) ]
94
+ : [ '[`onUpdate:${' , ...genExpression ( prop . key , context ) , '}`]' ]
95
+ const handler = genModelHandler ( prop . values [ 0 ] , context )
96
+
97
+ return [ ',' , ...name , ':' , ...handler ]
98
+ }
99
+
100
+ function genModifiers ( prop : IRProp , context : CodegenContext ) {
101
+ const { key, modifiers } = prop
102
+ if ( ! isArray ( modifiers ) || modifiers . length === 0 ) return [ ]
103
+
104
+ const modifiersKey = key . isStatic
105
+ ? key . content === 'modelValue'
106
+ ? [ `modelModifiers` ]
107
+ : [ `${ key . content } Modifiers` ]
108
+ : [ '[`${' , ...genExpression ( key , context ) , '}Modifiers`]' ]
109
+
110
+ const modifiersVal = genDirectiveModifiers ( modifiers )
111
+ return [ ',' , ...modifiersKey , ':' , `() => ({${ modifiersVal } })` ]
112
+ }
85
113
}
86
114
}
0 commit comments