Using react-simple-tree-view lib for preferences links

Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com>
This commit is contained in:
Alex Andreev committed 2021-02-26 12:56:45 +03:00
1 parent 90a1101a0f
commit 69e7397e17
6 files changed
+75 -23

No files matched your search

@@ -6,6 +6,7 @@ import { autobind, cssNames, IClassName } from "../../utils";
import { Icon } from "../icon";
import { navigation } from "../../navigation";
import { ScrollSpy } from "../scroll-spy/scroll-spy";
import { RecursiveTreeView } from "../tree-view";
export interface PageLayoutProps extends React.DOMAttributes<any> {
className?: IClassName;
@@ -80,11 +81,7 @@ export class PageLayout extends React.Component<PageLayoutProps> {
<div className="content-wrapper">
{ showNavigation && (
<div className="content-navigation">
<ul>
<li>Section 1</li>
<li>Section 2</li>
<li>{navigation.toString()}</li>
</ul>
<RecursiveTreeView data={navigation}/>
</div>
)}
<div className={cssNames("content", contentGaps && "flex column gaps", contentClass)}>
@@ -1,11 +1,12 @@
import React, { useEffect } from "react";
import React, { useEffect, useState } from "react";
import { NavigationTree } from "../tree-view";
interface Props extends React.DOMAttributes<any> {
render: (data: any) => any
render: (data: NavigationTree[]) => any
}
export function ScrollSpy(props: Props) {
let navigationData: any = {};
const [tree, setTree] = useState<NavigationTree[]>([]);
const updateNavigation = () => {
const firstSection = document.querySelector("section");
@@ -14,31 +15,28 @@ export function ScrollSpy(props: Props) {
throw new Error("No <section/> tag founded! Content should be placed inside <section></section> elements to activate navigation.");
}
navigationData = {
id: "root",
name: "Parent",
children: getNavigation(firstSection.parentElement)
};
console.log(navigationData);
setTree([
...tree,
...getNavigation(firstSection.parentElement)
]);
};
const getNavigation = (element: Element) => {
const sections = element.querySelectorAll(":scope > section"); // Searching only direct children of an element. Impossible without :scope
const children: any = [];
const children: NavigationTree[] = [];
sections.forEach(section => {
const id = section.getAttribute("id");
const name = section.querySelector(":first-child").textContent;
const key = section.getAttribute("id");
const label = section.querySelector(":first-child").textContent;
if (!name || !id) {
if (!label || !key) {
return;
}
children.push({
id,
name,
children: getNavigation(section)
key,
label,
nodes: getNavigation(section)
});
});
@@ -53,7 +51,7 @@ export function ScrollSpy(props: Props) {
return (
<div className="ScrollSpy">
{props.render(navigationData)}
{props.render(tree)}
</div>
);
}
@@ -0,0 +1 @@
export * from "./tree-view";
@@ -0,0 +1,25 @@
import React from "react";
import TreeMenu from "react-simple-tree-menu";
import { Icon } from "../icon";
export interface NavigationTree {
key: string;
label: string;
nodes?: NavigationTree[];
}
interface Props {
data: NavigationTree[]
}
export function RecursiveTreeView({ data }: Props) {
return (
<TreeMenu
data={data}
hasSearch={false}
initialActiveKey="application/appearance"
resetOpenNodesOnDataUpdate={true}
debounceTime={125}
/>
);
}