import { useId } from "@radix-ui/react-id"; import { forwardRef, ReactNode } from "react"; import { FormProvider, SubmitHandler, UseFormReturn } from "react-hook-form"; import classNames from "@lib/classNames"; import { getErrorFromUnknown } from "@lib/errors"; import showToast from "@lib/notification"; type InputProps = Omit & { name: string }; export const Input = forwardRef(function Input(props, ref) { return ( ); }); export function Label(props: JSX.IntrinsicElements["label"]) { return ( ); } export const TextField = forwardRef< HTMLInputElement, { label: ReactNode; } & React.ComponentProps & { labelProps?: React.ComponentProps; } >(function TextField(props, ref) { const id = useId(); const { label, ...passThroughToInput } = props; // TODO: use `useForm()` from RHF and get error state here too! return (
); }); /** * Form helper that creates a rect-hook-form Provider and helps with submission handling & default error handling */ export function Form( props: { /** * Pass in the return from `react-hook-form`s `useForm()` */ form: UseFormReturn; /** * Submit handler - you'll get the typed form values back */ handleSubmit?: SubmitHandler; /** * Optional - Override the default error handling * By default it shows a toast with the error */ handleError?: (err: ReturnType) => void; } & Omit ) { const { form, handleSubmit, handleError = (err) => { showToast(err.message, "error"); }, ...passThrough } = props; return (
{ try { await handleSubmit(...args); } catch (_err) { const err = getErrorFromUnknown(_err); handleError(err); } }) : undefined } {...passThrough}> {props.children}
); } export function FieldsetLegend(props: JSX.IntrinsicElements["legend"]) { return ( {props.children} ); } export function InputGroupBox(props: JSX.IntrinsicElements["div"]) { return (
{props.children}
); }