[Fixed issue/1302] Edited the behavior of form submitting when you send default values of fields (#1586)

* edited the behavior of form submitting

* removed useMemo hook in SendMessage

Co-authored-by: Roman Zabaluev <rzabaluev@provectus.com>
This commit is contained in:
Denys Malofeiev 2022-02-21 17:02:50 +02:00 committed by GitHub
parent d40af6d386
commit 77706ce670
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,6 +1,6 @@
import Editor from 'components/common/Editor/Editor'; import Editor from 'components/common/Editor/Editor';
import PageLoader from 'components/common/PageLoader/PageLoader'; import PageLoader from 'components/common/PageLoader/PageLoader';
import React from 'react'; import React, { useEffect } from 'react';
import { useForm, Controller } from 'react-hook-form'; import { useForm, Controller } from 'react-hook-form';
import { useHistory, useParams } from 'react-router'; import { useHistory, useParams } from 'react-router';
import { clusterTopicMessagesPath } from 'lib/paths'; import { clusterTopicMessagesPath } from 'lib/paths';
@ -28,12 +28,6 @@ interface RouterParams {
const SendMessage: React.FC = () => { const SendMessage: React.FC = () => {
const dispatch = useAppDispatch(); const dispatch = useAppDispatch();
const { clusterName, topicName } = useParams<RouterParams>(); const { clusterName, topicName } = useParams<RouterParams>();
const {
register,
handleSubmit,
formState: { isSubmitting, isDirty },
control,
} = useForm({ mode: 'onChange' });
const history = useHistory(); const history = useHistory();
jsf.option('fillProperties', false); jsf.option('fillProperties', false);
@ -73,6 +67,29 @@ const SendMessage: React.FC = () => {
); );
}, [messageSchema, schemaIsFetched]); }, [messageSchema, schemaIsFetched]);
const {
register,
handleSubmit,
formState: { isSubmitting, isDirty },
control,
reset,
} = useForm({
mode: 'onChange',
defaultValues: {
key: keyDefaultValue,
content: contentDefaultValue,
headers: undefined,
partition: undefined,
},
});
useEffect(() => {
reset({
key: keyDefaultValue,
content: contentDefaultValue,
});
}, [keyDefaultValue, contentDefaultValue]);
const onSubmit = async (data: { const onSubmit = async (data: {
key: string; key: string;
content: string; content: string;
@ -162,12 +179,12 @@ const SendMessage: React.FC = () => {
<Controller <Controller
control={control} control={control}
name="key" name="key"
render={({ field: { name, onChange } }) => ( render={({ field: { name, onChange, value } }) => (
<Editor <Editor
readOnly={isSubmitting} readOnly={isSubmitting}
defaultValue={keyDefaultValue}
name={name} name={name}
onChange={onChange} onChange={onChange}
value={value}
/> />
)} )}
/> />
@ -177,12 +194,12 @@ const SendMessage: React.FC = () => {
<Controller <Controller
control={control} control={control}
name="content" name="content"
render={({ field: { name, onChange } }) => ( render={({ field: { name, onChange, value } }) => (
<Editor <Editor
readOnly={isSubmitting} readOnly={isSubmitting}
defaultValue={contentDefaultValue}
name={name} name={name}
onChange={onChange} onChange={onChange}
value={value}
/> />
)} )}
/> />