@@ -8,6 +8,7 @@ import Popover from '../Popover/Popover.tsx'
8
8
9
9
import { classNames } from '../../utils/classNames'
10
10
import { debounce } from '../../utils/debounce'
11
+ import { on } from '../../utils/DOMUtils'
11
12
import { modal } from '../../utils/modal'
12
13
import { closePopover , popover , type PopoverPosition } from '../../utils/popover'
13
14
@@ -28,6 +29,7 @@ const Select = ({
28
29
position = 'bottom' ,
29
30
className,
30
31
onChange,
32
+ onClose,
31
33
...rest
32
34
} : ReactSelectProps ) => {
33
35
const inferredValue = rest . itemGroups . map ( group => group . items )
@@ -51,6 +53,7 @@ const Select = ({
51
53
)
52
54
53
55
let popoverInstance : any
56
+ let focusByTab = false
54
57
55
58
const select = ( event : ListEventType ) => {
56
59
closePopover ( `.w-options-${ name } ` )
@@ -68,6 +71,14 @@ const Select = ({
68
71
useEffect ( ( ) => {
69
72
let observer : ResizeObserver | undefined
70
73
74
+ on ( document , 'keydown' , ( event : KeyboardEvent ) => {
75
+ if ( event . key === 'Tab' ) {
76
+ focusByTab = true
77
+ }
78
+ } )
79
+
80
+ on ( document , 'mousedown' , ( ) => focusByTab = false )
81
+
71
82
if ( position === 'modal' ) {
72
83
modal ( {
73
84
trigger : `.w-select-${ name } ` ,
@@ -78,6 +89,9 @@ const Select = ({
78
89
if ( search ) {
79
90
search . focus ( )
80
91
}
92
+ } ,
93
+ onClose ( event ) {
94
+ onClose ?.( event )
81
95
}
82
96
} )
83
97
} else {
@@ -104,11 +118,20 @@ const Select = ({
104
118
if ( search ) {
105
119
search . focus ( )
106
120
}
121
+ } ,
122
+ onClose ( event ) {
123
+ onClose ?.( event )
107
124
}
108
125
} )
109
126
} , 0 )
110
127
}
111
128
129
+ on ( `.w-select-${ name } ` , 'focus' , ( event : Event ) => {
130
+ if ( focusByTab ) {
131
+ ( event . currentTarget as HTMLInputElement ) . click ( )
132
+ }
133
+ } )
134
+
112
135
return ( ) => {
113
136
popoverInstance ?. remove ( )
114
137
observer ?. unobserve ( document . body )
0 commit comments