mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Expanding parent nodes when scrolling along child ones
Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com>
This commit is contained in:
1 parent
d0e25e90b0
commit
09a009049f
4 files changed
+67
-15
No files matched your search
@@ -29,6 +29,7 @@ export function ScrollSpy(props: Props) {
|
||||
|
||||
sections.forEach(section => {
|
||||
const id = section.getAttribute("id");
|
||||
const parentId = section.parentElement.id;
|
||||
const name = section.querySelector("h1, h2, h3, h4, h5, h6").textContent;
|
||||
const selected = id === activeElementId;
|
||||
|
||||
@@ -38,6 +39,7 @@ export function ScrollSpy(props: Props) {
|
||||
|
||||
children.push({
|
||||
id,
|
||||
parentId,
|
||||
name,
|
||||
selected,
|
||||
children: getNavigation(section)
|
||||
@@ -56,6 +58,9 @@ export function ScrollSpy(props: Props) {
|
||||
const observeSections = () => {
|
||||
const options: IntersectionObserverInit = {
|
||||
threshold: [0],
|
||||
// Shrinking root area from the bottom
|
||||
// Allows to fire observer event only if target scrolled up to top of the page)
|
||||
// https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API#creating_an_intersection_observer
|
||||
rootMargin: "0px 0px -85%",
|
||||
};
|
||||
|
||||
|
||||
@@ -1,14 +1,15 @@
|
||||
import "./tree-view.scss";
|
||||
|
||||
import React from "react";
|
||||
import React, { useEffect, useRef } from "react";
|
||||
import { Icon } from "../icon";
|
||||
import TreeView from "@material-ui/lab/TreeView";
|
||||
import TreeItem from "@material-ui/lab/TreeItem";
|
||||
import { cssNames } from "../../utils";
|
||||
import flattenDeep from "lodash/flattenDeep";
|
||||
import findDeep from "deepdash-es/findDeep";
|
||||
|
||||
export interface NavigationTree {
|
||||
id: string;
|
||||
parentId: string;
|
||||
name: string;
|
||||
selected?: boolean;
|
||||
children?: NavigationTree[];
|
||||
@@ -18,14 +19,49 @@ interface Props {
|
||||
data: NavigationTree[]
|
||||
}
|
||||
|
||||
const scrollToItem = (id: string) => {
|
||||
const element = document.getElementById(id);
|
||||
|
||||
element?.scrollIntoView();
|
||||
};
|
||||
|
||||
const getSelectedNode = (data: NavigationTree[]) => {
|
||||
return findDeep(data, (value, key) => key === "selected" && value === true)?.parent;
|
||||
};
|
||||
|
||||
export function RecursiveTreeView({ data }: Props) {
|
||||
const [expanded, setExpanded] = React.useState<string[]>([]);
|
||||
const prevData = useRef<NavigationTree[]>(data);
|
||||
|
||||
const handleToggle = (event: React.ChangeEvent<{}>, nodeIds: string[]) => {
|
||||
setExpanded(nodeIds);
|
||||
};
|
||||
|
||||
const expandTopLevelNodes = () => {
|
||||
setExpanded(data.map(node => node.id));
|
||||
};
|
||||
|
||||
const expandParentNode = () => {
|
||||
const node = getSelectedNode(data) as any as NavigationTree;
|
||||
const id = node?.parentId;
|
||||
|
||||
if (id && !expanded.includes(id)) {
|
||||
setExpanded([...expanded, id]);
|
||||
}
|
||||
};
|
||||
|
||||
const onLabelClick = (event: React.MouseEvent, nodeId: string) => {
|
||||
event.preventDefault();
|
||||
scrollToItem(nodeId);
|
||||
};
|
||||
|
||||
const renderTree = (nodes: NavigationTree[]) => {
|
||||
return nodes.map(node => (
|
||||
<TreeItem
|
||||
key={node.id}
|
||||
nodeId={node.id}
|
||||
label={node.name}
|
||||
onLabelClick={() => scrollToItem(node.id)}
|
||||
onLabelClick={(event) => onLabelClick(event, node.id)}
|
||||
className={cssNames({selected: node.selected})}
|
||||
>
|
||||
{Array.isArray(node.children) ? node.children.map((node) => renderTree([node])) : null}
|
||||
@@ -33,17 +69,14 @@ export function RecursiveTreeView({ data }: Props) {
|
||||
));
|
||||
};
|
||||
|
||||
const scrollToItem = (id: string) => {
|
||||
const element = document.getElementById(id);
|
||||
|
||||
element?.scrollIntoView();
|
||||
};
|
||||
|
||||
const getAllNodeIds = (nodes: NavigationTree[]): string[] => {
|
||||
return flattenDeep(nodes.map(node => {
|
||||
return [node.id, ...node.children.map(item => getAllNodeIds([item]))];
|
||||
}));
|
||||
};
|
||||
useEffect(() => {
|
||||
if (!prevData.current.length) {
|
||||
expandTopLevelNodes();
|
||||
} else {
|
||||
expandParentNode();
|
||||
}
|
||||
prevData.current = data;
|
||||
}, [data]);
|
||||
|
||||
if (!data.length) {
|
||||
return null;
|
||||
@@ -52,7 +85,8 @@ export function RecursiveTreeView({ data }: Props) {
|
||||
return (
|
||||
<TreeView
|
||||
className="TreeView"
|
||||
defaultExpanded={getAllNodeIds(data)}
|
||||
expanded={expanded}
|
||||
onNodeToggle={handleToggle}
|
||||
defaultCollapseIcon={<Icon material="expand_more"/>}
|
||||
defaultExpandIcon={<Icon material="chevron_right" />}
|
||||
>
|
||||
|
||||
Reference in new issue
Block a user