Skip to content

Commit c4b5c79

Browse files
hebbehpirelenito
andauthored
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 118ff93 commit c4b5c79

File tree

2 files changed

+32
-10
lines changed

2 files changed

+32
-10
lines changed

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

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

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

+14-10
Original file line numberDiff line numberDiff line change
@@ -6,12 +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-
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
1516

1617
return (
1718
<>
@@ -22,13 +23,14 @@ export const Map = <T,>({ array, children, equalityCheck }: MapProps<T>) => {
2223
key={index}
2324
arrayFacet={array}
2425
index={index}
26+
length={lengthNumber}
2527
equalityCheck={equalityCheck}
2628
children={children}
2729
/>
2830
) : (
29-
<MapChild<T> key={index} arrayFacet={array} index={index} children={children} />
31+
<MapChild<T> key={index} arrayFacet={array} index={index} length={lengthNumber} children={children} />
3032
),
31-
countValue !== NO_VALUE ? countValue : 0,
33+
lengthNumber,
3234
)}
3335
</>
3436
)
@@ -37,11 +39,12 @@ export const Map = <T,>({ array, children, equalityCheck }: MapProps<T>) => {
3739
type MapChildMemoProps<T> = {
3840
arrayFacet: Facet<T[]>
3941
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
4144
equalityCheck: EqualityCheck<T>
4245
}
4346

44-
const MapChildMemo = <T,>({ arrayFacet, index, children, equalityCheck }: MapChildMemoProps<T>) => {
47+
const MapChildMemo = <T,>({ arrayFacet, index, length, children, equalityCheck }: MapChildMemoProps<T>) => {
4548
const childFacet = useFacetMemo(
4649
(array) => {
4750
if (index < array.length) return array[index]
@@ -51,16 +54,17 @@ const MapChildMemo = <T,>({ arrayFacet, index, children, equalityCheck }: MapChi
5154
[arrayFacet],
5255
equalityCheck,
5356
)
54-
return children(childFacet, index)
57+
return children(childFacet, index, length)
5558
}
5659

5760
type MapChildProps<T> = {
5861
arrayFacet: Facet<T[]>
5962
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
6165
}
6266

63-
const MapChild = <T,>({ arrayFacet, index, children }: MapChildProps<T>) => {
67+
const MapChild = <T,>({ arrayFacet, index, length, children }: MapChildProps<T>) => {
6468
const childFacet = useFacetMap(
6569
(array) => {
6670
if (index < array.length) return array[index]
@@ -70,7 +74,7 @@ const MapChild = <T,>({ arrayFacet, index, children }: MapChildProps<T>) => {
7074
[arrayFacet],
7175
)
7276

73-
return children(childFacet, index)
77+
return children(childFacet, index, length)
7478
}
7579

7680
interface TimesFn<T> {

0 commit comments

Comments
 (0)