From 4c304c653afa6ef235deda3d2b3c27f0738925b1 Mon Sep 17 00:00:00 2001 From: Hein Jeong Date: Fri, 19 Aug 2022 00:45:45 +0000 Subject: [PATCH] chore: change form validation --- ...studio-ui-codegen-react-forms.test.ts.snap | 428 +++++++----------- .../lib/forms/form-renderer-helper.ts | 302 ++++++++---- .../lib/forms/react-form-renderer.ts | 32 +- 3 files changed, 383 insertions(+), 379 deletions(-) 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 00058a495..9d96c7ad7 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,11 @@ exports[`amplify form renderer tests custom form tests should render a custom backed form 1`] = ` "/* eslint-disable */ import * as React from \\"react\\"; -import { validateField } from \\"@aws-amplify/codegen-ui-react\\"; import { getOverrideProps, useStateMutationAction, } from \\"@aws-amplify/ui-react/internal\\"; +import { validateField } from \\"@aws-amplify/codegen-ui-react\\"; import { Button, Flex, @@ -25,24 +25,23 @@ export default function customDataForm(props) { overrides, ...rest } = props; - const [nameFieldError, setNameFieldError] = useStateMutationAction({ - hasError: false, - errorMessage: \\"\\", - }); - const [emailFieldError, setEmailFieldError] = useStateMutationAction({ - hasError: false, - errorMessage: \\"\\", - }); - const [cityFieldError, setCityFieldError] = useStateMutationAction({ - hasError: false, - errorMessage: \\"\\", - }); - const [categoryFieldError, setCategoryFieldError] = useStateMutationAction({ - hasError: false, - errorMessage: \\"\\", - }); const [modelFields, setModelFields] = useStateMutationAction({}); const [formValid, setFormValid] = useStateMutationAction(true); + const [errors, setErrors] = useStateMutationAction({}); + const validations = { + name: [], + email: [], + city: [], + category: [], + }; + async function runValidationTasks(fieldName, value) { + const validationResponse = { + ...validateField(value, validations[fieldName]), + ...(await onValidate?.[fieldName]?.(value)), + }; + setErrors((errors) => ({ ...errors, [fieldName]: validationResponse })); + return validationResponse; + } return (
{ @@ -69,15 +68,11 @@ export default function customDataForm(props) { defaultValue=\\"John Doe\\" onChange={async (e) => { const { value } = e.target; - const isValidResult = onValidate?.[\\"name\\"] - ? await onValidate[\\"name\\"](value) - : validateField(value, []); - setNameFieldError({ ...nameFieldError, ...isValidResult }); - setFormValid(!nameFieldError.hasError); + await runValidationTasks(\\"name\\", value); setModelFields({ ...modelFields, name: value }); }} - errorMessage={nameFieldError.errorMessage} - hasError={nameFieldError.hasError} + errorMessage={errors.name.errorMessage} + hasError={errors.name.hasError} {...getOverrideProps(overrides, \\"name\\")} > @@ -93,15 +88,11 @@ export default function customDataForm(props) { defaultValue=\\"johndoe@amplify.com\\" onChange={async (e) => { const { value } = e.target; - const isValidResult = onValidate?.[\\"email\\"] - ? await onValidate[\\"email\\"](value) - : validateField(value, []); - setEmailFieldError({ ...emailFieldError, ...isValidResult }); - setFormValid(!emailFieldError.hasError); + await runValidationTasks(\\"email\\", value); setModelFields({ ...modelFields, email: value }); }} - errorMessage={emailFieldError.errorMessage} - hasError={emailFieldError.hasError} + errorMessage={errors.email.errorMessage} + hasError={errors.email.hasError} {...getOverrideProps(overrides, \\"email\\")} > @@ -115,15 +106,11 @@ export default function customDataForm(props) { label=\\"Label\\" onChange={async (e) => { const { value } = e.target; - const isValidResult = onValidate?.[\\"city\\"] - ? await onValidate[\\"city\\"](value) - : validateField(value, []); - setCityFieldError({ ...cityFieldError, ...isValidResult }); - setFormValid(!cityFieldError.hasError); + await runValidationTasks(\\"city\\", value); setModelFields({ ...modelFields, city: value }); }} - errorMessage={cityFieldError.errorMessage} - hasError={cityFieldError.hasError} + errorMessage={errors.city.errorMessage} + hasError={errors.city.hasError} {...getOverrideProps(overrides, \\"city\\")} >