File tree Expand file tree Collapse file tree 3 files changed +47
-1
lines changed Expand file tree Collapse file tree 3 files changed +47
-1
lines changed Original file line number Diff line number Diff line change @@ -616,6 +616,7 @@ export function addNodeUnderParent({
616
616
getNodeKey,
617
617
ignoreCollapsed = true ,
618
618
expandParent = false ,
619
+ addAsFirstChild = false ,
619
620
} ) {
620
621
if ( parentKey === null ) {
621
622
return {
@@ -668,9 +669,13 @@ export function addNodeUnderParent({
668
669
669
670
insertedTreeIndex = nextTreeIndex ;
670
671
672
+ const children = addAsFirstChild
673
+ ? [ newNode , ...parentNode . children ]
674
+ : [ ...parentNode . children , newNode ] ;
675
+
671
676
return {
672
677
...parentNode ,
673
- children : [ ... parentNode . children , newNode ] ,
678
+ children,
674
679
} ;
675
680
} ,
676
681
} ) ;
Original file line number Diff line number Diff line change @@ -1112,6 +1112,33 @@ describe('addNodeUnderParent', () => {
1112
1112
) ;
1113
1113
expect ( result . treeIndex ) . toEqual ( 5 ) ;
1114
1114
} ) ;
1115
+
1116
+ it ( 'should add new node as last child by default' , ( ) => {
1117
+ const result = addNodeUnderParent ( {
1118
+ ...nestedParams ,
1119
+ parentKey : 0 ,
1120
+ getNodeKey : keyFromKey ,
1121
+ } ) ;
1122
+
1123
+ const [ existingChild0 , existingChild1 , expectedNewNode ] = result . treeData [ 0 ] . children ;
1124
+
1125
+ expect ( expectedNewNode ) . toEqual ( nestedParams . newNode ) ;
1126
+ expect ( [ existingChild0 , existingChild1 ] ) . toEqual ( nestedParams . treeData [ 0 ] . children ) ;
1127
+ } ) ;
1128
+
1129
+ it ( 'should add new node as first child if addAsFirstChild is true' , ( ) => {
1130
+ const result = addNodeUnderParent ( {
1131
+ ...nestedParams ,
1132
+ parentKey : 0 ,
1133
+ getNodeKey : keyFromKey ,
1134
+ addAsFirstChild : true ,
1135
+ } ) ;
1136
+
1137
+ const [ expectedNewNode , ...previousChildren ] = result . treeData [ 0 ] . children ;
1138
+
1139
+ expect ( expectedNewNode ) . toEqual ( nestedParams . newNode ) ;
1140
+ expect ( previousChildren ) . toEqual ( nestedParams . treeData [ 0 ] . children ) ;
1141
+ } ) ;
1115
1142
} ) ;
1116
1143
1117
1144
describe ( 'insertNode' , ( ) => {
Original file line number Diff line number Diff line change @@ -60,6 +60,7 @@ export default class App extends Component {
60
60
61
61
this . state = {
62
62
treeData : [ { title : 'Peter Olofsson' } , { title : 'Karl Johansson' } ] ,
63
+ addAsFirstChild : false ,
63
64
} ;
64
65
}
65
66
@@ -88,6 +89,7 @@ export default class App extends Component {
88
89
node . title . split ( ' ' ) [ 0 ]
89
90
} sson`,
90
91
} ,
92
+ addAsFirstChild : state . addAsFirstChild ,
91
93
} ) . treeData ,
92
94
} ) )
93
95
}
@@ -123,6 +125,18 @@ export default class App extends Component {
123
125
>
124
126
Add more
125
127
</ button >
128
+ < br />
129
+ < label Htmlfor = "addAsFirstChild" >
130
+ Add new nodes at start
131
+ < input
132
+ name = "addAsFirstChild"
133
+ type = "checkbox"
134
+ checked = { this . state . addAsFirstChild }
135
+ onChange = { ( ) => this . setState ( state => ( {
136
+ addAsFirstChild : ! state . addAsFirstChild ,
137
+ } ) ) }
138
+ />
139
+ </ label >
126
140
</ div >
127
141
) ;
128
142
}
You can’t perform that action at this time.
0 commit comments