Skip to content

Commit d3e097b

Browse files
author
Dane Pilcher
authored
feat: add built-in iconset (#219)
1 parent b84ea82 commit d3e097b

File tree

16 files changed

+748
-7263
lines changed

16 files changed

+748
-7263
lines changed

.github/workflows/check.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,4 @@ jobs:
113113
install: false
114114
start: npm start
115115
wait-on: 'http://localhost:3000'
116-
wait-on-timeout: 180
116+
wait-on-timeout: 210

packages/studio-ui-codegen-react/lib/__tests__/__snapshots__/studio-ui-codegen-react.test.ts.snap

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2377,6 +2377,37 @@ export default function SimplePropertyBindingDefaultValue(
23772377
}
23782378
`;
23792379
2380+
exports[`amplify render tests primitives Built-in Iconset 1`] = `
2381+
"/* eslint-disable */
2382+
import React from \\"react\\";
2383+
import {
2384+
EscapeHatchProps,
2385+
IconCloud,
2386+
IconProps,
2387+
getOverrideProps,
2388+
} from \\"@aws-amplify/ui-react\\";
2389+
2390+
export type MyIconCloudProps = React.PropsWithChildren<
2391+
Partial<IconProps> & {
2392+
overrides?: EscapeHatchProps | undefined | null;
2393+
}
2394+
>;
2395+
export default function MyIconCloud(
2396+
props: MyIconCloudProps
2397+
): React.ReactElement {
2398+
const { overrides: overridesProp, ...rest } = props;
2399+
const overrides = { ...overridesProp };
2400+
return (
2401+
/* @ts-ignore: TS2322 */
2402+
<IconCloud
2403+
{...rest}
2404+
{...getOverrideProps(overrides, \\"IconCloud\\")}
2405+
></IconCloud>
2406+
);
2407+
}
2408+
"
2409+
`;
2410+
23802411
exports[`amplify render tests primitives SliderField 1`] = `
23812412
"/* eslint-disable */
23822413
import React from \\"react\\";

packages/studio-ui-codegen-react/lib/__tests__/studio-ui-codegen-react.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,5 +401,9 @@ describe('amplify render tests', () => {
401401
test('SliderField', () => {
402402
expect(generateWithAmplifyRenderer('primitives/SliderFieldPrimitive').componentText).toMatchSnapshot();
403403
});
404+
405+
test('Built-in Iconset', () => {
406+
expect(generateWithAmplifyRenderer('builtInIconset').componentText).toMatchSnapshot();
407+
});
404408
});
405409
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"componentType": "IconCloud",
3+
"name": "MyIconCloud",
4+
"properties": {}
5+
}

packages/studio-ui-codegen-react/lib/amplify-ui-renderers/amplify-renderer.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ import {
5454
VisuallyHiddenProps,
5555
TextProps,
5656
} from '@aws-amplify/ui-react';
57-
import Primitive from '../primitive';
57+
import Primitive, { isBuiltInIcon } from '../primitive';
5858
import { ReactStudioTemplateRenderer } from '../react-studio-template-renderer';
5959
import CustomComponentRenderer from './customComponent';
6060
import CollectionRenderer from './collection';
@@ -69,6 +69,12 @@ export class AmplifyRenderer extends ReactStudioTemplateRenderer {
6969
const node = new StudioNode(component, parent);
7070
const renderChildren = (children: StudioComponentChild[]) => children.map((child) => this.renderJsx(child, node));
7171

72+
if (isBuiltInIcon(component.componentType)) {
73+
return new ReactComponentWithChildrenRenderer<IconProps>(component, this.importCollection, parent).renderElement(
74+
renderChildren,
75+
);
76+
}
77+
7278
// add Primitive in alphabetical order
7379
switch (component.componentType) {
7480
case Primitive.Alert:

packages/studio-ui-codegen-react/lib/primitive.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
limitations under the License.
1515
*/
1616
import { factory, SyntaxKind, TypeParameterDeclaration, TypeNode } from 'typescript';
17+
// use require style import to get CommonJS version of Amplify UI React
18+
const AmplifyUI = require('@aws-amplify/ui-react'); // eslint-disable-line @typescript-eslint/no-var-requires
1719

1820
enum Primitive {
1921
Alert = 'Alert',
@@ -97,3 +99,9 @@ export const PrimitiveTypeParameter: Partial<
9799
reference: () => [factory.createKeywordTypeNode(SyntaxKind.AnyKeyword)],
98100
},
99101
};
102+
103+
export const iconset = new Set(Object.keys(AmplifyUI).filter((name) => name.match(/^Icon\w/)));
104+
105+
export function isBuiltInIcon(componentType: string): boolean {
106+
return iconset.has(componentType);
107+
}

packages/studio-ui-codegen-react/lib/react-studio-template-renderer.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ import {
7272
jsonToLiteral,
7373
bindingPropertyUsesHook,
7474
} from './react-studio-template-renderer-helper';
75-
import Primitive, { isPrimitive, PrimitiveTypeParameter } from './primitive';
75+
import Primitive, { isPrimitive, PrimitiveTypeParameter, isBuiltInIcon } from './primitive';
7676
import { RequiredKeys } from './utils/type-utils';
7777

7878
export abstract class ReactStudioTemplateRenderer extends StudioTemplateRenderer<
@@ -316,7 +316,7 @@ export abstract class ReactStudioTemplateRenderer extends StudioTemplateRenderer
316316
const propsType = this.getPropsTypeName(component);
317317

318318
const componentIsPrimitive = isPrimitive(component.componentType);
319-
if (componentIsPrimitive) {
319+
if (componentIsPrimitive || isBuiltInIcon(component.componentType)) {
320320
this.importCollection.addImport('@aws-amplify/ui-react', propsType);
321321
} else {
322322
this.importCollection.addImport(`./${component.componentType}`, `${component.componentType}Props`);
@@ -1050,6 +1050,9 @@ export abstract class ReactStudioTemplateRenderer extends StudioTemplateRenderer
10501050
}
10511051

10521052
private getPropsTypeName(component: StudioComponent): string {
1053+
if (isBuiltInIcon(component.componentType)) {
1054+
return 'IconProps';
1055+
}
10531056
return `${component.componentType}Props`;
10541057
}
10551058

0 commit comments

Comments
 (0)