Skip to content

Commit

Permalink
feat: add built-in iconset
Browse files Browse the repository at this point in the history
  • Loading branch information
dpilch committed Nov 16, 2021
1 parent 58be31a commit 6199a72
Show file tree
Hide file tree
Showing 15 changed files with 643 additions and 2,472 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2322,6 +2322,37 @@ export default function SimplePropertyBindingDefaultValue(
}
`;
exports[`amplify render tests primitives Built-in Iconset 1`] = `
"/* eslint-disable */
import React from \\"react\\";
import {
EscapeHatchProps,
IconCloud,
IconProps,
getOverrideProps,
} from \\"@aws-amplify/ui-react\\";
export type MyIconCloudProps = React.PropsWithChildren<
Partial<IconProps> & {
overrides?: EscapeHatchProps | undefined | null;
}
>;
export default function MyIconCloud(
props: MyIconCloudProps
): React.ReactElement {
const { overrides: overridesProp, ...rest } = props;
const overrides = { ...overridesProp };
return (
/* @ts-ignore: TS2322 */
<IconCloud
{...rest}
{...getOverrideProps(overrides, \\"IconCloud\\")}
></IconCloud>
);
}
"
`;
exports[`amplify render tests primitives SliderField 1`] = `
"/* eslint-disable */
import React from \\"react\\";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -396,5 +396,9 @@ describe('amplify render tests', () => {
test('SliderField', () => {
expect(generateWithAmplifyRenderer('primitives/SliderFieldPrimitive').componentText).toMatchSnapshot();
});

test('Built-in Iconset', () => {
expect(generateWithAmplifyRenderer('builtInIconset').componentText).toMatchSnapshot();
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"componentType": "IconCloud",
"name": "MyIconCloud",
"properties": {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ import {
ViewProps,
VisuallyHiddenProps,
} from '@aws-amplify/ui-react';
import Primitive from '../primitive';
import Primitive, { isBuiltInIcon } from '../primitive';
import { ReactStudioTemplateRenderer } from '../react-studio-template-renderer';
import TextRenderer from './text';
import CustomComponentRenderer from './customComponent';
Expand All @@ -69,6 +69,12 @@ export class AmplifyRenderer extends ReactStudioTemplateRenderer {
const node = new StudioNode(component, parent);
const renderChildren = (children: StudioComponentChild[]) => children.map((child) => this.renderJsx(child, node));

if (isBuiltInIcon(component.componentType)) {
return new ReactComponentWithChildrenRenderer<IconProps>(component, this.importCollection, parent).renderElement(
renderChildren,
);
}

// add Primitive in alphabetical order
switch (component.componentType) {
case Primitive.Alert:
Expand Down
8 changes: 8 additions & 0 deletions packages/studio-ui-codegen-react/lib/primitive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
limitations under the License.
*/
import { factory, SyntaxKind, TypeParameterDeclaration, TypeNode } from 'typescript';
// use require style import to get CommonJS version of Amplify UI React
const AmplifyUI = require('@aws-amplify/ui-react'); // eslint-disable-line @typescript-eslint/no-var-requires

enum Primitive {
Alert = 'Alert',
Expand Down Expand Up @@ -97,3 +99,9 @@ export const PrimitiveTypeParameter: Partial<
reference: () => [factory.createKeywordTypeNode(SyntaxKind.AnyKeyword)],
},
};

export const iconset = new Set(Object.keys(AmplifyUI).filter((name) => name.match(/^Icon\w/)));

export function isBuiltInIcon(componentType: string): boolean {
return iconset.has(componentType);
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ import {
jsonToLiteral,
bindingPropertyUsesHook,
} from './react-studio-template-renderer-helper';
import Primitive, { isPrimitive, PrimitiveTypeParameter } from './primitive';
import Primitive, { isPrimitive, PrimitiveTypeParameter, isBuiltInIcon } from './primitive';
import { RequiredKeys } from './utils/type-utils';

export abstract class ReactStudioTemplateRenderer extends StudioTemplateRenderer<
Expand Down Expand Up @@ -318,7 +318,7 @@ export abstract class ReactStudioTemplateRenderer extends StudioTemplateRenderer
const propsType = this.getPropsTypeName(component);

const componentIsPrimitive = isPrimitive(component.componentType);
if (componentIsPrimitive) {
if (componentIsPrimitive || isBuiltInIcon(component.componentType)) {
this.importCollection.addImport('@aws-amplify/ui-react', propsType);
} else {
this.importCollection.addImport(`./${component.componentType}`, `${component.componentType}Props`);
Expand Down Expand Up @@ -1052,6 +1052,9 @@ export abstract class ReactStudioTemplateRenderer extends StudioTemplateRenderer
}

private getPropsTypeName(component: StudioComponent): string {
if (isBuiltInIcon(component.componentType)) {
return 'IconProps';
}
return `${component.componentType}Props`;
}

Expand Down
Loading

0 comments on commit 6199a72

Please sign in to comment.