- {[...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 };