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 |   80 ++++++++++++++++++++++-----------------
 1 files changed, 45 insertions(+), 35 deletions(-)

diff --git a/zy-acs-flow/src/map/settings/CopyDrawer.jsx b/zy-acs-flow/src/map/settings/CopyDrawer.jsx
index 60b46e2..6aa9880 100644
--- a/zy-acs-flow/src/map/settings/CopyDrawer.jsx
+++ b/zy-acs-flow/src/map/settings/CopyDrawer.jsx
@@ -1,5 +1,5 @@
 import React, { useEffect } from 'react';
-import { useForm, Controller } from 'react-hook-form';
+import { useForm, useWatch, Controller } from 'react-hook-form';
 import {
     Box,
     Grid,
@@ -15,7 +15,6 @@
     Divider,
     Drawer,
     IconButton,
-    LinearProgress,
     Switch,
     FormControlLabel,
     FormLabel,
@@ -47,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();
@@ -55,51 +63,47 @@
     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 (watch('autoIncrement') && incrementOptions && incrementOptions.length > 0) {
-            const currentIncrementValue = watch('incrementValue');
-            if (!currentIncrementValue) {
+        if (sprite && value && Object.keys(value).length > 0) {
+            reset(getDefaultFormValues(value));
+        }
+    }, [sprite, value, reset, incrementOptions, setValue]);
+
+    useEffect(() => {
+        if (autoIncrement && incrementOptions && incrementOptions.length > 0) {
+            if (!incrementValue) {
                 setValue('incrementValue', incrementOptions[0].value);
             }
         } else {
             setValue('incrementValue', undefined);
         }
-    }, [watch('autoIncrement'), incrementOptions, setValue, watch]);
-
-    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]);
+    }, [autoIncrement, incrementOptions, setValue]);
 
     const handleClose = () => {
         onCancel();
     }
 
-    const onFormSubmit = (data) => {
-        if (data) {
-            console.log(data);
+    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;
+    };
 
-        }
-        if (handleOnCopy) {
-            handleOnCopy(data);
-        }
+    const onFormSubmit = (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,9 +206,14 @@
                                                 />
                                             )}
                                         />
+                                        {errors.autoIncrement && (
+                                            <Typography color="error">
+                                                {errors.autoIncrement.message}
+                                            </Typography>
+                                        )}
                                     </Grid>
 
-                                    {(!!incrementOptions?.length && watch('autoIncrement')) && (
+                                    {(!!incrementOptions?.length && autoIncrement) && (
                                         <Grid item xs={12}>
                                             <FormControl fullWidth>
                                                 <FormLabel sx={{ mb: 1 }}>
@@ -234,7 +244,7 @@
                                         </Grid>
                                     )}
 
-                                    {watch('autoIncrement') && (
+                                    {autoIncrement && (
                                         <Grid item xs={12}>
                                             <FormControl fullWidth>
                                                 <FormLabel sx={{

--
Gitblit v1.9.1