复杂set修改使用immer简化

修复底部工具点击区域遮挡问题
master
LiuJiaNan 2026-03-02 16:35:37 +08:00
parent d1de5b0d43
commit 589aa29376
4 changed files with 37 additions and 18 deletions

View File

@ -30,6 +30,7 @@
"autofit.js": "^3.2.8", "autofit.js": "^3.2.8",
"dayjs": "^1.11.7", "dayjs": "^1.11.7",
"echarts": "^6.0.0", "echarts": "^6.0.0",
"immer": "^11.1.4",
"lodash-es": "^4.17.21", "lodash-es": "^4.17.21",
"mitt": "^3.0.1", "mitt": "^3.0.1",
"motion": "^12.23.26", "motion": "^12.23.26",

View File

@ -1,3 +1,4 @@
import { produce } from "immer";
import { AnimatePresence, motion } from "motion/react"; import { AnimatePresence, motion } from "motion/react";
import { useContext, useEffect, useState } from "react"; import { useContext, useEffect, useState } from "react";
import bg7 from "~/assets/images/map_bi/bottom_utils/bg7.png"; import bg7 from "~/assets/images/map_bi/bottom_utils/bg7.png";
@ -52,12 +53,13 @@ function BottomUtils(props) {
); );
const resetAllCheck = () => { const resetAllCheck = () => {
setList((prevList) => { setList(produce((draft) => {
return prevList.map(item => ({ draft.forEach((item) => {
...item, item.list.forEach((item1) => {
list: item.list.map(item1 => ({ ...item1, check: false })), item1.check = false;
})); });
}); });
}));
}; };
useEffect(() => { useEffect(() => {
@ -95,11 +97,9 @@ function BottomUtils(props) {
const optionsItemsClick = (index, index1, item, item1) => { const optionsItemsClick = (index, index1, item, item1) => {
// 子选项点击处理 // 子选项点击处理
setList((prevList) => { setList(produce((draft) => {
const updatedList = [...prevList]; draft[index].list[index1].check = !draft[index].list[index1].check;
updatedList[index].list[index1].check = !updatedList[index].list[index1].check; }));
return updatedList;
});
}; };
const renderPortUtils = () => { const renderPortUtils = () => {
@ -118,6 +118,7 @@ function BottomUtils(props) {
onClick={() => !portUtilsIsAnimating && !isAllowClick && optionsClick(index)} onClick={() => !portUtilsIsAnimating && !isAllowClick && optionsClick(index)}
style={{ style={{
cursor: isAllowClick ? "default" : "pointer", cursor: isAllowClick ? "default" : "pointer",
zIndex: isAllowClick ? -1 : 0,
}} }}
> >
<img src={isCurrentActive ? item.checkImg : item.img} alt="" className="img" /> <img src={isCurrentActive ? item.checkImg : item.img} alt="" className="img" />

View File

@ -55,11 +55,11 @@
.child_item { .child_item {
padding: 0 20px; padding: 0 20px;
text-align: center; text-align: center;
cursor: pointer;
.child_img { .child_img {
width: 60px; width: 60px;
height: 60px; height: 60px;
cursor: pointer;
&:hover { &:hover {
animation: bottomUtilsContainerSlideY 0.5s; animation: bottomUtilsContainerSlideY 0.5s;
@ -67,7 +67,7 @@
} }
.child_label { .child_label {
margin-top: -10px; margin-top: -5px;
font-size: 12px; font-size: 12px;
width: 80px; width: 80px;
color: rgba(255, 255, 255, 0.8); color: rgba(255, 255, 255, 0.8);

View File

@ -1,4 +1,5 @@
import { message } from "antd"; import { message } from "antd";
import { produce } from "immer";
import { motion } from "motion/react"; import { motion } from "motion/react";
import { useContext, useEffect, useState } from "react"; import { useContext, useEffect, useState } from "react";
import tooltipImg2 from "~/assets/images/map_bi/right_utils/branch_office/bg11.png"; import tooltipImg2 from "~/assets/images/map_bi/right_utils/branch_office/bg11.png";
@ -44,7 +45,12 @@ function RightUtils(props) {
useEffect(() => { useEffect(() => {
mitt.on(clickBackMittKey, () => { mitt.on(clickBackMittKey, () => {
setList(prev => prev.map(item => item.type === "fourColor" || item.type === "wall" ? { ...item, check: false } : item)); setList(produce((draft) => {
draft.forEach((item) => {
if (item.type === "fourColor" || item.type === "wall")
item.check = false;
});
}));
}); });
return () => { return () => {
@ -53,14 +59,21 @@ function RightUtils(props) {
}, []); }, []);
useEffect(() => { useEffect(() => {
setList(list.map(item => item.type === "full" ? { ...item, check: props.isFullscreen } : item)); setList(produce((draft) => {
draft.forEach((item) => {
if (item.type === "full")
item.check = props.isFullscreen;
});
}));
}, [props.isFullscreen]); }, [props.isFullscreen]);
const clickRightTools = (index, type) => { const clickRightTools = (index, type) => {
let check; let check;
if (list[index].check !== undefined) { if (list[index].check !== undefined) {
check = !list[index].check; check = !list[index].check;
setList(list.map((item, i) => index === i ? { ...item, check: !item.check } : item)); setList(produce((draft) => {
draft[index].check = !draft[index].check;
}));
} }
switch (type) { switch (type) {
@ -98,7 +111,9 @@ function RightUtils(props) {
case "fourColor": case "fourColor":
if (!currentPort) { if (!currentPort) {
message.warning("请选择要查看的港口"); message.warning("请选择要查看的港口");
setList(list.map((item, i) => index === i ? { ...item, check: false } : item)); setList(produce((draft) => {
draft[index].check = false;
}));
return; return;
} }
if (check) { if (check) {
@ -114,7 +129,9 @@ function RightUtils(props) {
case "wall": case "wall":
if (!currentPort) { if (!currentPort) {
message.warning("请选择要查看的港口"); message.warning("请选择要查看的港口");
setList(list.map((item, i) => index === i ? { ...item, check: false } : item)); setList(produce((draft) => {
draft[index].check = false;
}));
return; return;
} }
if (check) if (check)