diff --git a/src/pages/supplier/category/CategoryLibraryManage/components/CategoryAddModal.tsx b/src/pages/supplier/category/CategoryLibraryManage/components/CategoryAddModal.tsx index 05b140d..3b662bd 100644 --- a/src/pages/supplier/category/CategoryLibraryManage/components/CategoryAddModal.tsx +++ b/src/pages/supplier/category/CategoryLibraryManage/components/CategoryAddModal.tsx @@ -12,12 +12,39 @@ const approveTypeOptions = [ { label: '是', value: 'online' }, { label: '否', value: 'offline' }, ]; -function collectSelectedNodes(nodes: TreeNodeType[], checkedKeys: string[]): { id: string; categoryName: string; categoryType:string }[] { - const result: { id: string; categoryName: string; categoryType:string; lockType:number }[] = []; + +function collectCheckedWithParents( + nodes: TreeNodeType[], + checked: string[] +): Set { + const result = new Set(); + function dfs(node: TreeNodeType, parents: string[]) { + if (checked.includes(node.key)) { + result.add(node.key); + parents.forEach(pid => result.add(pid)); + } + node.children?.forEach(child => dfs(child, [...parents, node.key])); + } + nodes.forEach(root => dfs(root, [])); + return result; +} + // 从树中获取所有选中节点及其父级节点 +function collectSelectedNodesWithParents( + nodes: TreeNodeType[], + checkedKeys: string[] +): { id: string; categoryName: string; lockType: number; categoryType: string }[] { + const allKeysSet = collectCheckedWithParents(nodes, checkedKeys); + + const result: { id: string; categoryName: string; lockType: number; categoryType: string }[] = []; function dfs(list: TreeNodeType[]) { list.forEach(node => { - if (checkedKeys.includes(node.key)) { - result.push({ id: node.key, categoryName: node.categoryName, lockType: 0, categoryType: node.children?.length === 0? '1': '0' }); + if (allKeysSet.has(node.key)) { + result.push({ + id: node.key, + categoryName: node.categoryName, + lockType: 0, + categoryType: node.children?.length === 0 ? '1' : '0', + }); } if (node.children && node.children.length) dfs(node.children); }); @@ -25,6 +52,7 @@ function collectSelectedNodes(nodes: TreeNodeType[], checkedKeys: string[]): { i dfs(nodes); return result; } + // TS 类型定义 interface TreeNodeType { id: string; @@ -166,10 +194,9 @@ const CategoryAddModal: React.FC = ({ visible, onCancel, onSuccess }) => form.setFieldsValue({ categoryKeys: checkedKeysValue }); // 新增:存储 [{id, title}] - const selectedList = collectSelectedNodes(treeData, checkedKeysValue); + const selectedList = collectSelectedNodesWithParents(treeData, checkedKeysValue); form.setFieldsValue({ coscoAccessCategoryList: selectedList }); - if (hasFuelCategory(checkedKeysValue, treeData)) { setAreaRequired(true); form.validateFields(['area']); @@ -177,21 +204,8 @@ const CategoryAddModal: React.FC = ({ visible, onCancel, onSuccess }) => setAreaRequired(false); } }; - // 从树中获取所有选中节点及其父级节点 - const collectCheckedWithParents = (nodes: TreeNodeType[], checked: string[]): string[] => { - const result = new Set(); - const dfs = (node: TreeNodeType, parents: string[]) => { - if (checked.includes(node.key)) { - result.add(node.key); - parents.forEach(pid => result.add(pid)); // 加入所有父级 - } - node.children?.forEach(child => dfs(child, [...parents, node.key])); - }; - nodes.forEach(root => dfs(root, [])); - return Array.from(result); - }; // 提交校验 const handleOk = async () => {