File tree 2 files changed +17
-1
lines changed
2 files changed +17
-1
lines changed Original file line number Diff line number Diff line change @@ -13,11 +13,13 @@ export class Node<T extends Data> {
13
13
hasChildren : boolean ;
14
14
15
15
#treeId: string ;
16
+ #ancestorPath: string ;
16
17
#key: string ;
17
18
#childKey: string ;
18
19
19
20
constructor ( {
20
21
treeId,
22
+ ancestorPath,
21
23
key,
22
24
childKey,
23
25
level,
@@ -26,6 +28,7 @@ export class Node<T extends Data> {
26
28
data,
27
29
} : {
28
30
treeId : string ;
31
+ ancestorPath : string ;
29
32
key : string ;
30
33
childKey : string ;
31
34
level : number ;
@@ -43,6 +46,7 @@ export class Node<T extends Data> {
43
46
this . hasChildren = ! this . isLeaf ;
44
47
45
48
this . #treeId = treeId ;
49
+ this . #ancestorPath = ancestorPath ;
46
50
this . #key = key ;
47
51
this . #childKey = childKey ;
48
52
}
@@ -91,6 +95,7 @@ export class Node<T extends Data> {
91
95
addChild ( { data } : { data : T } ) : Node < T > {
92
96
const newChildNode = new Node < T > ( {
93
97
treeId : this . #treeId,
98
+ ancestorPath : this . #ancestorPath,
94
99
key : this . #key,
95
100
childKey : this . #childKey,
96
101
level : this . level + 1 ,
@@ -113,7 +118,9 @@ export class Node<T extends Data> {
113
118
getParentNode ( ) : Node < T > | undefined {
114
119
const tree = this . getTree ( ) ;
115
120
const parentNode = tree . find (
116
- ( node ) => node . data [ this . #key] === this . parentKey ,
121
+ ( node ) =>
122
+ node . data [ this . #key] === this . parentKey &&
123
+ this . #ancestorPath. includes ( node . #ancestorPath) ,
117
124
) ;
118
125
return parentNode ;
119
126
}
Original file line number Diff line number Diff line change @@ -140,13 +140,21 @@ export class Tree<T extends Data> {
140
140
key,
141
141
childKey,
142
142
nodeParam,
143
+ ancestorPath,
143
144
} : {
144
145
key : string ;
145
146
childKey : string ;
146
147
nodeParam : NodeParam < T > ;
148
+ ancestorPath ?: string ;
147
149
} ) {
150
+ const path =
151
+ ancestorPath === undefined
152
+ ? ( nodeParam . data [ key ] as string )
153
+ : ancestorPath ;
154
+
148
155
const node = new Node < T > ( {
149
156
treeId : this . treeId ,
157
+ ancestorPath : path ,
150
158
key,
151
159
childKey,
152
160
level : nodeParam . level ,
@@ -156,6 +164,7 @@ export class Tree<T extends Data> {
156
164
key,
157
165
childKey,
158
166
nodeParam : childNodeModel ,
167
+ ancestorPath : `${ path } /${ childNodeModel . data [ key ] as string } ` ,
159
168
} ) ,
160
169
) ,
161
170
data : nodeParam . data ,
You can’t perform that action at this time.
0 commit comments