zy-react-library/components/SelectTree/Basic/index.js

103 lines
3.1 KiB
JavaScript
Raw Normal View History

import { TreeSelect } from "antd";
import { useEffect } from "react";
2025-12-08 10:49:51 +08:00
import { arrayObjectDeduplication, getDataType, getTreeNodePaths, processTreeDataByLevel, processTreeDataForOnlyLastLevel } from "../../../utils";
/**
* 基础下拉树组件不建议直接使用此组件二次继承使用
*/
function BasicSelectTree(props) {
const {
onGetData,
2025-11-06 16:28:49 +08:00
onChange,
onGetLabel,
onGetNodePaths,
onGetNodePathsIsIncludeOneself = true,
placeholder = "",
treeData = [],
nameKey = "name",
idKey = "id",
childrenKey = "children",
level,
2025-11-11 14:40:56 +08:00
onlyLastLevel = false,
...restProps
} = props;
// 根据 level 处理树数据
2025-11-11 14:40:56 +08:00
let processedTreeData = level
? processTreeDataByLevel({
2025-12-11 09:06:30 +08:00
data: treeData,
level,
childrenKey,
currentLevel: 1,
})
: treeData;
2025-11-11 14:40:56 +08:00
// 根据 onlyLastLevel 处理树数据
processedTreeData = processTreeDataForOnlyLastLevel({ data: processedTreeData, childrenKey, onlyLastLevel });
2025-11-06 16:28:49 +08:00
const handleChange = (value, label, extra) => {
if (value) {
2025-12-08 10:49:51 +08:00
if (getDataType(value) === "Array") {
const parentNodes = [];
for (let i = 0; i < value.length; i++) {
2025-12-11 09:06:30 +08:00
const targetId = (restProps.labelInValue || restProps.treeCheckStrictly) ? value[i].value : value[i];
2025-12-08 10:49:51 +08:00
const currentParentNodes = getTreeNodePaths({
data: treeData,
2025-12-11 09:06:30 +08:00
targetId,
2025-12-08 10:49:51 +08:00
idKey,
childrenKey,
isIncludeOneself: onGetNodePathsIsIncludeOneself,
});
parentNodes.push(...currentParentNodes);
}
2025-12-11 09:06:30 +08:00
const deduplicationParentNodes = arrayObjectDeduplication(parentNodes, idKey);
onGetNodePaths?.(deduplicationParentNodes);
onGetLabel?.((restProps.labelInValue || restProps.treeCheckStrictly) ? value.map(item => item.label) : label);
2025-12-08 10:49:51 +08:00
}
2025-12-08 10:56:59 +08:00
else {
2025-12-11 09:06:30 +08:00
const targetId = (restProps.labelInValue || restProps.treeCheckStrictly) ? value.value : value;
2025-12-08 10:56:59 +08:00
const parentNodes = getTreeNodePaths({
data: treeData,
2025-12-11 09:06:30 +08:00
targetId,
2025-12-08 10:56:59 +08:00
idKey,
childrenKey,
isIncludeOneself: onGetNodePathsIsIncludeOneself,
});
onGetNodePaths?.(parentNodes);
onGetLabel?.(parentNodes[parentNodes.length - 1][nameKey]);
}
}
2025-11-06 16:28:49 +08:00
else {
onGetNodePaths?.([]);
onGetLabel?.("");
}
2025-12-11 09:06:30 +08:00
onChange?.((restProps.labelInValue || restProps.treeCheckStrictly) ? value.map(item => item.value) : value, label, extra);
};
useEffect(() => {
onGetData?.(treeData, processedTreeData);
}, [treeData, processedTreeData]);
return (
<TreeSelect
showSearch
style={{ width: "100%" }}
styles={{
popup: { root: { maxHeight: 400, overflow: "auto" } },
}}
placeholder={`请选择${placeholder}`}
2025-11-06 16:28:49 +08:00
onChange={handleChange}
allowClear
treeData={processedTreeData}
fieldNames={{ label: nameKey, value: idKey, children: childrenKey }}
2025-11-13 15:25:31 +08:00
treeNodeFilterProp={nameKey}
2025-12-11 09:06:30 +08:00
showCheckedStrategy={TreeSelect.SHOW_ALL}
{...restProps}
/>
);
}
BasicSelectTree.displayName = "BasicSelectTree";
export default BasicSelectTree;