Last active
July 16, 2024 14:48
-
-
Save mjbalcueva/b21f39a8787e558d4c536bf68e267398 to your computer and use it in GitHub Desktop.
shadcn ui custom password input
This file contains 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
"use client" | |
import { forwardRef, useState } from "react" | |
import { EyeIcon, EyeOffIcon } from "lucide-react" | |
import { Button } from "@/components/ui/button" | |
import { Input, InputProps } from "@/components/ui/input" | |
import { cn } from "@/lib/utils" | |
const PasswordInput = forwardRef<HTMLInputElement, InputProps>( | |
({ className, ...props }, ref) => { | |
const [showPassword, setShowPassword] = useState(false) | |
const disabled = props.value === "" || props.value === undefined || props.disabled | |
return ( | |
<div className="relative"> | |
<Input | |
type={showPassword ? "text" : "password"} | |
className={cn("hide-password-toggle pr-10", className)} | |
ref={ref} | |
{...props} | |
/> | |
<Button | |
type="button" | |
variant="ghost" | |
size="sm" | |
className="absolute right-0 top-0 h-full px-3 py-2 hover:bg-transparent" | |
onClick={() => setShowPassword((prev) => !prev)} | |
disabled={disabled} | |
> | |
{showPassword && !disabled ? ( | |
<EyeIcon | |
className="h-4 w-4" | |
aria-hidden="true" | |
/> | |
) : ( | |
<EyeOffIcon | |
className="h-4 w-4" | |
aria-hidden="true" | |
/> | |
)} | |
<span className="sr-only"> | |
{showPassword ? "Hide password" : "Show password"} | |
</span> | |
</Button> | |
{/* hides browsers password toggles */} | |
<style>{` | |
.hide-password-toggle::-ms-reveal, | |
.hide-password-toggle::-ms-clear { | |
visibility: hidden; | |
pointer-events: none; | |
display: none; | |
} | |
`}</style> | |
</div> | |
) | |
}, | |
) | |
PasswordInput.displayName = "PasswordInput" | |
export { PasswordInput } |
This file contains 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
"use client" | |
import { useState } from "react" | |
import { PasswordInput } from "@/components/password-input" | |
import { Button } from "@/components/ui/button" | |
import { Label } from "@/components/ui/label" | |
const SampleUseCase = () => { | |
const [currentPassword, setCurrentPassword] = useState("") | |
const [password, setPassword] = useState("") | |
const [passwordConfirmation, setPasswordConfirmation] = useState("") | |
return ( | |
<div className="space-y-4"> | |
<div> | |
<Label htmlFor="current_password">Current Password</Label> | |
<PasswordInput | |
id="current_password" | |
value={currentPassword} | |
onChange={(e) => setCurrentPassword(e.target.value)} | |
autoComplete="current-password" | |
/> | |
</div> | |
<div> | |
<Label htmlFor="password">New Password</Label> | |
<PasswordInput | |
id="password" | |
value={password} | |
onChange={(e) => setPassword(e.target.value)} | |
autoComplete="new-password" | |
/> | |
</div> | |
<div> | |
<Label htmlFor="password_confirmation">Confirm Password</Label> | |
<PasswordInput | |
id="password_confirmation" | |
value={passwordConfirmation} | |
onChange={(e) => setPasswordConfirmation(e.target.value)} | |
autoComplete="new-password" | |
/> | |
</div> | |
<Button type="submit">Save</Button> | |
</div> | |
) | |
} | |
export default SampleUseCase |
hi immdraselkhan, thanks! Can you share your Box component?
☺️
Hello @thomaslemoine, here you go...
import { cn } from "@/lib/shadcn/cn";
import React from "react";
interface BoxProps extends React.HTMLAttributes<HTMLDivElement> {}
const Box = React.forwardRef<HTMLDivElement, BoxProps>(
({ className, ...props }, ref) => (
<div className={cn(className)} ref={ref} {...props} />
),
);
Box.displayName = "Box";
export { Box };
Has anyone tried to implement this with zod?
Has anyone tried to implement this with zod?
👉 Usage example
const form = useForm<FieldKeysValues>({
mode: "onBlur",
resolver: zodResolver(passwordZodSchema),
defaultValues: {
password: "",
password2: "",
},
});
👉 passwordZodSchema
export const passwordZodSchema = z
.object({
password: passwordSchema,
password2: passwordSchema,
})
.refine(({ password, password2 }) => password === password2, {
path: ["password2"],
message: "Password didn't match.",
});
👉 passwordSchema
export const passwordSchema = z
.string({
required_error: "Password can not be empty.",
})
.regex(/^.{8,20}$/, {
message: "Minimum 8 and maximum 20 characters.",
})
.regex(/(?=.*[A-Z])/, {
message: "At least one uppercase character.",
})
.regex(/(?=.*[a-z])/, {
message: "At least one lowercase character.",
})
.regex(/(?=.*\d)/, {
message: "At least one digit.",
})
.regex(/[$&+,:;=?@#|'<>.^*()%!-]/, {
message: "At least one special character.",
});
Thanks! <3 Helped me a lot
awesome man, thanks!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hi immdraselkhan, thanks! Can you share your Box component?☺️