|
|
|
@@ -1,12 +1,12 @@
|
|
|
|
|
import {useLayoutEffect,useRef,useState,type CSSProperties} from 'react';
|
|
|
|
|
import {useLayoutEffect,useRef,useState,type CSSProperties,type ReactNode} from 'react';
|
|
|
|
|
import {Icon,type IconName} from './Icon.js';
|
|
|
|
|
|
|
|
|
|
export interface TreeNode {id:string;label:string;children?:TreeNode[];icon?:IconName;visible?:boolean}
|
|
|
|
|
export interface TreeProps {nodes:TreeNode[];selected:string|null;onSelect:(id:string)=>void;onVisibilityChange?:(id:string,visible:boolean)=>void;label:string;query?:string;revealKey?:number}
|
|
|
|
|
export interface TreeProps {nodes:TreeNode[];selected:string|null;onSelect:(id:string)=>void;onVisibilityChange?:(id:string,visible:boolean)=>void;label:string;query?:string;revealKey?:number;defaultExpandedDepth?:number;renderActions?:(node:TreeNode)=>ReactNode}
|
|
|
|
|
|
|
|
|
|
/** Compact outliner. Selection reveals ancestors without scrolling its host page. */
|
|
|
|
|
export function Tree({nodes,selected,onSelect,onVisibilityChange,label,query='',revealKey}:TreeProps){
|
|
|
|
|
const [collapsed,setCollapsed]=useState<Set<string>>(()=>new Set());
|
|
|
|
|
export function Tree({nodes,selected,onSelect,onVisibilityChange,label,query='',revealKey,defaultExpandedDepth=Infinity,renderActions}:TreeProps){
|
|
|
|
|
const [collapsed,setCollapsed]=useState<Map<string,boolean>>(()=>new Map());
|
|
|
|
|
const [focus,setFocus]=useState<string|null>(null);
|
|
|
|
|
const host=useRef<HTMLDivElement>(null),elements=useRef(new Map<string,HTMLDivElement>());
|
|
|
|
|
const parents=new Map<string,string>(),index=new Map<string,TreeNode>();
|
|
|
|
@@ -14,11 +14,11 @@ export function Tree({nodes,selected,onSelect,onVisibilityChange,label,query='',
|
|
|
|
|
const ancestors=new Set<string>();let parent=selected?parents.get(selected):undefined;while(parent){ancestors.add(parent);parent=parents.get(parent);}
|
|
|
|
|
const matches=(item:TreeNode):boolean=>!query||item.label.toLocaleLowerCase().includes(query.toLocaleLowerCase())||!!item.children?.some(matches);
|
|
|
|
|
const rows:{node:TreeNode;depth:number;open:boolean}[]=[];
|
|
|
|
|
const flatten=(items:TreeNode[],depth=0)=>{for(const node of items){if(!matches(node))continue;const open=!!query||!collapsed.has(node.id);rows.push({node,depth,open});if(open)flatten(node.children??[],depth+1);}};flatten(nodes);
|
|
|
|
|
const flatten=(items:TreeNode[],depth=0)=>{for(const node of items){if(!matches(node))continue;const open=!!query||!(collapsed.get(node.id)??depth>=defaultExpandedDepth);rows.push({node,depth,open});if(open)flatten(node.children??[],depth+1);}};flatten(nodes);
|
|
|
|
|
const reveal=(id:string)=>{const target=elements.current.get(id),container=host.current;if(!target||!container)return;const row=target.getBoundingClientRect(),box=container.getBoundingClientRect();if(row.top<box.top)container.scrollTop-=box.top-row.top;else if(row.bottom>box.bottom)container.scrollTop+=row.bottom-box.bottom;};
|
|
|
|
|
useLayoutEffect(()=>{if(selected){setFocus(selected);setCollapsed(old=>{if(![...ancestors].some(id=>old.has(id)))return old;const next=new Set(old);for(const id of ancestors)next.delete(id);return next;});}},[selected,revealKey]);
|
|
|
|
|
useLayoutEffect(()=>{if(selected){setFocus(selected);setCollapsed(old=>{if([...ancestors].every(id=>old.get(id)===false))return old;const next=new Map(old);for(const id of ancestors)next.set(id,false);return next;});}},[selected,revealKey,nodes]);
|
|
|
|
|
useLayoutEffect(()=>{if(selected)reveal(selected);},[selected,revealKey,query,collapsed]);
|
|
|
|
|
const toggle=(id:string)=>setCollapsed(old=>{const next=new Set(old);if(next.has(id))next.delete(id);else next.add(id);return next;});
|
|
|
|
|
const toggle=(id:string)=>setCollapsed(old=>{const next=new Map(old);const depth=rows.find(row=>row.node.id===id)?.depth??0;next.set(id,!(old.get(id)??depth>=defaultExpandedDepth));return next;});
|
|
|
|
|
const select=(id:string)=>{setFocus(id);onSelect(id);elements.current.get(id)?.focus({preventScroll:true});reveal(id);};
|
|
|
|
|
return <div ref={host} className="nodedc-tree" role="tree" aria-label={label}>
|
|
|
|
|
{rows.map(({node,depth,open},position)=><div key={node.id} ref={element=>{if(element)elements.current.set(node.id,element);else elements.current.delete(node.id);}} role="treeitem" aria-level={depth+1} aria-selected={selected===node.id} aria-expanded={node.children?.length?open:undefined} tabIndex={(focus??selected??rows[0]?.node.id)===node.id?0:-1} className="nodedc-tree__row" style={{'--tree-depth':depth} as CSSProperties} title={node.label} onFocus={()=>setFocus(node.id)} onClick={()=>select(node.id)} onDoubleClick={()=>node.children?.length&&toggle(node.id)} onKeyDown={event=>{
|
|
|
|
@@ -37,6 +37,7 @@ export function Tree({nodes,selected,onSelect,onVisibilityChange,label,query='',
|
|
|
|
|
<span className="nodedc-tree__branch">{node.children?.length?<button type="button" tabIndex={-1} aria-label={`${open?'Свернуть':'Развернуть'} ${node.label}`} onClick={event=>{event.stopPropagation();toggle(node.id);}}><Icon name={open?'chevron-down':'chevron-right'} size={14}/></button>:null}</span>
|
|
|
|
|
<Icon name={node.icon??(node.children?.length?'folder':'layers')} size={14}/>
|
|
|
|
|
<span className="nodedc-tree__label">{node.label}</span>
|
|
|
|
|
{renderActions&&<span className="nodedc-tree__actions" onClick={event=>event.stopPropagation()} onDoubleClick={event=>event.stopPropagation()}>{renderActions(node)}</span>}
|
|
|
|
|
{onVisibilityChange&&node.visible!==undefined&&<button type="button" className="nodedc-tree__visibility" tabIndex={focus===node.id?0:-1} aria-label={`${node.visible?'Скрыть':'Показать'} ${node.label}`} aria-pressed={node.visible} onClick={event=>{event.stopPropagation();onVisibilityChange(node.id,!node.visible);}}><Icon name={node.visible?'eye':'eye-off'} size={14}/></button>}
|
|
|
|
|
</div>)}
|
|
|
|
|
</div>;
|
|
|
|
|