@@ -17,6 +17,7 @@ import {
1717 useRole ,
1818 useTypeahead
1919} from '@floating-ui/react-dom-interactions'
20+ import { IconChevronDown } from '@tabler/icons-react'
2021import { AnimatePresence , motion } from 'framer-motion'
2122import { ellipsis } from 'polished'
2223import React , {
@@ -28,7 +29,6 @@ import React, {
2829 useRef ,
2930 useState
3031} from 'react'
31- import { IoChevronDown } from 'react-icons/io5'
3232import { PP , PR } from '../types/PolymorphicElementProps'
3333import { forwardRefWithGenerics } from '../utils/ref'
3434import { cssClickable , cssDisablable } from '../utils/styles'
@@ -107,7 +107,6 @@ export const Select = forwardRefWithGenerics(
107107 value,
108108 onChange,
109109 render = ( e ) => ( typeof e === 'string' ? e : e . value ) ,
110- as,
111110 ListItemProps,
112111 ...rest
113112 } = props
@@ -125,9 +124,13 @@ export const Select = forwardRefWithGenerics(
125124 const [ controlledScrolling , setControlledScrolling ] = useState ( false )
126125 const [ touch , setTouch ] = useState ( false )
127126
128- useEffect ( ( ) => {
129- if ( onChange ) onChange ( items [ selectedIndex ] )
130- } , [ selectedIndex ] )
127+ const handleCommit = ( index : number ) : void => {
128+ setSelectedIndex ( index )
129+ if ( onChange ) {
130+ onChange ( items [ index ] )
131+ }
132+ setOpen ( false )
133+ }
131134
132135 useEffect ( ( ) => {
133136 const idx = items . findIndex ( ( it ) =>
@@ -180,7 +183,7 @@ export const Select = forwardRefWithGenerics(
180183 useTypeahead ( context , {
181184 listRef : listContentRef ,
182185 activeIndex,
183- onMatch : open ? setActiveIndex : setSelectedIndex ,
186+ onMatch : open ? setActiveIndex : handleCommit ,
184187 } ) ,
185188 ] )
186189
@@ -267,7 +270,7 @@ export const Select = forwardRefWithGenerics(
267270 >
268271 { selected ? render ( selected ) : null }
269272 < SelectInputAdornment >
270- < IoChevronDown />
273+ < IconChevronDown />
271274 </ SelectInputAdornment >
272275 </ SelectDisplay >
273276 < FloatingPortal >
@@ -340,14 +343,12 @@ export const Select = forwardRefWithGenerics(
340343 onKeyDown ( e ) {
341344 allowSelectRef . current = true
342345 if ( e . key === 'Enter' && allowSelectRef . current ) {
343- setSelectedIndex ( i )
344- setOpen ( false )
346+ handleCommit ( i )
345347 }
346348 } ,
347349 onClick ( ) {
348350 if ( allowSelectRef . current ) {
349- setSelectedIndex ( i )
350- setOpen ( false )
351+ handleCommit ( i )
351352 }
352353 } ,
353354 onMouseUp ( ) {
@@ -356,8 +357,7 @@ export const Select = forwardRefWithGenerics(
356357 }
357358
358359 if ( allowSelectRef . current ) {
359- setSelectedIndex ( i )
360- setOpen ( false )
360+ handleCommit ( i )
361361 }
362362
363363 clearTimeout ( selectTimeoutRef . current )
0 commit comments