From 7d549f074a154850d2b1d8c581503efaf2122207 Mon Sep 17 00:00:00 2001 From: luxiaotao1123 <t1341870251@163.com> Date: 星期五, 11 十月 2024 16:02:40 +0800 Subject: [PATCH] # --- zy-acs-flow/src/map/settings/CopyDrawer.jsx | 56 ++++++++++++++++++++++++++------------------------------ 1 files changed, 26 insertions(+), 30 deletions(-) diff --git a/zy-acs-flow/src/map/settings/CopyDrawer.jsx b/zy-acs-flow/src/map/settings/CopyDrawer.jsx index 60b46e2..075dc5b 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(); @@ -54,39 +62,28 @@ const deviceType = sprite?.data?.type; 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', - }, + const { control, handleSubmit, reset, watch, setValue } = useForm({ + 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(); @@ -95,7 +92,6 @@ const onFormSubmit = (data) => { if (data) { console.log(data); - } if (handleOnCopy) { handleOnCopy(data); @@ -203,7 +199,7 @@ /> </Grid> - {(!!incrementOptions?.length && watch('autoIncrement')) && ( + {(!!incrementOptions?.length && autoIncrement) && ( <Grid item xs={12}> <FormControl fullWidth> <FormLabel sx={{ mb: 1 }}> @@ -234,7 +230,7 @@ </Grid> )} - {watch('autoIncrement') && ( + {autoIncrement && ( <Grid item xs={12}> <FormControl fullWidth> <FormLabel sx={{ -- Gitblit v1.9.1