From 3a3e13c61b773dd51be237d14106095357020e1a Mon Sep 17 00:00:00 2001 From: swaysway <7465495+SwaySway@users.noreply.github.com> Date: Fri, 2 Sep 2022 23:21:16 +0000 Subject: [PATCH] feat: add support for nested json --- ...studio-ui-codegen-react-forms.test.ts.snap | 453 +++++++++++++----- .../__utils__/amplify-renderer-generator.ts | 20 +- .../__snapshots__/form-state.test.ts.snap | 9 + .../__snapshots__/type-helper.test.ts.snap | 25 + .../lib/__tests__/forms/form-state.test.ts | 59 +++ .../lib/__tests__/forms/type-helper.test.ts | 61 +++ .../form-renderer-helper.test.ts.snap | 8 +- .../studio-ui-codegen-react-forms.test.ts | 6 + .../lib/__tests__/utils/fetch-by-path.test.ts | 39 ++ .../lib/amplify-ui-renderers/form.ts | 4 +- .../lib/forms/form-renderer-helper.ts | 382 +++++---------- .../codegen-ui-react/lib/forms/form-state.ts | 174 +++++++ .../lib/forms/react-form-renderer.ts | 58 ++- .../codegen-ui-react/lib/forms/type-helper.ts | 215 +++++++++ .../lib/forms/typescript-type-map.ts | 1 + .../lib/imports/import-mapping.ts | 2 + packages/codegen-ui-react/lib/index.ts | 1 + .../lib/react-component-renderer.ts | 2 +- .../react-utils-studio-template-renderer.ts | 5 +- .../lib/utils/json-path-fetch.ts | 232 +++++++++ packages/codegen-ui-react/package.json | 6 + .../forms/bio-nested-create.json | 59 +++ .../lib/types/form/form-metadata.ts | 1 - .../lib/utils/form-component-metadata.ts | 2 +- 24 files changed, 1399 insertions(+), 425 deletions(-) create mode 100644 packages/codegen-ui-react/lib/__tests__/forms/__snapshots__/form-state.test.ts.snap create mode 100644 packages/codegen-ui-react/lib/__tests__/forms/__snapshots__/type-helper.test.ts.snap create mode 100644 packages/codegen-ui-react/lib/__tests__/forms/form-state.test.ts create mode 100644 packages/codegen-ui-react/lib/__tests__/forms/type-helper.test.ts create mode 100644 packages/codegen-ui-react/lib/__tests__/utils/fetch-by-path.test.ts create mode 100644 packages/codegen-ui-react/lib/forms/form-state.ts create mode 100644 packages/codegen-ui-react/lib/forms/type-helper.ts create mode 100644 packages/codegen-ui-react/lib/utils/json-path-fetch.ts create mode 100644 packages/codegen-ui/example-schemas/forms/bio-nested-create.json diff --git a/packages/codegen-ui-react/lib/__tests__/__snapshots__/studio-ui-codegen-react-forms.test.ts.snap b/packages/codegen-ui-react/lib/__tests__/__snapshots__/studio-ui-codegen-react-forms.test.ts.snap index f5f77b0da..3caff2caf 100644 --- a/packages/codegen-ui-react/lib/__tests__/__snapshots__/studio-ui-codegen-react-forms.test.ts.snap +++ b/packages/codegen-ui-react/lib/__tests__/__snapshots__/studio-ui-codegen-react-forms.test.ts.snap @@ -3,11 +3,8 @@ exports[`amplify form renderer tests custom form tests should render a custom backed form 1`] = ` "/* eslint-disable */ import * as React from \\"react\\"; -import { - getOverrideProps, - useStateMutationAction, -} from \\"@aws-amplify/ui-react/internal\\"; import { validateField } from \\"./utils\\"; +import { getOverrideProps } from \\"@aws-amplify/ui-react/internal\\"; import { Button, Flex, @@ -25,8 +22,7 @@ export default function CustomDataForm(props) { overrides, ...rest } = props; - const [modelFields, setModelFields] = useStateMutationAction({}); - const [errors, setErrors] = useStateMutationAction({}); + const [errors, setErrors] = React.useState({}); const validations = { name: [{ type: \\"Required\\" }], email: [{ type: \\"Required\\" }], @@ -35,11 +31,9 @@ export default function CustomDataForm(props) { }; const runValidationTasks = async (fieldName, value) => { let validationResponse = validateField(value, validations[fieldName]); - if (onValidate?.[fieldName]) { - validationResponse = await onValidate[fieldName]( - value, - validationResponse - ); + const customValidator = fetchByPath(onValidate, fieldName); + if (customValidator) { + validationResponse = await customValidator(value, validationResponse); } setErrors((errors) => ({ ...errors, [fieldName]: validationResponse })); return validationResponse; @@ -48,6 +42,12 @@ export default function CustomDataForm(props) {