Skip to content

Commit

Permalink
fix: remove text value from props and render bound property (#70)
Browse files Browse the repository at this point in the history
Resolves #67
  • Loading branch information
dpilch committed Sep 14, 2021
1 parent d9e0216 commit aabed87
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ export default function CustomText(props: CustomTextProps): JSX.Element {
<Text
color=\\"#ff0000\\"
width=\\"20px\\"
value=\\"Text Value\\"
{...props}
{...getOverrideProps(props.overrides, \\"Text\\")}
>
Expand Down Expand Up @@ -162,6 +161,38 @@ export default function BoxWithButton(props: BoxWithButtonProps): JSX.Element {
"
`;

exports[`amplify render tests component with binding should render build property on Text 1`] = `
"/* eslint-disable */
import React from \\"react\\";
import {
EscapeHatchProps,
Text,
getOverrideProps,
} from \\"@aws-amplify/ui-react\\";
export type TextWithDataBindingProps = {
textValue?: String,
} & {
overrides?: EscapeHatchProps | undefined | null,
};
export default function TextWithDataBinding(
props: TextWithDataBindingProps
): JSX.Element {
const { textValue } = props;
return (
<Text
color=\\"#ff0000\\"
width=\\"20px\\"
{...props}
{...getOverrideProps(props.overrides, \\"Text\\")}
>
{textValue}
</Text>
);
}
"
`;

exports[`amplify render tests component with data binding should add model imports 1`] = `
"/* eslint-disable */
import React from \\"react\\";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@ describe('amplify render tests', () => {
});
});

describe('component with binding', () => {
it('should render build property on Text', () => {
const generatedCode = generateWithAmplifyRenderer('textWithDataBinding');
expect(generatedCode).toMatchSnapshot();
});
});

describe('custom render config', () => {
it('should render ES5', () => {
expect(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"id": "1234-5678-9010",
"componentType": "Text",
"name": "TextWithDataBinding",
"properties": {
"color": {
"value": "#ff0000"
},
"width": {
"value": "20px"
},
"value": {
"bindingProperties": {
"property": "textValue"
}
}
},
"bindingProperties": {
"textValue": {
"type": "String"
}
}
}
29 changes: 23 additions & 6 deletions packages/studio-ui-codegen-react/lib/amplify-ui-renderers/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,39 @@ import {
StudioComponentProperties,
} from '@amzn/amplify-ui-codegen-schema';

import { factory, JsxElement } from 'typescript';
import { factory, JsxElement, JsxChild } from 'typescript';
import { ReactComponentRenderer } from '../react-component-renderer';
import { isBoundProperty } from '../react-component-render-helper';

export default class TextRenderer extends ReactComponentRenderer<TextProps> {
renderElement(): JsxElement {
const tagName = 'Text';
const textValue = this.component.properties.value
? (this.component.properties.value as FixedStudioComponentProperty).value ?? ''
: '';

// value should be child of Text, not a prop
const { value, ...properties } = this.component.properties;

const element = factory.createJsxElement(
this.renderOpeningElement(factory, this.component.properties, tagName),
[factory.createJsxText(textValue.toString())],
this.renderOpeningElement(factory, properties, tagName),
this.getChildren(),
factory.createJsxClosingElement(factory.createIdentifier(tagName)),
);

this.importCollection.addImport('@aws-amplify/ui-react', tagName);
return element;
}

getChildren(): JsxChild[] {
const { value } = this.component.properties;
if (isBoundProperty(value)) {
const {
bindingProperties: { property },
} = value;
return [factory.createJsxExpression(undefined, factory.createIdentifier(property))];
}
return [
factory.createJsxText(
(value ? (this.component.properties.value as FixedStudioComponentProperty).value ?? '' : '').toString(),
),
];
}
}
1 change: 1 addition & 0 deletions packages/test-generator/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ export { default as SampleCodeSnippet } from './sampleCodeSnippet.json';
export { default as TextGolden } from './textGolden.json';
export { default as CollectionWithBinding } from './collectionWithBinding.json';
export { default as ComponentWithDataBinding } from './componentWithDataBinding.json';
export { default as TextWithDataBinding } from './textWithDataBinding.json';
23 changes: 23 additions & 0 deletions packages/test-generator/lib/textWithDataBinding.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"id": "1234-5678-9010",
"componentType": "Text",
"name": "TextWithDataBinding",
"properties": {
"color": {
"value": "#ff0000"
},
"width": {
"value": "20px"
},
"value": {
"bindingProperties": {
"property": "textValue"
}
}
},
"bindingProperties": {
"textValue": {
"type": "String"
}
}
}

0 comments on commit aabed87

Please sign in to comment.