Skip to content

Commit d96117c

Browse files
authored
Add renderItem prop to Core Data RelatedList parent components (#285)
* add onclicks to related components * fix onClick args * replace new onClicks with renderItems * remove redundant RelatedList example * remove stray onclick reference * change renderItem output type
1 parent 4b81f04 commit d96117c

File tree

8 files changed

+96
-12
lines changed

8 files changed

+96
-12
lines changed

packages/core-data/src/components/RelatedOrganizations.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,12 @@ type Props = {
2828
/**
2929
* Callback fired when the component is mounted to fetch the data.
3030
*/
31-
onLoad: () => any
31+
onLoad: () => any,
32+
33+
/**
34+
* Render function used to determine how to present the passed item.
35+
*/
36+
renderItem: (item: any) => JSX.Element,
3237
};
3338

3439
/**
@@ -42,14 +47,14 @@ const RelatedOrganizations = (props: Props) => (
4247
itemsPerRow={props.itemsPerRow}
4348
moreLabel={props.moreLabel}
4449
onLoad={props.onLoad}
45-
renderItem={(organization) => (
50+
renderItem={props.renderItem || ((organization) => (
4651
<>
4752
<Building2
4853
className='h-4 w-4 mr-1.5'
4954
/>
5055
{ organization.name }
5156
</>
52-
)}
57+
))}
5358
/>
5459
);
5560

packages/core-data/src/components/RelatedPeople.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,12 @@ type Props = {
3131
/**
3232
* Callback fired when the component is mounted to fetch the data.
3333
*/
34-
onLoad: () => any
34+
onLoad: () => any,
35+
36+
/**
37+
* Render function used to determine how to present the passed item.
38+
*/
39+
renderItem: (item: any) => JSX.Element,
3540
};
3641

3742
/**
@@ -57,14 +62,14 @@ const RelatedPeople = (props: Props) => {
5762
itemsPerRow={props.itemsPerRow}
5863
moreLabel={props.moreLabel}
5964
onLoad={props.onLoad}
60-
renderItem={(person) => (
65+
renderItem={props.renderItem || ((person) => (
6166
<>
6267
<UserCircle
6368
className='h-4 w-4 mr-1.5'
6469
/>
6570
{ getName(person) }
6671
</>
67-
)}
72+
))}
6873
showMoreLabel={props.showMoreLabel}
6974
/>
7075
);

packages/core-data/src/components/RelatedPlaces.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,12 @@ type Props = {
2828
/**
2929
* Callback fired when the component is mounted to fetch the data.
3030
*/
31-
onLoad: () => any
31+
onLoad: () => any,
32+
33+
/**
34+
* Render function used to determine how to present the passed item.
35+
*/
36+
renderItem: (item: any) => JSX.Element,
3237
};
3338

3439
/**
@@ -42,14 +47,14 @@ const RelatedPlaces = (props: Props) => (
4247
itemsPerRow={props.itemsPerRow}
4348
moreLabel={props.moreLabel}
4449
onLoad={props.onLoad}
45-
renderItem={(place) => (
50+
renderItem={props.renderItem || ((place) => (
4651
<>
4752
<MapPin
4853
className='h-4 w-4 mr-0.5 inline-block mb-0.5'
4954
/>
5055
{ place.name }
5156
</>
52-
)}
57+
))}
5358
/>
5459
);
5560

packages/core-data/src/components/RelatedTaxonomies.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,12 @@ type Props = {
2828
/**
2929
* Callback fired when the component is mounted to fetch the data.
3030
*/
31-
onLoad: () => any
31+
onLoad: () => any,
32+
33+
/**
34+
* Render function used to determine how to present the passed item.
35+
*/
36+
renderItem: (item: any) => JSX.Element,
3237
};
3338

3439
/**
@@ -42,14 +47,14 @@ const RelatedTaxonomies = (props: Props) => (
4247
itemsPerRow={props.itemsPerRow}
4348
moreLabel={props.moreLabel}
4449
onLoad={props.onLoad}
45-
renderItem={(taxonomy) => (
50+
renderItem={props.renderItem || ((taxonomy) => (
4651
<>
4752
<ListTree
4853
className='h-4 w-4 mr-1.5'
4954
/>
5055
{ taxonomy.name }
5156
</>
52-
)}
57+
))}
5358
/>
5459
);
5560

packages/storybook/src/core-data/RelatedOrganizations.stories.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,26 @@ export const Default = withCoreDataContextProvider(() => {
2121
);
2222
});
2323

24+
export const CustomRender = withCoreDataContextProvider(() => {
25+
const PlacesService = usePlacesService();
26+
27+
return (
28+
<RelatedOrganizations
29+
onLoad={(params) => PlacesService.fetchRelatedOrganizations('1', params)}
30+
renderItem={(item) => (
31+
<p>
32+
{`${item.name}:`}
33+
&nbsp;
34+
{Math.random() > 0.5
35+
? <span style={{ color: 'green' }}></span>
36+
: <span style={{ color: 'red' }}></span>}
37+
<strong>{`$${(Math.random() * 100).toPrecision(4)}`}</strong>
38+
</p>
39+
)}
40+
/>
41+
);
42+
});
43+
2444
export const EmptyList = withCoreDataContextProvider(() => (
2545
<RelatedOrganizations
2646
emptyMessage='No related organizations'

packages/storybook/src/core-data/RelatedPeople.stories.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,24 @@ export const Default = withCoreDataContextProvider(() => {
2323
);
2424
});
2525

26+
export const CustomRender = withCoreDataContextProvider(() => {
27+
const PlacesService = usePlacesService();
28+
29+
return (
30+
<RelatedPeople
31+
onLoad={(params) => (
32+
PlacesService.fetchRelatedPeople('1', params)
33+
)}
34+
renderItem={(item) => (
35+
<p>
36+
{`My name is ${item.first_name} ${item.middle_name}\
37+
${item.last_name} but you can just call me ${item.first_name}`}
38+
</p>
39+
)}
40+
/>
41+
);
42+
});
43+
2644
export const Horizontal = withCoreDataContextProvider(() => {
2745
const PlacesService = usePlacesService();
2846

packages/storybook/src/core-data/RelatedPlaces.stories.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,21 @@ export const Default = withCoreDataContextProvider(() => {
2121
);
2222
});
2323

24+
export const CustomRender = withCoreDataContextProvider(() => {
25+
const PlacesService = usePlacesService();
26+
27+
return (
28+
<RelatedPlaces
29+
onLoad={(params) => PlacesService.fetchRelatedPlaces('1', params)}
30+
renderItem={(item) => (
31+
<p>
32+
{`I've ${Math.random() < 0.5 ? 'never' : ''} been to ${item.name}.`}
33+
</p>
34+
)}
35+
/>
36+
);
37+
});
38+
2439
export const EmptyMessage = withCoreDataContextProvider(() => (
2540
<RelatedPlaces
2641
emptyMessage='No related places'

packages/storybook/src/core-data/RelatedTaxonimies.stories.js renamed to packages/storybook/src/core-data/RelatedTaxonomies.stories.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,17 @@ export const Default = withCoreDataContextProvider(() => {
2121
);
2222
});
2323

24+
export const CustomRender = withCoreDataContextProvider(() => {
25+
const PlacesService = usePlacesService();
26+
27+
return (
28+
<RelatedTaxonomies
29+
onLoad={(params) => PlacesService.fetchRelatedTaxonomies('1', params)}
30+
renderItem={(item) => <p style={{ color: 'pink' }}>{item.name}</p>}
31+
/>
32+
);
33+
});
34+
2435
export const EmptyList = withCoreDataContextProvider(() => (
2536
<RelatedTaxonomies
2637
emptyMessage='No related taxonomies'

0 commit comments

Comments
 (0)