import React from 'react'; import { FormError } from 'components/common/Input/Input.styled'; import { ErrorMessage } from '@hookform/error-message'; import { useForm, Controller, useFieldArray, FormProvider, } from 'react-hook-form'; import { Button } from 'components/common/Button/Button'; import IconButtonWrapper from 'components/common/Icons/IconButtonWrapper'; import CloseCircleIcon from 'components/common/Icons/CloseCircleIcon'; import { yupResolver } from '@hookform/resolvers/yup'; import yup from 'lib/yupExtended'; import PlusIcon from 'components/common/Icons/PlusIcon'; import ReactAce from 'react-ace'; import Input from 'components/common/Input/Input'; import * as S from './QueryForm.styled'; interface QueryFormProps { fetching: boolean; hasResults: boolean; resetResults: () => void; submitHandler: (values: FormValues) => void; } 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, submitHandler, resetResults, }) => { const methods = useForm({ mode: 'onTouched', resolver: yupResolver(validationSchema), defaultValues: { ksql: '', streamsProperties: [{ key: '', value: '' }], }, }); const { handleSubmit, setValue, control, watch, formState: { errors, isDirty }, } = methods; const { fields, append, remove, update } = useFieldArray< FormValues, 'streamsProperties' >({ control, name: 'streamsProperties', }); const watchStreamProps = watch('streamsProperties'); const appendProperty = () => { append({ key: '', value: '' }); }; const removeProperty = (index: number) => () => { if (fields.length === 1) { update(index, { key: '', value: '' }); return; } remove(index); }; const isAppendDisabled = fetching || !!watchStreamProps.find((field) => !field.key); const inputRef = React.useRef(null); const handleFocus = () => { // eslint-disable-next-line @typescript-eslint/no-explicit-any const textInput = inputRef?.current?.editor?.textInput as any; if (textInput) { textInput.focus(); } }; const handleClear = () => { handleFocus(); resetResults(); }; return (
( { handleSubmit(submitHandler)(); }, }, ]} readOnly={fetching} ref={inputRef} /> )} /> Stream properties: {fields.map((field, index) => ( ))}
); }; export default QueryForm;