|
| 1 | +import React, { useState } from 'react'; |
| 2 | +import PropTypes from 'prop-types'; |
| 3 | +import { UncontrolledTreeEnvironment, Tree } from 'react-complex-tree'; |
| 4 | +import useComplexTreeViewApi from './useComplexTreeViewApi'; |
| 5 | + |
| 6 | +const CSS_CLASS = 'complex-tree-view'; |
| 7 | +const TREE_ID = 'tree-1'; |
| 8 | + |
| 9 | +const ComplexTreeView = ({ |
| 10 | + currentRecordID = 0, |
| 11 | + apiEndpoint = '/admin/pages/tree/jsonview', |
| 12 | +}) => { |
| 13 | + const [loadingNodeId, setLoadingNodeId] = useState(null); |
| 14 | + const { |
| 15 | + dataProvider, |
| 16 | + error, |
| 17 | + initialViewState, |
| 18 | + loading, |
| 19 | + rootId, |
| 20 | + loadSubtree, |
| 21 | + mergeSubtree, |
| 22 | + } = useComplexTreeViewApi(TREE_ID, currentRecordID, apiEndpoint); |
| 23 | + |
| 24 | + /** |
| 25 | + * Renders the DOM for a tree item label. |
| 26 | + * For synthetic limited indicator nodes, renders as a clickable link. |
| 27 | + * Otherwise renders the regular title. |
| 28 | + */ |
| 29 | + const renderTreeItemTitle = ({ item }) => { |
| 30 | + const isLimitedIndicator = item.data.isLimitedIndicator === true; |
| 31 | + const isLoading = loadingNodeId === item.index; |
| 32 | + |
| 33 | + if (isLimitedIndicator) { |
| 34 | + return ( |
| 35 | + <span className={`${CSS_CLASS}__item-link ${CSS_CLASS}__item-link--limited`}> |
| 36 | + Too many records ( |
| 37 | + <a href={item.data.linkUrl} className={`${CSS_CLASS}__limited-link`}>show as list</a> |
| 38 | + ) |
| 39 | + </span> |
| 40 | + ); |
| 41 | + } |
| 42 | + |
| 43 | + if (isLoading) { |
| 44 | + return ( |
| 45 | + <span className={`${CSS_CLASS}__item-link`}> |
| 46 | + <span className={`${CSS_CLASS}__loading`}>Loading...</span> |
| 47 | + {item.data.title} |
| 48 | + </span> |
| 49 | + ); |
| 50 | + } |
| 51 | + |
| 52 | + return ( |
| 53 | + <span className={`${CSS_CLASS}__item-link`}> |
| 54 | + {item.data.title} |
| 55 | + </span> |
| 56 | + ); |
| 57 | + }; |
| 58 | + |
| 59 | + /** |
| 60 | + * This is triggered by Double Click (mouse) or Enter (keyboard) on a focused item. |
| 61 | + */ |
| 62 | + const handlePrimaryAction = (items) => { |
| 63 | + window.location.href = `/admin/pages/edit/show/${items.data.id}`; |
| 64 | + }; |
| 65 | + |
| 66 | + /** |
| 67 | + * Handler for item selection - no-op for now |
| 68 | + */ |
| 69 | + const handleSelectItems = () => {}; |
| 70 | + |
| 71 | + /** |
| 72 | + * Handles expanding a tree item. If the item has unloaded children, |
| 73 | + * loads the subtree and merges it into the tree. |
| 74 | + */ |
| 75 | + // eslint-disable-next-line no-unused-vars |
| 76 | + const handleExpandItem = async (item, treeId) => { |
| 77 | + const hasUnloadedChildren = item.data.expanded === false && item.data.count > 0 && item.children.length === 0; |
| 78 | + if (hasUnloadedChildren) { |
| 79 | + try { |
| 80 | + setLoadingNodeId(item.index); |
| 81 | + const subtree = await loadSubtree(item.data.id); |
| 82 | + await mergeSubtree(item.data.id, subtree.items); |
| 83 | + } finally { |
| 84 | + setLoadingNodeId(null); |
| 85 | + } |
| 86 | + } |
| 87 | + }; |
| 88 | + |
| 89 | + /** |
| 90 | + * INTERCEPT KEYDOWN |
| 91 | + * This is the key fix. We intercept the event at the container level. |
| 92 | + */ |
| 93 | + const handleRootKeyDown = (e) => { |
| 94 | + // Check if the key pressed is Space |
| 95 | + if (e.key !== ' ') { |
| 96 | + return; |
| 97 | + } |
| 98 | + // Find the closest tree item container |
| 99 | + const treeItem = e.target.closest('[role="treeitem"]'); |
| 100 | + |
| 101 | + // If we are on a tree item... |
| 102 | + if (treeItem) { |
| 103 | + // Check if it is a Folder. Folders have 'aria-expanded' (true or false). |
| 104 | + // Leaves do NOT have this attribute. |
| 105 | + const isFolder = treeItem.hasAttribute('aria-expanded'); |
| 106 | + |
| 107 | + // If it's NOT a folder (it's a leaf), prevent the default browser behavior. |
| 108 | + // This stops the browser from turning "Space" into a "Click" event. |
| 109 | + if (!isFolder) { |
| 110 | + e.preventDefault(); |
| 111 | + } |
| 112 | + } |
| 113 | + }; |
| 114 | + |
| 115 | + if (loading) { |
| 116 | + return <div>Loading page tree...</div>; |
| 117 | + } |
| 118 | + if (error) { |
| 119 | + return <div style={{ color: 'red' }}>Error loading data: {error}</div>; |
| 120 | + } |
| 121 | + if (!dataProvider || !initialViewState) { |
| 122 | + return <div>No data available.</div>; |
| 123 | + } |
| 124 | + |
| 125 | + return ( |
| 126 | + <div className={CSS_CLASS} onKeyDown={handleRootKeyDown}> |
| 127 | + <UncontrolledTreeEnvironment |
| 128 | + dataProvider={dataProvider} |
| 129 | + getItemTitle={item => item.data.title} |
| 130 | + viewState={initialViewState} |
| 131 | + // canDragAndDrop={false} |
| 132 | + // canDropOnFolder={false} |
| 133 | + // canReorderItems={false} |
| 134 | + canRenameItems={false} |
| 135 | + canSearchItems={false} |
| 136 | + canSearchByStartingTyping={false} |
| 137 | + renderItemTitle={renderTreeItemTitle} |
| 138 | + onPrimaryAction={handlePrimaryAction} |
| 139 | + onSelectItems={handleSelectItems} |
| 140 | + onExpandItem={handleExpandItem} |
| 141 | + > |
| 142 | + <Tree treeId={TREE_ID} rootItem={rootId} treeLabel="Page Tree" /> |
| 143 | + </UncontrolledTreeEnvironment> |
| 144 | + </div> |
| 145 | + ); |
| 146 | +}; |
| 147 | + |
| 148 | +ComplexTreeView.propTypes = { |
| 149 | + currentRecordID: PropTypes.number, |
| 150 | + apiEndpoint: PropTypes.string, |
| 151 | +}; |
| 152 | + |
| 153 | +export { ComplexTreeView as Component }; |
| 154 | +export default ComplexTreeView; |
0 commit comments