import "./Breadcrumb.css" import * as React from "react" import classnames from "classnames" import { BaseTreeMapInPutData } from "../TreeMap/types" import { HierarchyRectangularNode } from "d3-hierarchy" export interface BreadcrumbProps { originalTopNode: HierarchyRectangularNode selectedNodeId: number className?: string zoomTo: (nodeId: number) => void namePropInData: string } export interface BreadcrumbItem { /** * Text to display to the user for the breadcrumb */ text: string /** * Arbitrary unique string associated with the breadcrumb */ key: number /** * Callback issued when the breadcrumb is selected. */ onClick?: (ev?: React.MouseEvent, item?: BreadcrumbItem) => void } export const Breadcrumb = ({ className, originalTopNode, selectedNodeId, zoomTo, namePropInData, }: BreadcrumbProps) => { const selectedNodeFromOriginalTree = originalTopNode .descendants() .filter((n: HierarchyRectangularNode) => { return n.data.id === selectedNodeId }) .pop() const items = originalTopNode .path(selectedNodeFromOriginalTree) .map(({ data }: HierarchyRectangularNode) => { return { text: data[namePropInData], key: data.id, onClick: data.id !== selectedNodeId ? () => zoomTo(data.id) : undefined, } }) const itemsFinal = items && items.length ? items : [{ text: originalTopNode.data[namePropInData], key: 0 }] return (
{itemsFinal.map((item: BreadcrumbItem, index: number) => (
{item.text} {index < itemsFinal.length - 1 ? ( / ) : null}
))}
) }