import { useId } from "@radix-ui/react-id"; import { forwardRef, ReactElement, ReactNode, Ref } from "react"; import { FieldValues, FormProvider, SubmitHandler, useFormContext, UseFormReturn } from "react-hook-form"; import classNames from "@lib/classNames"; import { getErrorFromUnknown } from "@lib/errors"; import { useLocale } from "@lib/hooks/useLocale"; import showToast from "@lib/notification"; import { Alert } from "@components/ui/Alert"; type InputProps = Omit & { name: string }; export const Input = forwardRef(function Input(props, ref) { return ( ); }); export function Label(props: JSX.IntrinsicElements["label"]) { return ( ); } type InputFieldProps = { label?: ReactNode; addOnLeading?: ReactNode; } & React.ComponentProps & { labelProps?: React.ComponentProps; }; const InputField = forwardRef(function InputField(props, ref) { const id = useId(); const { t } = useLocale(); const methods = useFormContext(); const { label = t(props.name), labelProps, placeholder = t(props.name + "_placeholder") !== props.name + "_placeholder" ? t(props.name + "_placeholder") : "", className, addOnLeading, ...passThroughToInput } = props; return (
{!!props.name && ( )} {addOnLeading ? (
{addOnLeading}
) : ( )} {methods?.formState?.errors[props.name] && ( )}
); }); export const TextField = forwardRef(function TextField(props, ref) { return ; }); export const PasswordField = forwardRef(function PasswordField( props, ref ) { return ; }); export const EmailField = forwardRef(function EmailField(props, ref) { return ; }); type FormProps = { form: UseFormReturn; handleSubmit: SubmitHandler } & Omit< JSX.IntrinsicElements["form"], "onSubmit" >; const PlainForm = (props: FormProps, ref: Ref) => { const { form, handleSubmit, ...passThrough } = props; return (
{ form .handleSubmit(handleSubmit)(event) .catch((err) => { showToast(`${getErrorFromUnknown(err).message}`, "error"); }); }} {...passThrough}> {props.children}
); }; export const Form = forwardRef(PlainForm) as ( p: FormProps & { ref?: Ref } ) => ReactElement; export function FieldsetLegend(props: JSX.IntrinsicElements["legend"]) { return ( {props.children} ); } export function InputGroupBox(props: JSX.IntrinsicElements["div"]) { return (
{props.children}
); }