From ed8ce9ae792adc1efb28af585ac2a6d2fcf65726 Mon Sep 17 00:00:00 2001 From: shixiaohua Date: Sun, 4 Feb 2024 12:30:40 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E5=B7=A6=E4=BE=A7=E6=A0=91=E4=BF=A1?= =?UTF-8?q?=E6=81=AF=E6=8C=81=E4=B9=85=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/ItemTree/index.jsx | 56 ++++++++++++++----------------- src/redux/clickFile_reducer.js | 1 + 2 files changed, 26 insertions(+), 31 deletions(-) diff --git a/src/components/ItemTree/index.jsx b/src/components/ItemTree/index.jsx index 0c5b042..207ed4b 100644 --- a/src/components/ItemTree/index.jsx +++ b/src/components/ItemTree/index.jsx @@ -5,6 +5,7 @@ import "./index.less" const {Search} = Input; import {store} from "../../redux/store"; import {clickFile} from "../../redux/clickFile_reducer"; +import {useSelector} from "react-redux"; const defaultData = []; // 将树平铺用于查找 @@ -58,41 +59,34 @@ function generateChildList(fileList) { const ItemTree = () => { const [expandedKeys, setExpandedKeys] = useState([]); const [searchValue, setSearchValue] = useState(''); - const [defaultValueState, setDefaultValueState] = useState([]); + const [autoExpandParent, setAutoExpandParent] = useState(true); - useEffect(() => { - let unsubscribe = store.subscribe(() => { - let fileDirDate = store.getState().dirMessage.data; - if (fileDirDate.length===0){ - return + const defaultValueStateSet = []; + let fileDirDate = useSelector(state => state.dirMessage.data); + if (fileDirDate.length>0){ + for (let i = 0; i < fileDirDate.length; i++) { + const node = fileDirDate[i]; + console.log("node:", node) + const {fileName, filePath, childList, dirFlag} = node; + const childListM = [] + if (childList.length > 0) { + childListM.push(...generateChildList(childList)); } - console.log("打开目录1fileDirDate:", fileDirDate) - for (let i = 0; i < fileDirDate.length; i++) { - const node = fileDirDate[i]; - console.log("node:", node) - const {fileName, filePath, childList, dirFlag} = node; - const childListM = [] - if (childList.length > 0) { - childListM.push(...generateChildList(childList)); - } - if (defaultData.filter(fileMessage => - fileMessage.key === filePath - ).length === 0) { - defaultData.push({ - "key": filePath, - "title": fileName, - "icon": , - "dirFlag": dirFlag, - "children": childListM - }); - } + if (defaultData.filter(fileMessage => fileMessage.key === filePath).length === 0) { + defaultData.push({ + "key": filePath, + "title": fileName, + "icon": , + "dirFlag": dirFlag, + "children": childListM + }); } - console.log("Array.from(new Set(defaultData)):", Array.from(new Set(defaultData))) - setDefaultValueState(Array.from(new Set(defaultData))) - }) - return ()=>unsubscribe() - }, []) + } + console.log("Array.from(new Set(defaultData)):", Array.from(new Set(defaultData))) + defaultValueStateSet.push(...Array.from(new Set(defaultData))) + } + const [defaultValueState, setDefaultValueState] = useState(defaultValueStateSet); const onExpand = (newExpandedKeys) => { setExpandedKeys(newExpandedKeys); diff --git a/src/redux/clickFile_reducer.js b/src/redux/clickFile_reducer.js index 0373533..19b6e1a 100644 --- a/src/redux/clickFile_reducer.js +++ b/src/redux/clickFile_reducer.js @@ -7,6 +7,7 @@ export const clickFileSlice = createSlice({ data: {} }, reducers: { + // 点击文件添加item,设置单前文件 clickFile: (state, action) => { console.log("clickFileSlice:clickFile", state, action) state.data=action.payload