1- import { EyeIcon , EyeOffIcon , RefreshCcw } from "lucide-react" ;
1+ import copy from "copy-to-clipboard" ;
2+ import { Clipboard , EyeIcon , EyeOffIcon , RefreshCcw } from "lucide-react" ;
23import * as React from "react" ;
4+ import { toast } from "sonner" ;
35
46import { generateRandomPassword } from "@/lib/password-utils" ;
57import { cn } from "@/lib/utils" ;
8+ import { Button } from "./button" ;
69
710export interface InputProps extends React . ComponentProps < "input" > {
811 errorMessage ?: string ;
912 enablePasswordGenerator ?: boolean ;
1013 passwordGeneratorLength ?: number ;
14+ enableCopyButton ?: boolean ;
1115}
1216
1317function Input ( {
@@ -16,6 +20,7 @@ function Input({
1620 errorMessage,
1721 enablePasswordGenerator = false ,
1822 passwordGeneratorLength,
23+ enableCopyButton = false ,
1924 ref,
2025 ...props
2126} : InputProps ) {
@@ -65,49 +70,67 @@ function Input({
6570 input . dispatchEvent ( new Event ( "input" , { bubbles : true } ) ) ;
6671 } ;
6772
68- return (
69- < >
70- < div className = "relative w-full" >
71- < input
72- type = { inputType }
73- data-slot = "input"
74- className = { cn (
75- "h-10 w-full min-w-0 rounded-lg border border-input bg-transparent px-3 py-2 text-sm transition-colors outline-none file:inline-flex file:h-7 file:border-0 file:bg-transparent file:text-sm file:font-medium file:text-foreground placeholder:text-muted-foreground focus-visible:border-ring focus-visible:ring-3 focus-visible:ring-ring/50 disabled:pointer-events-none disabled:cursor-not-allowed disabled:bg-input/50 disabled:opacity-50 aria-invalid:border-destructive aria-invalid:ring-3 aria-invalid:ring-destructive/20 md:text-sm dark:bg-input/30 dark:disabled:bg-input/80 dark:aria-invalid:border-destructive/50 dark:aria-invalid:ring-destructive/40" ,
76- isPassword && ( shouldShowGenerator ? "pr-16" : "pr-10" ) ,
77- className ,
78- ) }
79- ref = { setRefs }
80- { ...props }
81- />
82- { isPassword && (
83- < div className = "absolute inset-y-0 right-0 flex items-center gap-1 pr-3 text-muted-foreground" >
84- { shouldShowGenerator && (
85- < button
86- type = "button"
87- className = "hover:text-foreground focus:outline-none"
88- onClick = { handleGeneratePassword }
89- aria-label = "Generate password"
90- title = "Generate password"
91- tabIndex = { - 1 }
92- >
93- < RefreshCcw className = "h-4 w-4" />
94- </ button >
95- ) }
73+ const handleCopy = ( ) => {
74+ copy ( inputRef . current ?. value || "" ) ;
75+ toast . success ( "Value is copied to clipboard" ) ;
76+ } ;
77+
78+ const inputElement = (
79+ < div className = "relative w-full" >
80+ < input
81+ type = { inputType }
82+ data-slot = "input"
83+ className = { cn (
84+ "h-10 w-full min-w-0 rounded-lg border border-input bg-transparent px-3 py-2 text-sm transition-colors outline-none file:inline-flex file:h-7 file:border-0 file:bg-transparent file:text-sm file:font-medium file:text-foreground placeholder:text-muted-foreground focus-visible:border-ring focus-visible:ring-3 focus-visible:ring-ring/50 disabled:pointer-events-none disabled:cursor-not-allowed disabled:bg-input/50 disabled:opacity-50 aria-invalid:border-destructive aria-invalid:ring-3 aria-invalid:ring-destructive/20 md:text-sm dark:bg-input/30 dark:disabled:bg-input/80 dark:aria-invalid:border-destructive/50 dark:aria-invalid:ring-destructive/40" ,
85+ isPassword && ( shouldShowGenerator ? "pr-16" : "pr-10" ) ,
86+ className ,
87+ ) }
88+ ref = { setRefs }
89+ { ...props }
90+ />
91+ { isPassword && (
92+ < div className = "absolute inset-y-0 right-0 flex items-center gap-1 pr-3 text-muted-foreground" >
93+ { shouldShowGenerator && (
9694 < button
9795 type = "button"
9896 className = "hover:text-foreground focus:outline-none"
99- onClick = { ( ) => setShowPassword ( ! showPassword ) }
97+ onClick = { handleGeneratePassword }
98+ aria-label = "Generate password"
99+ title = "Generate password"
100100 tabIndex = { - 1 }
101101 >
102- { showPassword ? (
103- < EyeOffIcon className = "h-4 w-4" />
104- ) : (
105- < EyeIcon className = "h-4 w-4" />
106- ) }
102+ < RefreshCcw className = "h-4 w-4" />
107103 </ button >
108- </ div >
109- ) }
110- </ div >
104+ ) }
105+ < button
106+ type = "button"
107+ className = "hover:text-foreground focus:outline-none"
108+ onClick = { ( ) => setShowPassword ( ! showPassword ) }
109+ tabIndex = { - 1 }
110+ >
111+ { showPassword ? (
112+ < EyeOffIcon className = "h-4 w-4" />
113+ ) : (
114+ < EyeIcon className = "h-4 w-4" />
115+ ) }
116+ </ button >
117+ </ div >
118+ ) }
119+ </ div >
120+ ) ;
121+
122+ return (
123+ < >
124+ { enableCopyButton ? (
125+ < div className = "flex w-full items-center space-x-2" >
126+ { inputElement }
127+ < Button type = "button" variant = { "secondary" } onClick = { handleCopy } >
128+ < Clipboard className = "size-4 text-muted-foreground" />
129+ </ Button >
130+ </ div >
131+ ) : (
132+ inputElement
133+ ) }
111134 { errorMessage && (
112135 < span className = "text-sm text-red-600 text-secondary-foreground" >
113136 { errorMessage }
0 commit comments