@@ -13,6 +13,7 @@ import {
13
13
renderEffect ,
14
14
setText ,
15
15
template ,
16
+ withDestructure ,
16
17
} from '../src'
17
18
import { makeRender } from './_utils'
18
19
@@ -319,7 +320,7 @@ describe('component: slots', () => {
319
320
const Comp = defineComponent ( ( ) => {
320
321
const n0 = template ( '<div></div>' ) ( )
321
322
insert (
322
- createSlot ( 'header' , { title : ( ) => 'header' } ) ,
323
+ createSlot ( 'header' , [ { title : ( ) => 'header' } ] ) ,
323
324
n0 as any as ParentNode ,
324
325
)
325
326
return n0
@@ -330,26 +331,150 @@ describe('component: slots', () => {
330
331
Comp ,
331
332
{ } ,
332
333
{
333
- header : ( { title } ) => {
334
- const el = template ( '<h1></h1>' ) ( )
335
- renderEffect ( ( ) => {
336
- setText ( el , title ( ) )
337
- } )
338
- return el
339
- } ,
334
+ header : withDestructure (
335
+ ( { title } ) => [ title ] ,
336
+ ctx => {
337
+ const el = template ( '<h1></h1>' ) ( )
338
+ renderEffect ( ( ) => {
339
+ setText ( el , ctx [ 0 ] )
340
+ } )
341
+ return el
342
+ } ,
343
+ ) ,
340
344
} ,
341
345
)
342
346
} ) . render ( )
343
347
344
348
expect ( host . innerHTML ) . toBe ( '<div><h1>header</h1></div>' )
345
349
} )
346
350
351
+ test ( 'dynamic slot props' , async ( ) => {
352
+ let props : any
353
+
354
+ const bindObj = ref < Record < string , any > > ( { foo : 1 , baz : 'qux' } )
355
+ const Comp = defineComponent ( ( ) =>
356
+ createSlot ( 'default' , [ ( ) => bindObj . value ] ) ,
357
+ )
358
+ define ( ( ) =>
359
+ createComponent (
360
+ Comp ,
361
+ { } ,
362
+ { default : _props => ( ( props = _props ) , [ ] ) } ,
363
+ ) ,
364
+ ) . render ( )
365
+
366
+ expect ( props ) . toEqual ( { foo : 1 , baz : 'qux' } )
367
+
368
+ bindObj . value . foo = 2
369
+ await nextTick ( )
370
+ expect ( props ) . toEqual ( { foo : 2 , baz : 'qux' } )
371
+
372
+ delete bindObj . value . baz
373
+ await nextTick ( )
374
+ expect ( props ) . toEqual ( { foo : 2 } )
375
+ } )
376
+
377
+ test ( 'dynamic slot props with static slot props' , async ( ) => {
378
+ let props : any
379
+
380
+ const foo = ref ( 0 )
381
+ const bindObj = ref < Record < string , any > > ( { foo : 100 , baz : 'qux' } )
382
+ const Comp = defineComponent ( ( ) =>
383
+ createSlot ( 'default' , [ { foo : ( ) => foo . value } , ( ) => bindObj . value ] ) ,
384
+ )
385
+ define ( ( ) =>
386
+ createComponent (
387
+ Comp ,
388
+ { } ,
389
+ { default : _props => ( ( props = _props ) , [ ] ) } ,
390
+ ) ,
391
+ ) . render ( )
392
+
393
+ expect ( props ) . toEqual ( { foo : 100 , baz : 'qux' } )
394
+
395
+ foo . value = 2
396
+ await nextTick ( )
397
+ expect ( props ) . toEqual ( { foo : 100 , baz : 'qux' } )
398
+
399
+ delete bindObj . value . foo
400
+ await nextTick ( )
401
+ expect ( props ) . toEqual ( { foo : 2 , baz : 'qux' } )
402
+ } )
403
+
404
+ test ( 'slot class binding should be merged' , async ( ) => {
405
+ let props : any
406
+
407
+ const className = ref ( 'foo' )
408
+ const classObj = ref ( { bar : true } )
409
+ const Comp = defineComponent ( ( ) =>
410
+ createSlot ( 'default' , [
411
+ { class : ( ) => className . value } ,
412
+ ( ) => ( { class : [ 'baz' , 'qux' ] } ) ,
413
+ { class : ( ) => classObj . value } ,
414
+ ] ) ,
415
+ )
416
+ define ( ( ) =>
417
+ createComponent (
418
+ Comp ,
419
+ { } ,
420
+ { default : _props => ( ( props = _props ) , [ ] ) } ,
421
+ ) ,
422
+ ) . render ( )
423
+
424
+ expect ( props ) . toEqual ( { class : 'foo baz qux bar' } )
425
+
426
+ classObj . value . bar = false
427
+ await nextTick ( )
428
+ expect ( props ) . toEqual ( { class : 'foo baz qux' } )
429
+
430
+ className . value = ''
431
+ await nextTick ( )
432
+ expect ( props ) . toEqual ( { class : 'baz qux' } )
433
+ } )
434
+
435
+ test ( 'slot style binding should be merged' , async ( ) => {
436
+ let props : any
437
+
438
+ const style = ref < any > ( { fontSize : '12px' } )
439
+ const Comp = defineComponent ( ( ) =>
440
+ createSlot ( 'default' , [
441
+ { style : ( ) => style . value } ,
442
+ ( ) => ( { style : { width : '100px' , color : 'blue' } } ) ,
443
+ { style : ( ) => 'color: red' } ,
444
+ ] ) ,
445
+ )
446
+ define ( ( ) =>
447
+ createComponent (
448
+ Comp ,
449
+ { } ,
450
+ { default : _props => ( ( props = _props ) , [ ] ) } ,
451
+ ) ,
452
+ ) . render ( )
453
+
454
+ expect ( props ) . toEqual ( {
455
+ style : {
456
+ fontSize : '12px' ,
457
+ width : '100px' ,
458
+ color : 'red' ,
459
+ } ,
460
+ } )
461
+
462
+ style . value = null
463
+ await nextTick ( )
464
+ expect ( props ) . toEqual ( {
465
+ style : {
466
+ width : '100px' ,
467
+ color : 'red' ,
468
+ } ,
469
+ } )
470
+ } )
471
+
347
472
test ( 'dynamic slot should be render correctly with binds' , async ( ) => {
348
473
const Comp = defineComponent ( ( ) => {
349
474
const n0 = template ( '<div></div>' ) ( )
350
475
prepend (
351
476
n0 as any as ParentNode ,
352
- createSlot ( 'header' , { title : ( ) => 'header' } ) ,
477
+ createSlot ( 'header' , [ { title : ( ) => 'header' } ] ) ,
353
478
)
354
479
return n0
355
480
} )
@@ -359,7 +484,7 @@ describe('component: slots', () => {
359
484
return createComponent ( Comp , { } , { } , [
360
485
( ) => ( {
361
486
name : 'header' ,
362
- fn : ( { title } ) => template ( ` ${ title ( ) } ` ) ( ) ,
487
+ fn : props => template ( props . title ) ( ) ,
363
488
} ) ,
364
489
] )
365
490
} ) . render ( )
@@ -374,7 +499,7 @@ describe('component: slots', () => {
374
499
n0 as any as ParentNode ,
375
500
createSlot (
376
501
( ) => 'header' , // dynamic slot outlet name
377
- { title : ( ) => 'header' } ,
502
+ [ { title : ( ) => 'header' } ] ,
378
503
) ,
379
504
)
380
505
return n0
@@ -384,7 +509,7 @@ describe('component: slots', () => {
384
509
return createComponent (
385
510
Comp ,
386
511
{ } ,
387
- { header : ( { title } ) => template ( ` ${ title ( ) } ` ) ( ) } ,
512
+ { header : props => template ( props . title ) ( ) } ,
388
513
)
389
514
} ) . render ( )
390
515
@@ -395,7 +520,7 @@ describe('component: slots', () => {
395
520
const Comp = defineComponent ( ( ) => {
396
521
const n0 = template ( '<div></div>' ) ( )
397
522
insert (
398
- createSlot ( 'header' , { } , ( ) => template ( 'fallback' ) ( ) ) ,
523
+ createSlot ( 'header' , undefined , ( ) => template ( 'fallback' ) ( ) ) ,
399
524
n0 as any as ParentNode ,
400
525
)
401
526
return n0
@@ -415,8 +540,8 @@ describe('component: slots', () => {
415
540
const temp0 = template ( '<p></p>' )
416
541
const el0 = temp0 ( )
417
542
const el1 = temp0 ( )
418
- const slot1 = createSlot ( 'one' , { } , ( ) => template ( 'one fallback' ) ( ) )
419
- const slot2 = createSlot ( 'two' , { } , ( ) => template ( 'two fallback' ) ( ) )
543
+ const slot1 = createSlot ( 'one' , [ ] , ( ) => template ( 'one fallback' ) ( ) )
544
+ const slot2 = createSlot ( 'two' , [ ] , ( ) => template ( 'two fallback' ) ( ) )
420
545
insert ( slot1 , el0 as any as ParentNode )
421
546
insert ( slot2 , el1 as any as ParentNode )
422
547
return [ el0 , el1 ]
@@ -458,7 +583,7 @@ describe('component: slots', () => {
458
583
const el0 = temp0 ( )
459
584
const slot1 = createSlot (
460
585
( ) => slotOutletName . value ,
461
- { } ,
586
+ undefined ,
462
587
( ) => template ( 'fallback' ) ( ) ,
463
588
)
464
589
insert ( slot1 , el0 as any as ParentNode )
0 commit comments