| 694 | } |
| 695 | |
| 696 | function createItemProps<T extends { id: string; children?: T[] }>( |
| 697 | item: TreeNodeItem<T>, |
| 698 | virtualItem: VirtualItem, |
| 699 | state: TreeState<T>, |
| 700 | dispatch: Dispatch<TreeAction> |
| 701 | ): () => React.HTMLAttributes<HTMLElement> { |
| 702 | const { depth, pos, size, node, isCollapsed } = item; |
| 703 | |
| 704 | return () => ({ |
| 705 | "aria-expanded": node.children && node.children.length > 0 && !isCollapsed, |
| 706 | "aria-level": depth + 1, |
| 707 | "aria-posinset": pos, |
| 708 | "aria-setsize": size, |
| 709 | role: "treeitem", |
| 710 | tabIndex: node.id === state.focusedNodeId ? -1 : undefined, |
| 711 | onClick: (e) => { |
| 712 | if (e.defaultPrevented) { |
| 713 | return; // Do nothing if the event was already processed |
| 714 | } |
| 715 | |
| 716 | if (node.id !== state.focusedNodeId) { |
| 717 | dispatch({ type: "FOCUS_NODE", id: node.id }); |
| 718 | } |
| 719 | }, |
| 720 | }); |
| 721 | } |
| 722 | |
| 723 | // Finds the node in the list of nodes or recursively in the children |
| 724 | function findNodeInTreeById<T extends { id: string; children?: T[] }>( |