import React from 'react'; import { FormError } from 'components/common/Input/Input.styled'; import { ErrorMessage } from '@hookform/error-message'; import { useForm, Controller, useFieldArray } from 'react-hook-form'; import { Button } from 'components/common/Button/Button'; import IconButtonWrapper from 'components/common/Icons/IconButtonWrapper'; import CloseIcon from 'components/common/Icons/CloseIcon'; import { yupResolver } from '@hookform/resolvers/yup'; import yup from 'lib/yupExtended'; import * as S from './QueryForm.styled'; export interface Props { fetching: boolean; hasResults: boolean; handleClearResults: () => void; handleSSECancel: () => void; submitHandler: (values: FormValues) => void; } export type StreamsPropertiesType = { key: string; value: string; }; export type FormValues = { ksql: string; streamsProperties: StreamsPropertiesType[]; }; const streamsPropertiesSchema = yup.object().shape({ key: yup.string().trim(), value: yup.string().trim(), }); const validationSchema = yup.object({ ksql: yup.string().trim().required(), streamsProperties: yup.array().of(streamsPropertiesSchema), }); const QueryForm: React.FC = ({ fetching, hasResults, handleClearResults, handleSSECancel, submitHandler, }) => { const { handleSubmit, setValue, control, formState: { errors }, } = useForm({ mode: 'onTouched', resolver: yupResolver(validationSchema), defaultValues: { ksql: '', streamsProperties: [{ key: '', value: '' }], }, }); const { fields, append, remove } = useFieldArray< FormValues, 'streamsProperties' >({ control, name: 'streamsProperties', }); return (
( { handleSubmit(submitHandler)(); }, }, ]} readOnly={fetching} /> )} /> Stream properties: {fields.map((item, index) => ( ( )} /> ( )} /> remove(index)}> ))}
); }; export default QueryForm;