Browse Source

feat: added tzdef to device config

pull/536/head
Dan Ditomaso 1 year ago
parent
commit
59d97008f2
  1. 77
      src/components/Form/FormInput.tsx
  2. 13
      src/components/PageComponents/Config/Device/index.tsx

77
src/components/Form/FormInput.tsx

@ -7,7 +7,7 @@ import type { LucideIcon } from "lucide-react";
import { Eye, EyeOff } from "lucide-react"; import { Eye, EyeOff } from "lucide-react";
import type { ChangeEventHandler } from "react"; import type { ChangeEventHandler } from "react";
import { useState } 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<T> extends BaseFormBuilderProps<T> { export interface InputFieldProps<T> extends BaseFormBuilderProps<T> {
type: "text" | "number" | "password"; type: "text" | "number" | "password";
@ -17,6 +17,12 @@ export interface InputFieldProps<T> extends BaseFormBuilderProps<T> {
prefix?: string; prefix?: string;
suffix?: string; suffix?: string;
step?: number; step?: number;
fieldLength?: {
min?: number;
max?: number;
currentValueLength?: number;
showCharacterCount?: boolean;
},
action?: { action?: {
icon: LucideIcon; icon: LucideIcon;
onClick: () => void; onClick: () => void;
@ -29,42 +35,59 @@ export function GenericInput<T extends FieldValues>({
disabled, disabled,
field, field,
}: GenericFormElementProps<T, InputFieldProps<T>>) { }: GenericFormElementProps<T, InputFieldProps<T>>) {
const { fieldLength, ...restProperties } = field.properties || {};
const [passwordShown, setPasswordShown] = useState(false); const [passwordShown, setPasswordShown] = useState(false);
const [currentLength, setCurrentLength] = useState<number>(fieldLength?.currentValueLength || 0);
const { field: controllerField } = useController({
name: field.name,
control,
});
const togglePasswordVisiblity = () => { const togglePasswordVisiblity = () => {
setPasswordShown(!passwordShown); setPasswordShown(!passwordShown);
}; };
const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
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 ( return (
<Controller <div className="relative w-full">
name={field.name} <Input
control={control} type={field.type === "password" && passwordShown ? "text" : field.type}
render={({ field: { value, onChange, ...rest } }) => ( action={
<Input field.type === "password"
type={field.type === "password" && passwordShown
? "text"
: field.type}
action={field.type === "password"
? { ? {
icon: passwordShown ? EyeOff : Eye, icon: passwordShown ? EyeOff : Eye,
onClick: togglePasswordVisiblity, onClick: togglePasswordVisiblity,
} }
: undefined} : undefined
step={field.properties?.step} }
value={field.type === "number" ? Number.parseFloat(value) : value} step={field.properties?.step}
id={field.name} value={field.type === "number" ? String(controllerField.value) : controllerField.value}
onChange={(e) => { id={field.name}
if (field.inputChange) field.inputChange(e); onChange={handleInputChange}
onChange( {...restProperties}
field.type === "number" disabled={disabled}
? Number.parseFloat(e.target.value) />
: e.target.value,
); {fieldLength?.showCharacterCount && fieldLength?.max && (
}} <div className="absolute inset-y-0 right-0 flex items-center pr-3 text-sm text-slate-500 dark:text-slate-400">
{...field.properties} {currentLength ?? fieldLength?.currentValueLength}/{fieldLength?.max}
{...rest} </div>
disabled={disabled}
/>
)} )}
/> </div>
); );
} }

13
src/components/PageComponents/Config/Device/index.tsx

@ -82,6 +82,19 @@ export const Device = () => {
label: "Disable Triple Click", label: "Disable Triple Click",
description: "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", type: "toggle",
name: "ledHeartbeatDisabled", name: "ledHeartbeatDisabled",

Loading…
Cancel
Save