2022-11-04 15:40:46 +00:00
|
|
|
import { forwardRef, ReactElement, ReactNode, Ref, useCallback, useId, useState } from "react";
|
|
|
|
import React from "react";
|
|
|
|
import { Eye, EyeOff } from "react-feather";
|
|
|
|
import { FieldValues, FormProvider, SubmitHandler, useFormContext, UseFormReturn } from "react-hook-form";
|
2022-07-23 00:39:50 +00:00
|
|
|
|
|
|
|
import classNames from "@calcom/lib/classNames";
|
|
|
|
import { getErrorFromUnknown } from "@calcom/lib/errors";
|
|
|
|
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
2022-11-04 17:25:07 +00:00
|
|
|
import { Alert } from "@calcom/ui/v2/core/Alert";
|
|
|
|
import { Tooltip } from "@calcom/ui/v2/core/Tooltip";
|
|
|
|
import showToast from "@calcom/ui/v2/core/notifications";
|
|
|
|
import { Skeleton } from "@calcom/ui/v2/core/skeleton";
|
2022-07-23 00:39:50 +00:00
|
|
|
|
2022-11-04 15:40:46 +00:00
|
|
|
import { HintsOrErrors } from "./HintOrErrors";
|
|
|
|
import { Label } from "./Label";
|
2022-07-23 00:39:50 +00:00
|
|
|
|
2022-09-02 19:00:41 +00:00
|
|
|
type InputProps = JSX.IntrinsicElements["input"];
|
2022-07-23 00:39:50 +00:00
|
|
|
|
|
|
|
export const Input = forwardRef<HTMLInputElement, InputProps>(function Input(props, ref) {
|
|
|
|
return (
|
|
|
|
<input
|
|
|
|
{...props}
|
|
|
|
ref={ref}
|
|
|
|
className={classNames(
|
2022-09-23 14:00:45 +00:00
|
|
|
"mb-2 block h-9 w-full rounded-md border border-gray-300 py-2 px-3 text-sm placeholder:text-gray-400 hover:border-gray-400 focus:border-neutral-300 focus:outline-none focus:ring-2 focus:ring-neutral-800 focus:ring-offset-1",
|
2022-07-23 00:39:50 +00:00
|
|
|
props.className
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
export function InputLeading(props: JSX.IntrinsicElements["div"]) {
|
|
|
|
return (
|
|
|
|
<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">
|
|
|
|
{props.children}
|
|
|
|
</span>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
type InputFieldProps = {
|
|
|
|
label?: ReactNode;
|
|
|
|
hint?: ReactNode;
|
2022-07-29 13:23:54 +00:00
|
|
|
hintErrors?: string[];
|
2022-07-23 00:39:50 +00:00
|
|
|
addOnLeading?: ReactNode;
|
|
|
|
addOnSuffix?: ReactNode;
|
|
|
|
addOnFilled?: boolean;
|
|
|
|
error?: string;
|
|
|
|
labelSrOnly?: boolean;
|
|
|
|
containerClassName?: string;
|
2022-08-03 16:59:40 +00:00
|
|
|
t?: (key: string) => string;
|
2022-07-23 00:39:50 +00:00
|
|
|
} & React.ComponentProps<typeof Input> & {
|
|
|
|
labelProps?: React.ComponentProps<typeof Label>;
|
2022-09-12 19:07:52 +00:00
|
|
|
labelClassName?: string;
|
2022-07-23 00:39:50 +00:00
|
|
|
};
|
|
|
|
|
2022-10-05 14:22:06 +00:00
|
|
|
type AddonProps = {
|
|
|
|
children: React.ReactNode;
|
|
|
|
isFilled?: boolean;
|
|
|
|
className?: string;
|
|
|
|
error?: boolean;
|
|
|
|
};
|
|
|
|
|
|
|
|
const Addon = ({ isFilled, children, className, error }: AddonProps) => (
|
|
|
|
<div
|
|
|
|
className={classNames(
|
|
|
|
"addon-wrapper h-9 border border-gray-300 px-3",
|
|
|
|
isFilled && "bg-gray-100",
|
|
|
|
className
|
|
|
|
)}>
|
2022-11-21 19:20:33 +00:00
|
|
|
<div className={classNames("flex h-full flex-col justify-center text-sm", error && "text-red-900")}>
|
2022-10-05 14:22:06 +00:00
|
|
|
<span className="whitespace-nowrap py-2.5">{children}</span>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
|
2022-11-04 15:40:46 +00:00
|
|
|
export const InputField = forwardRef<HTMLInputElement, InputFieldProps>(function InputField(props, ref) {
|
2022-07-23 00:39:50 +00:00
|
|
|
const id = useId();
|
2022-09-29 16:19:03 +00:00
|
|
|
const { t: _t, isLocaleReady, i18n } = useLocale();
|
2022-08-03 16:59:40 +00:00
|
|
|
const t = props.t || _t;
|
2022-09-02 19:00:41 +00:00
|
|
|
const name = props.name || "";
|
2022-07-23 00:39:50 +00:00
|
|
|
const {
|
2022-09-02 19:00:41 +00:00
|
|
|
label = t(name),
|
2022-07-23 00:39:50 +00:00
|
|
|
labelProps,
|
2022-09-12 19:07:52 +00:00
|
|
|
labelClassName,
|
2022-09-29 16:19:03 +00:00
|
|
|
placeholder = isLocaleReady && i18n.exists(name + "_placeholder") ? t(name + "_placeholder") : "",
|
2022-07-23 00:39:50 +00:00
|
|
|
className,
|
|
|
|
addOnLeading,
|
|
|
|
addOnSuffix,
|
|
|
|
addOnFilled = true,
|
|
|
|
hint,
|
2022-07-29 13:23:54 +00:00
|
|
|
hintErrors,
|
2022-07-23 00:39:50 +00:00
|
|
|
labelSrOnly,
|
|
|
|
containerClassName,
|
2022-08-03 16:59:40 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
|
|
t: __t,
|
2022-07-23 00:39:50 +00:00
|
|
|
...passThrough
|
|
|
|
} = props;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className={classNames(containerClassName)}>
|
2022-09-02 19:00:41 +00:00
|
|
|
{!!name && (
|
2022-09-21 16:01:47 +00:00
|
|
|
<Skeleton
|
|
|
|
as={Label}
|
2022-07-23 00:39:50 +00:00
|
|
|
htmlFor={id}
|
2022-09-21 16:01:47 +00:00
|
|
|
loadingClassName="w-16"
|
2022-07-23 00:39:50 +00:00
|
|
|
{...labelProps}
|
2022-09-12 19:07:52 +00:00
|
|
|
className={classNames(labelClassName, labelSrOnly && "sr-only", props.error && "text-red-900")}>
|
2022-07-23 00:39:50 +00:00
|
|
|
{label}
|
2022-09-21 16:01:47 +00:00
|
|
|
</Skeleton>
|
2022-07-23 00:39:50 +00:00
|
|
|
)}
|
|
|
|
{addOnLeading || addOnSuffix ? (
|
2022-10-05 14:22:06 +00:00
|
|
|
<div className="relative mb-1 flex items-center rounded-md focus-within:outline-none focus-within:ring-2 focus-within:ring-neutral-800 focus-within:ring-offset-1">
|
|
|
|
{addOnLeading && (
|
|
|
|
<Addon isFilled={addOnFilled} className="rounded-l-md border-r-0">
|
|
|
|
{addOnLeading}
|
|
|
|
</Addon>
|
|
|
|
)}
|
2022-07-23 00:39:50 +00:00
|
|
|
<Input
|
|
|
|
id={id}
|
2022-09-29 16:19:03 +00:00
|
|
|
placeholder={placeholder}
|
2022-07-23 00:39:50 +00:00
|
|
|
className={classNames(
|
|
|
|
className,
|
|
|
|
addOnLeading && "rounded-l-none",
|
|
|
|
addOnSuffix && "rounded-r-none",
|
|
|
|
"!my-0 !ring-0"
|
|
|
|
)}
|
|
|
|
{...passThrough}
|
|
|
|
ref={ref}
|
|
|
|
/>
|
2022-10-05 14:22:06 +00:00
|
|
|
{addOnSuffix && (
|
|
|
|
<Addon isFilled={addOnFilled} className="rounded-r-md border-l-0">
|
|
|
|
{addOnSuffix}
|
|
|
|
</Addon>
|
|
|
|
)}
|
2022-07-23 00:39:50 +00:00
|
|
|
</div>
|
|
|
|
) : (
|
2022-09-29 16:19:03 +00:00
|
|
|
<Input id={id} placeholder={placeholder} className={className} {...passThrough} ref={ref} />
|
2022-07-23 00:39:50 +00:00
|
|
|
)}
|
2022-09-02 19:00:41 +00:00
|
|
|
<HintsOrErrors hintErrors={hintErrors} fieldName={name} t={t} />
|
2022-07-29 13:23:54 +00:00
|
|
|
{hint && <div className="text-gray mt-2 flex items-center text-sm text-gray-700">{hint}</div>}
|
2022-07-23 00:39:50 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
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
|
|
|
|
) {
|
2022-10-03 19:52:57 +00:00
|
|
|
const { t } = useLocale();
|
2022-09-13 16:35:14 +00:00
|
|
|
const [isPasswordVisible, setIsPasswordVisible] = useState(false);
|
|
|
|
const toggleIsPasswordVisible = useCallback(
|
|
|
|
() => setIsPasswordVisible(!isPasswordVisible),
|
|
|
|
[isPasswordVisible, setIsPasswordVisible]
|
|
|
|
);
|
|
|
|
const textLabel = isPasswordVisible ? t("hide_password") : t("show_password");
|
2022-10-03 19:52:57 +00:00
|
|
|
|
2022-09-13 16:35:14 +00:00
|
|
|
return (
|
2022-10-03 19:52:57 +00:00
|
|
|
<div className="relative [&_.group:hover_.addon-wrapper]:border-gray-400 [&_.group:focus-within_.addon-wrapper]:border-neutral-300">
|
2022-09-13 16:35:14 +00:00
|
|
|
<InputField
|
2022-10-03 19:52:57 +00:00
|
|
|
type={isPasswordVisible ? "text" : "password"}
|
2022-10-04 09:20:21 +00:00
|
|
|
placeholder={props.placeholder || "•••••••••••••"}
|
2022-09-13 16:35:14 +00:00
|
|
|
ref={ref}
|
|
|
|
{...props}
|
2022-10-03 19:52:57 +00:00
|
|
|
className={classNames("mb-0 border-r-0 pr-10", props.className)}
|
|
|
|
addOnFilled={false}
|
|
|
|
addOnSuffix={
|
|
|
|
<Tooltip content={textLabel}>
|
|
|
|
<button
|
|
|
|
className="absolute right-3 bottom-0 h-9 text-gray-900"
|
|
|
|
type="button"
|
|
|
|
onClick={() => toggleIsPasswordVisible()}>
|
|
|
|
{isPasswordVisible ? (
|
|
|
|
<EyeOff className="h-4 stroke-[2.5px]" />
|
|
|
|
) : (
|
|
|
|
<Eye className="h-4 stroke-[2.5px]" />
|
|
|
|
)}
|
|
|
|
<span className="sr-only">{textLabel}</span>
|
|
|
|
</button>
|
|
|
|
</Tooltip>
|
|
|
|
}
|
2022-09-13 16:35:14 +00:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
2022-07-23 00:39:50 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
export const EmailInput = forwardRef<HTMLInputElement, InputFieldProps>(function EmailInput(props, ref) {
|
|
|
|
return (
|
|
|
|
<Input
|
|
|
|
ref={ref}
|
|
|
|
type="email"
|
|
|
|
autoCapitalize="none"
|
|
|
|
autoComplete="email"
|
|
|
|
autoCorrect="off"
|
|
|
|
inputMode="email"
|
|
|
|
{...props}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
export const EmailField = forwardRef<HTMLInputElement, InputFieldProps>(function EmailField(props, ref) {
|
|
|
|
return (
|
|
|
|
<InputField
|
|
|
|
ref={ref}
|
|
|
|
type="email"
|
|
|
|
autoCapitalize="none"
|
|
|
|
autoComplete="email"
|
|
|
|
autoCorrect="off"
|
|
|
|
inputMode="email"
|
|
|
|
{...props}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2022-09-02 19:00:41 +00:00
|
|
|
type TextAreaProps = JSX.IntrinsicElements["textarea"];
|
2022-07-23 00:39:50 +00:00
|
|
|
|
|
|
|
export const TextArea = forwardRef<HTMLTextAreaElement, TextAreaProps>(function TextAreaInput(props, ref) {
|
|
|
|
return (
|
|
|
|
<textarea
|
|
|
|
ref={ref}
|
|
|
|
{...props}
|
|
|
|
className={classNames(
|
2022-09-05 14:23:18 +00:00
|
|
|
"block w-full rounded-md border border-gray-300 py-2 px-3 hover:border-gray-400 focus:border-neutral-300 focus:outline-none focus:ring-2 focus:ring-neutral-800 focus:ring-offset-1 sm:text-sm",
|
2022-07-23 00:39:50 +00:00
|
|
|
props.className
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
type TextAreaFieldProps = {
|
|
|
|
label?: ReactNode;
|
2022-08-03 16:59:40 +00:00
|
|
|
t?: (key: string) => string;
|
2022-07-23 00:39:50 +00:00
|
|
|
} & React.ComponentProps<typeof TextArea> & {
|
2022-09-02 19:00:41 +00:00
|
|
|
name: string;
|
2022-07-23 00:39:50 +00:00
|
|
|
labelProps?: React.ComponentProps<typeof Label>;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const TextAreaField = forwardRef<HTMLTextAreaElement, TextAreaFieldProps>(function TextField(
|
|
|
|
props,
|
|
|
|
ref
|
|
|
|
) {
|
|
|
|
const id = useId();
|
2022-08-03 16:59:40 +00:00
|
|
|
const { t: _t } = useLocale();
|
|
|
|
const t = props.t || _t;
|
2022-07-23 00:39:50 +00:00
|
|
|
const methods = useFormContext();
|
|
|
|
const {
|
|
|
|
label = t(props.name as string),
|
|
|
|
labelProps,
|
|
|
|
/** Prevents displaying untranslated placeholder keys */
|
|
|
|
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"
|
2022-11-04 15:40:46 +00:00
|
|
|
message={<>{methods.formState.errors[props.name]?.message}</>}
|
2022-08-17 17:38:21 +00:00
|
|
|
/>
|
2022-07-23 00:39:50 +00:00
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
type FormProps<T extends object> = { 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;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<FormProvider {...form}>
|
|
|
|
<form
|
|
|
|
ref={ref}
|
|
|
|
onSubmit={(event) => {
|
|
|
|
event.preventDefault();
|
2022-08-24 20:18:42 +00:00
|
|
|
event.stopPropagation();
|
2022-07-23 00:39:50 +00:00
|
|
|
form
|
|
|
|
.handleSubmit(handleSubmit)(event)
|
|
|
|
.catch((err) => {
|
|
|
|
showToast(`${getErrorFromUnknown(err).message}`, "error");
|
|
|
|
});
|
|
|
|
}}
|
|
|
|
{...passThrough}>
|
2022-11-16 13:41:40 +00:00
|
|
|
{props.children}
|
2022-07-23 00:39:50 +00:00
|
|
|
</form>
|
|
|
|
</FormProvider>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export const Form = forwardRef(PlainForm) as <T extends FieldValues>(
|
|
|
|
p: FormProps<T> & { ref?: Ref<HTMLFormElement> }
|
|
|
|
) => ReactElement;
|
|
|
|
|
|
|
|
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("space-y-2 rounded-sm border border-gray-300 bg-white p-2", props.className)}>
|
|
|
|
{props.children}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|