import React from 'react'; import { ErrorMessage } from '@hookform/error-message'; import { TOPIC_CUSTOM_PARAMS } from 'lib/constants'; import { FieldArrayWithId, useFormContext } from 'react-hook-form'; import { remove as _remove } from 'lodash'; import { TopicFormData } from 'redux/interfaces'; import CustomParamButton from './CustomParamButton'; interface Props { isDisabled: boolean; index: number; existingFields: string[]; field: FieldArrayWithId; remove: (index?: number | number[] | undefined) => void; setExistingFields: React.Dispatch>; } const CustomParamField: React.FC = ({ field, isDisabled, index, remove, existingFields, setExistingFields, }) => { const { register, formState: { errors }, setValue, watch, } = useFormContext(); const nameValue = watch(`customParams.${index}.name`); let prevName = ''; React.useEffect(() => { prevName = nameValue; }, []); React.useEffect(() => { if (nameValue !== prevName) { let newExistingFields = [...existingFields]; if (prevName) { newExistingFields = _remove(newExistingFields, (el) => el === prevName); } prevName = nameValue; newExistingFields.push(nameValue); setExistingFields(newExistingFields); setValue(`customParams.${index}.value`, TOPIC_CUSTOM_PARAMS[nameValue]); } }, [nameValue]); return (

remove(index)} />
); }; export default React.memo(CustomParamField);