@@ -6,13 +6,13 @@ import { EqualityCheck, Facet, NO_VALUE } from '../types'
6
6
7
7
export type MapProps < T > = {
8
8
array : Facet < T [ ] >
9
- children : ( item : Facet < T > , index : number ) => ReactElement | null
9
+ children : ( item : Facet < T > , index : number , length : number ) => ReactElement | null
10
10
equalityCheck ?: EqualityCheck < T >
11
11
}
12
12
13
13
export const Map = < T , > ( { array, children, equalityCheck } : MapProps < T > ) => {
14
- // When mounting lists, we always want to defer
15
- const countValue = useFacetUnwrap ( useFacetMap ( ( array ) => array . length , [ ] , [ array ] ) )
14
+ const lengthValue = useFacetUnwrap ( useFacetMap ( ( array ) => array . length , [ ] , [ array ] ) )
15
+ const lengthNumber = lengthValue !== NO_VALUE ? lengthValue : 0
16
16
17
17
return (
18
18
< >
@@ -23,13 +23,14 @@ export const Map = <T,>({ array, children, equalityCheck }: MapProps<T>) => {
23
23
key = { index }
24
24
arrayFacet = { array }
25
25
index = { index }
26
+ length = { lengthNumber }
26
27
equalityCheck = { equalityCheck }
27
28
children = { children }
28
29
/>
29
30
) : (
30
- < MapChild < T > key = { index } arrayFacet = { array } index = { index } children = { children } />
31
+ < MapChild < T > key = { index } arrayFacet = { array } index = { index } length = { lengthNumber } children = { children } />
31
32
) ,
32
- countValue !== NO_VALUE ? countValue : 0 ,
33
+ lengthNumber ,
33
34
) }
34
35
</ >
35
36
)
@@ -38,11 +39,12 @@ export const Map = <T,>({ array, children, equalityCheck }: MapProps<T>) => {
38
39
type MapChildMemoProps < T > = {
39
40
arrayFacet : Facet < T [ ] >
40
41
index : number
41
- children : ( item : Facet < T > , index : number ) => ReactElement | null
42
+ length : number
43
+ children : ( item : Facet < T > , index : number , length : number ) => ReactElement | null
42
44
equalityCheck : EqualityCheck < T >
43
45
}
44
46
45
- const MapChildMemo = < T , > ( { arrayFacet, index, children, equalityCheck } : MapChildMemoProps < T > ) => {
47
+ const MapChildMemo = < T , > ( { arrayFacet, index, length , children, equalityCheck } : MapChildMemoProps < T > ) => {
46
48
const childFacet = useFacetMemo (
47
49
( array ) => {
48
50
if ( index < array . length ) return array [ index ]
@@ -52,16 +54,17 @@ const MapChildMemo = <T,>({ arrayFacet, index, children, equalityCheck }: MapChi
52
54
[ arrayFacet ] ,
53
55
equalityCheck ,
54
56
)
55
- return children ( childFacet , index )
57
+ return children ( childFacet , index , length )
56
58
}
57
59
58
60
type MapChildProps < T > = {
59
61
arrayFacet : Facet < T [ ] >
60
62
index : number
61
- children : ( item : Facet < T > , index : number ) => ReactElement | null
63
+ length : number
64
+ children : ( item : Facet < T > , index : number , length : number ) => ReactElement | null
62
65
}
63
66
64
- const MapChild = < T , > ( { arrayFacet, index, children } : MapChildProps < T > ) => {
67
+ const MapChild = < T , > ( { arrayFacet, index, length , children } : MapChildProps < T > ) => {
65
68
const childFacet = useFacetMap (
66
69
( array ) => {
67
70
if ( index < array . length ) return array [ index ]
@@ -71,7 +74,7 @@ const MapChild = <T,>({ arrayFacet, index, children }: MapChildProps<T>) => {
71
74
[ arrayFacet ] ,
72
75
)
73
76
74
- return children ( childFacet , index )
77
+ return children ( childFacet , index , length )
75
78
}
76
79
77
80
interface TimesFn < T > {
0 commit comments