@@ -58,12 +58,7 @@ function nextDropTarget(
5858 } else {
5959 nextKey = keyboardDelegate . getKeyBelow ?.( target . key ) ;
6060 }
61- let nextCollectionKey = collection . getKeyAfter ( target . key ) ;
62- let nextCollectionNode = nextCollectionKey && collection . getItem ( nextCollectionKey ) ;
63- while ( nextCollectionNode && nextCollectionNode . type === 'content' ) {
64- nextCollectionKey = nextCollectionKey ? collection . getKeyAfter ( nextCollectionKey ) : null ;
65- nextCollectionNode = nextCollectionKey && collection . getItem ( nextCollectionKey ) ;
66- }
61+ let nextCollectionKey = getNextItem ( collection , target . key , key => collection . getKeyAfter ( key ) ) ;
6762
6863 // If the keyboard delegate did not move to the next key in the collection,
6964 // jump to that key with the same drop position. Otherwise, try the other
@@ -191,7 +186,7 @@ function previousDropTarget(
191186 } else {
192187 prevKey = keyboardDelegate . getKeyAbove ?.( target . key ) ;
193188 }
194- let prevCollectionKey = collection . getKeyBefore ( target . key ) ;
189+ let prevCollectionKey = getNextItem ( collection , target . key , key => collection . getKeyBefore ( key ) ) ;
195190
196191 // If the keyboard delegate did not move to the next key in the collection,
197192 // jump to that key with the same drop position. Otherwise, try the other
@@ -263,7 +258,7 @@ function getLastChild(collection: Collection<Node<unknown>>, key: Key): DropTarg
263258 // getChildNodes still returns child tree items even when the item is collapsed.
264259 // Checking if the next item has a greater level is a silly way to determine if the item is expanded.
265260 let targetNode = collection . getItem ( key ) ;
266- let nextKey = collection . getKeyAfter ( key ) ;
261+ let nextKey = getNextItem ( collection , key , key => collection . getKeyAfter ( key ) ) ;
267262 let nextNode = nextKey != null ? collection . getItem ( nextKey ) : null ;
268263 if ( targetNode && nextNode && nextNode . level > targetNode . level ) {
269264 let children = getChildNodes ( targetNode , collection ) ;
@@ -285,3 +280,14 @@ function getLastChild(collection: Collection<Node<unknown>>, key: Key): DropTarg
285280
286281 return null ;
287282}
283+
284+ // Find the next or previous item in a collection, skipping over other types of nodes (e.g. content).
285+ function getNextItem ( collection : Collection < Node < unknown > > , key : Key , getNextKey : ( key : Key ) => Key | null ) : Key | null {
286+ let nextCollectionKey = getNextKey ( key ) ;
287+ let nextCollectionNode = nextCollectionKey != null ? collection . getItem ( nextCollectionKey ) : null ;
288+ while ( nextCollectionNode && nextCollectionNode . type !== 'item' ) {
289+ nextCollectionKey = getNextKey ( nextCollectionNode . key ) ;
290+ nextCollectionNode = nextCollectionKey != null ? collection . getItem ( nextCollectionKey ) : null ;
291+ }
292+ return nextCollectionKey ;
293+ }
0 commit comments