资源分类

This commit is contained in:
禺狨
2023-03-08 18:07:07 +08:00
parent 8cde51d53b
commit db1c7652d1
5 changed files with 177 additions and 105 deletions

View File

@@ -4,4 +4,5 @@ export * from "./left-menu";
export * from "./upload-image-button";
export * from "./tree-department";
export * from "./back-bar";
export * from "./permission-button";
export * from "./permission-button";
export * from "./tree-category";

View File

@@ -0,0 +1,60 @@
import { Button, Input, message, Tree } from "antd";
import { useState, useEffect } from "react";
import { resourceCategory } from "../../api/index";
interface Option {
key: string | number;
title: string;
children?: Option[];
}
interface PropInterface {
onUpdate: (keys: any) => void;
}
export const TreeCategory = (props: PropInterface) => {
const [treeData, setTreeData] = useState<any>([]);
const [loading, setLoading] = useState<boolean>(true);
useEffect(() => {
setLoading(true);
resourceCategory.resourceCategoryList().then((res: any) => {
const categories = res.data.categories;
const new_arr: Option[] = [
{
key: "",
title: "全部",
children: checkArr(categories, 0),
},
];
setTreeData(new_arr);
setTimeout(() => {
setLoading(false);
}, 1000);
});
}, []);
const checkArr = (categories: any[], id: number) => {
const arr = [];
for (let i = 0; i < categories[id].length; i++) {
if (!categories[categories[id][i].id]) {
arr.push({
title: categories[id][i].name,
key: categories[id][i].id,
});
} else {
const new_arr: Option[] = checkArr(categories, categories[id][i].id);
arr.push({
title: categories[id][i].name,
key: categories[id][i].id,
children: new_arr,
});
}
}
return arr;
};
const onSelect = (selectedKeys: any, info: any) => {
props.onUpdate(selectedKeys);
};
return <Tree onSelect={onSelect} treeData={treeData} />;
};