Przeglądaj źródła

[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>
Denys Malofeiev 3 lat temu
rodzic
commit
77706ce670

+ 28 - 11
kafka-ui-react-app/src/components/Topics/Topic/SendMessage/SendMessage.tsx

@@ -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}
                 />
                 />
               )}
               )}
             />
             />