feat:setExpandedKeys
This commit is contained in:
parent
c58eca1d9a
commit
7db5996c08
|
@ -6,7 +6,7 @@ import "./index.less"
|
||||||
|
|
||||||
const {Search} = Input;
|
const {Search} = Input;
|
||||||
import {useSelector, useDispatch} from "react-redux";
|
import {useSelector, useDispatch} from "react-redux";
|
||||||
import {addTableBarItem} from "../../redux/tableBarItem_reducer";
|
import {addExpandedKeys, addTableBarItem, setExpandedKeys} from "../../redux/tableBarItem_reducer";
|
||||||
import {readDir} from "../../utils/File";
|
import {readDir} from "../../utils/File";
|
||||||
import {nextDirAdd} from "../../redux/dirMessage_reducer";
|
import {nextDirAdd} from "../../redux/dirMessage_reducer";
|
||||||
// const defaultData = [];
|
// const defaultData = [];
|
||||||
|
@ -127,9 +127,10 @@ const flushTree = (fileDirDate) => {
|
||||||
}
|
}
|
||||||
const ItemTree = (prop) => {
|
const ItemTree = (prop) => {
|
||||||
console.log("prop.filePath:", prop.filePath)
|
console.log("prop.filePath:", prop.filePath)
|
||||||
const [expandedKeys, setExpandedKeys] = useState([]);
|
|
||||||
const [searchValue, setSearchValue] = useState('');
|
|
||||||
const dispatch = useDispatch()
|
const dispatch = useDispatch()
|
||||||
|
// const [expandedKeys, setExpandedKeys] = useState([]);
|
||||||
|
const expandedKeys = useSelector(state => state.tableBarItem.expandedKeyList)
|
||||||
|
const [searchValue, setSearchValue] = useState('');
|
||||||
const [autoExpandParent, setAutoExpandParent] = useState(true);
|
const [autoExpandParent, setAutoExpandParent] = useState(true);
|
||||||
// let filePath = useSelector(state => state.dirMessage.data);
|
// let filePath = useSelector(state => state.dirMessage.data);
|
||||||
const [defaultValueState, setDefaultValueState] = useState(flushTree(prop.filePath));
|
const [defaultValueState, setDefaultValueState] = useState(flushTree(prop.filePath));
|
||||||
|
@ -137,7 +138,7 @@ const ItemTree = (prop) => {
|
||||||
setDefaultValueState(flushTree(prop.filePath))
|
setDefaultValueState(flushTree(prop.filePath))
|
||||||
}, [prop]);
|
}, [prop]);
|
||||||
const onExpand = (newExpandedKeys) => {
|
const onExpand = (newExpandedKeys) => {
|
||||||
setExpandedKeys(newExpandedKeys);
|
dispatch(setExpandedKeys(newExpandedKeys));
|
||||||
setAutoExpandParent(false);
|
setAutoExpandParent(false);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -163,7 +164,7 @@ const ItemTree = (prop) => {
|
||||||
return null;
|
return null;
|
||||||
})
|
})
|
||||||
.filter((item, i, self) => !!(item && self.indexOf(item) === i));
|
.filter((item, i, self) => !!(item && self.indexOf(item) === i));
|
||||||
setExpandedKeys(newExpandedKeys);
|
dispatch(setExpandedKeys(newExpandedKeys));
|
||||||
setSearchValue(value);
|
setSearchValue(value);
|
||||||
setAutoExpandParent(true);
|
setAutoExpandParent(true);
|
||||||
};
|
};
|
||||||
|
@ -185,8 +186,7 @@ const ItemTree = (prop) => {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
// 打开当前目录
|
// 打开当前目录
|
||||||
expandedKeys.push(e.node.key)
|
dispatch(addExpandedKeys([e.node.key]));
|
||||||
setExpandedKeys([...expandedKeys]);
|
|
||||||
setAutoExpandParent(false);
|
setAutoExpandParent(false);
|
||||||
} else {
|
} else {
|
||||||
// 打开文件
|
// 打开文件
|
||||||
|
@ -244,6 +244,8 @@ const ItemTree = (prop) => {
|
||||||
// 是否自动展开父节点
|
// 是否自动展开父节点
|
||||||
autoExpandParent={autoExpandParent}
|
autoExpandParent={autoExpandParent}
|
||||||
showIcon={true}
|
showIcon={true}
|
||||||
|
defaultSelectedKeys={[useSelector(state => state.tableBarItem.activeKey)]}
|
||||||
|
defaultExpandedKeys={useSelector(state => state.tableBarItem.expandedKeyList)}
|
||||||
// treeNodes 数据,如果设置则不需要手动构造 TreeNode 节点(key 在整个树范围内唯一)
|
// treeNodes 数据,如果设置则不需要手动构造 TreeNode 节点(key 在整个树范围内唯一)
|
||||||
treeData={defaultValueState}
|
treeData={defaultValueState}
|
||||||
onSelect={onSelect}
|
onSelect={onSelect}
|
||||||
|
|
|
@ -14,7 +14,8 @@ export const tableBarItemSlice = createSlice({
|
||||||
initialState: {
|
initialState: {
|
||||||
type:"tableBarItem",
|
type:"tableBarItem",
|
||||||
data: [],
|
data: [],
|
||||||
activeKey:""
|
activeKey:"",
|
||||||
|
expandedKeyList:[]
|
||||||
},
|
},
|
||||||
reducers: {
|
reducers: {
|
||||||
addTableBarItem: (state, action) => {
|
addTableBarItem: (state, action) => {
|
||||||
|
@ -55,11 +56,25 @@ export const tableBarItemSlice = createSlice({
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
state.activeKey = action.payload.filePath
|
state.activeKey = action.payload.filePath
|
||||||
|
},
|
||||||
|
addExpandedKeys:(state, action)=>{
|
||||||
|
state.expandedKeyList.push(...action.payload)
|
||||||
|
state.expandedKeyList=Array.from(new Set([...state.expandedKeyList]))
|
||||||
|
},
|
||||||
|
setExpandedKeys:(state, action)=>{
|
||||||
|
state.expandedKeyList=action.payload
|
||||||
|
},
|
||||||
|
removeExpandedKeys:(state, action)=>{
|
||||||
|
state.expandedKeyList=state.expandedKeyList.filter(key=>key!==action.payload)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
export const { addTableBarItem,
|
export const { addTableBarItem,
|
||||||
removeTableBarItem,
|
removeTableBarItem,
|
||||||
setActiveKey,
|
setActiveKey,
|
||||||
updatedSavedFile} = tableBarItemSlice.actions
|
updatedSavedFile,
|
||||||
|
setExpandedKeys,
|
||||||
|
removeExpandedKeys,
|
||||||
|
addExpandedKeys
|
||||||
|
} = tableBarItemSlice.actions
|
||||||
export default tableBarItemSlice.reducer
|
export default tableBarItemSlice.reducer
|
||||||
|
|
Loading…
Reference in New Issue