1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Highlight selected item

Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com>
This commit is contained in:
Alex Andreev 2021-03-05 09:57:29 +03:00
parent 06e747dfa8
commit 3194fa9139
2 changed files with 12 additions and 8 deletions

View File

@ -1,8 +1,8 @@
import React, { useEffect, useRef, useState } from "react"; import React, { useEffect, useRef, useState } from "react";
import { NavigationTree } from "../tree-view"; import { NavigationTree } from "../tree-view";
interface Props extends React.DOMAttributes<any> { interface Props extends React.DOMAttributes<HTMLElement> {
render: (data: NavigationTree[]) => any render: (data: NavigationTree[]) => JSX.Element
} }
export function ScrollSpy(props: Props) { export function ScrollSpy(props: Props) {
@ -20,10 +20,7 @@ export function ScrollSpy(props: Props) {
}; };
const updateNavigation = () => { const updateNavigation = () => {
setTree([ setTree(getNavigation(sections.current[0].parentElement));
...tree,
...getNavigation(sections.current[0].parentElement)
]);
}; };
const getNavigation = (element: Element) => { const getNavigation = (element: Element) => {
@ -33,6 +30,7 @@ export function ScrollSpy(props: Props) {
sections.forEach(section => { sections.forEach(section => {
const id = section.getAttribute("id"); const id = section.getAttribute("id");
const name = section.querySelector(":first-child").textContent; const name = section.querySelector(":first-child").textContent;
const selected = id === activeElementId;
if (!name || !id) { if (!name || !id) {
return; return;
@ -41,6 +39,7 @@ export function ScrollSpy(props: Props) {
children.push({ children.push({
id, id,
name, name,
selected,
children: getNavigation(section) children: getNavigation(section)
}); });
}); });
@ -70,10 +69,13 @@ export function ScrollSpy(props: Props) {
useEffect(() => { useEffect(() => {
setSections(); setSections();
observeSections(); observeSections();
updateNavigation();
// TODO: Attach on dom change event // TODO: Attach on dom change event
}, []); }, []);
useEffect(() => {
updateNavigation();
}, [activeElementId]);
console.log(activeElementId); console.log(activeElementId);
return ( return (

View File

@ -4,10 +4,12 @@ import React from "react";
import { Icon } from "../icon"; import { Icon } from "../icon";
import TreeView from "@material-ui/lab/TreeView"; import TreeView from "@material-ui/lab/TreeView";
import TreeItem from "@material-ui/lab/TreeItem"; import TreeItem from "@material-ui/lab/TreeItem";
import { cssNames } from "../../utils";
export interface NavigationTree { export interface NavigationTree {
id: string; id: string;
name: string; name: string;
selected?: boolean;
children?: NavigationTree[]; children?: NavigationTree[];
} }
@ -18,7 +20,7 @@ interface Props {
export function RecursiveTreeView({ data }: Props) { export function RecursiveTreeView({ data }: Props) {
const renderTree = (nodes: NavigationTree[]) => { const renderTree = (nodes: NavigationTree[]) => {
return nodes.map(node => ( return nodes.map(node => (
<TreeItem key={node.id} nodeId={node.id} label={node.name}> <TreeItem key={node.id} nodeId={node.id} label={node.name} className={cssNames({"Mui-selected": node.selected})}>
{Array.isArray(node.children) ? node.children.map((node) => renderTree([node])) : null} {Array.isArray(node.children) ? node.children.map((node) => renderTree([node])) : null}
</TreeItem> </TreeItem>
)); ));