From f5d7c67134a70c8ece2ebc03e89d11691446052d Mon Sep 17 00:00:00 2001 From: charles shin Date: Mon, 22 May 2023 16:28:22 -0700 Subject: [PATCH] fix: removing obselete snapshot --- ...studio-ui-codegen-react-forms.test.ts.snap | 790 ------------------ 1 file changed, 790 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 5b2887a8..cc034376 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 @@ -3521,796 +3521,6 @@ export default function BookCreateForm( " `; -exports[`amplify form renderer tests GraphQL form tests should generate a update form 1`] = ` -"/* eslint-disable */ -import * as React from \\"react\\"; -import { - Badge, - Button, - Divider, - Flex, - Grid, - GridProps, - Icon, - ScrollView, - Text, - TextAreaField, - TextAreaFieldProps, - TextField, - TextFieldProps, - useTheme, -} from \\"@aws-amplify/ui-react\\"; -import { - EscapeHatchProps, - getOverrideProps, -} from \\"@aws-amplify/ui-react/internal\\"; -import { Post } from \\"../API\\"; -import { fetchByPath, validateField } from \\"./utils\\"; -import { API } from \\"aws-amplify\\"; -import { getPost } from \\"../graphql/queries\\"; -import { updatePost } from \\"../graphql/mutations\\"; - -export declare type ValidationResponse = { - hasError: boolean; - errorMessage?: string; -}; -export declare type ValidationFunction = ( - value: T, - validationResponse: ValidationResponse -) => ValidationResponse | Promise; -export declare type MyPostFormInputValues = { - TextAreaFieldbbd63464?: string; - caption?: string; - username?: string; - profile_url?: string; - post_url?: string; - metadata?: string; - nonModelField?: string; - nonModelFieldArray?: string[]; -}; -export declare type MyPostFormValidationValues = { - TextAreaFieldbbd63464?: ValidationFunction; - caption?: ValidationFunction; - username?: ValidationFunction; - profile_url?: ValidationFunction; - post_url?: ValidationFunction; - metadata?: ValidationFunction; - nonModelField?: ValidationFunction; - nonModelFieldArray?: ValidationFunction; -}; -export declare type PrimitiveOverrideProps = Partial & - React.DOMAttributes; -export declare type MyPostFormOverridesProps = { - MyPostFormGrid?: PrimitiveOverrideProps; - TextAreaFieldbbd63464?: PrimitiveOverrideProps; - caption?: PrimitiveOverrideProps; - username?: PrimitiveOverrideProps; - profile_url?: PrimitiveOverrideProps; - post_url?: PrimitiveOverrideProps; - metadata?: PrimitiveOverrideProps; - nonModelField?: PrimitiveOverrideProps; - nonModelFieldArray?: PrimitiveOverrideProps; -} & EscapeHatchProps; -export type MyPostFormProps = React.PropsWithChildren< - { - overrides?: MyPostFormOverridesProps | undefined | null; - } & { - id?: string; - post?: Post; - onSubmit?: (fields: MyPostFormInputValues) => MyPostFormInputValues; - onSuccess?: (fields: MyPostFormInputValues) => void; - onError?: (fields: MyPostFormInputValues, errorMessage: string) => void; - onCancel?: () => void; - onChange?: (fields: MyPostFormInputValues) => MyPostFormInputValues; - onValidate?: MyPostFormValidationValues; - } & React.CSSProperties ->; -function ArrayField({ - items = [], - onChange, - label, - inputFieldRef, - children, - hasError, - setFieldValue, - currentFieldValue, - defaultFieldValue, - lengthLimit, - getBadgeText, - errorMessage, -}) { - const labelElement = {label}; - const { - tokens: { - components: { - fieldmessages: { error: errorStyles }, - }, - }, - } = useTheme(); - const [selectedBadgeIndex, setSelectedBadgeIndex] = React.useState(); - const [isEditing, setIsEditing] = React.useState(); - React.useEffect(() => { - if (isEditing) { - inputFieldRef?.current?.focus(); - } - }, [isEditing]); - const removeItem = async (removeIndex) => { - const newItems = items.filter((value, index) => index !== removeIndex); - await onChange(newItems); - setSelectedBadgeIndex(undefined); - }; - const addItem = async () => { - if ( - currentFieldValue !== undefined && - currentFieldValue !== null && - currentFieldValue !== \\"\\" && - !hasError - ) { - const newItems = [...items]; - if (selectedBadgeIndex !== undefined) { - newItems[selectedBadgeIndex] = currentFieldValue; - setSelectedBadgeIndex(undefined); - } else { - newItems.push(currentFieldValue); - } - await onChange(newItems); - setIsEditing(false); - } - }; - const arraySection = ( - - {!!items?.length && ( - - {items.map((value, index) => { - return ( - { - setSelectedBadgeIndex(index); - setFieldValue(items[index]); - setIsEditing(true); - }} - > - {getBadgeText ? getBadgeText(value) : value.toString()} - { - event.stopPropagation(); - removeItem(index); - }} - /> - - ); - })} - - )} - - - ); - if (lengthLimit !== undefined && items.length >= lengthLimit && !isEditing) { - return ( - - {labelElement} - {arraySection} - - ); - } - return ( - - {labelElement} - {isEditing && children} - {!isEditing ? ( - <> - - {errorMessage && hasError && ( - - {errorMessage} - - )} - - ) : ( - - {(currentFieldValue || isEditing) && ( - - )} - - - )} - {arraySection} - - ); -} -export default function MyPostForm(props: MyPostFormProps): React.ReactElement { - const { - id: idProp, - post: postModelProp, - onSuccess, - onError, - onSubmit, - onCancel, - onValidate, - onChange, - overrides, - ...rest - } = props; - const initialValues = { - TextAreaFieldbbd63464: \\"\\", - caption: \\"\\", - username: \\"\\", - profile_url: \\"\\", - post_url: \\"\\", - metadata: \\"\\", - nonModelField: \\"\\", - nonModelFieldArray: [], - }; - const [TextAreaFieldbbd63464, setTextAreaFieldbbd63464] = React.useState( - initialValues.TextAreaFieldbbd63464 - ); - const [caption, setCaption] = React.useState(initialValues.caption); - const [username, setUsername] = React.useState(initialValues.username); - const [profile_url, setProfile_url] = React.useState( - initialValues.profile_url - ); - const [post_url, setPost_url] = React.useState(initialValues.post_url); - const [metadata, setMetadata] = React.useState(initialValues.metadata); - const [nonModelField, setNonModelField] = React.useState( - initialValues.nonModelField - ); - const [nonModelFieldArray, setNonModelFieldArray] = React.useState( - initialValues.nonModelFieldArray - ); - const [errors, setErrors] = React.useState({}); - const resetStateValues = () => { - const cleanValues = postRecord - ? { ...initialValues, ...postRecord } - : initialValues; - setTextAreaFieldbbd63464(cleanValues.TextAreaFieldbbd63464); - setCaption(cleanValues.caption); - setUsername(cleanValues.username); - setProfile_url(cleanValues.profile_url); - setPost_url(cleanValues.post_url); - setMetadata( - typeof cleanValues.metadata === \\"string\\" - ? cleanValues.metadata - : JSON.stringify(cleanValues.metadata) - ); - setNonModelField( - typeof cleanValues.nonModelField === \\"string\\" - ? cleanValues.nonModelField - : JSON.stringify(cleanValues.nonModelField) - ); - setNonModelFieldArray( - cleanValues.nonModelFieldArray?.map((item) => - typeof item === \\"string\\" ? item : JSON.stringify(item) - ) ?? [] - ); - setCurrentNonModelFieldArrayValue(\\"\\"); - setErrors({}); - }; - const [postRecord, setPostRecord] = React.useState(postModelProp); - React.useEffect(() => { - const queryData = async () => { - const record = idProp - ? await API.graphql({ - query: getPost, - variables: { - input: { - idProp, - }, - }, - }) - : postModelProp; - setPostRecord(record); - }; - queryData(); - }, [idProp, postModelProp]); - React.useEffect(resetStateValues, [postRecord]); - const [currentNonModelFieldArrayValue, setCurrentNonModelFieldArrayValue] = - React.useState(\\"\\"); - const nonModelFieldArrayRef = React.createRef(); - const validations = { - TextAreaFieldbbd63464: [], - caption: [], - username: [], - profile_url: [{ type: \\"URL\\" }], - post_url: [{ type: \\"URL\\" }], - metadata: [{ type: \\"JSON\\" }], - nonModelField: [{ type: \\"JSON\\" }], - nonModelFieldArray: [{ type: \\"JSON\\" }], - }; - const runValidationTasks = async ( - fieldName, - currentValue, - getDisplayValue - ) => { - const value = - currentValue && getDisplayValue - ? getDisplayValue(currentValue) - : currentValue; - let validationResponse = validateField(value, validations[fieldName]); - const customValidator = fetchByPath(onValidate, fieldName); - if (customValidator) { - validationResponse = await customValidator(value, validationResponse); - } - setErrors((errors) => ({ ...errors, [fieldName]: validationResponse })); - return validationResponse; - }; - return ( - /* @ts-ignore: TS2322 */ - { - event.preventDefault(); - let modelFields = { - TextAreaFieldbbd63464, - caption, - username, - profile_url, - post_url, - metadata, - nonModelField, - nonModelFieldArray, - }; - const validationResponses = await Promise.all( - Object.keys(validations).reduce((promises, fieldName) => { - if (Array.isArray(modelFields[fieldName])) { - promises.push( - ...modelFields[fieldName].map((item) => - runValidationTasks(fieldName, item) - ) - ); - return promises; - } - promises.push( - runValidationTasks(fieldName, modelFields[fieldName]) - ); - return promises; - }, []) - ); - if (validationResponses.some((r) => r.hasError)) { - return; - } - if (onSubmit) { - modelFields = onSubmit(modelFields); - } - try { - Object.entries(modelFields).forEach(([key, value]) => { - if (typeof value === \\"string\\" && value.trim() === \\"\\") { - modelFields[key] = undefined; - } - }); - const modelFieldsToSave = { - caption: modelFields.caption, - username: modelFields.username, - profile_url: modelFields.profile_url, - post_url: modelFields.post_url, - metadata: modelFields.metadata, - nonModelFieldArray: modelFields.nonModelFieldArray.map((s) => - JSON.parse(s) - ), - nonModelField: modelFields.nonModelField - ? JSON.parse(modelFields.nonModelField) - : modelFields.nonModelField, - }; - await API.graphql({ - query: updatePost, - variables: { - input: { - ...modelFieldsToSave, - }, - }, - }); - if (onSuccess) { - onSuccess(modelFields); - } - } catch (err) { - if (onError) { - onError(modelFields, err.message); - } - } - }} - {...getOverrideProps(overrides, \\"MyPostForm\\")} - {...rest} - > - - - - - - - - { - let { value } = e.target; - if (onChange) { - const modelFields = { - TextAreaFieldbbd63464: value, - caption, - username, - profile_url, - post_url, - metadata, - nonModelField, - nonModelFieldArray, - }; - const result = onChange(modelFields); - value = result?.TextAreaFieldbbd63464 ?? value; - } - if (errors.TextAreaFieldbbd63464?.hasError) { - runValidationTasks(\\"TextAreaFieldbbd63464\\", value); - } - setTextAreaFieldbbd63464(value); - }} - onBlur={() => - runValidationTasks(\\"TextAreaFieldbbd63464\\", TextAreaFieldbbd63464) - } - errorMessage={errors.TextAreaFieldbbd63464?.errorMessage} - hasError={errors.TextAreaFieldbbd63464?.hasError} - {...getOverrideProps(overrides, \\"TextAreaFieldbbd63464\\")} - > - { - let { value } = e.target; - if (onChange) { - const modelFields = { - TextAreaFieldbbd63464, - caption: value, - username, - profile_url, - post_url, - metadata, - nonModelField, - nonModelFieldArray, - }; - const result = onChange(modelFields); - value = result?.caption ?? value; - } - if (errors.caption?.hasError) { - runValidationTasks(\\"caption\\", value); - } - setCaption(value); - }} - onBlur={() => runValidationTasks(\\"caption\\", caption)} - errorMessage={errors.caption?.errorMessage} - hasError={errors.caption?.hasError} - {...getOverrideProps(overrides, \\"caption\\")} - > - { - let { value } = e.target; - if (onChange) { - const modelFields = { - TextAreaFieldbbd63464, - caption, - username: value, - profile_url, - post_url, - metadata, - nonModelField, - nonModelFieldArray, - }; - const result = onChange(modelFields); - value = result?.username ?? value; - } - if (errors.username?.hasError) { - runValidationTasks(\\"username\\", value); - } - setUsername(value); - }} - onBlur={() => runValidationTasks(\\"username\\", username)} - errorMessage={errors.username?.errorMessage} - hasError={errors.username?.hasError} - {...getOverrideProps(overrides, \\"username\\")} - > - { - let { value } = e.target; - if (onChange) { - const modelFields = { - TextAreaFieldbbd63464, - caption, - username, - profile_url: value, - post_url, - metadata, - nonModelField, - nonModelFieldArray, - }; - const result = onChange(modelFields); - value = result?.profile_url ?? value; - } - if (errors.profile_url?.hasError) { - runValidationTasks(\\"profile_url\\", value); - } - setProfile_url(value); - }} - onBlur={() => runValidationTasks(\\"profile_url\\", profile_url)} - errorMessage={errors.profile_url?.errorMessage} - hasError={errors.profile_url?.hasError} - {...getOverrideProps(overrides, \\"profile_url\\")} - > - { - let { value } = e.target; - if (onChange) { - const modelFields = { - TextAreaFieldbbd63464, - caption, - username, - profile_url, - post_url: value, - metadata, - nonModelField, - nonModelFieldArray, - }; - const result = onChange(modelFields); - value = result?.post_url ?? value; - } - if (errors.post_url?.hasError) { - runValidationTasks(\\"post_url\\", value); - } - setPost_url(value); - }} - onBlur={() => runValidationTasks(\\"post_url\\", post_url)} - errorMessage={errors.post_url?.errorMessage} - hasError={errors.post_url?.hasError} - {...getOverrideProps(overrides, \\"post_url\\")} - > - { - let { value } = e.target; - if (onChange) { - const modelFields = { - TextAreaFieldbbd63464, - caption, - username, - profile_url, - post_url, - metadata: value, - nonModelField, - nonModelFieldArray, - }; - const result = onChange(modelFields); - value = result?.metadata ?? value; - } - if (errors.metadata?.hasError) { - runValidationTasks(\\"metadata\\", value); - } - setMetadata(value); - }} - onBlur={() => runValidationTasks(\\"metadata\\", metadata)} - errorMessage={errors.metadata?.errorMessage} - hasError={errors.metadata?.hasError} - {...getOverrideProps(overrides, \\"metadata\\")} - > - { - let { value } = e.target; - if (onChange) { - const modelFields = { - TextAreaFieldbbd63464, - caption, - username, - profile_url, - post_url, - metadata, - nonModelField: value, - nonModelFieldArray, - }; - const result = onChange(modelFields); - value = result?.nonModelField ?? value; - } - if (errors.nonModelField?.hasError) { - runValidationTasks(\\"nonModelField\\", value); - } - setNonModelField(value); - }} - onBlur={() => runValidationTasks(\\"nonModelField\\", nonModelField)} - errorMessage={errors.nonModelField?.errorMessage} - hasError={errors.nonModelField?.hasError} - {...getOverrideProps(overrides, \\"nonModelField\\")} - > - { - let values = items; - if (onChange) { - const modelFields = { - TextAreaFieldbbd63464, - caption, - username, - profile_url, - post_url, - metadata, - nonModelField, - nonModelFieldArray: values, - }; - const result = onChange(modelFields); - values = result?.nonModelFieldArray ?? values; - } - setNonModelFieldArray(values); - setCurrentNonModelFieldArrayValue(\\"\\"); - }} - currentFieldValue={currentNonModelFieldArrayValue} - label={\\"Non model field array\\"} - items={nonModelFieldArray} - hasError={errors?.nonModelFieldArray?.hasError} - errorMessage={errors?.nonModelFieldArray?.errorMessage} - setFieldValue={setCurrentNonModelFieldArrayValue} - inputFieldRef={nonModelFieldArrayRef} - defaultFieldValue={\\"\\"} - > - { - let { value } = e.target; - if (errors.nonModelFieldArray?.hasError) { - runValidationTasks(\\"nonModelFieldArray\\", value); - } - setCurrentNonModelFieldArrayValue(value); - }} - onBlur={() => - runValidationTasks( - \\"nonModelFieldArray\\", - currentNonModelFieldArrayValue - ) - } - errorMessage={errors.nonModelFieldArray?.errorMessage} - hasError={errors.nonModelFieldArray?.hasError} - ref={nonModelFieldArrayRef} - labelHidden={true} - {...getOverrideProps(overrides, \\"nonModelFieldArray\\")} - > - - - - - - - - - - ); -} -" -`; - exports[`amplify form renderer tests GraphQL form tests should generate a update form without relationships 1`] = ` "/* eslint-disable */ import * as React from \\"react\\";