Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(codegen-ui-react): prevent useless props forward to custom components #1009

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 49 additions & 29 deletions packages/codegen-ui-react/lib/react-component-renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
StudioComponentChild,
ComponentMetadata,
StudioGenericEvent,
StudioComponentProperty,
} from '@aws-amplify/codegen-ui';
import {
JsxAttributeLike,
Expand Down Expand Up @@ -47,6 +48,7 @@ import { addFormAttributes } from './forms';
import { shouldWrapInArrayField, renderArrayFieldComponent, getDecoratedLabel } from './forms/form-renderer-helper';
import { getIsRequiredValue } from './forms/form-renderer-helper/label-decorator';
import { renderStorageFieldComponent } from './utils/forms/storage-field-component';
import { Primitive } from './primitive';

export class ReactComponentRenderer<TPropIn> extends ComponentRendererBase<
TPropIn,
Expand Down Expand Up @@ -123,38 +125,56 @@ export class ReactComponentRenderer<TPropIn> extends ComponentRendererBase<
this.componentMetadata.stateReferences,
);

const propertyAttributes = Object.entries(this.component.properties).map(([key, value]) => {
if (key in localStateReferences) {
const stateName = getStateName({ componentName: this.component.name, property: key });
return buildOpeningElementProperties(
this.componentMetadata,
{ bindingProperties: { property: stateName } },
key,
);
}
if (
this.componentMetadata.formMetadata &&
this.componentMetadata.formMetadata.fieldConfigs[this.component.name]
) {
const {
labelDecorator,
fieldConfigs: {
[this.component.name]: { isArray },
},
} = this.componentMetadata.formMetadata;
const isRequired = getIsRequiredValue(this.component.properties.isRequired);
const primitivesTypes = Object.values(Primitive);

const isPrimitive = primitivesTypes.includes(this.component.componentType as Primitive);
const isHTML = this.component.componentType[0].match(/[a-z]/);

const shouldForwardProperty = ([key, value]: [string, StudioComponentProperty]) => {
return (
isPrimitive ||
isHTML ||
key in localStateReferences ||
'collectionBindingProperties' in value ||
'concat' in value ||
value.configured
);
};

const propertyAttributes = Object.entries(this.component.properties)
.filter(shouldForwardProperty)
.map(([key, value]) => {
if (key in localStateReferences) {
const stateName = getStateName({ componentName: this.component.name, property: key });
return buildOpeningElementProperties(
this.componentMetadata,
{ bindingProperties: { property: stateName } },
key,
);
}
if (
((key === 'children' && this.component.componentType === 'ToggleButton') || key === 'label') &&
((labelDecorator && labelDecorator === 'required' && isRequired) ||
(labelDecorator === 'optional' && !isRequired)) &&
'value' in value &&
!isArray
this.componentMetadata.formMetadata &&
this.componentMetadata.formMetadata.fieldConfigs[this.component.name]
) {
return getDecoratedLabel(key, value.value.toString(), labelDecorator);
const {
labelDecorator,
fieldConfigs: {
[this.component.name]: { isArray },
},
} = this.componentMetadata.formMetadata;
const isRequired = getIsRequiredValue(this.component.properties.isRequired);
if (
((key === 'children' && this.component.componentType === 'ToggleButton') || key === 'label') &&
((labelDecorator && labelDecorator === 'required' && isRequired) ||
(labelDecorator === 'optional' && !isRequired)) &&
'value' in value &&
!isArray
) {
return getDecoratedLabel(key, value.value.toString(), labelDecorator);
}
}
}
return buildOpeningElementProperties(this.componentMetadata, value, key);
});
return buildOpeningElementProperties(this.componentMetadata, value, key);
});

// Reverse, and create element properties for localStateReferences which are not yet defined props
const unmodeledPropertyAttributes = Object.entries(localStateReferences)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,20 @@
"componentType": "ListingCard",
"properties": {
"marginRight": {
"value": "0"
"value": "0",
"configured": true
},
"marginBottom": {
"value": "0"
"value": "0",
"configured": true
},
"marginTop": {
"value": "0"
"value": "0",
"configured": true
},
"marginLeft": {
"value": "0"
"value": "0",
"configured": true
}
},
"overrides": {},
Expand Down
6 changes: 4 additions & 2 deletions packages/codegen-ui/example-schemas/complexTest3.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,12 @@
"name": "ReneButton",
"properties": {
"width": {
"value": "293px"
"value": "293px",
"configured": true
},
"height": {
"value": "45px"
"value": "45px",
"configured": true
}
}
},
Expand Down
6 changes: 4 additions & 2 deletions packages/codegen-ui/example-schemas/customChild.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
"name": "MyCustomButton",
"properties": {
"color": {
"value": "#ff0000"
"value": "#ff0000",
"configured": true
},
"width": {
"value": "20px"
"value": "20px",
"configured": true
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
{
"value": "Value!"
}
]
],
"configured": true
}
}
}
Expand Down
66 changes: 44 additions & 22 deletions packages/codegen-ui/example-schemas/parsedFixedValues.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
"name": "MyInputString",
"properties": {
"id": {
"value": "string-value"
"value": "string-value",
"configured": true
},
"value": {
"value": "raw string value"
"value": "raw string value",
"configured": true
}
}
},
Expand All @@ -25,10 +27,12 @@
"name": "MyInputNumber",
"properties": {
"id": {
"value": "number-value"
"value": "number-value",
"configured": true
},
"value": {
"value": "67548"
"value": "67548",
"configured": true
}
}
},
Expand All @@ -37,11 +41,13 @@
"name": "MyInputParsedNumber",
"properties": {
"id": {
"value": "parsed-number-value"
"value": "parsed-number-value",
"configured": true
},
"value": {
"value": "67548",
"type": "Number"
"type": "Number",
"configured": true
}
}
},
Expand All @@ -50,10 +56,12 @@
"name": "MyInputBoolean",
"properties": {
"id": {
"value": "boolean-value"
"value": "boolean-value",
"configured": true
},
"value": {
"value": "true"
"value": "true",
"configured": true
}
}
},
Expand All @@ -62,11 +70,13 @@
"name": "MyInputPrasedBoolean",
"properties": {
"id": {
"value": "parsed-boolean-value"
"value": "parsed-boolean-value",
"configured": true
},
"value": {
"value": "true",
"type": "Boolean"
"type": "Boolean",
"configured": true
}
}
},
Expand All @@ -75,10 +85,12 @@
"name": "MyInputJson",
"properties": {
"id": {
"value": "json-value"
"value": "json-value",
"configured": true
},
"value": {
"value": "{\"foo\": \"bar\"}"
"value": "{\"foo\": \"bar\"}",
"configured": true
}
}
},
Expand All @@ -87,11 +99,13 @@
"name": "MyInputParsedJson",
"properties": {
"id": {
"value": "parsed-json-value"
"value": "parsed-json-value",
"configured": true
},
"value": {
"value": "{\"foo\": \"bar\"}",
"type": "Object"
"type": "Object",
"configured": true
}
}
},
Expand All @@ -100,10 +114,12 @@
"name": "MyInputArray",
"properties": {
"id": {
"value": "array-value"
"value": "array-value",
"configured": true
},
"value": {
"value": "[1,2,3]"
"value": "[1,2,3]",
"configured": true
}
}
},
Expand All @@ -112,11 +128,13 @@
"name": "MyInputParsedArray",
"properties": {
"id": {
"value": "parsed-array-value"
"value": "parsed-array-value",
"configured": true
},
"value": {
"value": "[1,2,3]",
"type": "Object"
"type": "Object",
"configured": true
}
}
},
Expand All @@ -125,10 +143,12 @@
"name": "MyInputNull",
"properties": {
"id": {
"value": "null-value"
"value": "null-value",
"configured": true
},
"value": {
"value": "null"
"value": "null",
"configured": true
}
}
},
Expand All @@ -137,11 +157,13 @@
"name": "MyInputParsedNull",
"properties": {
"id": {
"value": "parsed-null-value"
"value": "parsed-null-value",
"configured": true
},
"value": {
"value": "null",
"type": "Object"
"type": "Object",
"configured": true
}
}
}
Expand Down
9 changes: 6 additions & 3 deletions packages/codegen-ui/example-schemas/sampleCodeSnippet.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,17 @@
"name": "MyCustomButton",
"properties": {
"color": {
"value": "#ff0000"
"value": "#ff0000",
"configured": true
},
"width": {
"value": "20px"
"value": "20px",
"configured": true
},
"buttonText": {
"exposedAs": "buttonText",
"value": "Click Me"
"value": "Click Me",
"configured": true
}
}
}
Expand Down