2021-10-12 09:35:44 +00:00
|
|
|
import { useId } from "@radix-ui/react-id";
|
2021-11-11 05:44:53 +00:00
|
|
|
import { forwardRef, ReactElement, ReactNode, Ref } from "react";
|
|
|
|
import { FieldValues, FormProvider, SubmitHandler, useFormContext, UseFormReturn } from "react-hook-form";
|
2021-10-12 09:35:44 +00:00
|
|
|
|
|
|
|
import classNames from "@lib/classNames";
|
2021-11-10 11:16:32 +00:00
|
|
|
import { getErrorFromUnknown } from "@lib/errors";
|
2021-11-11 05:44:53 +00:00
|
|
|
import { useLocale } from "@lib/hooks/useLocale";
|
2021-11-10 11:16:32 +00:00
|
|
|
import showToast from "@lib/notification";
|
2021-10-12 09:35:44 +00:00
|
|
|
|
2021-11-11 05:44:53 +00:00
|
|
|
import { Alert } from "@components/ui/Alert";
|
|
|
|
|
2021-10-18 07:02:25 +00:00
|
|
|
type InputProps = Omit<JSX.IntrinsicElements["input"], "name"> & { name: string };
|
|
|
|
export const Input = forwardRef<HTMLInputElement, InputProps>(function Input(props, ref) {
|
2021-10-12 09:35:44 +00:00
|
|
|
return (
|
|
|
|
<input
|
|
|
|
{...props}
|
|
|
|
ref={ref}
|
|
|
|
className={classNames(
|
2021-12-09 23:51:30 +00:00
|
|
|
"mt-1 block w-full border border-gray-300 rounded-sm shadow-sm py-2 px-3 focus:outline-none focus:ring-1 focus:ring-neutral-800 focus:border-neutral-800 sm:text-sm",
|
2021-10-12 09:35:44 +00:00
|
|
|
props.className
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
export function Label(props: JSX.IntrinsicElements["label"]) {
|
|
|
|
return (
|
|
|
|
<label {...props} className={classNames("block text-sm font-medium text-gray-700", props.className)}>
|
|
|
|
{props.children}
|
|
|
|
</label>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-11-11 05:44:53 +00:00
|
|
|
type InputFieldProps = {
|
|
|
|
label?: ReactNode;
|
|
|
|
addOnLeading?: ReactNode;
|
|
|
|
} & React.ComponentProps<typeof Input> & {
|
|
|
|
labelProps?: React.ComponentProps<typeof Label>;
|
|
|
|
};
|
2021-10-12 09:35:44 +00:00
|
|
|
|
2021-11-11 05:44:53 +00:00
|
|
|
const InputField = forwardRef<HTMLInputElement, InputFieldProps>(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;
|
2021-10-12 09:35:44 +00:00
|
|
|
return (
|
|
|
|
<div>
|
2021-12-09 23:51:30 +00:00
|
|
|
{!!props.name && (
|
|
|
|
<Label htmlFor={id} {...labelProps}>
|
|
|
|
{label}
|
|
|
|
</Label>
|
|
|
|
)}
|
2021-11-11 05:44:53 +00:00
|
|
|
{addOnLeading ? (
|
|
|
|
<div className="flex mt-1 rounded-md shadow-sm">
|
|
|
|
{addOnLeading}
|
|
|
|
<Input
|
|
|
|
id={id}
|
|
|
|
placeholder={placeholder}
|
|
|
|
className={classNames(className, "mt-0")}
|
|
|
|
{...passThroughToInput}
|
|
|
|
ref={ref}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
) : (
|
|
|
|
<Input id={id} placeholder={placeholder} className={className} {...passThroughToInput} ref={ref} />
|
|
|
|
)}
|
|
|
|
{methods?.formState?.errors[props.name] && (
|
|
|
|
<Alert className="mt-1" severity="error" message={methods.formState.errors[props.name].message} />
|
|
|
|
)}
|
2021-10-12 09:35:44 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2021-11-11 05:44:53 +00:00
|
|
|
export const TextField = forwardRef<HTMLInputElement, InputFieldProps>(function TextField(props, ref) {
|
|
|
|
return <InputField ref={ref} {...props} />;
|
|
|
|
});
|
|
|
|
|
|
|
|
export const PasswordField = forwardRef<HTMLInputElement, InputFieldProps>(function PasswordField(
|
|
|
|
props,
|
|
|
|
ref
|
2021-11-10 11:16:32 +00:00
|
|
|
) {
|
2021-11-11 05:44:53 +00:00
|
|
|
return <InputField type="password" placeholder="•••••••••••••" ref={ref} {...props} />;
|
|
|
|
});
|
|
|
|
|
|
|
|
export const EmailField = forwardRef<HTMLInputElement, InputFieldProps>(function EmailField(props, ref) {
|
|
|
|
return <InputField type="email" inputMode="email" ref={ref} {...props} />;
|
|
|
|
});
|
|
|
|
|
|
|
|
type FormProps<T> = { form: UseFormReturn<T>; handleSubmit: SubmitHandler<T> } & Omit<
|
|
|
|
JSX.IntrinsicElements["form"],
|
|
|
|
"onSubmit"
|
|
|
|
>;
|
|
|
|
|
|
|
|
const PlainForm = <T extends FieldValues>(props: FormProps<T>, ref: Ref<HTMLFormElement>) => {
|
|
|
|
const { form, handleSubmit, ...passThrough } = props;
|
2021-10-12 09:35:44 +00:00
|
|
|
|
2021-11-10 11:16:32 +00:00
|
|
|
return (
|
|
|
|
<FormProvider {...form}>
|
|
|
|
<form
|
2021-11-11 05:44:53 +00:00
|
|
|
ref={ref}
|
|
|
|
onSubmit={(event) => {
|
|
|
|
form
|
|
|
|
.handleSubmit(handleSubmit)(event)
|
|
|
|
.catch((err) => {
|
|
|
|
showToast(`${getErrorFromUnknown(err).message}`, "error");
|
|
|
|
});
|
|
|
|
}}
|
2021-11-10 11:16:32 +00:00
|
|
|
{...passThrough}>
|
|
|
|
{props.children}
|
|
|
|
</form>
|
|
|
|
</FormProvider>
|
|
|
|
);
|
2021-11-11 05:44:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export const Form = forwardRef(PlainForm) as <T extends FieldValues>(
|
|
|
|
p: FormProps<T> & { ref?: Ref<HTMLFormElement> }
|
|
|
|
) => ReactElement;
|
2021-10-18 07:02:25 +00:00
|
|
|
|
|
|
|
export function FieldsetLegend(props: JSX.IntrinsicElements["legend"]) {
|
|
|
|
return (
|
|
|
|
<legend {...props} className={classNames("text-sm font-medium text-gray-700", props.className)}>
|
|
|
|
{props.children}
|
|
|
|
</legend>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function InputGroupBox(props: JSX.IntrinsicElements["div"]) {
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
{...props}
|
|
|
|
className={classNames("p-2 bg-white border border-gray-300 rounded-sm space-y-2", props.className)}>
|
|
|
|
{props.children}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|