Skip to content

Commit 8912b99

Browse files
committed
feat: show content inside nodes
Signed-off-by: Andres Correa Casablanca <castarco@coderspirit.xyz>
1 parent ad5dac7 commit 8912b99

File tree

2 files changed

+54
-28
lines changed

2 files changed

+54
-28
lines changed

src/BeautifulTree.tsx

Lines changed: 53 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ export type NodeShapeGetter = (
1111
data?: Readonly<Record<string, unknown>> | undefined,
1212
) => { type: 'circle' | 'rect' }
1313

14+
export type NodeContentGetter = (
15+
data?: Readonly<Record<string, unknown>> | undefined,
16+
) => JSX.Element | string
17+
1418
export interface BeautifulTreeProps {
1519
readonly id: string
1620
readonly svgProps: {
@@ -26,6 +30,7 @@ export interface BeautifulTreeProps {
2630
) => Readonly<WrappedTreeWithLayout>
2731
readonly getNodeClass?: CssClassesGetter | undefined
2832
readonly getNodeShape?: NodeShapeGetter | undefined
33+
readonly getNodeContent?: NodeContentGetter | undefined
2934
readonly getEdgeClass?: CssClassesGetter | undefined
3035
}
3136

@@ -46,6 +51,7 @@ export function BeautifulTree({
4651
computeLayout,
4752
getNodeClass,
4853
getNodeShape,
54+
getNodeContent,
4955
getEdgeClass,
5056
}: Readonly<BeautifulTreeProps>): JSX.Element {
5157
const { tree: treeWithLayout, mX, mY } = computeLayout(tree)
@@ -71,7 +77,11 @@ export function BeautifulTree({
7177
className={'beautiful-tree-react'}
7278
>
7379
<style>
74-
{'line{stroke:black;}circle,rect{stroke:black;fill:white;}'}
80+
{`line{stroke:black;}
81+
circle,rect{stroke:black;fill:white;}
82+
div.beautiful-tree-node-content{margin:0;height:100%;width:100%;text-align:center;line-height:${maxNodeHeight}${sizeUnit};font-size:${
83+
maxNodeHeight * 0.5
84+
}${sizeUnit};}`}
7585
</style>
7686
{Array.from(edgesIterator(treeWithLayout), (edge, idx) => {
7787
return (
@@ -92,34 +102,49 @@ export function BeautifulTree({
92102
const nm = node.meta
93103

94104
const _nodeShape = getNodeShape?.(node.data) ?? nodeShape ?? 'circle'
105+
const _nodeClass = `${nm.isRoot ? ' beautiful-tree-root' : ''}${
106+
nm.isLeaf ? ' beautiful-tree-leaf' : ''
107+
}${runClassesGetter(getNodeClass, node.data)}`
95108

96-
return _nodeShape === 'rect' ? (
97-
<rect
98-
key={`${id}-node-${idx}`}
99-
className={`beautiful-tree-node${
100-
nm.isRoot ? ' beautiful-tree-root' : ''
101-
}${nm.isLeaf ? ' beautiful-tree-leaf' : ''}${runClassesGetter(
102-
getNodeClass,
103-
node.data,
104-
)}`}
105-
x={(nm.pos.x + 1) * xCoef - widthCenterShift}
106-
y={(nm.pos.y + 1) * yCoef - heightCenterShift}
107-
width={maxNodeWidth}
108-
height={maxNodeHeight}
109-
/>
110-
) : (
111-
<circle
112-
key={`${id}-node-${idx}`}
113-
className={`beautiful-tree-node${
114-
nm.isRoot ? ' beautiful-tree-root' : ''
115-
}${nm.isLeaf ? ' beautiful-tree-leaf' : ''}${runClassesGetter(
116-
getNodeClass,
117-
node.data,
118-
)}`}
119-
cx={(nm.pos.x + 1) * xCoef}
120-
cy={(nm.pos.y + 1) * yCoef}
121-
r={maxNodeRadius}
122-
/>
109+
return (
110+
<>
111+
{_nodeShape === 'rect' ? (
112+
<rect
113+
key={`${id}-node-${idx}`}
114+
className={`beautiful-tree-node${
115+
nm.isRoot ? ' beautiful-tree-root' : ''
116+
}${nm.isLeaf ? ' beautiful-tree-leaf' : ''}${runClassesGetter(
117+
getNodeClass,
118+
node.data,
119+
)}`}
120+
x={(nm.pos.x + 1) * xCoef - widthCenterShift}
121+
y={(nm.pos.y + 1) * yCoef - heightCenterShift}
122+
width={maxNodeWidth}
123+
height={maxNodeHeight}
124+
/>
125+
) : (
126+
<circle
127+
key={`${id}-node-${idx}`}
128+
className={`beautiful-tree-node${_nodeClass}`}
129+
cx={(nm.pos.x + 1) * xCoef}
130+
cy={(nm.pos.y + 1) * yCoef}
131+
r={maxNodeRadius}
132+
/>
133+
)}
134+
{getNodeContent ? (
135+
<foreignObject
136+
key={`${id}-node-content-${idx}`}
137+
x={(nm.pos.x + 1) * xCoef - widthCenterShift}
138+
y={(nm.pos.y + 1) * yCoef - heightCenterShift}
139+
width={maxNodeWidth}
140+
height={maxNodeHeight}
141+
>
142+
<div className={`beautiful-tree-node-content${_nodeClass}`}>
143+
{getNodeContent(node.data)}
144+
</div>
145+
</foreignObject>
146+
) : undefined}
147+
</>
123148
)
124149
})}
125150
</svg>

src/stories/BeautifulTree.stories.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,7 @@ export const Centered3_Wide_Tree_A: Story = {
362362
tree: wideTree_A,
363363
computeLayout: computeSmartLayout,
364364
getNodeClass: getCssFromNodeData,
365+
getNodeContent: (data) => data?.['v']?.toString() ?? '',
365366
getEdgeClass: getCssFromEdgeData,
366367
},
367368
}

0 commit comments

Comments
 (0)