@@ -26,13 +26,16 @@ export function init(id, invoke) {
2626 if ( duration > 0 ) {
2727 ac . debounce = true
2828 EventHandler . on ( input , 'keyup' , debounce ( e => {
29+ e . preventDefault ( ) ;
2930 handlerKeyup ( ac , e ) ;
3031 } , duration , e => {
3132 return [ 'ArrowUp' , 'ArrowDown' , 'Escape' , 'Enter' , 'NumpadEnter' ] . indexOf ( e . key ) > - 1
3233 } ) )
3334 }
3435 else {
3536 EventHandler . on ( input , 'keyup' , e => {
37+ e . stopPropagation ( ) ;
38+ e . preventDefault ( ) ;
3639 handlerKeyup ( ac , e ) ;
3740 } )
3841 }
@@ -127,26 +130,12 @@ const handlerKeyup = (ac, e) => {
127130 else if ( index > items . length - 1 ) {
128131 index = 0 ;
129132 }
130- items [ index ] . classList . add ( 'active' ) ;
131- const top = getTop ( menu , index ) ;
132- const hehavior = el . getAttribute ( 'data-bb-scroll-behavior' ) ?? 'smooth' ;
133- menu . scrollTo ( { top : top , left : 0 , behavior : hehavior } ) ;
133+ current = items [ index ] ;
134+ current . classList . add ( 'active' ) ;
135+ scrollIntoView ( el , current ) ;
134136 }
135137}
136138
137- const getTop = ( menu , index ) => {
138- const styles = getComputedStyle ( menu )
139- const maxHeight = parseInt ( styles . maxHeight ) / 2
140- const itemHeight = getHeight ( menu . querySelector ( '.dropdown-item' ) )
141- const height = itemHeight * index
142- const count = Math . floor ( maxHeight / itemHeight ) ;
143- let top = 0 ;
144- if ( height > maxHeight ) {
145- top = itemHeight * ( index > count ? index - count : index )
146- }
147- return top ;
148- }
149-
150139export function showList ( id ) {
151140 const ac = Data . get ( id )
152141 if ( ac ) {
@@ -177,4 +166,9 @@ export function dispose(id) {
177166 }
178167}
179168
169+ const scrollIntoView = ( el , item ) => {
170+ const behavior = el . getAttribute ( 'data-bb-scroll-behavior' ) ?? 'smooth' ;
171+ item . scrollIntoView ( { behavior : behavior , block : "nearest" , inline : "start" } ) ;
172+ }
173+
180174export { handleKeyUp , select , selectAllByFocus , selectAllByEnter }
0 commit comments