/* Copyright (c) Microsoft Corporation. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ import * as React from 'react'; import { ListView } from './listView'; export type TreeItem = { id: string, parent: TreeItem | undefined, children: TreeItem[], }; export type TreeState = { expandedItems: Map; }; export type TreeViewProps = { rootItem: T, render: (item: T) => React.ReactNode, icon?: (item: T) => string | undefined, isError?: (item: T) => boolean, selectedItem?: T, onAccepted?: (item: T) => void, onSelected?: (item: T) => void, onHighlighted?: (item: T | undefined) => void, noItemsMessage?: string, dataTestId?: string, treeState: TreeState, setTreeState: (treeState: TreeState) => void, }; const TreeListView = ListView; export function TreeView({ rootItem, render, icon, isError, selectedItem, onAccepted, onSelected, onHighlighted, treeState, setTreeState, noItemsMessage, }: TreeViewProps) { const treeItems = React.useMemo(() => { for (let item: TreeItem | undefined = selectedItem?.parent; item; item = item.parent) treeState.expandedItems.set(item.id, true); return flattenTree(rootItem, treeState.expandedItems); }, [rootItem, selectedItem, treeState]); return item.id} render={item => { const rendered = render(item as T); return <> {icon &&
} {typeof rendered === 'string' ?
{rendered}
: rendered} ; }} icon={item => { const expanded = treeItems.get(item as T)!.expanded; if (typeof expanded === 'boolean') return expanded ? 'codicon-chevron-down' : 'codicon-chevron-right'; }} isError={item => isError?.(item as T) || false} indent={item => treeItems.get(item as T)!.depth} selectedItem={selectedItem} onAccepted={item => onAccepted?.(item as T)} onSelected={item => onSelected?.(item as T)} onHighlighted={item => onHighlighted?.(item as T)} onLeftArrow={item => { const { expanded, parent } = treeItems.get(item as T)!; if (expanded) { treeState.expandedItems.set(item.id, false); setTreeState({ ...treeState }); } else if (parent) { onSelected?.(parent as T); } }} onRightArrow={item => { if (item.children.length) { treeState.expandedItems.set(item.id, true); setTreeState({ ...treeState }); } }} onIconClicked={item => { const { expanded } = treeItems.get(item as T)!; if (expanded) { // Move nested selection up. for (let i: TreeItem | undefined = selectedItem; i; i = i.parent) { if (i === item) { onSelected?.(item as T); break; } } treeState.expandedItems.set(item.id, false); } else { treeState.expandedItems.set(item.id, true); } setTreeState({ ...treeState }); }} noItemsMessage={noItemsMessage} />; } type TreeItemData = { depth: number, expanded: boolean | undefined, parent: TreeItem | null, }; function flattenTree(rootItem: T, expandedItems: Map): Map { const result = new Map(); const appendChildren = (parent: T, depth: number) => { for (const item of parent.children as T[]) { const expandState = expandedItems.get(item.id); const autoExpandMatches = depth === 0 && result.size < 25 && expandState !== false; const expanded = item.children.length ? expandState || autoExpandMatches : undefined; result.set(item, { depth, expanded, parent: rootItem === parent ? null : parent }); if (expanded) appendChildren(item, depth + 1); } }; appendChildren(rootItem, 0); return result; }