@@ -11,7 +11,8 @@ import {
11
11
useElementIds ,
12
12
getItemAndIndex ,
13
13
getInitialValue ,
14
- isDropdownsStateEqual
14
+ isDropdownsStateEqual ,
15
+ useIsInitialMount ,
15
16
} from '../utils'
16
17
import {
17
18
getInitialState ,
@@ -44,7 +45,7 @@ function useCombobox(userProps = {}) {
44
45
downshiftUseComboboxReducer ,
45
46
props ,
46
47
getInitialState ,
47
- isDropdownsStateEqual
48
+ isDropdownsStateEqual ,
48
49
)
49
50
const { isOpen, highlightedIndex, selectedItem, inputValue} = state
50
51
@@ -53,7 +54,8 @@ function useCombobox(userProps = {}) {
53
54
const itemRefs = useRef ( { } )
54
55
const inputRef = useRef ( null )
55
56
const toggleButtonRef = useRef ( null )
56
- const isInitialMountRef = useRef ( true )
57
+ const isInitialMount = useIsInitialMount ( )
58
+
57
59
// prevent id re-generation between renders.
58
60
const elementIds = useElementIds ( props )
59
61
// used to keep track of how many items we had on previous cycle.
@@ -72,7 +74,6 @@ function useCombobox(userProps = {}) {
72
74
getA11yStatusMessage ,
73
75
[ isOpen , highlightedIndex , inputValue , items ] ,
74
76
{
75
- isInitialMount : isInitialMountRef . current ,
76
77
previousResultCount : previousResultCountRef . current ,
77
78
items,
78
79
environment,
@@ -82,7 +83,6 @@ function useCombobox(userProps = {}) {
82
83
)
83
84
// Sets a11y status message on changes in selectedItem.
84
85
useA11yMessageSetter ( getA11ySelectionMessage , [ selectedItem ] , {
85
- isInitialMount : isInitialMountRef . current ,
86
86
previousResultCount : previousResultCountRef . current ,
87
87
items,
88
88
environment,
@@ -99,7 +99,6 @@ function useCombobox(userProps = {}) {
99
99
getItemNodeFromIndex,
100
100
} )
101
101
useControlPropsValidator ( {
102
- isInitialMount : isInitialMountRef . current ,
103
102
props,
104
103
state,
105
104
} )
@@ -113,11 +112,9 @@ function useCombobox(userProps = {}) {
113
112
// eslint-disable-next-line react-hooks/exhaustive-deps
114
113
} , [ ] )
115
114
useEffect ( ( ) => {
116
- if ( isInitialMountRef . current ) {
117
- return
115
+ if ( ! isInitialMount ) {
116
+ previousResultCountRef . current = items . length
118
117
}
119
-
120
- previousResultCountRef . current = items . length
121
118
} )
122
119
// Add mouse/touch events to document.
123
120
const mouseAndTouchTrackersRef = useMouseAndTouchTracker (
@@ -135,14 +132,6 @@ function useCombobox(userProps = {}) {
135
132
'getInputProps' ,
136
133
'getMenuProps' ,
137
134
)
138
- // Make initial ref false.
139
- useEffect ( ( ) => {
140
- isInitialMountRef . current = false
141
-
142
- return ( ) => {
143
- isInitialMountRef . current = true
144
- }
145
- } , [ ] )
146
135
// Reset itemRefs on close.
147
136
useEffect ( ( ) => {
148
137
if ( ! isOpen ) {
@@ -319,10 +308,15 @@ function useCombobox(userProps = {}) {
319
308
: onClick
320
309
321
310
const itemHandleMouseMove = ( ) => {
322
- if ( index === latestState . highlightedIndex ) {
311
+ if (
312
+ mouseAndTouchTrackersRef . current . isTouchEnd ||
313
+ index === latestState . highlightedIndex
314
+ ) {
323
315
return
324
316
}
317
+
325
318
shouldScrollRef . current = false
319
+
326
320
dispatch ( {
327
321
type : stateChangeTypes . ItemMouseMove ,
328
322
index,
@@ -358,7 +352,8 @@ function useCombobox(userProps = {}) {
358
352
...rest ,
359
353
}
360
354
} ,
361
- [ dispatch , latest , shouldScrollRef , elementIds ] ,
355
+
356
+ [ dispatch , elementIds , latest , mouseAndTouchTrackersRef , shouldScrollRef ] ,
362
357
)
363
358
364
359
const getToggleButtonProps = useCallback (
0 commit comments