Skip to content

Commit c8327e2

Browse files
committed
MP-316 - fix input-select: reposition on visible & fix click outside debouncing
1 parent 2fe99fd commit c8327e2

File tree

1 file changed

+17
-6
lines changed
  • src/libs/ui/lib/components/form/form-groups/form-input/input-select

1 file changed

+17
-6
lines changed

src/libs/ui/lib/components/form/form-groups/form-input/input-select/InputSelect.tsx

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
MutableRefObject,
99
ReactNode,
1010
SetStateAction,
11+
useEffect,
1112
useRef,
1213
useState,
1314
} from 'react'
@@ -76,7 +77,11 @@ const InputSelect: FC<InputSelectProps> = (props: InputSelectProps) => {
7677
option ? option.label ?? option.value : ''
7778
)
7879

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+
}
8085

8186
const select: (option: InputSelectOption) => (event: MouseEvent<HTMLDivElement>) => void
8287
= (option: InputSelectOption) => (
@@ -87,32 +92,38 @@ const InputSelect: FC<InputSelectProps> = (props: InputSelectProps) => {
8792
props.onChange({
8893
target: { value: option.value },
8994
} as unknown as ChangeEvent<HTMLInputElement>)
90-
buttonRef.current?.focus() // this will close the dropdown menu
95+
toggleMenu(false)
9196
}
9297

9398
function toggleIfNotDisabled(event:
9499
MouseEvent<HTMLButtonElement>
95100
| FocusEvent<HTMLButtonElement>
96101
| KeyboardEvent<HTMLButtonElement>
97-
| undefined)
102+
| undefined, toggle?: boolean)
98103
: void {
99104
event?.stopPropagation()
100105
event?.preventDefault()
101106
if (props.disabled) {
102107
return
103108
}
104109

105-
toggleMenu()
110+
toggleMenu(toggle)
106111
}
107112

108-
useClickOutside(triggerRef.current, () => setMenuIsVisible(false))
113+
useClickOutside(triggerRef.current, () => setMenuIsVisible(false), menuIsVisible)
109114

110115
function handleKeyDown(event: KeyboardEvent<HTMLButtonElement> | undefined): void {
111116
if (event?.key === 'Enter') {
112117
toggleIfNotDisabled(event)
113118
}
114119
}
115120

121+
useEffect(() => {
122+
if (menuIsVisible) {
123+
popper.update?.()
124+
}
125+
}, [menuIsVisible])
126+
116127
return (
117128
<InputWrapper
118129
{...props}
@@ -135,7 +146,7 @@ const InputSelect: FC<InputSelectProps> = (props: InputSelectProps) => {
135146
disabled={!!props.disabled}
136147
onFocus={function onFocus(event: FocusEvent<HTMLButtonElement> | undefined) {
137148
setIsFocus(true)
138-
toggleIfNotDisabled(event)
149+
toggleIfNotDisabled(event, true)
139150
}}
140151
onBlur={function onBlur() {
141152
setIsFocus(false)

0 commit comments

Comments
 (0)