File tree 2 files changed +48
-1
lines changed
2 files changed +48
-1
lines changed Original file line number Diff line number Diff line change @@ -279,6 +279,41 @@ describe('SFC <script setup> helpers', () => {
279
279
expect ( serializeInner ( root ) ) . toBe ( 'bar' )
280
280
} )
281
281
282
+ test ( 'without parent listener (local mutation)' , async ( ) => {
283
+ let foo : any
284
+ const update = ( ) => {
285
+ foo . value = 'bar'
286
+ }
287
+
288
+ const compRender = vi . fn ( )
289
+ const Comp = defineComponent ( {
290
+ props : [ 'foo' ] ,
291
+ emits : [ 'update:foo' ] ,
292
+ setup ( props ) {
293
+ foo = useModel ( props , 'foo' )
294
+ return ( ) => {
295
+ compRender ( )
296
+ return foo . value
297
+ }
298
+ } ,
299
+ } )
300
+
301
+ const root = nodeOps . createElement ( 'div' )
302
+ // provide initial value
303
+ render ( h ( Comp , { foo : 'initial' } ) , root )
304
+ expect ( compRender ) . toBeCalledTimes ( 1 )
305
+ expect ( serializeInner ( root ) ) . toBe ( 'initial' )
306
+
307
+ expect ( foo . value ) . toBe ( 'initial' )
308
+ update ( )
309
+ // when parent didn't provide value, local mutation is enabled
310
+ expect ( foo . value ) . toBe ( 'bar' )
311
+
312
+ await nextTick ( )
313
+ expect ( compRender ) . toBeCalledTimes ( 2 )
314
+ expect ( serializeInner ( root ) ) . toBe ( 'bar' )
315
+ } )
316
+
282
317
test ( 'default value' , async ( ) => {
283
318
let count : any
284
319
const inc = ( ) => {
Original file line number Diff line number Diff line change 3
3
type LooseRequired ,
4
4
type Prettify ,
5
5
type UnionToIntersection ,
6
+ camelize ,
6
7
extend ,
7
8
hasChanged ,
8
9
isArray ,
@@ -380,6 +381,8 @@ export function useModel(
380
381
return ref ( ) as any
381
382
}
382
383
384
+ const camelizedName = camelize ( name )
385
+
383
386
const res = customRef ( ( track , trigger ) => {
384
387
let localValue : any
385
388
watchSyncEffect ( ( ) => {
@@ -396,7 +399,16 @@ export function useModel(
396
399
} ,
397
400
set ( value ) {
398
401
const rawProps = i . vnode ! . props
399
- if ( ! ( rawProps && name in rawProps ) && hasChanged ( value , localValue ) ) {
402
+ if (
403
+ ! (
404
+ rawProps &&
405
+ // check if parent has passed v-model
406
+ ( name in rawProps || camelizedName in rawProps ) &&
407
+ ( `onUpdate:${ name } ` in rawProps ||
408
+ `onUpdate:${ camelizedName } ` in rawProps )
409
+ ) &&
410
+ hasChanged ( value , localValue )
411
+ ) {
400
412
localValue = value
401
413
trigger ( )
402
414
}
You can’t perform that action at this time.
0 commit comments