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