首页 > 其他分享 >React-tree-biuld bug2.0

React-tree-biuld bug2.0

时间:2023-01-18 01:55:06浏览次数:52  
标签:node const title tree React item bug2.0 key children

import { Tree,Button,Card } from 'antd';
import {useEffect, useState} from 'react';
import './App.css'
const flattren = (treeData)=>{
    let res = []
    const mapTreeHelper =(node)=>{
        if (!node.children) {
            res.push(node)
            return;
        }
        if(node.children){
            res.push(node)
            node.children.forEach(item=>mapTreeHelper(item))
        }
    }
    treeData.forEach(node => mapTreeHelper(node));
    return res
}
const treeData = [
    {
        title: '0-0',
        key: '0-0',
        // children: [
        //     {
        //         title: '0-0-0',
        //         key: '0-0-0',
        //         // disabled: true,
        //         children: [
        //             {
        //                 title: '0-0-0-0',
        //                 key: '0-0-0-0',
        //                 children:[
        //                     {
        //                         title: '0-0-0-0-0',
        //                         key: '0-0-0-0-0',
        //                         children:[
        //                             {
        //                                 title: '0-0-0-0-0-0',
        //                                 key: '0-0-0-0-0-0',
        //                                 // disabled: true,
        //                             }
        //                         ]
        //                     },
        //                     {
        //                         title: '0-0-0-0-1',
        //                         key: '0-0-0-0-1',
        //                     }
        //                 ]
        //             },
        //             {
        //                 title: '0-0-0-1',
        //                 key: '0-0-0-1',
        //             },
        //             {
        //                 title: '0-0-0-2',
        //                 key: '0-0-0-2',
        //             },
        //         ],
        //     },
        //     {
        //         title: '0-0-1',
        //         key: '0-0-1',
        //         children: [
        //             {
        //                 title: '0-0-1-0',
        //                 key: '0-0-1-0',
        //             },
        //             {
        //                 title: '0-0-1-1',
        //                 key: '0-0-1-1',
        //             },
        //             {
        //                 title: '0-0-1-2',
        //                 key: '0-0-1-2',
        //             },
        //         ],
        //     },
        //     {
        //         title: '0-0-2',
        //         key: '0-0-2',
        //     },
        // ],
    },
    {
        title: '0-1',
        key: '0-1',
        children: [
            {
                title: '0-1-0-0',
                key: '0-1-0-0',
            },
            {
                title: '0-1-0-1',
                key: '0-1-0-1',
            },
            {
                title: '0-1-0-2',
                key: '0-1-0-2',
            },
        ],
    },
    {
        title: '0-2',
        key: '0-2-222222222',

    },
];
const App = () => {
    const [expandedKeys, setExpandedKeys] = useState(['0-0-0', '0-0-1']);
    const [autoExpandParent, setAutoExpandParent] = useState(true);

    const [checkedKeys, setCheckedKeys] = useState(['0-0-0']); //keys数组
    const [selectedKeys, setSelectedKeys] = useState([]);

    const onExpand = (expandedKeysValue) => {
        console.log('onExpand', expandedKeysValue);
        // if not set autoExpandParent to false, if children expanded, parent can not collapse.
        // or, you can remove all expanded children keys.
        setExpandedKeys(expandedKeysValue);
        setAutoExpandParent(false);
    };
    const onCheck = (checkedKeysValue,a) => {

        console.log('onCheck--------checkedKeysValue', checkedKeysValue , a.checkedNodes.filter(item=>{
            return !item.children
        }));
        let checkLeefsValue = a.checkedNodes.filter(item=>{
            return !item.children
        })

        setCheckedKeys(checkedKeysValue);
        //?
        setRowData(checkLeefsValue)
    };
    const onSelect = (selectedKeysValue, info) => {
        console.log('onSelect', info);
        setSelectedKeys(selectedKeysValue);
    };
    const onButtonClick = ()=>{
        const res = mapTree(tree, rowData, {disabled: true});
        setTree([...res]);
        updateCardRowData([...rowData]);
    }

    const mapTree = (tree,rowData, {disabled}) => {
        const mapTreeHelper = (node) => {
            if (rowData.some((row) => row.key === node.key)) {
                node.disabled = disabled;
                node.keysReset = +new Date();
            }
            if (!node.children) return;
            node.children.forEach(item => mapTreeHelper(item));
        }
        tree.forEach(node => mapTreeHelper(node));
        return tree;
    }

    const deletefun = (delNode)=>{
        const res = mapTree(tree, [delNode], { disabled: false })
        console.log('deleteres,进行左边树的渲染哦', res);
        //t]ree的child的disable 1.删除 。改 rowdata nonono,不要改rowdata,只需要cardRowData,row 2.改tree的disable。
        setTree([...res])
        // setRowData(rowData.filter(old => old.key !== delNode.key))
        updateCardRowData(cardRowData.filter(old => old.key !== delNode.key));
    }

    const [tree, setTree] = useState(treeData); //为了重新渲染
    const [rowData,setRowData] = useState([]) // row是child叶子节点。
    const [cardRowData,updateCardRowData] = useState([])
    useEffect(()=>{
        console.log("row",rowData)//异步

    },[rowData])

    /**
     * const rollback = [];
     *
     */

    return (
        <div className="tree-select">
            <div className="tree-left">
                <div className="tree-left-top">项目选择</div>
                <div className="tree-left-corn">
                    <Tree
                        key={+new Date()}
                        checkable
                        onExpand={onExpand}
                        expandedKeys={expandedKeys}
                        autoExpandParent={autoExpandParent}
                        onCheck={onCheck}
                        checkedKeys={checkedKeys}
                        onSelect={onSelect}
                        selectedKeys={selectedKeys}
                        treeData={tree}
                    />
                </div>
            </div>
            <div className="tree-middle" onClick={ onButtonClick }>
                <Button/>
            </div>
            <div className="tree-right">
                <div className="tree-right-top">项目选择</div>
                <div className="tree-right-list">
                    <Card>
                        <ul>
                            {
                                (cardRowData || []).map(item => <li key={item.key} >
                                    <span>{item.title}</span>
                                    <button onClick={() => {deletefun(item)}}> del </button>
                                </li> )
                            }
                        </ul>
                    </Card>
                </div>
            </div>
        </div>

    );
};
export default App;

  

标签:node,const,title,tree,React,item,bug2.0,key,children
From: https://www.cnblogs.com/hacker-caomei/p/17059033.html

相关文章

  • React与TS的配合
    React与TS配合props限制npxcreate-react-appreact-ts-study--templatetypescriptsrc/index.tsximportAppfrom'./01_react-ts-props';src/01_react-ts-props.......
  • React路由与RTK的使用
    ReactRouter与ReduxReactRouterV6.4npmireact-router-domnpminstallsasssrc/App.js//importlogofrom'./logo.svg';import'./App.css';import{Link,Ou......
  • 安装react脚手架,运行
    npmuninstallcreate-react-app-g(以前创建过脚手架的话先进行卸载再重新安装以适配最新版本此步骤可逃过失败也是正常现象)npminstallcreate-react-app-g安装脚手......
  • React:开发者工具谷歌插件下载安装
    React:开发者工具谷歌插件最近学习前端react主要是想大概浏览一下,这里提供尚硅谷的是视屏资料中的谷歌插件的下载。(如有侵权联系删除)这里提供我的下载地址,其中除了插件,还有......
  • 自定义react项目脚手架
    呀呀呀呀,快过年啦,开心!而且之前一直想做的2个事,一个从0到1自定义开发ui组件库(从0到1开发一个自己的ui库)已经做好啦,还有一个做个自定义的脚手架,现在也做好啦。2个flag完成,开......
  • React: 路由重定向
    解决方案参考链接https://v5.reactrouter.com/web/example/route-config......
  • React:styled-components有趣的用法
    背景用于记录一些styled-components的有趣的用法绑定a标签的链接编写伪类在styleComponents中使用参数传入参数......
  • React: 动态添加样式
    问题背景在软件开发过程中,经常会出现动态添加style或className,比如:同一个表格组件在A处调用,需要固定前四列数据,B处调用则不用,那这时候,动态添加元素就派上了用场。解决方......
  • LibreOJ L6210 「美团 CodeM 决赛」tree
    链接难度:\(\texttt{?}\)有一颗\(n\)个点的树,每个点有权值\(x_i\),定义一条简单路径的权值为\(f(a_1\toa_2\to...\toa_k)=\frac{x_{a_1}\timesx_{a_2}\times...\t......
  • React-antd-tree-transfer
    import{Tree,Button,Card}from'antd';import{useEffect,useState}from'react';import'./App.css'consttreeData=[{title:'0-0',key:'0......