From 46f3a1367b818ab115e9dcaf87d78bef3d3e5790 Mon Sep 17 00:00:00 2001
From: luxiaotao1123 <t1341870251@163.com>
Date: 星期五, 11 十月 2024 16:38:14 +0800
Subject: [PATCH] #

---
 zy-acs-flow/src/map/settings/CopyDrawer.jsx |   62 ++++++++++++++++++-------------
 1 files changed, 36 insertions(+), 26 deletions(-)

diff --git a/zy-acs-flow/src/map/settings/CopyDrawer.jsx b/zy-acs-flow/src/map/settings/CopyDrawer.jsx
index 41a495c..6aa9880 100644
--- a/zy-acs-flow/src/map/settings/CopyDrawer.jsx
+++ b/zy-acs-flow/src/map/settings/CopyDrawer.jsx
@@ -46,6 +46,15 @@
     ],
 };
 
+const getDefaultFormValues = (value = {}) => ({
+    copyDirect: value.copyDirect || '',
+    copyCount: value.copyCount || '',
+    gap: 0.0,
+    autoIncrement: false,
+    incrementMode: 'ascending',
+    incrementValue: undefined,
+});
+
 const CopyDrawer = (props) => {
     const { open, onCancel, sprite, value, width = PAGE_DRAWER_WIDTH, handleOnCopy } = props;
     const notify = useNotification();
@@ -54,17 +63,17 @@
     const incrementOptions = incrementOptionsMap[deviceType]
 
     const { control, handleSubmit, reset, watch, setValue, formState: { errors } } = useForm({
-        defaultValues: {
-            copyDirect: value?.copyDirect || '',
-            copyCount: value?.copyCount || '',
-            gap: 0.0,
-            autoIncrement: false,
-            incrementMode: 'ascending',
-        },
+        defaultValues: getDefaultFormValues(value),
     });
 
     const autoIncrement = useWatch({ control, name: 'autoIncrement' });
     const incrementValue = useWatch({ control, name: 'incrementValue' });
+
+    useEffect(() => {
+        if (sprite && value && Object.keys(value).length > 0) {
+            reset(getDefaultFormValues(value));
+        }
+    }, [sprite, value, reset, incrementOptions, setValue]);
 
     useEffect(() => {
         if (autoIncrement && incrementOptions && incrementOptions.length > 0) {
@@ -76,30 +85,25 @@
         }
     }, [autoIncrement, incrementOptions, setValue]);
 
-    useEffect(() => {
-        if (sprite && value && Object.keys(value).length > 0) {
-            reset({
-                copyDirect: value.copyDirect || '',
-                copyCount: value.copyCount || '',
-                gap: 0.0,
-                autoIncrement: false,
-                incrementMode: 'ascending',
-                incrementValue: undefined,
-            });
-        }
-    }, [sprite, value, reset, incrementOptions, setValue]);
-
     const handleClose = () => {
         onCancel();
     }
 
+    const validateIncrement = (value) => {
+        if (!value) {
+            return true;
+        }
+        if (deviceType === DEVICE_TYPE.SHELF) {
+            if (!sprite?.data?.row || !sprite?.data?.bay) {
+                return 'Row or Bay value is required for SHELF device type.';
+            }
+        }
+        return true;
+    };
+
     const onFormSubmit = (data) => {
-        if (data) {
-            console.log(data);
-        }
-        if (handleOnCopy) {
-            handleOnCopy(data);
-        }
+        console.log(data);
+        handleOnCopy?.(data);
         notify.info(translate('common.response.success'));
     };
 
@@ -188,6 +192,7 @@
                                         <Controller
                                             name="autoIncrement"
                                             control={control}
+                                            rules={{ validate: validateIncrement }}
                                             render={({ field }) => (
                                                 <FormControlLabel
                                                     control={
@@ -201,6 +206,11 @@
                                                 />
                                             )}
                                         />
+                                        {errors.autoIncrement && (
+                                            <Typography color="error">
+                                                {errors.autoIncrement.message}
+                                            </Typography>
+                                        )}
                                     </Grid>
 
                                     {(!!incrementOptions?.length && autoIncrement) && (

--
Gitblit v1.9.1