-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9c4126e
commit 279d289
Showing
7 changed files
with
104 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { Typography } from "@material-tailwind/react"; | ||
|
||
export const ErrorText = ({ text, className }: { text: string; className?: string }) => { | ||
return ( | ||
<Typography variant="small" color="red" className={`mt-1 font-medium ${className}`}> | ||
{text} | ||
</Typography> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { Input, InputProps } from "@material-tailwind/react"; | ||
import { FieldError, FieldValues, RegisterOptions, useFormContext } from "react-hook-form"; | ||
import { ErrorText } from "./ErrorText"; | ||
|
||
interface FormInputProps extends Pick<InputProps, "size" | "label" | "placeholder" | "type"> { | ||
name: string; | ||
registerOptions?: RegisterOptions<FieldValues, string>; | ||
fieldError?: FieldError; | ||
} | ||
|
||
export const FormInput = ({ name, registerOptions, fieldError, ...props }: FormInputProps) => { | ||
const { | ||
register, | ||
formState: { errors }, | ||
} = useFormContext(); | ||
|
||
const error = fieldError?.message || errors?.[name]?.message?.toString() || ""; | ||
|
||
return ( | ||
<div className="w-full"> | ||
<Input {...register(name, registerOptions)} size="lg" error={!!error} {...props} /> | ||
{error && <ErrorText text={error} />} | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./FormInput"; | ||
export * from "./ErrorText"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ import { toast } from "react-toastify"; | |
import { useCountries } from "use-react-countries"; | ||
import { z } from "zod"; | ||
import { DatePicker } from "@components/DatePicker/DatePicker.tsx"; | ||
import { ErrorText, FormInput } from "@components/Form"; | ||
import { REGISTER_USER } from "@utils/queries/RegisterUser.ts"; | ||
|
||
const formSchema = z | ||
|
@@ -51,7 +52,6 @@ export function RegisterForm() { | |
}, | ||
}); | ||
const { | ||
register, | ||
handleSubmit, | ||
reset, | ||
formState: { errors }, | ||
|
@@ -84,36 +84,12 @@ export function RegisterForm() { | |
return ( | ||
<FormProvider {...form}> | ||
<form onSubmit={handleSubmit(onSubmit)} className="space-y-6 py-4"> | ||
<Input | ||
{...register("email")} | ||
name="email" | ||
type="email" | ||
size="lg" | ||
label="Email address" | ||
placeholder="[email protected]" | ||
error={!!errors.email} | ||
/> | ||
<FormInput name="email" type="email" label="Email address" placeholder="[email protected]" /> | ||
|
||
<div> | ||
<div className="flex flex-col space-y-2 md:flex-row md:space-x-4 md:space-y-0"> | ||
<Input | ||
{...register("password")} | ||
name="password" | ||
size="lg" | ||
type="password" | ||
label="Password" | ||
placeholder="*******" | ||
error={!!errors.password} | ||
/> | ||
<Input | ||
{...register("passwordConfirm")} | ||
name="passwordConfirm" | ||
size="lg" | ||
type="password" | ||
label="Password confirmation" | ||
placeholder="*******" | ||
error={!!errors.passwordConfirm} | ||
/> | ||
<FormInput name="password" type="password" label="Password" placeholder="*******" /> | ||
<FormInput name="passwordConfirm" type="password" label="Password confirmation" placeholder="*******" /> | ||
</div> | ||
|
||
<Typography variant="small" color="gray" className="mt-2 flex items-center gap-1 font-normal"> | ||
|
@@ -122,51 +98,43 @@ export function RegisterForm() { | |
</Typography> | ||
</div> | ||
|
||
<Input | ||
{...register("firstName")} | ||
name="firstName" | ||
size="lg" | ||
label="First name" | ||
placeholder="" | ||
error={!!errors.firstName} | ||
/> | ||
<FormInput name="firstName" label="First name" placeholder="" /> | ||
|
||
<Input | ||
{...register("lastName")} | ||
name="lastName" | ||
size="lg" | ||
label="Last name" | ||
placeholder="" | ||
error={!!errors.lastName} | ||
/> | ||
<Input name="lastName" label="Last name" /> | ||
|
||
<DatePicker | ||
name="dateOfBirth" | ||
label="Date of birth" | ||
disabled={{ after: new Date() }} | ||
error={!!errors.dateOfBirth} | ||
/> | ||
<div> | ||
<DatePicker | ||
name="dateOfBirth" | ||
label="Date of birth" | ||
disabled={{ after: new Date() }} | ||
error={!!errors.dateOfBirth} | ||
/> | ||
<ErrorText text={errors.dateOfBirth?.message || ""} /> | ||
</div> | ||
|
||
<Select | ||
size="lg" | ||
label="Select Country" | ||
onChange={value => form.setValue("country", value || "")} | ||
selected={element => | ||
element && | ||
cloneElement(element, { | ||
disabled: true, | ||
className: "flex items-center opacity-100 px-0 gap-2 pointer-events-none", | ||
}) | ||
} | ||
error={!!errors.country} | ||
> | ||
{orderedCountries().map(({ name, flags }) => ( | ||
<Option key={name} value={name} className="flex items-center gap-2"> | ||
<img src={flags.svg} alt={name} className="h-5 w-5 rounded-full object-cover" /> | ||
{name} | ||
</Option> | ||
))} | ||
</Select> | ||
<div> | ||
<Select | ||
size="lg" | ||
label="Select Country" | ||
onChange={value => form.setValue("country", value || "")} | ||
selected={element => | ||
element && | ||
cloneElement(element, { | ||
disabled: true, | ||
className: "flex items-center opacity-100 px-0 gap-2 pointer-events-none", | ||
}) | ||
} | ||
error={!!errors.country} | ||
> | ||
{orderedCountries().map(({ name, flags }) => ( | ||
<Option key={name} value={name} className="flex items-center gap-2"> | ||
<img src={flags.svg} alt={name} className="h-5 w-5 rounded-full object-cover" /> | ||
{name} | ||
</Option> | ||
))} | ||
</Select> | ||
<ErrorText text={errors.country?.message || ""} /> | ||
</div> | ||
|
||
{mutationError?.message && ( | ||
<Typography variant="small" color="red"> | ||
|