"use client";
import classNames from "classnames";
import type { FormEvent } from "react";
import React, { forwardRef, useState, useEffect, useId, useCallback } from "react";
import { useLocale } from "@calcom/lib/hooks/useLocale";
import { Label } from "@calcom/ui/components/form";
import { Input } from "@calcom/ui/components/form";
import type { InputFieldProps } from "@calcom/ui/components/form";
import { Icon } from "@calcom/ui/components/icon";
import { Skeleton } from "@calcom/ui/components/skeleton";
type AddonProps = {
children: React.ReactNode;
className?: string;
error?: boolean;
onClickAddon?: () => void;
};
const Addon = ({ children, className, error }: AddonProps) => (
);
export const KeyField: React.FC = forwardRef<
HTMLInputElement,
InputFieldProps & { defaultValue: string }
>(function KeyField(props, ref) {
const id = useId();
const [isPasswordVisible, setIsPasswordVisible] = useState(false);
const [currentValue, setCurrentValue] = useState("");
const toggleIsPasswordVisible = useCallback(
() => setIsPasswordVisible(!isPasswordVisible),
[isPasswordVisible, setIsPasswordVisible]
);
const { t: _t, isLocaleReady, i18n } = useLocale();
const t = props.t || _t;
const name = props.name || "";
const {
label = t(name),
labelProps,
labelClassName,
LockedIcon,
placeholder = isLocaleReady && i18n.exists(`${name}_placeholder`) ? t(`${name}_placeholder`) : "",
className,
addOnLeading,
addOnClassname,
inputIsFullWidth,
labelSrOnly,
noLabel,
containerClassName,
readOnly,
showAsteriskIndicator,
defaultValue,
...passThrough
} = props;
useEffect(() => {
if (currentValue.trim().length === 0) {
setIsPasswordVisible(true);
}
}, [currentValue]);
useEffect(() => {
setCurrentValue(defaultValue);
if (defaultValue.length > 0) {
setIsPasswordVisible(false);
}
}, [defaultValue]);
const getHiddenKey = (): string => {
let hiddenKey = currentValue;
const length = currentValue.length;
if (length > 6) {
const start = currentValue.slice(0, 3);
const end = currentValue.slice(length - 3);
hiddenKey = `${start}${"*".repeat(length - 6)}${end}`;
}
return hiddenKey;
};
const onInput = (event: FormEvent) => {
const target = event.target as HTMLInputElement;
const fullValue = target.value;
setCurrentValue(fullValue);
target.value = fullValue;
};
return (
{!!label && !noLabel && (
{label}
{showAsteriskIndicator && !readOnly && passThrough.required ? (
*
) : null}
{LockedIcon}
)}
);
});
export default KeyField;