Skip to content

Commit

Permalink
pc/modals: FormErrorMessage that always takes up space (#2379)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssangervasi authored Jan 26, 2023
1 parent 13a90a1 commit 327a6e1
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 21 deletions.
25 changes: 25 additions & 0 deletions clients/privacy-center/components/FormErrorMessage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import {
Box,
FormErrorMessage as ChakraFormErrorMessage,
forwardRef,
useFormControlContext,
} from "@fidesui/react";

/**
* This is a thin wrapper around Chakra's FormErrorMessage that leaves room for the error message
* even when the field is valid. This prevents the form from changing height when validation runs,
* which can be annoying on blur.
*
* Chakra Source:
* https://github.com/chakra-ui/chakra-ui/blob/%40chakra-ui/react%401.8.8/packages/form-control/src/form-error.tsx
*/
export const FormErrorMessage: typeof ChakraFormErrorMessage = forwardRef(
(props, ref) => {
const field = useFormControlContext();
if (!field?.isInvalid) {
return <Box mt={2} minH={4} />;
}

return <ChakraFormErrorMessage ref={ref} {...props} />;
}
);
4 changes: 2 additions & 2 deletions clients/privacy-center/components/modals/VerificationForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
Button,
chakra,
FormControl,
FormErrorMessage,
HStack,
Input,
ModalBody,
Expand All @@ -21,6 +20,7 @@ import { ErrorToastOptions } from "~/common/toast-options";
import { Headers } from "headers-polyfill";
import { addCommonHeaders } from "~/common/CommonHeaders";
import { useLocalStorage } from "~/common/hooks";
import { FormErrorMessage } from "~/components/FormErrorMessage";

import { hostUrl } from "~/constants";
import { ModalViews, VerificationType } from "./types";
Expand Down Expand Up @@ -178,7 +178,7 @@ const VerificationForm: React.FC<VerificationFormProps> = ({
A verification code has been sent. Return to this window and enter
the code below.
</Text>
<Stack spacing={3}>
<Stack>
<FormControl
id="code"
isInvalid={touched.code && Boolean(errors.code)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
Button,
chakra,
FormControl,
FormErrorMessage,
FormLabel,
Input,
ModalBody,
Expand All @@ -20,6 +19,7 @@ import { ErrorToastOptions } from "~/common/toast-options";

import { Headers } from "headers-polyfill";
import { addCommonHeaders } from "~/common/CommonHeaders";
import { FormErrorMessage } from "~/components/FormErrorMessage";

import { config, defaultIdentityInput, hostUrl } from "~/constants";
import dynamic from "next/dynamic";
Expand Down Expand Up @@ -174,15 +174,14 @@ const ConsentRequestForm: React.FC<ConsentRequestFormProps> = ({
We will send you a verification code.
</Text>
) : null}
<Stack spacing={3}>
<Stack>
{identityInputs.email ? (
<FormControl
id="email"
isInvalid={touched.email && Boolean(errors.email)}
isRequired={identityInputs.email === "required"}
>
<FormLabel>
{identityInputs.email === "required" ? "Email*" : "Email"}
</FormLabel>
<FormLabel>Email</FormLabel>
<Input
id="email"
name="email"
Expand All @@ -201,10 +200,9 @@ const ConsentRequestForm: React.FC<ConsentRequestFormProps> = ({
<FormControl
id="phone"
isInvalid={touched.phone && Boolean(errors.phone)}
isRequired={identityInputs.phone === "required"}
>
<FormLabel>
{identityInputs.phone === "required" ? "Phone*" : "Phone"}
</FormLabel>
<FormLabel>Phone</FormLabel>
<Input
as={PhoneInput}
id="phone"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {
Button,
chakra,
FormControl,
FormErrorMessage,
FormLabel,
Input,
ModalBody,
Expand All @@ -20,6 +19,7 @@ import { Headers } from "headers-polyfill";
import { addCommonHeaders } from "~/common/CommonHeaders";
import { ErrorToastOptions, SuccessToastOptions } from "~/common/toast-options";
import { PrivacyRequestStatus } from "~/types";
import { FormErrorMessage } from "~/components/FormErrorMessage";

import { PrivacyRequestOption } from "~/types/config";
import { hostUrl, config, defaultIdentityInput } from "~/constants";
Expand Down Expand Up @@ -212,15 +212,14 @@ const PrivacyRequestForm: React.FC<PrivacyRequestFormProps> = ({
<Text fontSize="sm" color="gray.500" mb={4}>
{action.description}
</Text>
<Stack spacing={3}>
<Stack>
{identityInputs.name ? (
<FormControl
id="name"
isInvalid={touched.name && Boolean(errors.name)}
isRequired={identityInputs.name === "required"}
>
<FormLabel>
{identityInputs.name === "required" ? "Name*" : "Name"}
</FormLabel>
<FormLabel>Name</FormLabel>
<Input
id="name"
name="name"
Expand All @@ -237,10 +236,9 @@ const PrivacyRequestForm: React.FC<PrivacyRequestFormProps> = ({
<FormControl
id="email"
isInvalid={touched.email && Boolean(errors.email)}
isRequired={identityInputs.email === "required"}
>
<FormLabel>
{identityInputs.email === "required" ? "Email*" : "Email"}
</FormLabel>
<FormLabel>Email</FormLabel>
<Input
id="email"
name="email"
Expand All @@ -258,10 +256,9 @@ const PrivacyRequestForm: React.FC<PrivacyRequestFormProps> = ({
<FormControl
id="phone"
isInvalid={touched.phone && Boolean(errors.phone)}
isRequired={identityInputs.phone === "required"}
>
<FormLabel>
{identityInputs.phone === "required" ? "Phone*" : "Phone"}
</FormLabel>
<FormLabel>Phone</FormLabel>
<Input
as={PhoneInput}
id="phone"
Expand Down

0 comments on commit 327a6e1

Please sign in to comment.