From 59d97008f281cf61aeac957b0756b9ec4463e332 Mon Sep 17 00:00:00 2001 From: Dan Ditomaso Date: Thu, 27 Mar 2025 20:50:27 -0400 Subject: [PATCH] feat: added tzdef to device config --- src/components/Form/FormInput.tsx | 77 ++++++++++++------- .../PageComponents/Config/Device/index.tsx | 13 ++++ 2 files changed, 63 insertions(+), 27 deletions(-) diff --git a/src/components/Form/FormInput.tsx b/src/components/Form/FormInput.tsx index a9398bc8..6ecbfcdc 100644 --- a/src/components/Form/FormInput.tsx +++ b/src/components/Form/FormInput.tsx @@ -7,7 +7,7 @@ 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"; +import { useController, type FieldValues } from "react-hook-form"; export interface InputFieldProps extends BaseFormBuilderProps { type: "text" | "number" | "password"; @@ -17,6 +17,12 @@ export interface InputFieldProps extends BaseFormBuilderProps { prefix?: string; suffix?: string; step?: number; + fieldLength?: { + min?: number; + max?: number; + currentValueLength?: number; + showCharacterCount?: boolean; + }, action?: { icon: LucideIcon; onClick: () => void; @@ -29,42 +35,59 @@ export function GenericInput({ disabled, field, }: GenericFormElementProps>) { + const { fieldLength, ...restProperties } = field.properties || {}; + const [passwordShown, setPasswordShown] = useState(false); + const [currentLength, setCurrentLength] = useState(fieldLength?.currentValueLength || 0); + + const { field: controllerField } = useController({ + name: field.name, + control, + }); + const togglePasswordVisiblity = () => { setPasswordShown(!passwordShown); }; + const handleInputChange = (e: React.ChangeEvent) => { + const newValue = e.target.value; + + if (field.properties?.fieldLength?.max && newValue.length > field.properties?.fieldLength?.max) { + return; + } + setCurrentLength(newValue.length); + + if (field.inputChange) field.inputChange(e); + + controllerField.onChange(field.type === "number" ? Number.parseFloat(newValue).toString() : newValue); + }; + + return ( - ( - + { - if (field.inputChange) field.inputChange(e); - onChange( - field.type === "number" - ? Number.parseFloat(e.target.value) - : e.target.value, - ); - }} - {...field.properties} - {...rest} - disabled={disabled} - /> + : undefined + } + step={field.properties?.step} + value={field.type === "number" ? String(controllerField.value) : controllerField.value} + id={field.name} + onChange={handleInputChange} + {...restProperties} + disabled={disabled} + /> + + {fieldLength?.showCharacterCount && fieldLength?.max && ( +
+ {currentLength ?? fieldLength?.currentValueLength}/{fieldLength?.max} +
)} - /> + ); } diff --git a/src/components/PageComponents/Config/Device/index.tsx b/src/components/PageComponents/Config/Device/index.tsx index 845995b9..95498f2d 100644 --- a/src/components/PageComponents/Config/Device/index.tsx +++ b/src/components/PageComponents/Config/Device/index.tsx @@ -82,6 +82,19 @@ export const Device = () => { label: "Disable Triple Click", description: "Disable triple click", }, + { + type: 'text', + name: 'tzdef', + label: 'POSIX Timezone', + description: 'The POSIX timezone string for the device', + properties: { + fieldLength: { + max: 64, + currentValueLength: config.device?.tzdef?.length, + showCharacterCount: true, + } + }, + }, { type: "toggle", name: "ledHeartbeatDisabled",