33 Dispatch ,
44 FC ,
55 FocusEvent ,
6+ KeyboardEvent ,
67 MouseEvent ,
78 MutableRefObject ,
89 ReactNode ,
@@ -41,7 +42,9 @@ interface InputSelectProps {
4142
4243const InputSelect : FC < InputSelectProps > = ( props : InputSelectProps ) => {
4344 const triggerRef : MutableRefObject < any > = useRef ( undefined )
45+ const buttonRef : MutableRefObject < HTMLButtonElement | null > = useRef ( null )
4446 const [ menuIsVisible , setMenuIsVisible ] : [ boolean , Dispatch < SetStateAction < boolean > > ] = useState ( false )
47+ const [ isFocus , setIsFocus ] : [ boolean , Dispatch < SetStateAction < boolean > > ] = useState ( false )
4548
4649 const selectedOption : InputSelectOption | undefined = props . options . find ( option => option . value === props . value )
4750
@@ -60,10 +63,17 @@ const InputSelect: FC<InputSelectProps> = (props: InputSelectProps) => {
6063 props . onChange ( {
6164 target : { value : option . value } ,
6265 } as unknown as ChangeEvent < HTMLInputElement > )
63- toggleMenu ( )
66+ buttonRef . current ?. focus ( ) // this will close the dropdown menu
6467 }
6568
66- function toggleIfNotDisabled ( ) : void {
69+ function toggleIfNotDisabled ( event :
70+ MouseEvent < HTMLButtonElement >
71+ | FocusEvent < HTMLButtonElement >
72+ | KeyboardEvent < HTMLButtonElement >
73+ | undefined )
74+ : void {
75+ event ?. stopPropagation ( )
76+ event ?. preventDefault ( )
6777 if ( props . disabled ) {
6878 return
6979 }
@@ -73,6 +83,12 @@ const InputSelect: FC<InputSelectProps> = (props: InputSelectProps) => {
7383
7484 useClickOutside ( triggerRef . current , ( ) => setMenuIsVisible ( false ) )
7585
86+ function handleKeyDown ( event : KeyboardEvent < HTMLButtonElement > | undefined ) : void {
87+ if ( event ?. key === 'Enter' ) {
88+ toggleIfNotDisabled ( event )
89+ }
90+ }
91+
7692 return (
7793 < InputWrapper
7894 { ...props }
@@ -84,19 +100,23 @@ const InputSelect: FC<InputSelectProps> = (props: InputSelectProps) => {
84100 className = { styles [ 'select-input-wrapper' ] }
85101 hideInlineErrors = { props . hideInlineErrors }
86102 ref = { triggerRef }
87- forceFocusStyle = { menuIsVisible }
103+ forceFocusStyle = { menuIsVisible || isFocus }
88104 >
89105 < button
90106 tabIndex = { props . tabIndex }
91107 className = { styles . selected }
92- onClick = { toggleIfNotDisabled }
108+ onMouseDown = { toggleIfNotDisabled }
109+ onKeyDown = { handleKeyDown }
93110 type = 'button'
94111 disabled = { ! ! props . disabled }
95112 onFocus = { function onFocus ( event : FocusEvent < HTMLButtonElement > | undefined ) {
96- event ?. stopPropagation ( )
97- event ?. preventDefault ( )
98- setMenuIsVisible ( true )
113+ setIsFocus ( true )
114+ toggleIfNotDisabled ( event )
115+ } }
116+ onBlur = { function onBlur ( ) {
117+ setIsFocus ( false )
99118 } }
119+ ref = { buttonRef }
100120 >
101121 < span className = 'body-small' > { selectedOption ? label ( selectedOption ) : '' } </ span >
102122 < span className = 'body-small' > { ! selectedOption && ! ! props . placeholder ? props . placeholder : '' } </ span >
0 commit comments