From eb9b9b6c2b7705689a86fafdd7a0a11d889d4e0a Mon Sep 17 00:00:00 2001 From: Ildar Kamalov Date: Fri, 24 Jan 2025 17:09:54 +0300 Subject: [PATCH] fix dhcp form --- .../components/Settings/Dhcp/FormDHCPv4.tsx | 159 ++++++++++-------- .../components/Settings/Dhcp/FormDHCPv6.tsx | 134 ++++++++------- client/src/components/Settings/Dhcp/index.tsx | 4 +- .../components/Settings/Encryption/Form.tsx | 2 - .../components/Settings/Encryption/index.tsx | 1 - 5 files changed, 165 insertions(+), 135 deletions(-) diff --git a/client/src/components/Settings/Dhcp/FormDHCPv4.tsx b/client/src/components/Settings/Dhcp/FormDHCPv4.tsx index 729b6a80..e0aec054 100644 --- a/client/src/components/Settings/Dhcp/FormDHCPv4.tsx +++ b/client/src/components/Settings/Dhcp/FormDHCPv4.tsx @@ -1,5 +1,5 @@ import React, { useMemo } from 'react'; -import { useFormContext } from 'react-hook-form'; +import { Controller, useFormContext } from 'react-hook-form'; import { useTranslation } from 'react-i18next'; import { UINT32_RANGE } from '../../../helpers/constants'; @@ -12,6 +12,8 @@ import { validateRequiredValue, } from '../../../helpers/validators'; import { DhcpFormValues } from '.'; +import { Input } from '../../ui/Controls/Input'; +import { toNumber } from '../../../helpers/form'; type FormDHCPv4Props = { processingConfig?: boolean; @@ -30,9 +32,9 @@ const FormDHCPv4 = ({ processingConfig, ipv4placeholders, interfaces, onSubmit } const { t } = useTranslation(); const { - register, handleSubmit, formState: { errors, isSubmitting }, + control, watch, } = useFormContext(); @@ -52,121 +54,138 @@ const FormDHCPv4 = ({ processingConfig, ipv4placeholders, interfaces, onSubmit }
- - (isEmptyConfig ? undefined : validateRequiredValue(value)), notInRange: validateNotInRange, }, - })} + }} + render={({ field, fieldState }) => ( + + )} /> - {errors.v4?.gateway_ip && ( -
{t(errors.v4.gateway_ip.message)}
- )}
- - (isEmptyConfig ? undefined : validateRequiredValue(value)), subnet: validateGatewaySubnetMask, }, - })} + }} + render={({ field, fieldState }) => ( + + )} /> - {errors.v4?.subnet_mask && ( -
{t(errors.v4.subnet_mask.message)}
- )}
-
+
- ( + + )} /> - {errors.v4?.range_start && ( -
- {t(errors.v4.range_start.message)} -
- )}
- ( + + )} /> - {errors.v4?.range_end && ( -
- {errors.v4.range_end.message} -
- )}
- - (isEmptyConfig ? undefined : validateRequiredValue(value)), }, - })} + }} + render={({ field, fieldState }) => ( + { + const { value } = e.target; + field.onChange(toNumber(value)); + }} + /> + )} /> - {errors.v4?.lease_duration && ( -
- {t(errors.v4.lease_duration.message)} -
- )}
diff --git a/client/src/components/Settings/Dhcp/FormDHCPv6.tsx b/client/src/components/Settings/Dhcp/FormDHCPv6.tsx index 16facdb4..2f5cd2b8 100644 --- a/client/src/components/Settings/Dhcp/FormDHCPv6.tsx +++ b/client/src/components/Settings/Dhcp/FormDHCPv6.tsx @@ -1,10 +1,12 @@ import React, { useMemo } from 'react'; -import { useFormContext } from 'react-hook-form'; +import { Controller, useFormContext } from 'react-hook-form'; import { useTranslation } from 'react-i18next'; import { UINT32_RANGE } from '../../../helpers/constants'; import { validateIpv6, validateRequiredValue } from '../../../helpers/validators'; import { DhcpFormValues } from '.'; +import { Input } from '../../ui/Controls/Input'; +import { toNumber } from '../../../helpers/form'; type FormDHCPv6Props = { processingConfig?: boolean; @@ -20,9 +22,9 @@ type FormDHCPv6Props = { const FormDHCPv6 = ({ processingConfig, ipv6placeholders, interfaces, onSubmit }: FormDHCPv6Props) => { const { t } = useTranslation(); const { - register, handleSubmit, - formState: { errors, isSubmitting, isValid }, + formState: { isSubmitting, isValid }, + control, watch, } = useFormContext(); @@ -40,54 +42,60 @@ const FormDHCPv6 = ({ processingConfig, ipv6placeholders, interfaces, onSubmit }
-
+
- - isInterfaceIncludesIpv6 ? undefined : validateRequiredValue(value), - }, - })} + ( + + )} /> - {errors.v6?.range_start && ( -
- {t(errors.v6.range_start.message)} -
- )}
- - isInterfaceIncludesIpv6 ? undefined : validateRequiredValue(value), - }, - })} + ( + + )} /> - {errors.v6?.range_end && ( -
- {t(errors.v6.range_end.message)} -
- )}
@@ -96,26 +104,34 @@ const FormDHCPv6 = ({ processingConfig, ipv6placeholders, interfaces, onSubmit }
- - - isInterfaceIncludesIpv6 ? undefined : validateRequiredValue(value), - }, - })} + ( + { + const { value } = e.target; + field.onChange(toNumber(value)); + }} + /> + )} /> - {errors.v6?.lease_duration && ( -
{t(errors.v6.lease_duration.message)}
- )}
diff --git a/client/src/components/Settings/Dhcp/index.tsx b/client/src/components/Settings/Dhcp/index.tsx index 1f6957e6..fc807bb2 100644 --- a/client/src/components/Settings/Dhcp/index.tsx +++ b/client/src/components/Settings/Dhcp/index.tsx @@ -168,7 +168,7 @@ const Dhcp = () => { } }; - const handleSubmit = (values: any) => { + const handleSubmit = (values: DhcpFormValues) => { dispatch( setDhcpConfig({ interface_name, @@ -293,7 +293,6 @@ const Dhcp = () => { -
{ />
-
{ @@ -187,7 +186,6 @@ export const Form = ({ if (JSON.stringify(previousValues) !== JSON.stringify(watchedValues)) { // TODO(ik) onChange TLS config validation console.log('debouncedConfigValidation'); - debouncedConfigValidation(watchedValues); previousValuesRef.current = watchedValues; } }, [watchedValues]); diff --git a/client/src/components/Settings/Encryption/index.tsx b/client/src/components/Settings/Encryption/index.tsx index 4f3e142a..4dda55bc 100644 --- a/client/src/components/Settings/Encryption/index.tsx +++ b/client/src/components/Settings/Encryption/index.tsx @@ -72,7 +72,6 @@ export const Encryption = ({ encryption, setTlsConfig, validateTlsConfig }: Prop const submitValues = getSubmitValues(values); if (submitValues.enabled) { - console.log('validateTlsConfig'); validateTlsConfig(submitValues); } }, DEBOUNCE_TIMEOUT),