import * as React from "react"; import classnames from "classnames"; import { CopyButton } from "../../buttons/CopyButton/CopyButton"; import styles from "./CopyInput.scss"; import { IBasicInputProps } from "../BasicInput/BasicInput"; import { Tooltip, TooltipProps } from '../../overlays/Tooltip/Tooltip'; export interface ICopyInputProps extends IBasicInputProps { /* Whether value is invalid - shows the invalid state of text input if true */ invalid?: boolean; /* Label to be shown above text input */ label?: string; /* Message to be shown underneath an enabled input e.g. length requirement, example input, etc. */ message?: React.ReactNode; /* Only show message when invalid is true - useful for only displaying error/validity CTAs */ onlyShowMessageWhenInvalid?: boolean; /* Options for CopyButton Tooltip */ copyButtonTooltipProps?: TooltipProps; /* Whether or not to disable the input */ disabled?: boolean; /* Options for disabled input Tooltip */ inputTooltipProps?: TooltipProps; } export const CopyInput = (props: ICopyInputProps) => { const { placeholder, value, onChange, invalid, disabled, label, message, onlyShowMessageWhenInvalid, copyButtonTooltipProps, inputTooltipProps } = props; const [textToCopy, setTextToCopy] = React.useState(value); React.useEffect(() => { setTextToCopy(value); }, [value]); const showMessage = !disabled && (onlyShowMessageWhenInvalid ? invalid && message : !!message); const isInvalid = invalid && !disabled; const handleChange = (event: React.ChangeEvent) => { const newValue: string = event.target.value; setTextToCopy(newValue); onChange?.(newValue); }; return (
{label && ( )}
{showMessage && ( {message} )}
); }; CopyInput.defaultProps = { invalid: false, disabled: false, onlyShowMessageWhenInvalid: false, };