feat(RegisterForm): add custom input and button components support with component as prop pattern
This commit is contained in:
@@ -1,22 +1,45 @@
|
||||
import { useState, type ChangeEvent, type SubmitEvent } from "react";
|
||||
import {
|
||||
cloneElement,
|
||||
useState,
|
||||
type ChangeEvent,
|
||||
type ReactElement,
|
||||
type SubmitEvent,
|
||||
} from "react";
|
||||
import {
|
||||
selectEmail,
|
||||
selectFormValid,
|
||||
selectPassword,
|
||||
selectStatusIsLoading,
|
||||
useAuthStore,
|
||||
} from "../../model";
|
||||
import {
|
||||
DefaultInputComponent,
|
||||
type InputAttributes,
|
||||
} from "../DefaultInput/DefaultInput";
|
||||
import {
|
||||
DefaultButtonComponent,
|
||||
type ButtonAttributes,
|
||||
} from "../DefaultButton/DefaultButton";
|
||||
|
||||
export interface Props {
|
||||
InputComponent?: ReactElement<InputAttributes>;
|
||||
ButtonComponent?: ReactElement<ButtonAttributes>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register form component
|
||||
*/
|
||||
export function RegisterForm() {
|
||||
const { formData, setEmail, setPassword, register } = useAuthStore();
|
||||
export function RegisterForm({ InputComponent, ButtonComponent }: Props) {
|
||||
const { setEmail, setPassword, register } = useAuthStore();
|
||||
const email = useAuthStore(selectEmail);
|
||||
const password = useAuthStore(selectPassword);
|
||||
|
||||
const [confirmedPassword, setConfirmedPassword] = useState("");
|
||||
|
||||
const formValid = useAuthStore(selectFormValid);
|
||||
const isLoading = useAuthStore(selectStatusIsLoading);
|
||||
|
||||
const passwordMatch = formData?.password?.value === confirmedPassword;
|
||||
const passwordMatch = password === confirmedPassword;
|
||||
const disabled = isLoading || !formValid || !passwordMatch;
|
||||
|
||||
const handleEmailChange = (e: ChangeEvent<HTMLInputElement>) => {
|
||||
@@ -38,27 +61,63 @@ export function RegisterForm() {
|
||||
|
||||
return (
|
||||
<form aria-label="Register Form" onSubmit={handleSubmit}>
|
||||
<input
|
||||
{/* EMAIL */}
|
||||
{InputComponent ? (
|
||||
cloneElement(InputComponent, {
|
||||
value: email,
|
||||
onChange: handleEmailChange,
|
||||
"aria-label": "Email",
|
||||
})
|
||||
) : (
|
||||
<DefaultInputComponent
|
||||
type="email"
|
||||
aria-label="Email"
|
||||
value={formData?.email?.value ?? ""}
|
||||
value={email}
|
||||
onChange={handleEmailChange}
|
||||
aria-label="Email"
|
||||
/>
|
||||
<input
|
||||
)}
|
||||
|
||||
{/* PASSWORD */}
|
||||
{InputComponent ? (
|
||||
cloneElement(InputComponent, {
|
||||
value: password,
|
||||
onChange: handlePasswordChange,
|
||||
"aria-label": "Password",
|
||||
})
|
||||
) : (
|
||||
<DefaultInputComponent
|
||||
type="password"
|
||||
aria-label="Password"
|
||||
value={formData?.password?.value ?? ""}
|
||||
value={password}
|
||||
onChange={handlePasswordChange}
|
||||
aria-label="Password"
|
||||
/>
|
||||
<input
|
||||
)}
|
||||
{/* PASSWORD */}
|
||||
{InputComponent ? (
|
||||
cloneElement(InputComponent, {
|
||||
value: confirmedPassword,
|
||||
onChange: handleConfirmChange,
|
||||
"aria-label": "Confirm",
|
||||
})
|
||||
) : (
|
||||
<DefaultInputComponent
|
||||
type="password"
|
||||
aria-label="Confirm"
|
||||
value={confirmedPassword}
|
||||
onChange={handleConfirmChange}
|
||||
aria-label="Confirm"
|
||||
/>
|
||||
<button type="submit" aria-label="Register" disabled={disabled}>
|
||||
)}
|
||||
{/* BUTTON */}
|
||||
{ButtonComponent ? (
|
||||
cloneElement(ButtonComponent, {
|
||||
type: "submit",
|
||||
disabled: disabled,
|
||||
})
|
||||
) : (
|
||||
<DefaultButtonComponent disabled={disabled}>
|
||||
Register
|
||||
</button>
|
||||
</DefaultButtonComponent>
|
||||
)}
|
||||
</form>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user