Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pc/modals: FormErrorMessage that always takes up space #2379

Merged
merged 2 commits into from
Jan 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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