Skip to content

Commit

Permalink
feat: add datastore object id as key to collections if not set
Browse files Browse the repository at this point in the history
  • Loading branch information
alharris-at committed Nov 23, 2021
1 parent ff57822 commit 63ffb89
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,10 @@ export default function CollectionOfCustomButtons(
{...getOverrideProps(overrides, \\"Collection\\")}
>
{(item, index) => (
<Flex {...getOverrideProps(overrides, \\"Collection.Flex[0]\\")}>
<Flex
key={item.id}
{...getOverrideProps(overrides, \\"Collection.Flex[0]\\")}
>
<Button
labelWidth={width}
backgroundColor={buttonColor?.favoriteColor}
Expand Down Expand Up @@ -322,7 +325,10 @@ export default function CollectionOfCustomButtons(
{...getOverrideProps(overrides, \\"Collection\\")}
>
{(item, index) => (
<Flex {...getOverrideProps(overrides, \\"Collection.Flex[0]\\")}>
<Flex
key={item.id}
{...getOverrideProps(overrides, \\"Collection.Flex[0]\\")}
>
<Button
labelWidth={width}
backgroundColor={buttonColor?.favoriteColor}
Expand Down Expand Up @@ -421,7 +427,10 @@ export default function CollectionOfCustomButtons(
{...getOverrideProps(overrides, \\"Collection\\")}
>
{(item, index) => (
<Flex {...getOverrideProps(overrides, \\"Collection.Flex[0]\\")}>
<Flex
key={item.id}
{...getOverrideProps(overrides, \\"Collection.Flex[0]\\")}
>
<Button
labelWidth={width}
backgroundColor={buttonColor?.favoriteColor}
Expand Down Expand Up @@ -489,6 +498,7 @@ export default function ListingCardCollection(
marginBottom=\\"0\\"
marginTop=\\"0\\"
marginLeft=\\"0\\"
key={item.id}
{...getOverrideProps(overrides, \\"Collection.ListingCard[0]\\")}
></ListingCard>
)}
Expand Down Expand Up @@ -532,6 +542,7 @@ export default function ListingCardCollection(
>
{(item, index) => (
<ListingCard
key={item.id}
{...getOverrideProps(overrides, \\"Collection.ListingCard[0]\\")}
></ListingCard>
)}
Expand Down Expand Up @@ -2267,6 +2278,7 @@ export default function CollectionDefaultValue(
>
{(item, index) => (
<Text
key={item.id}
children={item.username || \\"Collection Default Value\\"}
{...getOverrideProps(overrides, \\"Collection.Text[0]\\")}
></Text>
Expand Down
16 changes: 16 additions & 0 deletions packages/codegen-ui-react/lib/amplify-ui-renderers/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { ReactComponentWithChildrenRenderer } from '../react-component-with-chil

export default class CollectionRenderer extends ReactComponentWithChildrenRenderer<BaseComponentProps> {
renderElement(renderChildren: (children: StudioComponentChild[]) => JsxChild[]): JsxElement {
this.addKeyPropertyToChildren(this.component.children ?? []);
const childrenJsx = this.component.children ? renderChildren(this.component.children ?? []) : [];

const arrowFuncExpr = this.renderItemArrowFunctionExpr(childrenJsx);
Expand All @@ -35,6 +36,21 @@ export default class CollectionRenderer extends ReactComponentWithChildrenRender
return element;
}

private addKeyPropertyToChildren(children: StudioComponentChild[]) {
children.forEach((child: StudioComponentChild) => {
if ('key' in child.properties) {
return;
}
// eslint-disable-next-line no-param-reassign
child.properties.key = {
collectionBindingProperties: {
property: '',
field: 'id',
},
};
});
}

private findItemsVariableName(): string | undefined {
if (isStudioComponentWithCollectionProperties(this.component)) {
const collectionProps = Object.entries(this.component.collectionProperties ?? {});
Expand Down

0 comments on commit 63ffb89

Please sign in to comment.