8
8
MutableRefObject ,
9
9
ReactNode ,
10
10
SetStateAction ,
11
+ useEffect ,
11
12
useRef ,
12
13
useState ,
13
14
} from 'react'
@@ -76,7 +77,11 @@ const InputSelect: FC<InputSelectProps> = (props: InputSelectProps) => {
76
77
option ? option . label ?? option . value : ''
77
78
)
78
79
79
- const toggleMenu : ( ) => void = ( ) => setMenuIsVisible ( wasVisible => ! wasVisible )
80
+ const toggleMenu = ( toggle ?: boolean ) : void => {
81
+ setTimeout ( setMenuIsVisible , 150 , wasVisible => (
82
+ typeof toggle === 'boolean' ? toggle : ! wasVisible
83
+ ) )
84
+ }
80
85
81
86
const select : ( option : InputSelectOption ) => ( event : MouseEvent < HTMLDivElement > ) => void
82
87
= ( option : InputSelectOption ) => (
@@ -87,32 +92,38 @@ const InputSelect: FC<InputSelectProps> = (props: InputSelectProps) => {
87
92
props . onChange ( {
88
93
target : { value : option . value } ,
89
94
} as unknown as ChangeEvent < HTMLInputElement > )
90
- buttonRef . current ?. focus ( ) // this will close the dropdown menu
95
+ toggleMenu ( false )
91
96
}
92
97
93
98
function toggleIfNotDisabled ( event :
94
99
MouseEvent < HTMLButtonElement >
95
100
| FocusEvent < HTMLButtonElement >
96
101
| KeyboardEvent < HTMLButtonElement >
97
- | undefined )
102
+ | undefined , toggle ?: boolean )
98
103
: void {
99
104
event ?. stopPropagation ( )
100
105
event ?. preventDefault ( )
101
106
if ( props . disabled ) {
102
107
return
103
108
}
104
109
105
- toggleMenu ( )
110
+ toggleMenu ( toggle )
106
111
}
107
112
108
- useClickOutside ( triggerRef . current , ( ) => setMenuIsVisible ( false ) )
113
+ useClickOutside ( triggerRef . current , ( ) => setMenuIsVisible ( false ) , menuIsVisible )
109
114
110
115
function handleKeyDown ( event : KeyboardEvent < HTMLButtonElement > | undefined ) : void {
111
116
if ( event ?. key === 'Enter' ) {
112
117
toggleIfNotDisabled ( event )
113
118
}
114
119
}
115
120
121
+ useEffect ( ( ) => {
122
+ if ( menuIsVisible ) {
123
+ popper . update ?.( )
124
+ }
125
+ } , [ menuIsVisible ] )
126
+
116
127
return (
117
128
< InputWrapper
118
129
{ ...props }
@@ -135,7 +146,7 @@ const InputSelect: FC<InputSelectProps> = (props: InputSelectProps) => {
135
146
disabled = { ! ! props . disabled }
136
147
onFocus = { function onFocus ( event : FocusEvent < HTMLButtonElement > | undefined ) {
137
148
setIsFocus ( true )
138
- toggleIfNotDisabled ( event )
149
+ toggleIfNotDisabled ( event , true )
139
150
} }
140
151
onBlur = { function onBlur ( ) {
141
152
setIsFocus ( false )
0 commit comments