@@ -5,6 +5,7 @@ import { renderEffect } from './renderEffect'
5
5
import { type Block , type Fragment , fragmentKey } from './apiRender'
6
6
import { warn } from './warning'
7
7
import { componentKey } from './component'
8
+ import type { DynamicSlot } from './componentSlots'
8
9
9
10
interface ForBlock extends Fragment {
10
11
scope : EffectScope
@@ -299,44 +300,58 @@ export const createFor = (
299
300
remove ( nodes , parent ! )
300
301
scope . stop ( )
301
302
}
303
+ }
302
304
303
- function getLength ( source : any ) : number {
304
- if ( isArray ( source ) || isString ( source ) ) {
305
- return source . length
306
- } else if ( typeof source === 'number' ) {
307
- if ( __DEV__ && ! Number . isInteger ( source ) ) {
308
- warn ( `The v-for range expect an integer value but got ${ source } .` )
309
- }
310
- return source
311
- } else if ( isObject ( source ) ) {
312
- if ( source [ Symbol . iterator as any ] ) {
313
- return Array . from ( source as Iterable < any > ) . length
314
- } else {
315
- return Object . keys ( source ) . length
316
- }
305
+ export const createForSlots = (
306
+ src : ( ) => any [ ] | Record < any , any > | number | Set < any > | Map < any , any > ,
307
+ getSlot : ( item : any , key : any , index ?: number ) => DynamicSlot ,
308
+ ) => {
309
+ const source = src ( )
310
+ const sourceLength = getLength ( source )
311
+ const slots = new Array ( sourceLength )
312
+ for ( let i = 0 ; i < sourceLength ; i ++ ) {
313
+ const [ item , key , index ] = getItem ( source , i )
314
+ slots [ i ] = getSlot ( item , key , index )
315
+ }
316
+ return slots
317
+ }
318
+
319
+ function getLength ( source : any ) : number {
320
+ if ( isArray ( source ) || isString ( source ) ) {
321
+ return source . length
322
+ } else if ( typeof source === 'number' ) {
323
+ if ( __DEV__ && ! Number . isInteger ( source ) ) {
324
+ warn ( `The v-for range expect an integer value but got ${ source } .` )
325
+ }
326
+ return source
327
+ } else if ( isObject ( source ) ) {
328
+ if ( source [ Symbol . iterator as any ] ) {
329
+ return Array . from ( source as Iterable < any > ) . length
330
+ } else {
331
+ return Object . keys ( source ) . length
317
332
}
318
- return 0
319
333
}
334
+ return 0
335
+ }
320
336
321
- function getItem (
322
- source : any ,
323
- idx : number ,
324
- ) : [ item : any , key : any , index ?: number ] {
325
- if ( isArray ( source ) || isString ( source ) ) {
337
+ function getItem (
338
+ source : any ,
339
+ idx : number ,
340
+ ) : [ item : any , key : any , index ?: number ] {
341
+ if ( isArray ( source ) || isString ( source ) ) {
342
+ return [ source [ idx ] , idx , undefined ]
343
+ } else if ( typeof source === 'number' ) {
344
+ return [ idx + 1 , idx , undefined ]
345
+ } else if ( isObject ( source ) ) {
346
+ if ( source && source [ Symbol . iterator as any ] ) {
347
+ source = Array . from ( source as Iterable < any > )
326
348
return [ source [ idx ] , idx , undefined ]
327
- } else if ( typeof source === 'number' ) {
328
- return [ idx + 1 , idx , undefined ]
329
- } else if ( isObject ( source ) ) {
330
- if ( source && source [ Symbol . iterator as any ] ) {
331
- source = Array . from ( source as Iterable < any > )
332
- return [ source [ idx ] , idx , undefined ]
333
- } else {
334
- const key = Object . keys ( source ) [ idx ]
335
- return [ source [ key ] , key , idx ]
336
- }
349
+ } else {
350
+ const key = Object . keys ( source ) [ idx ]
351
+ return [ source [ key ] , key , idx ]
337
352
}
338
- return null !
339
353
}
354
+ return null !
340
355
}
341
356
342
357
function normalizeAnchor ( node : Block ) : Node {
0 commit comments