File tree Expand file tree Collapse file tree
packages/@react-facet/core/src/components Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ // Copyright (c) Mojang AB. All rights reserved.
2+
3+ import { render } from '@react-facet/dom-fiber-testing-library'
4+ import { createFacet } from '../facet'
5+ import React from 'react'
6+ import { Times } from './Times'
7+
8+ it ( 'renders the child n times, passing the index' , ( ) => {
9+ const mockFacet = createFacet ( { initialValue : 3 } )
10+ const result = render ( < Times count = { mockFacet } > { ( index ) => < > { index } </ > } </ Times > )
11+
12+ // Has all the indexes, proving it looped and rendered all the children
13+ expect ( result . container ) . toHaveTextContent ( '012' )
14+ } )
Original file line number Diff line number Diff line change 1+ // Copyright (c) Mojang AB. All rights reserved.
2+
3+ import React , { ReactElement } from 'react'
4+ import { Facet } from '../types'
5+ import { Unwrap } from './Unwrap'
6+
7+ /**
8+ * Given a facet with a number, it renders its children "count" times.
9+ *
10+ * @param count facet with the amount of items to render
11+ * @param children function that will be called passing an index and the total.
12+ */
13+ export const Times = ( {
14+ count,
15+ children,
16+ } : {
17+ count : Facet < number >
18+ children : ( index : number , count : number ) => ReactElement | null
19+ } ) => {
20+ return (
21+ < Unwrap data = { count } >
22+ { ( count ) => {
23+ const array = new Array ( count )
24+
25+ for ( let index = 0 ; index < count ; index ++ ) {
26+ array [ index ] = < React . Fragment key = { index } > { children ( index , count ) } </ React . Fragment >
27+ }
28+
29+ return < > { array } </ >
30+ } }
31+ </ Unwrap >
32+ )
33+ }
Original file line number Diff line number Diff line change 1+ // Copyright (c) Mojang AB. All rights reserved.
2+
3+ import React from 'react'
4+ import { render , screen } from '@react-facet/dom-fiber-testing-library'
5+ import { createFacet } from '../facet'
6+ import { NO_VALUE } from '../types'
7+ import { Unwrap } from './Unwrap'
8+
9+ it ( 'renders the child with unwrapped data' , ( ) => {
10+ const mockText = 'Foo'
11+ const mockFacet = createFacet ( { initialValue : mockText } )
12+ render ( < Unwrap data = { mockFacet } > { ( data ) => < > { data } </ > } </ Unwrap > )
13+ expect ( screen . queryByText ( mockText ) ) . toBeInTheDocument ( )
14+ } )
15+
16+ it ( 'renders the child with unwrapped nullish data' , ( ) => {
17+ const mockText = 'Bar'
18+ const mockData = undefined
19+ const mockFacet = createFacet ( { initialValue : mockData } )
20+ render ( < Unwrap data = { mockFacet } > { ( data ) => < > { `${ mockText } ${ data } ` } </ > } </ Unwrap > )
21+ expect ( screen . queryByText ( `${ mockText } ${ mockData } ` ) ) . toBeInTheDocument ( )
22+ } )
23+
24+ it ( 'does not render the child with NO_VALUE' , ( ) => {
25+ const mockText = 'Baz'
26+ const mockFacet = createFacet < boolean > ( { initialValue : NO_VALUE } )
27+ render ( < Unwrap data = { mockFacet } > { ( ) => < > { mockText } </ > } </ Unwrap > )
28+ expect ( screen . queryByText ( mockText ) ) . not . toBeInTheDocument ( )
29+ } )
Original file line number Diff line number Diff line change 1+ // Copyright (c) Mojang AB. All rights reserved.
2+
3+ import { ReactElement } from 'react'
4+ import { useFacetUnwrap } from '../hooks'
5+ import { Facet , NO_VALUE , Value } from '../types'
6+
7+ /**
8+ * Renders a child with an unwrapped given facet value
9+ *
10+ * @param data facet value
11+ * @param children render prop which receives the unwrapped facet
12+ */
13+ export const Unwrap = < T extends Value > ( {
14+ data,
15+ children,
16+ } : {
17+ data : Facet < T >
18+ children : ( data : T ) => ReactElement | null
19+ } ) => {
20+ const unwrapped = useFacetUnwrap ( data )
21+ return unwrapped === NO_VALUE ? null : children ( unwrapped )
22+ }
Original file line number Diff line number Diff line change 11export * from './Map'
22export * from './Mount'
3+ export * from './Times'
4+ export * from './Unwrap'
35export * from './With'
You can’t perform that action at this time.
0 commit comments