Skip to content

Commit aca2b7d

Browse files
Hein Jeonghein-j
authored andcommitted
fix: change key name and remove ref
1 parent b24087f commit aca2b7d

File tree

6 files changed

+21
-44
lines changed

6 files changed

+21
-44
lines changed

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

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24820,7 +24820,6 @@ export default function CreateProductForm(props) {
2482024820
};
2482124821
const [name, setName] = React.useState(initialValues.name);
2482224822
const [imgKeys, setImgKeys] = React.useState(initialValues.imgKeys);
24823-
const imgKeysRef = React.useRef([]);
2482424823
const [errors, setErrors] = React.useState({});
2482524824
const resetStateValues = () => {
2482624825
setName(initialValues.name);
@@ -24937,9 +24936,8 @@ export default function CreateProductForm(props) {
2493724936
isReadOnly={false}
2493824937
>
2493924938
<StorageManager
24940-
ref={imgKeysRef}
2494124939
onUploadSuccess={(files) => {
24942-
let value = files.map(({ s3Key }) => s3Key);
24940+
let value = files.map(({ key }) => key);
2494324941
if (onChange) {
2494424942
const modelFields = {
2494524943
name,
@@ -25020,7 +25018,6 @@ export default function UpdateProductForm(props) {
2502025018
};
2502125019
const [name, setName] = React.useState(initialValues.name);
2502225020
const [imgKeys, setImgKeys] = React.useState(initialValues.imgKeys);
25023-
const imgKeysRef = React.useRef([]);
2502425021
const [errors, setErrors] = React.useState({});
2502525022
const resetStateValues = () => {
2502625023
const cleanValues = productRecord
@@ -25152,10 +25149,9 @@ export default function UpdateProductForm(props) {
2515225149
isReadOnly={false}
2515325150
>
2515425151
<StorageManager
25155-
ref={imgKeysRef}
25156-
defaultFiles={imgKeys.map((s3Key) => ({ s3Key }))}
25152+
defaultFiles={imgKeys.map((key) => ({ key }))}
2515725153
onUploadSuccess={(files) => {
25158-
let value = files.map(({ s3Key }) => s3Key);
25154+
let value = files.map(({ key }) => key);
2515925155
if (onChange) {
2516025156
const modelFields = {
2516125157
name,
@@ -25240,7 +25236,6 @@ export default function UpdateProductForm(props) {
2524025236
const [singleImgKey, setSingleImgKey] = React.useState(
2524125237
initialValues.singleImgKey
2524225238
);
25243-
const singleImgKeyRef = React.useRef([]);
2524425239
const [errors, setErrors] = React.useState({});
2524525240
const resetStateValues = () => {
2524625241
const cleanValues = productRecord
@@ -25372,10 +25367,9 @@ export default function UpdateProductForm(props) {
2537225367
isReadOnly={false}
2537325368
>
2537425369
<StorageManager
25375-
ref={singleImgKeyRef}
25376-
defaultFiles={[{ s3Key: singleImgKey }]}
25370+
defaultFiles={[{ key: singleImgKey }]}
2537725371
onUploadSuccess={(files) => {
25378-
let value = files?.[0]?.s3Key;
25372+
let value = files?.[0]?.key;
2537925373
if (onChange) {
2538025374
const modelFields = {
2538125375
name,

packages/codegen-ui-react/lib/forms/form-renderer-helper/event-targets.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
import { DataFieldDataType } from '@aws-amplify/codegen-ui';
1717
import { factory, NodeFlags, Expression, SyntaxKind, Identifier } from 'typescript';
18+
import { STORAGE_FILE_KEY } from '../../utils/constants';
1819

1920
/*
2021
Builds the event target variable. Example:
@@ -44,8 +45,8 @@ const expressionMap = {
4445
),
4546
};
4647

47-
// array: files.map(({ s3Key }) => s3Key)
48-
// non-array: files?.[0]?.s3Key;
48+
// array: files.map(({ key }) => key)
49+
// non-array: files?.[0]?.key;
4950
export function extractKeyByMapping(array: string, key: string, isArray?: boolean) {
5051
if (isArray) {
5152
return factory.createCallExpression(
@@ -82,7 +83,7 @@ export function extractKeyByMapping(array: string, key: string, isArray?: boolea
8283
factory.createNumericLiteral('0'),
8384
),
8485
factory.createToken(SyntaxKind.QuestionDotToken),
85-
factory.createIdentifier('s3Key'),
86+
factory.createIdentifier(STORAGE_FILE_KEY),
8687
);
8788
}
8889

@@ -126,7 +127,7 @@ export const buildTargetVariable = (
126127
identifier: expressionMap.value,
127128
},
128129
StorageField: {
129-
expression: extractKeyByMapping('files', 's3Key', isArray),
130+
expression: extractKeyByMapping('files', STORAGE_FILE_KEY, isArray),
130131
identifier: expressionMap.value,
131132
},
132133
};

packages/codegen-ui-react/lib/forms/form-renderer-helper/form-state.ts

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ import {
4646
getSetNameIdentifier,
4747
buildUseStateExpression,
4848
getControlledComponentDefaultValue,
49-
buildUseRefExpression,
5049
} from '../../helpers';
5150
import { getElementAccessExpression } from './invalid-variable-helpers';
5251
import { shouldWrapInArrayField } from './render-checkers';
@@ -261,21 +260,6 @@ export const getUseStateHooks = (fieldConfigs: Record<string, FieldConfigMetadat
261260
}, []);
262261
};
263262

264-
/**
265-
* iterates field configs to create useRef hooks for fields that need refs.
266-
* @param fieldConfigs
267-
* @returns
268-
*/
269-
export const getUseRefHooks = (fieldConfigs: Record<string, FieldConfigMetadata>): Statement[] => {
270-
return Object.entries(fieldConfigs)
271-
.filter(([, { componentType }]) => {
272-
return componentType === 'StorageField';
273-
})
274-
.map(([name]) => {
275-
return buildUseRefExpression(`${name}Ref`, factory.createArrayLiteralExpression([], false));
276-
});
277-
};
278-
279263
/**
280264
* function used by the Clear/ Reset button
281265
* it's a reset type but we also need to clear the state of the input fields as well

packages/codegen-ui-react/lib/forms/react-form-renderer.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ import {
9191
getUseStateHooks,
9292
resetStateFunction,
9393
getCanUnlinkModelName,
94-
getUseRefHooks,
9594
} from './form-renderer-helper/form-state';
9695
import { shouldWrapInArrayField } from './form-renderer-helper/render-checkers';
9796
import {
@@ -463,8 +462,6 @@ export abstract class ReactFormTemplateRenderer extends StudioTemplateRenderer<
463462

464463
statements.push(...getUseStateHooks(formMetadata.fieldConfigs));
465464

466-
statements.push(...getUseRefHooks(formMetadata.fieldConfigs));
467-
468465
statements.push(buildUseStateExpression('errors', factory.createObjectLiteralExpression()));
469466

470467
let defaultValueVariableName: undefined | string;

packages/codegen-ui-react/lib/utils/constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,5 @@
1414
limitations under the License.
1515
*/
1616
export const COMPOSITE_PRIMARY_KEY_PROP_NAME = 'id';
17+
18+
export const STORAGE_FILE_KEY = 'key';

packages/codegen-ui-react/lib/utils/forms/storage-field-component.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import { factory, JsxAttribute, JsxAttributeLike, JsxElement, JsxExpression, Syn
2626
import { getDecoratedLabel } from '../../forms/form-renderer-helper';
2727
import { buildOnChangeStatement } from '../../forms/form-renderer-helper/event-handler-props';
2828
import { propertyToExpression } from '../../react-component-render-helper';
29+
import { STORAGE_FILE_KEY } from '../constants';
2930

3031
const fieldKeys = new Set<keyof FormDefinitionStorageFieldElement['props']>(['label', 'isRequired', 'isReadOnly']);
3132

@@ -59,13 +60,6 @@ export const renderStorageFieldComponent = (
5960
const storageManagerAttributes: JsxAttribute[] = [];
6061
const fieldAttributes: JsxAttribute[] = [];
6162

62-
storageManagerAttributes.push(
63-
factory.createJsxAttribute(
64-
factory.createIdentifier('ref'),
65-
factory.createJsxExpression(undefined, factory.createIdentifier(`${component.name}Ref`)),
66-
),
67-
);
68-
6963
if (componentMetadata.formMetadata) {
7064
const errorKey =
7165
componentName.split('.').length > 1 || !isValidVariableName(componentName)
@@ -98,7 +92,7 @@ export const renderStorageFieldComponent = (
9892
undefined,
9993
undefined,
10094
undefined,
101-
factory.createIdentifier('s3Key'),
95+
factory.createIdentifier(STORAGE_FILE_KEY),
10296
undefined,
10397
undefined,
10498
undefined,
@@ -108,7 +102,12 @@ export const renderStorageFieldComponent = (
108102
factory.createToken(SyntaxKind.EqualsGreaterThanToken),
109103
factory.createParenthesizedExpression(
110104
factory.createObjectLiteralExpression(
111-
[factory.createShorthandPropertyAssignment(factory.createIdentifier('s3Key'), undefined)],
105+
[
106+
factory.createShorthandPropertyAssignment(
107+
factory.createIdentifier(STORAGE_FILE_KEY),
108+
undefined,
109+
),
110+
],
112111
false,
113112
),
114113
),
@@ -124,7 +123,7 @@ export const renderStorageFieldComponent = (
124123
factory.createObjectLiteralExpression(
125124
[
126125
factory.createPropertyAssignment(
127-
factory.createIdentifier('s3Key'),
126+
factory.createIdentifier(STORAGE_FILE_KEY),
128127
factory.createIdentifier('singleImgKey'),
129128
),
130129
],

0 commit comments

Comments
 (0)