function TreeContextMenuExample() {
const treeItems = [
{
type: "file",
name: "ToolWindowState.test.ts",
items: [
{
type: "spec",
name: "tool window state",
items: [
{
type: "case",
name: "hide or show return the same state if the key doesn't exist in the state",
},
{
type: "case",
name: "hide returns the same state if the window is already not visible",
},
{
type: "case",
name: "hiding a tool window only toggles the visibility of that window",
},
],
},
],
},
];
const getAllKeys = (items) =>
items.flatMap((item) => [item.name, ...getAllKeys(item.items || [])]);
const allKeys = getAllKeys(treeItems);
const [selectedKeys, setSelectedKeys] = useState(new Set());
return (
<ContextMenuContainer
renderMenu={() => {
const selectedKey = Array.from(selectedKeys)[0];
if (!selectedKey) {
return (
<Menu>
<Item>Nothing here</Item>
</Menu>
);
}
return (
<Menu>
<Item textValue={`Run '${selectedKey}'`}>
<MenuItemLayout
icon={<PlatformIcon icon="debugger/threadRunning.svg" />}
content={`Run '${selectedKey}'`}
shortcut="⌃⇧R"
/>
</Item>
<Item textValue={`Debug '${selectedKey}'`}>
<MenuItemLayout
icon={<PlatformIcon icon="actions/startDebugger.svg" />}
content={`Debug '${selectedKey}'`}
shortcut="⌃⇧D"
/>
</Item>
<Divider />
<Item textValue="Jump to source">
<MenuItemLayout
icon={<PlatformIcon icon="actions/editSource.svg" />}
content="Jump to source"
shortcut="⌘↓"
/>
</Item>
</Menu>
);
}}
>
<SpeedSearchTree
items={treeItems}
fillAvailableSpace
selectionMode="single"
defaultExpandedKeys={allKeys}
keepSearchActiveOnBlur
selectedKeys={selectedKeys}
onSelectionChange={setSelectedKeys}
>
{(item) => (
<Item key={item.name} textValue={item.name} childItems={item.items}>
<ItemLayout>
<PlatformIcon icon="runConfigurations/testPassed.svg" />
<HighlightedTextValue />
</ItemLayout>
</Item>
)}
</SpeedSearchTree>
</ContextMenuContainer>
);
}