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: update import file path to handle aliased children #814

Merged
merged 1 commit into from
Dec 8, 2022
Merged
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
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"
}