Skip to content

Commit

Permalink
fix: update import file path to handle aliased children (#814)
Browse files Browse the repository at this point in the history
  • Loading branch information
rtpascual authored Dec 8, 2022
1 parent 3530c45 commit ffe7659
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -4878,6 +4878,46 @@ export default function CustomButton(
"
`;

exports[`amplify render tests custom components alias parent should render declarations 1`] = `
"import * as React from \\"react\\";
import { EscapeHatchProps } from \\"@aws-amplify/ui-react/internal\\";
import { ViewTestProps } from \\"./ViewTest\\";
export declare type CustomParentAndChildrenProps = React.PropsWithChildren<Partial<ViewTestProps> & {
overrides?: EscapeHatchProps | undefined | null;
}>;
export default function CustomParentAndChildren(props: CustomParentAndChildrenProps): React.ReactElement;
"
`;

exports[`amplify render tests custom components alias parent should render parent with aliased child instead of primitive 1`] = `
"/* eslint-disable */
import * as React from \\"react\\";
import {
EscapeHatchProps,
getOverrideProps,
} from \\"@aws-amplify/ui-react/internal\\";
import { GridCustom } from \\"./Grid\\";
import ViewTest, { ViewTestProps } from \\"./ViewTest\\";

export type AliasParentProps = React.PropsWithChildren<
Partial<ViewTestProps> & {
overrides?: EscapeHatchProps | undefined | null;
}
>;
export default function AliasParent(
props: AliasParentProps
): React.ReactElement {
const { overrides, ...rest } = props;
return (
/* @ts-ignore: TS2322 */
<ViewTest {...getOverrideProps(overrides, \\"AliasParent\\")} {...rest}>
<GridCustom {...getOverrideProps(overrides, \\"Grid\\")}></GridCustom>
</ViewTest>
);
}
"
`;

exports[`amplify render tests custom components custom children should render component with custom children 1`] = `
"/* eslint-disable */
import * as React from \\"react\\";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,21 @@ describe('amplify render tests', () => {
).toMatchSnapshot();
});
});

describe('alias parent', () => {
it('should render parent with aliased child instead of primitive', () => {
expect(generateWithAmplifyRenderer('custom/aliasParent').componentText).toMatchSnapshot();
});

it('should render declarations', () => {
expect(
generateWithAmplifyRenderer('custom/customParentAndChildren', {
script: ScriptKind.JS,
renderTypeDeclarations: true,
}).declaration,
).toMatchSnapshot();
});
});
});

describe('primitives', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
import { StudioComponentChild } from '@aws-amplify/codegen-ui';
import { JsxChild, JsxElement, factory } from 'typescript';
import { Primitive } from '../primitive';
import { ReactComponentRenderer } from '../react-component-renderer';

export default class CustomComponentRenderer<TPropIn> extends ReactComponentRenderer<TPropIn> {
Expand All @@ -26,7 +27,11 @@ export default class CustomComponentRenderer<TPropIn> extends ReactComponentRend
factory.createJsxClosingElement(factory.createIdentifier(this.component.componentType)),
);

this.importCollection.addImport(`./${this.component.componentType}`, 'default');
if (Object.keys(Primitive).includes(this.component.name) && this.component.componentType.match(/(Custom$)/g)) {
this.importCollection.addImport(`./${this.component.name}`, this.component.componentType);
} else {
this.importCollection.addImport(`./${this.component.componentType}`, 'default');
}

return element;
}
Expand Down
14 changes: 14 additions & 0 deletions packages/codegen-ui/example-schemas/custom/aliasParent.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "AliasParent",
"children": [
{
"name": "Grid",
"componentType": "GridCustom",
"properties": {}
}
],
"properties": {},
"id": "1234-1234",
"componentType": "ViewTest",
"schemaVersion": "1.0"
}

0 comments on commit ffe7659

Please sign in to comment.