Skip to content

Create custom component for AWS Amplify Authenticator UI

0

So Amplify Authenticator component allows creation of custom fields as long as they are using Field components, in this example: https://ui.docs.amplify.aws/react/connected-components/authenticator/customization#sign-up-fields

it is a check box:

                <CheckboxField
                  errorMessage={validationErrors.acknowledgement as string}
                  hasError={!!validationErrors.acknowledgement}
                  name="acknowledgement"
                  value="yes"
                  label="I agree with the Terms and Conditions"
                />

What I want to do is a completely custom component (build using ToggleButtons which are not Fields). But no matter what I do, I can't pass value into formData so it is not getting validated or submitted.

I see two possible solutions: 1) use some function to pass my custom value into formData 2) inherit somehow from Field component and the form picks up my custom input component automatically.

Thoughts?

asked a year ago479 views
1 Answer
0

You're on the right track with your two possible solutions. Let's explore each one in more detail:

  1. Passing custom value into formData

    • You can leverage the useAuthenticator hook provided by Amplify Authenticator to access the formData and validationErrors objects.
    • Within your custom component, you can update the formData object with your custom field value and also handle the validation logic.
    • Here's an example of how you can achieve this:
    import { useAuthenticator } from '@aws-amplify/ui-react';
    
    const CustomToggleButtons = () => {
      const { formData, setFormData, validationErrors, setValidationErrors } = useAuthenticator();
    
      const handleToggleChange = (value) => {
        setFormData((prevFormData) => ({
          ...prevFormData,
          customField: value,
        }));
    
        // Validate the custom field
        if (!value) {
          setValidationErrors((prevErrors) => ({
            ...prevErrors,
            customField: 'This field is required',
          }));
        } else {
          setValidationErrors((prevErrors) => ({
            ...prevErrors,
            customField: undefined,
          }));
        }
      };
    
      return (
        <div>
          <ToggleButtons value={formData.customField} onChange={handleToggleChange} />
        </div>
      );
    };
  2. Inheriting from the Field component:

    • Amplify Authenticator's Field component is a wrapper around the FormField component from the @aws-amplify/ui-react library.
    • You can create your own custom field component that extends the FormField component and integrates your custom input (e.g., ToggleButtons).
    • Here's an example of how you can do this:
    import { FormField } from '@aws-amplify/ui-react';
    
    const CustomField extends FormField {
      render() {
        return (
          <FormField {...this.props}>
            <ToggleButtons value={this.props.value} onChange={this.props.onChange} />
          </FormField>
        );
      }
    }
    
    // Then, use your custom field in the Authenticator
    <Authenticator>
      <CustomField
        name="customField"
        label="Custom Field"
        errorMessage={validationErrors.customField as string}
        hasError={!!validationErrors.customField}
      />
    </Authenticator>

Both solutions have their advantages. The first approach (updating formData directly) gives you more control over the validation and submission process, but you'll need to integrate it with the Authenticator's state management. The second approach (inheriting from Field) allows you to leverage the existing Authenticator functionality more seamlessly, but you'll need to ensure your custom field component is compatible with the Authenticator's requirements.

the choice will depend on your specific use case and preference. I'd recommend trying both approaches and seeing which one works best for your application.

answered a year ago
  • Output fields formData, setFormData, setValidationErrors do not exist on useAuthenticator.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.