@@ -3,12 +3,12 @@ import { Component, ComponentType, KeyboardEvent } from 'react';
33
44import { focusOn } from './focus' ;
55
6- function getAllItems ( ref : HTMLElement ) {
6+ function getAllItems ( ref : HTMLElement ) : HTMLElement [ ] {
77 const nodes = ref . closest ( 'ul[role="tree"]' ) ;
88 if ( nodes ) {
9- return nodes . querySelectorAll ( 'li[role="treeitem"]' ) ;
9+ return Array . from ( nodes . querySelectorAll < HTMLElement > ( 'li[role="treeitem"]' ) ) ;
1010 }
11- return null ;
11+ return [ ] ;
1212}
1313
1414function getFirstItem ( ref : HTMLElement ) : HTMLElement | null {
@@ -21,8 +21,8 @@ function getFirstItem(ref: HTMLElement): HTMLElement | null {
2121
2222function getLastItem ( ref : HTMLElement ) : HTMLElement | null {
2323 const nodes = getAllItems ( ref ) ;
24- if ( nodes && nodes . length > 0 ) {
25- return nodes . item ( nodes . length - 1 ) as HTMLElement ;
24+ if ( nodes . length > 0 ) {
25+ return nodes [ nodes . length - 1 ] ;
2626 }
2727 return null ;
2828}
@@ -36,24 +36,21 @@ function getParentItem(ref: HTMLElement): HTMLElement | null {
3636}
3737
3838function getFirstChildItem ( ref : HTMLElement ) : HTMLElement | null {
39- return ref . querySelector ( 'li[role="treeitem"]' ) ;
39+ return ref . querySelector < HTMLElement > ( 'li[role="treeitem"]' ) ;
4040}
4141
4242function getNextItem ( ref : HTMLElement ) : HTMLElement | null {
4343 let nextElement = null ;
44- let currentFound ;
45- let hasNext ;
44+ let currentFound = false ;
45+ let hasNext = false ;
4646
47- const nodes = getAllItems ( ref ) ?. values ( ) ;
48- if ( ! nodes ) {
49- return nextElement ;
50- }
47+ const nodes = getAllItems ( ref ) . values ( ) ;
5148
5249 do {
5350 const { value, done } = nodes . next ( ) ;
5451
5552 if ( currentFound ) {
56- nextElement = value ;
53+ nextElement = value ?? null ;
5754 hasNext = false ;
5855 } else {
5956 currentFound = value === ref ;
@@ -64,14 +61,11 @@ function getNextItem(ref: HTMLElement): HTMLElement | null {
6461 return nextElement ;
6562}
6663
67- function getPreviousItem ( ref : HTMLElement ) {
64+ function getPreviousItem ( ref : HTMLElement ) : HTMLElement | null {
6865 let previousElement = null ;
69- let hasNext ;
66+ let hasNext = false ;
7067
71- const nodes = getAllItems ( ref ) ?. values ( ) ;
72- if ( ! nodes ) {
73- return previousElement ;
74- }
68+ const nodes = getAllItems ( ref ) . values ( ) ;
7569
7670 do {
7771 const { value, done } = nodes . next ( ) ;
@@ -80,7 +74,7 @@ function getPreviousItem(ref: HTMLElement) {
8074 if ( currentFound ) {
8175 hasNext = false ;
8276 } else {
83- previousElement = value ;
77+ previousElement = value ?? null ;
8478 hasNext = ! done ;
8579 }
8680 } while ( hasNext ) ;
0 commit comments