2021-10-12 09:35:44 +00:00
|
|
|
import { useId } from "@radix-ui/react-id";
|
2023-02-16 22:39:57 +00:00
|
|
|
import type { ReactElement, ReactNode, Ref } from "react";
|
|
|
|
import React, { forwardRef } from "react";
|
|
|
|
import type { FieldValues, SubmitHandler, UseFormReturn } from "react-hook-form";
|
|
|
|
import { FormProvider, useFormContext } from "react-hook-form";
|
2021-10-12 09:35:44 +00:00
|
|
|
|
2022-03-16 23:36:43 +00:00
|
|
|
import classNames from "@calcom/lib/classNames";
|
|
|
|
import { getErrorFromUnknown } from "@calcom/lib/errors";
|
|
|
|
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
2021-10-12 09:35:44 +00:00
|
|
|
|
2023-01-17 19:10:47 +00:00
|
|
|
import { Alert, showToast } from "../";
|
2021-11-11 05:44:53 +00:00
|
|
|
|
2021-10-18 07:02:25 +00:00
|
|
|
type InputProps = Omit<JSX.IntrinsicElements["input"], "name"> & { name: string };
|
2022-01-12 09:29:20 +00:00
|
|
|
|
2021-10-18 07:02:25 +00:00
|
|
|
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(
|
2022-02-09 00:05:13 +00:00
|
|
|
"mt-1 block w-full rounded-sm border border-gray-300 py-2 px-3 shadow-sm focus:border-neutral-800 focus:outline-none focus:ring-1 focus:ring-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>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-01-12 09:29:20 +00:00
|
|
|
export function InputLeading(props: JSX.IntrinsicElements["div"]) {
|
|
|
|
return (
|
2022-07-23 00:39:50 +00:00
|
|
|
<span className="inline-flex flex-shrink-0 items-center rounded-l-sm border border-r-0 border-gray-300 bg-gray-50 px-3 text-gray-500 sm:text-sm">
|
2022-01-12 09:29:20 +00:00
|
|
|
{props.children}
|
|
|
|
</span>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-11-11 05:44:53 +00:00
|
|
|
type InputFieldProps = {
|
|
|
|
label?: ReactNode;
|
2022-05-06 22:09:40 +00:00
|
|
|
hint?: ReactNode;
|
2021-11-11 05:44:53 +00:00
|
|
|
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,
|
2022-05-06 22:09:40 +00:00
|
|
|
hint,
|
2022-01-12 09:29:20 +00:00
|
|
|
...passThrough
|
2021-11-11 05:44:53 +00:00
|
|
|
} = 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 ? (
|
2022-07-23 00:39:50 +00:00
|
|
|
<div className="mt-1 flex rounded-md shadow-sm">
|
2021-11-11 05:44:53 +00:00
|
|
|
{addOnLeading}
|
|
|
|
<Input
|
|
|
|
id={id}
|
|
|
|
placeholder={placeholder}
|
2023-01-20 14:19:12 +00:00
|
|
|
className={classNames("mt-0", props.addOnLeading && "rounded-l-none", className)}
|
2022-01-12 09:29:20 +00:00
|
|
|
{...passThrough}
|
2021-11-11 05:44:53 +00:00
|
|
|
ref={ref}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
) : (
|
2022-01-12 09:29:20 +00:00
|
|
|
<Input id={id} placeholder={placeholder} className={className} {...passThrough} ref={ref} />
|
2021-11-11 05:44:53 +00:00
|
|
|
)}
|
2022-05-06 22:09:40 +00:00
|
|
|
{hint}
|
2022-08-17 17:38:21 +00:00
|
|
|
{methods?.formState?.errors[props.name]?.message && (
|
|
|
|
<Alert
|
|
|
|
className="mt-1"
|
|
|
|
severity="error"
|
|
|
|
message={<>{methods.formState.errors[props.name]!.message}</>}
|
|
|
|
/>
|
2021-11-11 05:44:53 +00:00
|
|
|
)}
|
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
|
|
|
) {
|
2022-08-31 20:57:53 +00:00
|
|
|
return (
|
|
|
|
<InputField data-testid="password" type="password" placeholder="•••••••••••••" ref={ref} {...props} />
|
|
|
|
);
|
2021-11-11 05:44:53 +00:00
|
|
|
});
|
|
|
|
|
2022-01-27 10:16:20 +00:00
|
|
|
export const EmailInput = forwardRef<HTMLInputElement, InputFieldProps>(function EmailInput(props, ref) {
|
2021-12-21 00:59:06 +00:00
|
|
|
return (
|
2022-01-27 10:16:20 +00:00
|
|
|
<Input
|
|
|
|
ref={ref}
|
2021-12-21 00:59:06 +00:00
|
|
|
type="email"
|
|
|
|
autoCapitalize="none"
|
|
|
|
autoComplete="email"
|
|
|
|
autoCorrect="off"
|
|
|
|
inputMode="email"
|
|
|
|
{...props}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2021-11-11 05:44:53 +00:00
|
|
|
export const EmailField = forwardRef<HTMLInputElement, InputFieldProps>(function EmailField(props, ref) {
|
2022-02-04 20:30:36 +00:00
|
|
|
return (
|
|
|
|
<InputField
|
|
|
|
ref={ref}
|
|
|
|
type="email"
|
|
|
|
autoCapitalize="none"
|
|
|
|
autoComplete="email"
|
|
|
|
autoCorrect="off"
|
|
|
|
inputMode="email"
|
|
|
|
{...props}
|
|
|
|
/>
|
|
|
|
);
|
2021-11-11 05:44:53 +00:00
|
|
|
});
|
|
|
|
|
2022-01-12 09:29:20 +00:00
|
|
|
type TextAreaProps = Omit<JSX.IntrinsicElements["textarea"], "name"> & { name: string };
|
|
|
|
|
|
|
|
export const TextArea = forwardRef<HTMLTextAreaElement, TextAreaProps>(function TextAreaInput(props, ref) {
|
|
|
|
return (
|
|
|
|
<textarea
|
|
|
|
ref={ref}
|
|
|
|
{...props}
|
|
|
|
className={classNames(
|
2022-07-14 00:10:45 +00:00
|
|
|
"block w-full rounded-sm border-gray-300 shadow-sm focus:border-neutral-900 focus:ring-neutral-900 sm:text-sm",
|
2022-01-12 09:29:20 +00:00
|
|
|
props.className
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
type TextAreaFieldProps = {
|
|
|
|
label?: ReactNode;
|
|
|
|
} & React.ComponentProps<typeof TextArea> & {
|
|
|
|
labelProps?: React.ComponentProps<typeof Label>;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const TextAreaField = forwardRef<HTMLTextAreaElement, TextAreaFieldProps>(function TextField(
|
|
|
|
props,
|
|
|
|
ref
|
|
|
|
) {
|
|
|
|
const id = useId();
|
|
|
|
const { t } = useLocale();
|
|
|
|
const methods = useFormContext();
|
|
|
|
const {
|
|
|
|
label = t(props.name as string),
|
|
|
|
labelProps,
|
|
|
|
placeholder = t(props.name + "_placeholder") !== props.name + "_placeholder"
|
|
|
|
? t(props.name + "_placeholder")
|
|
|
|
: "",
|
|
|
|
...passThrough
|
|
|
|
} = props;
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
{!!props.name && (
|
|
|
|
<Label htmlFor={id} {...labelProps}>
|
|
|
|
{label}
|
|
|
|
</Label>
|
|
|
|
)}
|
|
|
|
<TextArea ref={ref} placeholder={placeholder} {...passThrough} />
|
2022-08-17 17:38:21 +00:00
|
|
|
{methods?.formState?.errors[props.name]?.message && (
|
|
|
|
<Alert
|
|
|
|
className="mt-1"
|
|
|
|
severity="error"
|
|
|
|
message={<>{methods.formState.errors[props.name]!.message}</>}
|
|
|
|
/>
|
2022-01-12 09:29:20 +00:00
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2022-05-06 21:46:31 +00:00
|
|
|
type FormProps<T extends object> = { form: UseFormReturn<T>; handleSubmit: SubmitHandler<T> } & Omit<
|
2021-11-11 05:44:53 +00:00
|
|
|
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) => {
|
2022-05-06 22:09:40 +00:00
|
|
|
event.preventDefault();
|
2022-11-24 11:53:29 +00:00
|
|
|
event.stopPropagation();
|
|
|
|
|
2021-11-11 05:44:53 +00:00
|
|
|
form
|
|
|
|
.handleSubmit(handleSubmit)(event)
|
|
|
|
.catch((err) => {
|
|
|
|
showToast(`${getErrorFromUnknown(err).message}`, "error");
|
|
|
|
});
|
|
|
|
}}
|
2021-11-10 11:16:32 +00:00
|
|
|
{...passThrough}>
|
2022-05-06 21:46:31 +00:00
|
|
|
{
|
|
|
|
/* @see https://react-hook-form.com/advanced-usage/#SmartFormComponent */
|
|
|
|
React.Children.map(props.children, (child) => {
|
|
|
|
return typeof child !== "string" &&
|
|
|
|
typeof child !== "number" &&
|
|
|
|
typeof child !== "boolean" &&
|
|
|
|
child &&
|
|
|
|
"props" in child &&
|
|
|
|
child.props.name
|
|
|
|
? React.createElement(child.type, {
|
|
|
|
...{
|
|
|
|
...child.props,
|
|
|
|
register: form.register,
|
|
|
|
key: child.props.name,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
: child;
|
|
|
|
})
|
|
|
|
}
|
2021-11-10 11:16:32 +00:00
|
|
|
</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}
|
2022-02-09 00:05:13 +00:00
|
|
|
className={classNames("space-y-2 rounded-sm border border-gray-300 bg-white p-2", props.className)}>
|
2021-10-18 07:02:25 +00:00
|
|
|
{props.children}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|