import React, { useMemo } from 'react'; import { Controller, useFormContext } from 'react-hook-form'; import { useTranslation } from 'react-i18next'; import { UINT32_RANGE } from '../../../helpers/constants'; import { validateGatewaySubnetMask, validateIpForGatewaySubnetMask, validateIpv4, validateIpv4RangeEnd, validateNotInRange, validateRequiredValue, } from '../../../helpers/validators'; import { DhcpFormValues } from '.'; import { Input } from '../../ui/Controls/Input'; import { toNumber } from '../../../helpers/form'; type FormDHCPv4Props = { processingConfig?: boolean; ipv4placeholders?: { gateway_ip: string; subnet_mask: string; range_start: string; range_end: string; lease_duration: string; }; interfaces: any; onSubmit?: (data: DhcpFormValues) => void; }; const FormDHCPv4 = ({ processingConfig, ipv4placeholders, interfaces, onSubmit }: FormDHCPv4Props) => { const { t } = useTranslation(); const { handleSubmit, formState: { errors, isSubmitting }, control, watch, } = useFormContext(); const interfaceName = watch('interface_name'); const isInterfaceIncludesIpv4 = interfaces?.[interfaceName]?.ipv4_addresses; const formValues = watch('v4'); const isEmptyConfig = !Object.values(formValues || {}).some(Boolean); const hasV4Errors = errors.v4 && Object.keys(errors.v4).length > 0; const isDisabled = useMemo(() => { return isSubmitting || hasV4Errors || processingConfig || !isInterfaceIncludesIpv4 || isEmptyConfig; }, [isSubmitting, hasV4Errors, processingConfig, isInterfaceIncludesIpv4, isEmptyConfig]); return (
(isEmptyConfig ? undefined : validateRequiredValue(value)), notInRange: validateNotInRange, }, }} render={({ field, fieldState }) => ( )} />
(isEmptyConfig ? undefined : validateRequiredValue(value)), subnet: validateGatewaySubnetMask, }, }} render={({ field, fieldState }) => ( )} />
( )} />
( )} />
(isEmptyConfig ? undefined : validateRequiredValue(value)), }, }} render={({ field, fieldState }) => ( { const { value } = e.target; field.onChange(toNumber(value)); }} /> )} />
); }; export default FormDHCPv4;