From 13afe2f8da43a5b1e2aa3e1af876a128929d8cef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Z=C3=ADpek?= Date: Mon, 29 Aug 2022 09:48:30 +0200 Subject: [PATCH] Tiny form library --- client/src/assets/themes/_basic.scss | 2 +- .../components/BoxSettings/BoxSettings.tsx | 27 ++-- .../BoxSettings/components/DialSettings.tsx | 32 +---- .../BoxSettings/components/GraphSettings.tsx | 60 ++------- .../sensors/components/SensorFormModal.tsx | 6 +- client/src/utils/hooks/useForm.ts | 115 +++++++++++++++--- 6 files changed, 125 insertions(+), 117 deletions(-) diff --git a/client/src/assets/themes/_basic.scss b/client/src/assets/themes/_basic.scss index 7c6ea35..f6bf1fd 100644 --- a/client/src/assets/themes/_basic.scss +++ b/client/src/assets/themes/_basic.scss @@ -1,6 +1,6 @@ :root { --main-font: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; - --border-radius: 0.25rem; + --border-radius: 0.15rem; --main-bg-color: #eee; --main-fg-color: #000; --button-bg-color: #3988ff; diff --git a/client/src/pages/dashboard/components/BoxSettings/BoxSettings.tsx b/client/src/pages/dashboard/components/BoxSettings/BoxSettings.tsx index d2b5b3e..3af8b35 100644 --- a/client/src/pages/dashboard/components/BoxSettings/BoxSettings.tsx +++ b/client/src/pages/dashboard/components/BoxSettings/BoxSettings.tsx @@ -24,13 +24,9 @@ export const BoxSettings = ({ value, onSave, onClose, onRemove }: Props) => { const [data, setData] = useState(() => value.data) - const { - value: formState, - handleChange, - handleSubmit, - } = useForm({ + const { register, watch, handleSubmit } = useForm({ defaultValue: () => ({ - title: value.title, + title: value.title ?? '', type: value.data?.type ?? '', }), onSubmit: async (v) => { @@ -44,6 +40,8 @@ export const BoxSettings = ({ value, onSave, onClose, onRemove }: Props) => { }, }) + const type = watch('type') + const deleteConfirm = useConfirmModal({ content: 'Are you sure you want to delete the box?', onConfirm: () => { @@ -58,26 +56,17 @@ export const BoxSettings = ({ value, onSave, onClose, onRemove }: Props) => { <>
- +
-
- {formState.type === 'graph' && ( + {type === 'graph' && ( { /> )} - {formState.type === 'dial' && ( + {type === 'dial' && ( { - const { value: formState, handleChange } = useForm({ + const { register } = useForm({ defaultValue: () => ({ ...(value && omit(value, ['type'])), }), + onChange: (v) => onChange({ ...v, type: 'dial' }), }) - useEffect(() => { - onChange({ ...formState, type: 'dial' }) - }, [formState]) - return ( <>
- {sensors.map((s) => (
- +
- +
- +
) diff --git a/client/src/pages/dashboard/components/BoxSettings/components/GraphSettings.tsx b/client/src/pages/dashboard/components/BoxSettings/components/GraphSettings.tsx index c0415e3..9b0fa2d 100644 --- a/client/src/pages/dashboard/components/BoxSettings/components/GraphSettings.tsx +++ b/client/src/pages/dashboard/components/BoxSettings/components/GraphSettings.tsx @@ -2,7 +2,6 @@ import { SensorInfo } from '@/api/sensors' import { DashboardGraphData } from '@/utils/dashboard/parseDashboard' import { useForm } from '@/utils/hooks/useForm' import { omit } from '@/utils/omit' -import { useEffect } from 'preact/hooks' type Props = { sensors: SensorInfo[] @@ -11,26 +10,20 @@ type Props = { } export const GraphSettings = ({ value, onChange, sensors }: Props) => { - const { value: formState, handleChange } = useForm({ + const { register, watch } = useForm({ defaultValue: () => ({ ...(value && omit(value, ['type'])), }), + onChange: (v) => onChange({ ...v, type: 'graph' }), }) - useEffect(() => { - onChange({ ...formState, type: 'graph' }) - }, [formState]) + const colorMode = watch('colorMode') return ( <>
- {sensors.map((s) => (