import * as React from "react" import { copyText } from "../lib/clipboard" import { cn } from "../lib/utils" import { Tooltip, TooltipContent, TooltipTrigger } from "../ui/tooltip" /** * Значение, которое копируется по клику (ФИО, штрихкод, номер заявки). При наведении ничего не меняется, * после клика на полсекунды всплывает «Скопировано». Работает и с клавиатуры (Enter, пробел). */ export function CopyValue({ text, className, children }: { text: string; className?: string; children?: React.ReactNode }) { const [state, setState] = React.useState<"idle" | "copied" | "failed">("idle") React.useEffect(() => { if (state === "idle") return const timer = setTimeout(() => setState("idle"), state === "failed" ? 1500 : 500) return () => clearTimeout(timer) }, [state]) const copy = () => copyText(text).then( () => setState("copied"), () => setState("failed"), ) return ( { // в строке таблицы клик по значению не открывает строку e.stopPropagation() void copy() }} onKeyDown={(e) => { if (e.key === "Enter" || e.key === " ") { e.preventDefault() e.stopPropagation() void copy() } }} className={cn("cursor-copy rounded-sm outline-none focus-visible:ring-2 focus-visible:ring-ring/60", className)} > {children ?? text} {state === "failed" ? "Не удалось скопировать" : "Скопировано"} ) }