From c5334d48aa4329b975b98d6ad3fafe856bffce91 Mon Sep 17 00:00:00 2001 From: armanddidierjean <95971503+armanddidierjean@users.noreply.github.com> Date: Wed, 10 Jul 2024 12:19:04 +0200 Subject: [PATCH] fix: remove password strength indicator for login --- src/app/activate/page.tsx | 4 +- src/app/reset-password/page.tsx | 4 +- src/components/custom/PasswordInput.tsx | 68 ++++--------------- .../custom/PasswordInputWithStrength.tsx | 55 +++++++++++++++ 4 files changed, 73 insertions(+), 58 deletions(-) create mode 100644 src/components/custom/PasswordInputWithStrength.tsx diff --git a/src/app/activate/page.tsx b/src/app/activate/page.tsx index 067fe9e..0a501fb 100644 --- a/src/app/activate/page.tsx +++ b/src/app/activate/page.tsx @@ -5,7 +5,7 @@ import { CenteredCard } from "@/components/custom/CenteredCard"; import { CustomFormField } from "@/components/custom/CustomFormField"; import { DatePicker } from "@/components/custom/DatePicker"; import { LoadingButton } from "@/components/custom/LoadingButton"; -import { PasswordInput } from "@/components/custom/PasswordInput"; +import { PasswordInputWithStrength } from "@/components/custom/PasswordInputWithStrength"; import { PhoneCustomInput } from "@/components/custom/PhoneCustomInput"; import { CustomSelectTrigger } from "@/components/custom/SelectInput"; import { SuspenseConditional } from "@/components/custom/SuspenseConditional"; @@ -268,7 +268,7 @@ const RegisterPage = () => { form={form} name="password" label="Mot de passe" - render={(field) => } + render={(field) => } /> { name="password" label="Mot de passe" render={(field) => { - return ; + return ; }} /> ; const PasswordInput = React.forwardRef( ({ className, ...props }, ref) => { const [showPassword, setShowPassword] = React.useState(false); - const [score, setScore] = React.useState(0); const togglePasswordVisibility = () => setShowPassword(!showPassword); - const previousChange = props.onChange; - - const onChange = (e: React.ChangeEvent) => { - if (previousChange) { - const zxcvbnResult = zxcvbn(e.target.value); - setScore(zxcvbnResult.score); - previousChange(e); - } - }; - - props.onChange = onChange; - - function color(strength: number, index: number, length: number): string { - if (!length) return "bg-gray-200"; - if (strength < index) return "bg-gray-200"; - return [ - "bg-red-500", - "bg-orange-500", - "bg-amber-500", - "bg-yellow-400", - "bg-green-400", - ][strength]; - } - return ( -
-
- -
- {showPassword ? ( - - ) : ( - - )} -
-
-
- {[...Array(5)].map((_, index) => ( -
- ))} +
+ +
+ {showPassword ? ( + + ) : ( + + )}
); @@ -73,4 +33,4 @@ const PasswordInput = React.forwardRef( ); PasswordInput.displayName = "PasswordInput"; -export { PasswordInput }; \ No newline at end of file +export { PasswordInput }; diff --git a/src/components/custom/PasswordInputWithStrength.tsx b/src/components/custom/PasswordInputWithStrength.tsx new file mode 100644 index 0000000..b11f111 --- /dev/null +++ b/src/components/custom/PasswordInputWithStrength.tsx @@ -0,0 +1,55 @@ +"use client"; + +import { PasswordInput } from "./PasswordInput"; +import { zxcvbn } from "@zxcvbn-ts/core"; +import * as React from "react"; + +export type InputProps = React.InputHTMLAttributes; + +const PasswordInputWithStrength = React.forwardRef< + HTMLInputElement, + InputProps +>(({ ...props }, ref) => { + const [score, setScore] = React.useState(0); + + const previousChange = props.onChange; + + const onChange = (e: React.ChangeEvent) => { + if (previousChange) { + const zxcvbnResult = zxcvbn(e.target.value); + setScore(zxcvbnResult.score); + previousChange(e); + } + }; + + props.onChange = onChange; + + function color(strength: number, index: number, length: number): string { + if (!length) return "bg-gray-200"; + if (strength < index) return "bg-gray-200"; + return [ + "bg-red-500", + "bg-orange-500", + "bg-amber-500", + "bg-yellow-400", + "bg-green-400", + ][strength]; + } + + return ( +
+ +
+ {[...Array(5)].map((_, index) => ( +
+ ))} +
+
+ ); +}); +PasswordInputWithStrength.displayName = "PasswordInputWithStrength"; + +export { PasswordInputWithStrength };