Created
November 25, 2024 15:52
-
-
Save webtamizhan/0a89c050d4d55229cb08923914622bfc to your computer and use it in GitHub Desktop.
Shadcn ui - Amazon Like Password Field
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useState } from "react"; | |
import { Input } from "@/components/ui/input"; | |
import { Label } from "@/components/ui/label"; | |
export const AmazonPasswordField = React.forwardRef< | |
HTMLInputElement, | |
React.InputHTMLAttributes<HTMLInputElement> | |
>(({ className, ...props }, ref) => { | |
const [password, setPassword] = useState<string>(""); | |
const [showPassword, setShowPassword] = useState<boolean>(false); | |
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => { | |
setPassword(e.target.value); | |
}; | |
return ( | |
<div className="flex flex-col justify-start gap-2 items-start"> | |
<Label htmlFor="password" className="justify-start items-start"> | |
Password | |
</Label> | |
<Input | |
ref={ref} | |
{...props} | |
id="password" | |
type="password" | |
autoComplete="off" | |
value={password} | |
onChange={handleChange} | |
className={className} | |
onFocus={() => setShowPassword(true)} | |
onBlur={() => setShowPassword(false)} | |
/> | |
{showPassword && password && ( | |
<div className="p-0.5 -mt-1.5 bg-white border w-full rounded-md text-left text-sm"> | |
<p className="ml-2">{password}</p> | |
</div> | |
)} | |
</div> | |
); | |
}); | |
AmazonPasswordField.displayName = "AmazonPasswordField"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment