Skip to content

Commit 177700e

Browse files
hebbehpirelenito
authored andcommitted
Added property count to Map to make length of provided array available (#138)
* Added property count to Map, to make length of provided array available * Remove unused code * Revert unwanted changes to count returns length of provided array * Rename count to length * Change "count" to "length" in test as well --------- Co-authored-by: Paulo <[email protected]>
1 parent 78f09bf commit 177700e

File tree

2 files changed

+32
-11
lines changed

2 files changed

+32
-11
lines changed

packages/@react-facet/core/src/components/Map.spec.tsx

+18
Original file line numberDiff line numberDiff line change
@@ -149,3 +149,21 @@ it('updates only items that have changed', () => {
149149
expect(mock).toHaveBeenCalledTimes(1)
150150
expect(mock).toHaveBeenCalledWith({ a: '6' })
151151
})
152+
153+
it('provides the length of the array', () => {
154+
const data = createFacet({
155+
initialValue: [{ a: '1' }, { a: '2' }, { a: '3' }],
156+
})
157+
158+
const ExampleContent = ({ index, length }: { index: number; length: number }) => {
159+
return <>{length === index + 1 && <div data-testid={'length'}>{length}</div>}</>
160+
}
161+
162+
const Example = () => {
163+
return <Map array={data}>{(_, index, length) => <ExampleContent index={index} length={length} />}</Map>
164+
}
165+
166+
const { getByTestId } = render(<Example />)
167+
168+
expect(getByTestId('length')).toHaveTextContent('3')
169+
})

packages/@react-facet/core/src/components/Map.tsx

+14-11
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ import { EqualityCheck, Facet, NO_VALUE } from '../types'
66

77
export type MapProps<T> = {
88
array: Facet<T[]>
9-
children: (item: Facet<T>, index: number) => ReactElement | null
9+
children: (item: Facet<T>, index: number, length: number) => ReactElement | null
1010
equalityCheck?: EqualityCheck<T>
1111
}
1212

1313
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
1616

1717
return (
1818
<>
@@ -23,13 +23,14 @@ export const Map = <T,>({ array, children, equalityCheck }: MapProps<T>) => {
2323
key={index}
2424
arrayFacet={array}
2525
index={index}
26+
length={lengthNumber}
2627
equalityCheck={equalityCheck}
2728
children={children}
2829
/>
2930
) : (
30-
<MapChild<T> key={index} arrayFacet={array} index={index} children={children} />
31+
<MapChild<T> key={index} arrayFacet={array} index={index} length={lengthNumber} children={children} />
3132
),
32-
countValue !== NO_VALUE ? countValue : 0,
33+
lengthNumber,
3334
)}
3435
</>
3536
)
@@ -38,11 +39,12 @@ export const Map = <T,>({ array, children, equalityCheck }: MapProps<T>) => {
3839
type MapChildMemoProps<T> = {
3940
arrayFacet: Facet<T[]>
4041
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
4244
equalityCheck: EqualityCheck<T>
4345
}
4446

45-
const MapChildMemo = <T,>({ arrayFacet, index, children, equalityCheck }: MapChildMemoProps<T>) => {
47+
const MapChildMemo = <T,>({ arrayFacet, index, length, children, equalityCheck }: MapChildMemoProps<T>) => {
4648
const childFacet = useFacetMemo(
4749
(array) => {
4850
if (index < array.length) return array[index]
@@ -52,16 +54,17 @@ const MapChildMemo = <T,>({ arrayFacet, index, children, equalityCheck }: MapChi
5254
[arrayFacet],
5355
equalityCheck,
5456
)
55-
return children(childFacet, index)
57+
return children(childFacet, index, length)
5658
}
5759

5860
type MapChildProps<T> = {
5961
arrayFacet: Facet<T[]>
6062
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
6265
}
6366

64-
const MapChild = <T,>({ arrayFacet, index, children }: MapChildProps<T>) => {
67+
const MapChild = <T,>({ arrayFacet, index, length, children }: MapChildProps<T>) => {
6568
const childFacet = useFacetMap(
6669
(array) => {
6770
if (index < array.length) return array[index]
@@ -71,7 +74,7 @@ const MapChild = <T,>({ arrayFacet, index, children }: MapChildProps<T>) => {
7174
[arrayFacet],
7275
)
7376

74-
return children(childFacet, index)
77+
return children(childFacet, index, length)
7578
}
7679

7780
interface TimesFn<T> {

0 commit comments

Comments
 (0)