verou
2025-03-18 1ec363b2a7195cb47e35a7e119012e20366aa71a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import React, { useState, useRef, useEffect, useMemo, useCallback } from "react";
import request from '@/utils/request';
import {
    SavedQueriesList,
    FilterLiveSearch,
    useNotify,
    useListContext
} from 'react-admin';
import BookmarkIcon from '@mui/icons-material/BookmarkBorder';
import { Box, Typography, Card, CardContent, useTheme, TextField } from '@mui/material';
import { RichTreeView } from "@mui/x-tree-view/RichTreeView";
import { TreeItem2 } from "@mui/x-tree-view/TreeItem2";
 
const MatListAside = () => {
    const theme = useTheme();
    const notify = useNotify();
    const { setFilters } = useListContext(); // 获取列表上下文
    const [selectedOption, setSelectedOption] = useState(null);
    const [treeData, setTreeData] = useState([]);
    // 用于管理展开项的状态
    const [expandedItems, setExpandedItems] = useState([]);
 
    // 递归收集所有节点的 id
    const collectAllNodeIds = (nodes) => {
        let allIds = [];
        nodes.forEach((node) => {
            allIds.push(node.id.toString());
            if (node.children && Array.isArray(node.children)) {
                allIds = allIds.concat(collectAllNodeIds(node.children));
            }
        });
        return allIds;
    };
 
    const haveChildren = (item) => {
        // 如果 item 是一个数组,遍历数组中的每个元素
        if (Array.isArray(item)) {
            return item.map((k) => haveChildren(k));
        }
 
        // 如果 item 是一个对象
        if (item && typeof item === 'object') {
            // 将 id 转换为字符串
            if (item.id !== undefined) {
                item.id = item.id.toString();
            }
 
            // 如果存在 children,递归处理 children
            if (item.children && Array.isArray(item.children)) {
                item.children = haveChildren(item.children);
            }
        }
 
        return item;
    };
 
    useEffect(() => {
        request.post('/matnrGroup/tree')
            .then(res => {
                if (res?.data?.code === 200) {
                    let data = res.data.data;
                    let items = haveChildren(data);
                    setTreeData(items);
                    // 当树数据更新时,更新展开项状态
                    setExpandedItems(collectAllNodeIds(items));
                } else {
                    notify(res.data.msg);
                }
            })
            .catch(error => {
                notify('Error fetching tree data');
            });
    }, []);
 
    const treeData1 = [
        {
            id: '19',
            label: '半成品 ',
            editable: true,
            children: [
                {
                    id: 'grid-community',
                    label: '@mui/x-data-grid',
                    editable: true,
                    children: [
                        { id: 'grid-community22', label: '@mui/x-data-grid', editable: true },
                    ],
                },
                { id: 'grid-pro', label: '@mui/x-data-grid-pro', editable: true },
                { id: 'grid-premium', label: '@mui/x-data-grid-premium', editable: true },
            ],
        },
        {
            id: '18',
            label: '原材料',
        },
        {
            id: 'charts',
            label: 'Charts',
            children: [{ id: 'charts-community', label: '@mui/x-charts' }],
        },
        {
            id: 'tree-view',
            label: 'Tree View',
            children: [{ id: 'tree-view-community', label: '@mui/x-tree-view' }],
        },
        {
            id: 'tree-view2',
            label: 'Tree View3',
            children: [{ id: 'tree-view-community1', label: '@mui/x-tree-view' }],
        },
    ];
 
    const handleNodeSelect = (event, nodeId) => {
        console.log('Selected Node ID:', nodeId);
        setFilters({ groupId: nodeId });
        // 在这里可以根据 nodeId 更新主内容区域
    };
 
    const handleSearch = () => {
        console.log('Search Input:', selectedOption);
    };
 
    const CustomCheckbox = React.forwardRef(function CustomCheckbox(props, ref) {
        return <input type="checkbox" ref={ref} {...props} />;
    });
 
    const CustomTreeItem = React.forwardRef(function CustomTreeItem(props, ref) {
        return (
            <TreeItem2
                {...props}
                ref={ref}
                slots={{
                    checkbox: CustomCheckbox,
                }}
            />
        );
    });
 
    return (
        <Card
            sx={{
                order: -1,
                mr: 2,
                mt: 8,
                alignSelf: 'flex-start',
                border: theme.palette.mode === 'light' && '1px solid #e0e0e3',
                width: 250,
                minWidth: 150,
                height: `calc(100% - 120px)`,
            }}
        >
            <CardContent>
                <SavedQueriesList icon={<BookmarkIcon />} />
                <FilterLiveSearch source="condition" />
                <RichTreeView
                    // 使用 expandedItems 控制展开状态
                    expandedItems={expandedItems}
                    // 处理展开项状态的变化
                    onExpandedItemsChange={(newExpandedItems) => setExpandedItems(newExpandedItems)}
                    expansionTrigger="iconContainer"
                    items={treeData}
                    slots={CustomTreeItem}
                    onItemClick={handleNodeSelect} // 监听节点点击事件
                />
            </CardContent>
        </Card>
    );
};
 
export default MatListAside;