Skip to content

Commit

Permalink
feat: render default theme option (#749)
Browse files Browse the repository at this point in the history
Co-authored-by: David Lopez <lopezbnd@amazon.com>
  • Loading branch information
letsbelopez and David Lopez authored Nov 7, 2022
1 parent 9aa8c73 commit 11e08dd
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ exports[`react theme renderer tests renderThemeJson should render theme json suc
}"
`;

exports[`react theme renderer tests theme should render the default theme 1`] = `
"/* eslint-disable */
import { createTheme, defaultTheme } from \\"@aws-amplify/ui-react\\";
export default createTheme(defaultTheme);
"
`;

exports[`react theme renderer tests theme should render the theme 1`] = `
"/* eslint-disable */
import { createTheme } from \\"@aws-amplify/ui-react\\";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,19 @@
*/
import { StudioTemplateRendererFactory, StudioTheme } from '@aws-amplify/codegen-ui';
import { ScriptTarget, ScriptKind, ReactRenderConfig } from '..';
import { ReactThemeStudioTemplateRenderer } from '../react-theme-studio-template-renderer';
import {
ReactThemeStudioTemplateRenderer,
ReactThemeStudioTemplateRendererOptions,
} from '../react-theme-studio-template-renderer';
import { loadSchemaFromJSONFile } from './__utils__';

function generateWithThemeRenderer(jsonFile: string, renderConfig: ReactRenderConfig = {}): string {
function generateWithThemeRenderer(
jsonFile: string,
renderConfig: ReactRenderConfig = {},
options?: ReactThemeStudioTemplateRendererOptions,
): string {
const rendererFactory = new StudioTemplateRendererFactory(
(theme: StudioTheme) => new ReactThemeStudioTemplateRenderer(theme, renderConfig),
(theme: StudioTheme) => new ReactThemeStudioTemplateRenderer(theme, renderConfig, options),
);
return rendererFactory.buildRenderer(loadSchemaFromJSONFile(jsonFile)).renderComponent().componentText;
}
Expand Down Expand Up @@ -49,6 +56,10 @@ describe('react theme renderer tests', () => {
it('should render the theme with ES5', () => {
expect(generateWithThemeRenderer('theme', { target: ScriptTarget.ES5, script: ScriptKind.JS })).toMatchSnapshot();
});

it('should render the default theme', () => {
expect(generateWithThemeRenderer('theme', {}, { renderDefaultTheme: true })).toMatchSnapshot();
});
});

describe('renderThemeJson', () => {
Expand Down
2 changes: 2 additions & 0 deletions packages/codegen-ui-react/lib/imports/import-mapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,12 @@ export enum ImportValue {
VALIDATE_FIELD_CODEGEN = 'validateField',
FORMATTER = 'formatter',
FETCH_BY_PATH = 'fetchByPath',
DEFAULT_THEME = 'defaultTheme',
}

export const ImportMapping: Record<ImportValue, ImportSource> = {
[ImportValue.CREATE_THEME]: ImportSource.UI_REACT,
[ImportValue.DEFAULT_THEME]: ImportSource.UI_REACT,
[ImportValue.ESCAPE_HATCH_PROPS]: ImportSource.UI_REACT_INTERNAL,
[ImportValue.GET_OVERRIDE_PROPS]: ImportSource.UI_REACT_INTERNAL,
[ImportValue.USE_AUTH]: ImportSource.UI_REACT_INTERNAL,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ import {
} from './react-studio-template-renderer-helper';
import { RequiredKeys } from './utils/type-utils';

export type ReactThemeStudioTemplateRendererOptions = {
renderDefaultTheme?: boolean;
};

export class ReactThemeStudioTemplateRenderer extends StudioTemplateRenderer<
string,
StudioTheme,
Expand All @@ -57,15 +61,18 @@ export class ReactThemeStudioTemplateRenderer extends StudioTemplateRenderer<

protected renderConfig: RequiredKeys<ReactRenderConfig, keyof typeof defaultRenderConfig>;

protected options: ReactThemeStudioTemplateRendererOptions | undefined;

fileName: string;

constructor(theme: StudioTheme, renderConfig: ReactRenderConfig) {
constructor(theme: StudioTheme, renderConfig: ReactRenderConfig, options?: ReactThemeStudioTemplateRendererOptions) {
super(theme, new ReactOutputManager(), renderConfig);
this.renderConfig = {
...defaultRenderConfig,
...renderConfig,
};
this.fileName = `${this.component.name}.${scriptKindToFileExtensionNonReact(this.renderConfig.script)}`;
this.options = options;
}

renderComponentInternal() {
Expand Down Expand Up @@ -106,6 +113,9 @@ export class ReactThemeStudioTemplateRenderer extends StudioTemplateRenderer<
*/
private buildImports() {
this.importCollection.addMappedImport(ImportValue.CREATE_THEME);
if (this.options?.renderDefaultTheme) {
this.importCollection.addMappedImport(ImportValue.DEFAULT_THEME);
}

return this.importCollection.buildImportStatements(true);
}
Expand All @@ -118,7 +128,11 @@ export class ReactThemeStudioTemplateRenderer extends StudioTemplateRenderer<
undefined,
undefined,
undefined,
factory.createCallExpression(factory.createIdentifier('createTheme'), undefined, [this.buildThemeObject()]),
factory.createCallExpression(factory.createIdentifier('createTheme'), undefined, [
this.options?.renderDefaultTheme
? factory.createIdentifier(ImportValue.DEFAULT_THEME)
: this.buildThemeObject(),
]),
);
}

Expand Down

0 comments on commit 11e08dd

Please sign in to comment.