Advertisement
Pandaaaa906

react_tree_view

Dec 17th, 2021
1,026
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, {useCallback, useState} from 'react';
  2. import {Button, Checkbox, Grid} from "@material-ui/core";
  3. import produce from "immer";
  4.  
  5. TreeView.propTypes = {
  6.  
  7. };
  8.  
  9. const init_tree = [
  10.     {id: 1, name: 'node1', checked: false, childrenNode: [
  11.             {id: 12, name: 'node12', checked: false},
  12.             {id: 13, name: 'node13', checked: false, childrenNode: [
  13.                     {id: 131, name: 'node131', checked: false},
  14.                 ]},
  15.         ]},
  16.     {id: 2, name: 'node2', checked: false, childrenNode: [
  17.             {id: 21, name: 'node21', checked: false},
  18.             {id: 22, name: 'node22', checked: false},
  19.             {id: 23, name: 'node23', checked: false, childrenNode: [
  20.                     {id: 231, name: 'node231', checked: false},
  21.                     {id: 232, name: 'node232', checked: false},
  22.                     {id: 233, name: 'node233', checked: false},
  23.                 ]},
  24.         ]},
  25. ]
  26.  
  27. function useTree(init_tree = []){
  28.     const [tree, setTree] = useState(init_tree)
  29.  
  30.     const getCurNode = (root, indexes)=>{
  31.         const [n, ...left_indexes] = indexes
  32.         let node = root[n]
  33.         left_indexes.map(i=>{
  34.             node = node?.childrenNode[i]
  35.         })
  36.         return node
  37.     }
  38.  
  39.     const _setAllNodesChecked = (root, checked)=>{
  40.         if(!root.hasOwnProperty('childrenNode')){return}
  41.         root.childrenNode.map(node=>{
  42.             node.checked = checked
  43.             _setAllNodesChecked(node, checked)
  44.         })
  45.     }
  46.  
  47.     const toggleNode = useCallback((indexes)=>()=>{
  48.         setTree(produce(prevTree=>{
  49.             let node = getCurNode(prevTree, indexes)
  50.             node.checked = !node.checked
  51.         }))
  52.     }, [])
  53.  
  54.     const setAllNodesChecked = useCallback((indexes, checked)=>()=>{
  55.         setTree(produce(prevTree=>{
  56.             let node = getCurNode(prevTree, indexes)
  57.             node.checked = checked
  58.             _setAllNodesChecked(node, checked)
  59.         }))
  60.     }, [])
  61.  
  62.     return {
  63.         tree,
  64.         toggleNode,
  65.         setAllNodesChecked
  66.     }
  67. }
  68.  
  69. function TreeItem(props){
  70.     const {tree, name, checked, indexes, childrenNode} = props
  71.     return (
  72.         <React.Fragment>
  73.             <Grid container justifyContent={"space-between"}>
  74.                 <Grid item>
  75.                     <p style={{marginLeft: 16 * indexes.length}}>{name}</p>
  76.                 </Grid>
  77.                 <Grid item>
  78.                     <Checkbox checked={checked} onClick={tree.toggleNode(indexes)}/>
  79.                     <Button onClick={tree.setAllNodesChecked(indexes, true)}>全选</Button>
  80.                     <Button onClick={tree.setAllNodesChecked(indexes, false)}>取消全选</Button>
  81.                 </Grid>
  82.             </Grid>
  83.             {childrenNode?.map((item, index)=>(
  84.                 <TreeItem
  85.                     key={item.id}
  86.                     tree={tree}
  87.                     indexes={[...indexes, index]}
  88.                     {...item}
  89.                 />
  90.             ))}
  91.         </React.Fragment>
  92.     )
  93. }
  94.  
  95. function TreeView() {
  96.     const tree = useTree(init_tree)
  97.     return (
  98.         <Grid container justifyContent={"center"}>
  99.             <Grid item xs={6}>
  100.                 {tree.tree.map((item, index)=>(
  101.                     <TreeItem
  102.                         key={item.id}
  103.                         tree={tree} indexes={[index]}
  104.                         {...item}
  105.                     />
  106.                 ))}
  107.             </Grid>
  108.         </Grid>
  109.     );
  110. }
  111.  
  112. export default TreeView;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement