added password visibility toggle

This commit is contained in:
medentem
2024-10-02 15:54:53 -05:00
parent 06d2c393ce
commit 4b532fc7f8
4 changed files with 30 additions and 6 deletions
+12 -1
View File
@@ -4,7 +4,9 @@ import type {
} from "@components/Form/DynamicForm.js";
import { Input } from "@components/UI/Input.js";
import type { LucideIcon } from "lucide-react";
import { Eye, EyeOff } from "lucide-react";
import type { ChangeEventHandler } from "react";
import { useState } from "react";
import { Controller, type FieldValues } from "react-hook-form";
export interface InputFieldProps<T> extends BaseFormBuilderProps<T> {
@@ -27,13 +29,22 @@ export function GenericInput<T extends FieldValues>({
disabled,
field,
}: GenericFormElementProps<T, InputFieldProps<T>>) {
const [passwordShown, setPasswordShown] = useState(false);
const togglePasswordVisiblity = () => {
setPasswordShown(passwordShown ? false : true);
};
return (
<Controller
name={field.name}
control={control}
render={({ field: { value, onChange, ...rest } }) => (
<Input
type={field.type}
type={field.type == "password" && passwordShown ? "text" : field.type}
action={field.type == "password" ? {
icon: passwordShown ? EyeOff : Eye,
onClick: togglePasswordVisiblity
} : undefined}
step={field.properties?.step}
value={field.type === "number" ? Number.parseFloat(value) : value}
onChange={(e) => {
+13 -1
View File
@@ -4,6 +4,8 @@ import type {
} from "@components/Form/DynamicForm.js";
import { Generator } from "@components/UI/Generator.js";
import type { ChangeEventHandler, MouseEventHandler } from "react";
import { useState } from "react";
import { Eye, EyeOff } from "lucide-react";
import { Controller, type FieldValues } from "react-hook-form";
export interface PasswordGeneratorProps<T> extends BaseFormBuilderProps<T> {
@@ -21,13 +23,23 @@ export function PasswordGenerator<T extends FieldValues>({
field,
disabled,
}: GenericFormElementProps<T, PasswordGeneratorProps<T>>) {
const [passwordShown, setPasswordShown] = useState(false);
const togglePasswordVisiblity = () => {
setPasswordShown(passwordShown ? false : true);
};
return (
<Controller
name={field.name}
control={control}
render={({ field: { value, ...rest } }) => (
<Generator
hide={field.hide}
type={field.hide && !passwordShown ? "password" : "text"}
action={field.hide ? {
icon: passwordShown ? EyeOff : Eye,
onClick: togglePasswordVisiblity
} : undefined}
devicePSKBitCount={field.devicePSKBitCount}
bits={field.bits}
inputChange={field.inputChange}
+2 -1
View File
@@ -131,8 +131,9 @@ export const Channel = ({ channel }: SettingsPanelProps): JSX.Element => {
inputChange: inputChangeEvent,
selectChange: selectChangeEvent,
buttonClick: clickEvent,
hide: true,
properties: {
value: pass,
value: pass
},
},
{
+3 -3
View File
@@ -12,7 +12,7 @@ import {
import type { LucideIcon } from "lucide-react";
export interface GeneratorProps extends React.BaseHTMLAttributes<HTMLElement> {
hide?: boolean;
type: "text" | "password";
devicePSKBitCount?: number;
value: string;
variant: "default" | "invalid";
@@ -31,7 +31,7 @@ export interface GeneratorProps extends React.BaseHTMLAttributes<HTMLElement> {
const Generator = React.forwardRef<HTMLInputElement, GeneratorProps>(
(
{
hide = true,
type,
devicePSKBitCount,
variant,
value,
@@ -68,7 +68,7 @@ const Generator = React.forwardRef<HTMLInputElement, GeneratorProps>(
return (
<>
<Input
type={hide ? "password" : "text"}
type={type}
id="pskInput"
variant={variant}
value={value}