#
luxiaotao1123
2024-10-11 7d549f074a154850d2b1d8c581503efaf2122207
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={{