@@ -101,37 +101,67 @@ export function getItemKey<
101101 return ( item ?. item ?. key ?? item ?. key ) as TKey ;
102102}
103103
104+ /**
105+ * Get the position of the item with the given selected key in a list of items.
106+ */
104107export async function getPositionOfSelectedItemElement <
105108 TKey extends string | number | boolean | undefined ,
106109> ( {
107- children ,
110+ itemsOrSections ,
108111 itemHeight,
109112 itemHeightWithDescription,
113+ sectionTitleHeight,
114+ sectionEmptyTitleHeight,
110115 selectedKey,
111116 topOffset,
112117} : {
113- children : ( ItemElement | SectionElement ) [ ] ;
118+ itemsOrSections : ( ItemElement | SectionElement ) [ ] ;
114119 selectedKey : TKey | null | undefined ;
115120 itemHeight : number ;
116121 itemHeightWithDescription : number ;
122+ sectionTitleHeight : number ;
123+ sectionEmptyTitleHeight : number ;
117124 topOffset : number ;
118125} ) : Promise < number > {
119- let position = 0 ;
126+ let position = topOffset ;
127+
128+ if ( selectedKey == null ) {
129+ return position ;
130+ }
131+
132+ const getItemHeight = ( item : ItemElement ) =>
133+ isItemElementWithDescription ( item ) ? itemHeightWithDescription : itemHeight ;
120134
121135 // eslint-disable-next-line no-restricted-syntax
122- for ( const child of children ) {
123- if ( child . key === selectedKey ) {
124- break ;
136+ for ( const itemOrSection of itemsOrSections ) {
137+ if ( itemOrSection . key === selectedKey ) {
138+ return position ;
125139 }
126140
127- if ( isItemElementWithDescription ( child ) ) {
128- position += itemHeightWithDescription ;
141+ if ( isSectionElement ( itemOrSection ) ) {
142+ position +=
143+ ( itemOrSection . props . title ?? '' ) === ''
144+ ? sectionEmptyTitleHeight
145+ : sectionTitleHeight ;
146+
147+ const childItems = Array . isArray ( itemOrSection . props . children )
148+ ? itemOrSection . props . children
149+ : [ itemOrSection . props . children ] ;
150+
151+ // eslint-disable-next-line no-restricted-syntax
152+ for ( const childItem of childItems ) {
153+ if ( childItem . key === selectedKey ) {
154+ return position ;
155+ }
156+
157+ position += getItemHeight ( childItem ) ;
158+ }
129159 } else {
130- position += itemHeight ;
160+ position += getItemHeight ( itemOrSection ) ;
131161 }
132162 }
133163
134- return position + topOffset ;
164+ return topOffset ;
135165}
136166
137167/**
0 commit comments