feat:setExpandedKeys

This commit is contained in:
shixiaohua 2024-02-07 09:46:28 +08:00
parent c58eca1d9a
commit 7db5996c08
2 changed files with 26 additions and 9 deletions

View File

@ -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}

View File

@ -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