import React from 'react'; import { Controller, useForm } from 'react-hook-form'; import { Trans, useTranslation } from 'react-i18next'; import { CLIENT_ID_LINK } from '../../../../helpers/constants'; import { removeEmptyLines, trimMultilineString } from '../../../../helpers/helpers'; import { Textarea } from '../../../ui/Controls/Textarea'; const fields = [ { id: 'allowed_clients', title: 'access_allowed_title', subtitle: 'access_allowed_desc', normalizeOnBlur: removeEmptyLines, }, { id: 'disallowed_clients', title: 'access_disallowed_title', subtitle: 'access_disallowed_desc', normalizeOnBlur: trimMultilineString, }, { id: 'blocked_hosts', title: 'access_blocked_title', subtitle: 'access_blocked_desc', normalizeOnBlur: removeEmptyLines, }, ]; interface FormProps { initialValues?: { allowed_clients?: string; disallowed_clients?: string; blocked_hosts?: string; }; onSubmit: (data: any) => void; processingSet: boolean; } interface FormData { allowed_clients: string; disallowed_clients: string; blocked_hosts: string; } const Form = ({ initialValues, onSubmit, processingSet }: FormProps) => { const { t } = useTranslation(); const { control, handleSubmit, watch, formState: { isSubmitting, isDirty }, } = useForm({ mode: 'onChange', defaultValues: { allowed_clients: initialValues?.allowed_clients || '', disallowed_clients: initialValues?.disallowed_clients || '', blocked_hosts: initialValues?.blocked_hosts || '', }, }); const allowedClients = watch('allowed_clients'); const renderField = ({ id, title, subtitle, normalizeOnBlur, }: { id: keyof FormData; title: string; subtitle: string; normalizeOnBlur: (value: string) => string; }) => { const disabled = allowedClients && id === 'disallowed_clients'; return (
, }}> {subtitle}
(