diff --git a/packages/react/src/components/Authenticator/shared/FormField.tsx b/packages/react/src/components/Authenticator/shared/FormField.tsx index b433c399f5d..187b61443e0 100644 --- a/packages/react/src/components/Authenticator/shared/FormField.tsx +++ b/packages/react/src/components/Authenticator/shared/FormField.tsx @@ -5,8 +5,8 @@ import { PasswordField } from '../../../primitives/PasswordField'; import { PhoneNumberField } from '../../../primitives/PhoneNumberField'; import { TextField } from '../../../primitives/TextField'; import { useAuthenticator } from '@aws-amplify/ui-react-core'; -import { ValidationErrors } from './ValidationErrors'; import { useStableId } from '../../../primitives/utils/useStableId'; +import { ValidationErrors } from '../../shared/ValidationErrors'; export interface FormFieldProps extends Omit { // label is a required prop for the UI field components used in FormField @@ -45,7 +45,11 @@ export function FormField({ hasError={hasError} aria-describedby={ariaDescribedBy} /> - + ); } else if (type === 'password') { @@ -58,7 +62,11 @@ export function FormField({ hasError={hasError} aria-describedby={ariaDescribedBy} /> - + ); } else { @@ -72,7 +80,11 @@ export function FormField({ type={type} aria-describedby={ariaDescribedBy} /> - + ); } diff --git a/packages/react/src/components/Authenticator/shared/ValidationErrors.tsx b/packages/react/src/components/shared/ValidationErrors.tsx similarity index 69% rename from packages/react/src/components/Authenticator/shared/ValidationErrors.tsx rename to packages/react/src/components/shared/ValidationErrors.tsx index 16826db6fb7..5af74c78a1f 100644 --- a/packages/react/src/components/Authenticator/shared/ValidationErrors.tsx +++ b/packages/react/src/components/shared/ValidationErrors.tsx @@ -1,21 +1,25 @@ import React from 'react'; import { translate } from '@aws-amplify/ui'; -import { View } from '../../../primitives/View'; -import { Text } from '../../../primitives/Text'; +import { View } from '../../primitives/View'; +import { Text } from '../../primitives/Text'; export interface ValidationErrorsProps { errors: string[]; id?: string; + dataAttr?: string; } export const ValidationErrors = ({ errors, id, + dataAttr, }: ValidationErrorsProps): JSX.Element => { if (!(errors?.length > 0)) return null; + const dataAttrProp = dataAttr ? { [dataAttr]: true } : {}; + return ( - + {errors.map((error) => { return ( diff --git a/packages/react/src/components/shared/__tests__/ValidationErrors.test.tsx b/packages/react/src/components/shared/__tests__/ValidationErrors.test.tsx new file mode 100644 index 00000000000..4e338f8ac1b --- /dev/null +++ b/packages/react/src/components/shared/__tests__/ValidationErrors.test.tsx @@ -0,0 +1,30 @@ +import React from 'react'; +import { render, screen } from '@testing-library/react'; +import { ValidationErrors } from '../ValidationErrors'; + +describe('ValidationErrors', () => { + it('renders errors as expected', async () => { + const { container } = render( + + ); + expect(container).toMatchSnapshot(); + expect(await screen.findByText('mock error 1')).toBeDefined(); + expect(await screen.findByText('mock error 2')).toBeDefined(); + }); + + it('renders nothing if there are no errors', async () => { + const { container } = render(); + expect(container).toMatchInlineSnapshot(`
`); + }); + + it('renders dataAttr as expected', () => { + const dataAttr = 'data-amplify-sign-up-errors'; + const { container } = render( + + ); + expect(container).toMatchSnapshot(); + }); +}); diff --git a/packages/react/src/components/shared/__tests__/__snapshots__/ValidationErrors.test.tsx.snap b/packages/react/src/components/shared/__tests__/__snapshots__/ValidationErrors.test.tsx.snap new file mode 100644 index 00000000000..d714aa9e933 --- /dev/null +++ b/packages/react/src/components/shared/__tests__/__snapshots__/ValidationErrors.test.tsx.snap @@ -0,0 +1,45 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`ValidationErrors renders dataAttr as expected 1`] = ` +
+
+ + +
+
+`; + +exports[`ValidationErrors renders errors as expected 1`] = ` +
+
+ + +
+
+`; diff --git a/packages/react/src/components/shared/index.ts b/packages/react/src/components/shared/index.ts new file mode 100644 index 00000000000..f1c61bff0be --- /dev/null +++ b/packages/react/src/components/shared/index.ts @@ -0,0 +1 @@ +export { ValidationErrors } from './ValidationErrors';