File tree Expand file tree Collapse file tree 4 files changed +68
-1
lines changed Expand file tree Collapse file tree 4 files changed +68
-1
lines changed Original file line number Diff line number Diff line change @@ -604,6 +604,7 @@ export function getNodeAtPath({
604604 * @param {!function } getNodeKey - Function to get the key from the nodeData and tree index
605605 * @param {boolean= } ignoreCollapsed - Ignore children of nodes without `expanded` set to `true`
606606 * @param {boolean= } expandParent - If true, expands the parentNode specified by parentPath
607+ * @param {boolean= } addAsFirstChild - If true, adds new node as first child of tree
607608 *
608609 * @return {Object } result
609610 * @return {Object[] } result.treeData - The updated tree data
@@ -616,6 +617,7 @@ export function addNodeUnderParent({
616617 getNodeKey,
617618 ignoreCollapsed = true ,
618619 expandParent = false ,
620+ addAsFirstChild = false ,
619621} ) {
620622 if ( parentKey === null ) {
621623 return {
@@ -668,9 +670,13 @@ export function addNodeUnderParent({
668670
669671 insertedTreeIndex = nextTreeIndex ;
670672
673+ const children = addAsFirstChild
674+ ? [ newNode , ...parentNode . children ]
675+ : [ ...parentNode . children , newNode ] ;
676+
671677 return {
672678 ...parentNode ,
673- children : [ ... parentNode . children , newNode ] ,
679+ children,
674680 } ;
675681 } ,
676682 } ) ;
Original file line number Diff line number Diff line change @@ -1112,6 +1112,39 @@ describe('addNodeUnderParent', () => {
11121112 ) ;
11131113 expect ( result . treeIndex ) . toEqual ( 5 ) ;
11141114 } ) ;
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 [
1124+ existingChild0 ,
1125+ existingChild1 ,
1126+ expectedNewNode ,
1127+ ] = result . treeData [ 0 ] . children ;
1128+
1129+ expect ( expectedNewNode ) . toEqual ( nestedParams . newNode ) ;
1130+ expect ( [ existingChild0 , existingChild1 ] ) . toEqual (
1131+ nestedParams . treeData [ 0 ] . children
1132+ ) ;
1133+ } ) ;
1134+
1135+ it ( 'should add new node as first child if addAsFirstChild is true' , ( ) => {
1136+ const result = addNodeUnderParent ( {
1137+ ...nestedParams ,
1138+ parentKey : 0 ,
1139+ getNodeKey : keyFromKey ,
1140+ addAsFirstChild : true ,
1141+ } ) ;
1142+
1143+ const [ expectedNewNode , ...previousChildren ] = result . treeData [ 0 ] . children ;
1144+
1145+ expect ( expectedNewNode ) . toEqual ( nestedParams . newNode ) ;
1146+ expect ( previousChildren ) . toEqual ( nestedParams . treeData [ 0 ] . children ) ;
1147+ } ) ;
11151148} ) ;
11161149
11171150describe ( 'insertNode' , ( ) => {
Original file line number Diff line number Diff line change @@ -2243,6 +2243,18 @@ exports[`Storyshots Basics Add and remove nodes programmatically 1`] = `
22432243 >
22442244 Add more
22452245 </button >
2246+ <br />
2247+ <label
2248+ Htmlfor = " addAsFirstChild"
2249+ >
2250+ Add new nodes at start
2251+ <input
2252+ checked = { false }
2253+ name = " addAsFirstChild"
2254+ onChange = { [Function ]}
2255+ type = " checkbox"
2256+ />
2257+ </label >
22462258 </div >
22472259 <br />
22482260 <form
Original file line number Diff line number Diff line change @@ -60,6 +60,7 @@ export default class App extends Component {
6060
6161 this . state = {
6262 treeData : [ { title : 'Peter Olofsson' } , { title : 'Karl Johansson' } ] ,
63+ addAsFirstChild : false ,
6364 } ;
6465 }
6566
@@ -88,6 +89,7 @@ export default class App extends Component {
8889 node . title . split ( ' ' ) [ 0 ]
8990 } sson`,
9091 } ,
92+ addAsFirstChild : state . addAsFirstChild ,
9193 } ) . treeData ,
9294 } ) )
9395 }
@@ -123,6 +125,20 @@ export default class App extends Component {
123125 >
124126 Add more
125127 </ 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 = { ( ) =>
136+ this . setState ( state => ( {
137+ addAsFirstChild : ! state . addAsFirstChild ,
138+ } ) )
139+ }
140+ />
141+ </ label >
126142 </ div >
127143 ) ;
128144 }
You can’t perform that action at this time.
0 commit comments