2023-08-18 18:13:21 +00:00
|
|
|
import type { ReactNode } from "react";
|
2023-02-16 22:39:57 +00:00
|
|
|
import React, { forwardRef, useCallback, useId, useState } from "react";
|
2023-08-18 18:13:21 +00:00
|
|
|
import { useFormContext } from "react-hook-form";
|
2022-07-23 00:39:50 +00:00
|
|
|
|
|
|
|
import classNames from "@calcom/lib/classNames";
|
|
|
|
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
|
|
|
|
2023-08-18 18:13:21 +00:00
|
|
|
import { Alert, Input, InputField, Tooltip } from "../../..";
|
|
|
|
import { Eye, EyeOff, Search } from "../../icon";
|
2022-11-04 15:40:46 +00:00
|
|
|
import { Label } from "./Label";
|
2023-08-18 18:13:21 +00:00
|
|
|
import type { InputFieldProps } from "./types";
|
2022-07-23 00:39:50 +00:00
|
|
|
|
|
|
|
export function InputLeading(props: JSX.IntrinsicElements["div"]) {
|
|
|
|
return (
|
2023-05-02 21:49:17 +00:00
|
|
|
<span className="bg-muted border-default text-subtle inline-flex flex-shrink-0 items-center rounded-l-sm border px-3 ltr:border-r-0 rtl:border-l-0 sm:text-sm sm:leading-4">
|
2022-07-23 00:39:50 +00:00
|
|
|
{props.children}
|
|
|
|
</span>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
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 (
|
2023-04-05 18:14:46 +00:00
|
|
|
<div className="[&_.group:hover_.addon-wrapper]:border-emphasis relative [&_.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}
|
2023-04-19 14:12:55 +00:00
|
|
|
className={classNames(
|
|
|
|
"addon-wrapper mb-0 ltr:border-r-0 ltr:pr-10 rtl:border-l-0 rtl:pl-10",
|
|
|
|
props.className
|
|
|
|
)}
|
2022-10-03 19:52:57 +00:00
|
|
|
addOnFilled={false}
|
|
|
|
addOnSuffix={
|
|
|
|
<Tooltip content={textLabel}>
|
2023-08-30 07:33:48 +00:00
|
|
|
<button
|
|
|
|
className="text-emphasis h-9"
|
|
|
|
tabIndex={-1}
|
|
|
|
type="button"
|
|
|
|
onClick={() => toggleIsPasswordVisible()}>
|
2022-10-03 19:52:57 +00:00
|
|
|
{isPasswordVisible ? (
|
2023-04-12 15:26:31 +00:00
|
|
|
<EyeOff className="h-4 stroke-[2.5px]" />
|
2022-10-03 19:52:57 +00:00
|
|
|
) : (
|
2023-04-12 15:26:31 +00:00
|
|
|
<Eye className="h-4 stroke-[2.5px]" />
|
2022-10-03 19:52:57 +00:00
|
|
|
)}
|
|
|
|
<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(
|
2023-06-22 22:25:37 +00:00
|
|
|
"hover:border-emphasis border-default bg-default placeholder:text-muted text-emphasis disabled:hover:border-default disabled:bg-subtle focus:ring-brand-default mb-2 block w-full rounded-md border px-3 py-2 text-sm focus:border-neutral-300 focus:outline-none focus:ring-2 focus:ring-offset-1 disabled:cursor-not-allowed",
|
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>
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
export function FieldsetLegend(props: JSX.IntrinsicElements["legend"]) {
|
|
|
|
return (
|
2023-05-02 21:49:17 +00:00
|
|
|
<legend {...props} className={classNames("text-default text-sm font-medium leading-4", props.className)}>
|
2022-07-23 00:39:50 +00:00
|
|
|
{props.children}
|
|
|
|
</legend>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function InputGroupBox(props: JSX.IntrinsicElements["div"]) {
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
{...props}
|
2023-04-05 18:14:46 +00:00
|
|
|
className={classNames("bg-default border-default space-y-2 rounded-sm border p-2", props.className)}>
|
2022-07-23 00:39:50 +00:00
|
|
|
{props.children}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
2022-11-28 11:20:49 +00:00
|
|
|
|
2023-04-13 18:26:13 +00:00
|
|
|
export const NumberInput = forwardRef<HTMLInputElement, InputFieldProps>(function NumberInput(props, ref) {
|
|
|
|
return (
|
|
|
|
<Input
|
|
|
|
ref={ref}
|
|
|
|
type="number"
|
|
|
|
autoCapitalize="none"
|
|
|
|
autoComplete="numeric"
|
|
|
|
autoCorrect="off"
|
|
|
|
inputMode="numeric"
|
|
|
|
{...props}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
});
|
2023-07-05 03:24:42 +00:00
|
|
|
|
|
|
|
export const FilterSearchField = forwardRef<HTMLInputElement, InputFieldProps>(function TextField(
|
|
|
|
props,
|
|
|
|
ref
|
|
|
|
) {
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
dir="ltr"
|
|
|
|
className="focus-within:ring-brand-default group relative mx-3 mb-1 mt-2.5 flex items-center rounded-md focus-within:outline-none focus-within:ring-2">
|
|
|
|
<div className="addon-wrapper border-default [input:hover_+_&]:border-emphasis [input:hover_+_&]:border-l-default [&:has(+_input:hover)]:border-emphasis [&:has(+_input:hover)]:border-r-default flex h-7 items-center justify-center rounded-l-md border border-r-0">
|
2023-08-01 16:19:54 +00:00
|
|
|
<Search className="ms-3 h-4 w-4" data-testid="search-icon" />
|
2023-07-05 03:24:42 +00:00
|
|
|
</div>
|
|
|
|
<Input
|
|
|
|
ref={ref}
|
|
|
|
className="disabled:bg-subtle disabled:hover:border-subtle !my-0 h-7 rounded-l-none border-l-0 !ring-0 disabled:cursor-not-allowed"
|
|
|
|
{...props}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
});
|