@@ -109,15 +109,16 @@ export function DialogSelect<T>(props: DialogSelectProps<T>) {
109109
110110 createEffect (
111111 on ( [ ( ) => store . filter , ( ) => props . current ] , ( [ filter , current ] ) => {
112- if ( filter . length > 0 ) {
113- setStore ( "selected" , 0 )
114- } else if ( current ) {
115- const currentIndex = flat ( ) . findIndex ( ( opt ) => isDeepEqual ( opt . value , current ) )
116- if ( currentIndex >= 0 ) {
117- setStore ( "selected" , currentIndex )
112+ setTimeout ( ( ) => {
113+ if ( filter . length > 0 ) {
114+ moveTo ( 0 , true )
115+ } else if ( current ) {
116+ const currentIndex = flat ( ) . findIndex ( ( opt ) => isDeepEqual ( opt . value , current ) )
117+ if ( currentIndex >= 0 ) {
118+ moveTo ( currentIndex , true )
119+ }
118120 }
119- }
120- scroll ?. scrollTo ( 0 )
121+ } , 0 )
121122 } ) ,
122123 )
123124
@@ -129,7 +130,7 @@ export function DialogSelect<T>(props: DialogSelectProps<T>) {
129130 moveTo ( next )
130131 }
131132
132- function moveTo ( next : number ) {
133+ function moveTo ( next : number , center = false ) {
133134 setStore ( "selected" , next )
134135 props . onMove ?.( selected ( ) ! )
135136 if ( ! scroll ) return
@@ -138,13 +139,18 @@ export function DialogSelect<T>(props: DialogSelectProps<T>) {
138139 } )
139140 if ( ! target ) return
140141 const y = target . y - scroll . y
141- if ( y >= scroll . height ) {
142- scroll . scrollBy ( y - scroll . height + 1 )
143- }
144- if ( y < 0 ) {
145- scroll . scrollBy ( y )
146- if ( isDeepEqual ( flat ( ) [ 0 ] . value , selected ( ) ?. value ) ) {
147- scroll . scrollTo ( 0 )
142+ if ( center ) {
143+ const centerOffset = Math . floor ( scroll . height / 2 )
144+ scroll . scrollBy ( y - centerOffset )
145+ } else {
146+ if ( y >= scroll . height ) {
147+ scroll . scrollBy ( y - scroll . height + 1 )
148+ }
149+ if ( y < 0 ) {
150+ scroll . scrollBy ( y )
151+ if ( isDeepEqual ( flat ( ) [ 0 ] . value , selected ( ) ?. value ) ) {
152+ scroll . scrollTo ( 0 )
153+ }
148154 }
149155 }
150156 }
0 commit comments