@@ -11,6 +11,10 @@ export type NodeShapeGetter = (
11
11
data ?: Readonly < Record < string , unknown > > | undefined ,
12
12
) => { type : 'circle' | 'rect' }
13
13
14
+ export type NodeContentGetter = (
15
+ data ?: Readonly < Record < string , unknown > > | undefined ,
16
+ ) => JSX . Element | string
17
+
14
18
export interface BeautifulTreeProps {
15
19
readonly id : string
16
20
readonly svgProps : {
@@ -26,6 +30,7 @@ export interface BeautifulTreeProps {
26
30
) => Readonly < WrappedTreeWithLayout >
27
31
readonly getNodeClass ?: CssClassesGetter | undefined
28
32
readonly getNodeShape ?: NodeShapeGetter | undefined
33
+ readonly getNodeContent ?: NodeContentGetter | undefined
29
34
readonly getEdgeClass ?: CssClassesGetter | undefined
30
35
}
31
36
@@ -46,6 +51,7 @@ export function BeautifulTree({
46
51
computeLayout,
47
52
getNodeClass,
48
53
getNodeShape,
54
+ getNodeContent,
49
55
getEdgeClass,
50
56
} : Readonly < BeautifulTreeProps > ) : JSX . Element {
51
57
const { tree : treeWithLayout , mX, mY } = computeLayout ( tree )
@@ -71,7 +77,11 @@ export function BeautifulTree({
71
77
className = { 'beautiful-tree-react' }
72
78
>
73
79
< 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 } ;}`}
75
85
</ style >
76
86
{ Array . from ( edgesIterator ( treeWithLayout ) , ( edge , idx ) => {
77
87
return (
@@ -92,34 +102,49 @@ export function BeautifulTree({
92
102
const nm = node . meta
93
103
94
104
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 ) } `
95
108
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
+ </ >
123
148
)
124
149
} ) }
125
150
</ svg >
0 commit comments