Press n or j to go to the next uncovered block, b, p or k for the previous block.
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 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 | 6x 6x 2x 2x 2x 2x 6x 2x 2x 6x 6x 6x 6x 2x 2x 6x 14x 17x 8x 9x 9x 14x 14x 14x 14x 10x 10x 6x 10x 14x 14x 1x 1x 1x 4x 4x 7x 7x 7x 7x 7x 7x 7x 7x 4x 4x 4x 4x 3x 3x 3x 7x 1x | import React, { useState, useRef, useEffect } from "react"; import classNames from "classnames"; import { TreeNode, TreeNodeProps } from "./TreeNode"; import { CheckTree, CheckTreeRelation } from "../checktree"; import { TreeContext, TreeContextValue } from "./TreeContext"; import { useDefault } from "../_util/use-default"; import { isChildOfType } from "../_util/is-child-of-type"; import { StyledProps, Omit } from "../_type"; import { useConfig } from "../_util/config-context"; export interface TreeData extends Omit<TreeNodeProps, "children"> { /** * 子节点数据 */ children?: TreeData[]; } export interface TreeProps extends StyledProps { /** * 树节点是否支持选择 * * @default false */ selectable?: boolean; /** * 节点选择是否完全受控(父子节点状态取消关联) * * @default false */ selectStrictly?: boolean; /** * 默认选中的节点 */ defaultSelectedIds?: string[]; /** * 选中的节点(受控) */ selectedIds?: string[]; /** * 选择事件回调 */ onSelect?: ( selectedIds: string[], context?: { selected: boolean; nodeId: string } ) => void; /** * 树节点是否支持点击高亮 * * @default false */ activable?: boolean; /** * 默认高亮的节点 */ defaultActiveIds?: string[]; /** * 高亮的节点(受控) */ activeIds?: string[]; /** * 点击高亮事件回调 */ onActive?: ( activeIds: string[], context?: { active: boolean; nodeId: string } ) => void; /** * 默认展开的节点 */ defaultExpandedIds?: string[]; /** * 展开的节点(受控) */ expandedIds?: string[]; /** * 展开/收起事件回调 */ onExpand?: ( expandedIds: string[], context?: { expanded: boolean; nodeId: string } ) => void; /** * 异步加载数据 * * **需要返回 Promise** * * 节点展开时在 onExpand 前被调用,直到 Promise resolve 后调用 onExpand */ onLoad?: TreeContextValue["onLoad"]; /** * 异步加载数据失败回调 * * onLoad Promise reject 后调用 */ onLoadError?: TreeContextValue["onLoadError"]; /** * 树节点的展开/收起图标 * * @docType React.ReactNode | (context: { expanded: boolean }) => React.ReactNode */ switcherIcon?: TreeContextValue["switcherIcon"]; /** * 树节点数据,如果设置则无需手动构造 <TreeNode /> */ data?: TreeData[]; /** * 包含的树节点 <TreeNode /> */ children?: React.ReactNode; } /** * CheckTree 转换 * * 叶节点 -> 全部选中节点 */ function getAllIds( selectedIds: string[], relations: CheckTreeRelation, childrenMap: Map<string, Set<string>> ): string[] { const nodes = [...selectedIds]; // 插入缺失的父节点 for (const id of nodes) { const pid = relations[id]; const childrenSet = childrenMap.get(pid); // 加入子节点全部选中的父节点 Iif ( pid && childrenSet && !nodes.includes(pid) && ![...childrenSet].find(cid => !nodes.includes(cid)) ) { nodes.push(pid); } } // 插入缺失的子节点 for (const id of nodes) { const children = [...(childrenMap.get(id) || [])]; children.forEach(cid => { if (!nodes.includes(cid)) { nodes.push(cid); } }); } return nodes; } /** * CheckTree 转换 * * 全部选中节点 -> 叶节点 */ function getLeafIds( selectedIds: string[], relations: CheckTreeRelation, childrenMap: Map<string, Set<string>> ): string[] { const all = getAllIds(selectedIds, relations, childrenMap); const nodes = []; all.forEach(id => { Eif (!childrenMap.get(id)) { nodes.push(id); } }); return nodes; } export function isTreeNode( node: React.ReactNode ): node is React.ReactComponentElement<typeof TreeNode, TreeNodeProps> { return isChildOfType(node, TreeNode); } /** * 遍历子节点 */ function traverse( rootId: string, children: React.ReactNode, tree: { relations: CheckTreeRelation; childrenMap: Map<string, Set<string>>; disabledIds: string[]; } ) { if (!children) { return; } const { relations, childrenMap, disabledIds } = tree; React.Children.forEach<React.ReactNode>(children, child => { Eif (isTreeNode(child)) { const { id, selectable, disableSelect } = child.props; Eif (selectable !== false) { // 非最外层根 if (rootId) { relations[id] = rootId; if (!childrenMap.has(rootId)) { childrenMap.set(rootId, new Set()); } childrenMap.get(rootId).add(id); } // 禁用 Iif (disableSelect) { disabledIds.push(id); } } // 当前结点不可选时其子结点 rootId 为当前 rootId traverse(selectable !== false ? id : rootId, child.props.children, tree); } else if (React.isValidElement(child)) { // eslint-disable-next-line dot-notation traverse(rootId, child.props["children"], tree); } }); } /** * 构造树节点 */ function renderTreeNodes(data: TreeData[]) { return data.map(({ children, id, ...props }) => { Iif (children) { return ( <TreeNode key={id} id={id} {...props}> {renderTreeNodes(children)} </TreeNode> ); } return <TreeNode key={id} id={id} {...props} />; }); } export function Tree({ data, children, ...props }: TreeProps) { const nodes = data ? renderTreeNodes(data) : children; return <TreeView {...props}>{nodes}</TreeView>; } export function TreeView({ selectable, selectStrictly, selectedIds, defaultSelectedIds = [], onSelect, activable, activeIds, defaultActiveIds = [], onActive, expandedIds, defaultExpandedIds = [], onExpand = () => null, onLoad, onLoadError, switcherIcon, className, style, children, }: TreeProps) { const { classPrefix } = useConfig(); const childrenMap = useRef<Map<string, Set<string>>>(new Map()); const [relations, setRelations] = useState<CheckTreeRelation>({}); const [disabledIds, setDisabledIds] = useState<string[]>([]); const [selectedNodes, setSelectedNodes] = useDefault( selectedIds, defaultSelectedIds, onSelect ); const [activeNodes, setActiveNodes] = useDefault( activeIds, defaultActiveIds, onActive ); const [expandedNodes, setExpandedNodes] = useDefault( expandedIds, defaultExpandedIds, onExpand ); useEffect(() => { const relations = {}; const disabledIds = []; childrenMap.current = new Map(); if (selectable && !selectStrictly) { traverse(undefined, children, { relations, childrenMap: childrenMap.current, disabledIds, }); setRelations(relations); setDisabledIds(disabledIds); } }, [children, selectStrictly, selectable]); return ( <div className={classNames(`${classPrefix}-tree`, className)} style={style}> <TreeContext.Provider value={{ selectable, activable, activeIds: activeNodes, expandedIds: expandedNodes, onActive: activeId => setActiveNodes([activeId], { active: true, nodeId: activeId }), onExpand: (nodeId, expanded) => setExpandedNodes( expanded ? [...expandedNodes, nodeId] : expandedNodes.filter(id => id !== nodeId), { nodeId, expanded } ), onLoad, onLoadError, switcherIcon, }} > <ul> {selectable ? ( <CheckTree relations={relations} value={getLeafIds(selectedNodes, relations, childrenMap.current)} onChange={(value, { check }) => setSelectedNodes( getAllIds(value, relations, childrenMap.current), { nodeId: check.name, selected: check.value } ) } disabledNames={disabledIds} > {children} </CheckTree> ) : ( children )} </ul> </TreeContext.Provider> </div> ); } Tree.Node = TreeNode; |