Skip to content

Commit 63ffb89

Browse files
committed
feat: add datastore object id as key to collections if not set
1 parent ff57822 commit 63ffb89

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

packages/codegen-ui-react/lib/__tests__/__snapshots__/studio-ui-codegen-react.test.ts.snap

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,10 @@ export default function CollectionOfCustomButtons(
219219
{...getOverrideProps(overrides, \\"Collection\\")}
220220
>
221221
{(item, index) => (
222-
<Flex {...getOverrideProps(overrides, \\"Collection.Flex[0]\\")}>
222+
<Flex
223+
key={item.id}
224+
{...getOverrideProps(overrides, \\"Collection.Flex[0]\\")}
225+
>
223226
<Button
224227
labelWidth={width}
225228
backgroundColor={buttonColor?.favoriteColor}
@@ -322,7 +325,10 @@ export default function CollectionOfCustomButtons(
322325
{...getOverrideProps(overrides, \\"Collection\\")}
323326
>
324327
{(item, index) => (
325-
<Flex {...getOverrideProps(overrides, \\"Collection.Flex[0]\\")}>
328+
<Flex
329+
key={item.id}
330+
{...getOverrideProps(overrides, \\"Collection.Flex[0]\\")}
331+
>
326332
<Button
327333
labelWidth={width}
328334
backgroundColor={buttonColor?.favoriteColor}
@@ -421,7 +427,10 @@ export default function CollectionOfCustomButtons(
421427
{...getOverrideProps(overrides, \\"Collection\\")}
422428
>
423429
{(item, index) => (
424-
<Flex {...getOverrideProps(overrides, \\"Collection.Flex[0]\\")}>
430+
<Flex
431+
key={item.id}
432+
{...getOverrideProps(overrides, \\"Collection.Flex[0]\\")}
433+
>
425434
<Button
426435
labelWidth={width}
427436
backgroundColor={buttonColor?.favoriteColor}
@@ -489,6 +498,7 @@ export default function ListingCardCollection(
489498
marginBottom=\\"0\\"
490499
marginTop=\\"0\\"
491500
marginLeft=\\"0\\"
501+
key={item.id}
492502
{...getOverrideProps(overrides, \\"Collection.ListingCard[0]\\")}
493503
></ListingCard>
494504
)}
@@ -532,6 +542,7 @@ export default function ListingCardCollection(
532542
>
533543
{(item, index) => (
534544
<ListingCard
545+
key={item.id}
535546
{...getOverrideProps(overrides, \\"Collection.ListingCard[0]\\")}
536547
></ListingCard>
537548
)}
@@ -2267,6 +2278,7 @@ export default function CollectionDefaultValue(
22672278
>
22682279
{(item, index) => (
22692280
<Text
2281+
key={item.id}
22702282
children={item.username || \\"Collection Default Value\\"}
22712283
{...getOverrideProps(overrides, \\"Collection.Text[0]\\")}
22722284
></Text>

packages/codegen-ui-react/lib/amplify-ui-renderers/collection.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { ReactComponentWithChildrenRenderer } from '../react-component-with-chil
2020

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

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

39+
private addKeyPropertyToChildren(children: StudioComponentChild[]) {
40+
children.forEach((child: StudioComponentChild) => {
41+
if ('key' in child.properties) {
42+
return;
43+
}
44+
// eslint-disable-next-line no-param-reassign
45+
child.properties.key = {
46+
collectionBindingProperties: {
47+
property: '',
48+
field: 'id',
49+
},
50+
};
51+
});
52+
}
53+
3854
private findItemsVariableName(): string | undefined {
3955
if (isStudioComponentWithCollectionProperties(this.component)) {
4056
const collectionProps = Object.entries(this.component.collectionProperties ?? {});

0 commit comments

Comments
 (0)