downshift 5.2.1 → 5.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2273,25 +2273,26 @@ function useSelect(userProps) {
2273
2273
 
2274
2274
  var toggleButtonRef = react.useRef(null);
2275
2275
  var menuRef = react.useRef(null);
2276
- var isInitialMount = react.useRef(true);
2277
- var shouldScroll = react.useRef(true);
2278
- var clearTimeout = react.useRef(null);
2279
- var mouseAndTouchTrackers = react.useRef({
2276
+ var isInitialMountRef = react.useRef(true);
2277
+ var shouldScrollRef = react.useRef(true);
2278
+ var shouldBlurRef = react.useRef(true);
2279
+ var clearTimeoutRef = react.useRef(null);
2280
+ var mouseAndTouchTrackersRef = react.useRef({
2280
2281
  isMouseDown: false,
2281
2282
  isTouchMove: false
2282
2283
  });
2283
- var elementIds = react.useRef(getElementIds(props));
2284
+ var elementIdsRef = react.useRef(getElementIds(props));
2284
2285
  var previousResultCountRef = react.useRef(); // Some utils.
2285
2286
 
2286
2287
  var getItemNodeFromIndex = function (index) {
2287
- return environment.document.getElementById(elementIds.current.getItemId(index));
2288
+ return environment.document.getElementById(elementIdsRef.current.getItemId(index));
2288
2289
  }; // Effects.
2289
2290
 
2290
2291
  /* Sets a11y status message on changes in state. */
2291
2292
 
2292
2293
 
2293
2294
  react.useEffect(function () {
2294
- if (isInitialMount.current) {
2295
+ if (isInitialMountRef.current) {
2295
2296
  return;
2296
2297
  }
2297
2298
 
@@ -2312,7 +2313,7 @@ function useSelect(userProps) {
2312
2313
  /* Sets a11y status message on changes in selectedItem. */
2313
2314
 
2314
2315
  react.useEffect(function () {
2315
- if (isInitialMount.current) {
2316
+ if (isInitialMountRef.current) {
2316
2317
  return;
2317
2318
  }
2318
2319
 
@@ -2334,8 +2335,8 @@ function useSelect(userProps) {
2334
2335
 
2335
2336
  react.useEffect(function () {
2336
2337
  // init the clean function here as we need access to dispatch.
2337
- if (isInitialMount.current) {
2338
- clearTimeout.current = debounce(function (outerDispatch) {
2338
+ if (isInitialMountRef.current) {
2339
+ clearTimeoutRef.current = debounce(function (outerDispatch) {
2339
2340
  outerDispatch({
2340
2341
  type: FunctionSetInputValue,
2341
2342
  inputValue: ''
@@ -2347,13 +2348,13 @@ function useSelect(userProps) {
2347
2348
  return;
2348
2349
  }
2349
2350
 
2350
- clearTimeout.current(dispatch); // eslint-disable-next-line react-hooks/exhaustive-deps
2351
+ clearTimeoutRef.current(dispatch); // eslint-disable-next-line react-hooks/exhaustive-deps
2351
2352
  }, [inputValue]);
2352
2353
  /* Controls the focus on the menu or the toggle button. */
2353
2354
 
2354
2355
  react.useEffect(function () {
2355
2356
  // Don't focus menu on first render.
2356
- if (isInitialMount.current) {
2357
+ if (isInitialMountRef.current) {
2357
2358
  // Unless it was initialised as open.
2358
2359
  if ((initialIsOpen || defaultIsOpen || isOpen) && menuRef.current) {
2359
2360
  menuRef.current.focus();
@@ -2361,13 +2362,23 @@ function useSelect(userProps) {
2361
2362
 
2362
2363
  return;
2363
2364
  } // Focus menu on open.
2364
- // istanbul ignore next
2365
2365
 
2366
2366
 
2367
- if (isOpen && menuRef.current) {
2368
- menuRef.current.focus(); // Focus toggleButton on close.
2369
- } else if (environment.document.activeElement === menuRef.current && toggleButtonRef.current) {
2370
- toggleButtonRef.current.focus();
2367
+ if (isOpen) {
2368
+ // istanbul ignore else
2369
+ if (menuRef.current) {
2370
+ menuRef.current.focus();
2371
+ return;
2372
+ }
2373
+ } // Focus toggleButton on close, but on if was closed with (Shift+)Tab.
2374
+
2375
+
2376
+ if (environment.document.activeElement === menuRef.current) {
2377
+ // istanbul ignore else
2378
+ if (toggleButtonRef.current) {
2379
+ shouldBlurRef.current = false;
2380
+ toggleButtonRef.current.focus();
2381
+ }
2371
2382
  } // eslint-disable-next-line react-hooks/exhaustive-deps
2372
2383
 
2373
2384
  }, [isOpen]);
@@ -2378,15 +2389,15 @@ function useSelect(userProps) {
2378
2389
  return;
2379
2390
  }
2380
2391
 
2381
- if (shouldScroll.current === false) {
2382
- shouldScroll.current = true;
2392
+ if (shouldScrollRef.current === false) {
2393
+ shouldScrollRef.current = true;
2383
2394
  } else {
2384
2395
  scrollIntoView(getItemNodeFromIndex(highlightedIndex), menuRef.current);
2385
2396
  } // eslint-disable-next-line react-hooks/exhaustive-deps
2386
2397
 
2387
2398
  }, [highlightedIndex]);
2388
2399
  react.useEffect(function () {
2389
- if (isInitialMount.current) {
2400
+ if (isInitialMountRef.current) {
2390
2401
  return;
2391
2402
  }
2392
2403
 
@@ -2395,7 +2406,7 @@ function useSelect(userProps) {
2395
2406
  /* Make initial ref false. */
2396
2407
 
2397
2408
  react.useEffect(function () {
2398
- isInitialMount.current = false;
2409
+ isInitialMountRef.current = false;
2399
2410
  }, []);
2400
2411
  /* Add mouse/touch events to document. */
2401
2412
 
@@ -2403,11 +2414,11 @@ function useSelect(userProps) {
2403
2414
  // The same strategy for checking if a click occurred inside or outside downsift
2404
2415
  // as in downshift.js.
2405
2416
  var onMouseDown = function () {
2406
- mouseAndTouchTrackers.current.isMouseDown = true;
2417
+ mouseAndTouchTrackersRef.current.isMouseDown = true;
2407
2418
  };
2408
2419
 
2409
2420
  var onMouseUp = function (event) {
2410
- mouseAndTouchTrackers.current.isMouseDown = false;
2421
+ mouseAndTouchTrackersRef.current.isMouseDown = false;
2411
2422
 
2412
2423
  if (isOpen && !targetWithinDownshift(event.target, [toggleButtonRef.current, menuRef.current], environment.document)) {
2413
2424
  dispatch({
@@ -2417,15 +2428,15 @@ function useSelect(userProps) {
2417
2428
  };
2418
2429
 
2419
2430
  var onTouchStart = function () {
2420
- mouseAndTouchTrackers.current.isTouchMove = false;
2431
+ mouseAndTouchTrackersRef.current.isTouchMove = false;
2421
2432
  };
2422
2433
 
2423
2434
  var onTouchMove = function () {
2424
- mouseAndTouchTrackers.current.isTouchMove = true;
2435
+ mouseAndTouchTrackersRef.current.isTouchMove = true;
2425
2436
  };
2426
2437
 
2427
2438
  var onTouchEnd = function (event) {
2428
- if (isOpen && !mouseAndTouchTrackers.current.isTouchMove && !targetWithinDownshift(event.target, [toggleButtonRef.current, menuRef.current], environment.document, false)) {
2439
+ if (isOpen && !mouseAndTouchTrackersRef.current.isTouchMove && !targetWithinDownshift(event.target, [toggleButtonRef.current, menuRef.current], environment.document, false)) {
2429
2440
  dispatch({
2430
2441
  type: MenuBlur
2431
2442
  });
@@ -2529,7 +2540,13 @@ function useSelect(userProps) {
2529
2540
  };
2530
2541
 
2531
2542
  var menuHandleBlur = function () {
2532
- var shouldBlur = !mouseAndTouchTrackers.current.isMouseDown;
2543
+ // if the blur was a result of selection, we don't trigger this action.
2544
+ if (shouldBlurRef.current === false) {
2545
+ shouldBlurRef.current = true;
2546
+ return;
2547
+ }
2548
+
2549
+ var shouldBlur = !mouseAndTouchTrackersRef.current.isMouseDown;
2533
2550
  /* istanbul ignore else */
2534
2551
 
2535
2552
  if (shouldBlur) {
@@ -2570,7 +2587,7 @@ function useSelect(userProps) {
2570
2587
  return;
2571
2588
  }
2572
2589
 
2573
- shouldScroll.current = false;
2590
+ shouldScrollRef.current = false;
2574
2591
  dispatch({
2575
2592
  type: ItemMouseMove,
2576
2593
  index: index
@@ -2600,7 +2617,7 @@ function useSelect(userProps) {
2600
2617
 
2601
2618
  var toggleProps = _extends((_extends3 = {}, _extends3[refKey] = handleRefs(ref, function (toggleButtonNode) {
2602
2619
  toggleButtonRef.current = toggleButtonNode;
2603
- }), _extends3.id = elementIds.current.toggleButtonId, _extends3['aria-haspopup'] = 'listbox', _extends3['aria-expanded'] = isOpen, _extends3['aria-labelledby'] = elementIds.current.labelId + " " + elementIds.current.toggleButtonId, _extends3), rest);
2620
+ }), _extends3.id = elementIdsRef.current.toggleButtonId, _extends3['aria-haspopup'] = 'listbox', _extends3['aria-expanded'] = isOpen, _extends3['aria-labelledby'] = elementIdsRef.current.labelId + " " + elementIdsRef.current.toggleButtonId, _extends3), rest);
2604
2621
 
2605
2622
  if (!rest.disabled) {
2606
2623
  toggleProps.onClick = callAllEventHandlers(onClick, toggleButtonHandleClick);
@@ -2611,8 +2628,8 @@ function useSelect(userProps) {
2611
2628
  },
2612
2629
  getLabelProps: function getLabelProps(labelProps) {
2613
2630
  return _extends({
2614
- id: elementIds.current.labelId,
2615
- htmlFor: elementIds.current.toggleButtonId
2631
+ id: elementIdsRef.current.labelId,
2632
+ htmlFor: elementIdsRef.current.toggleButtonId
2616
2633
  }, labelProps);
2617
2634
  },
2618
2635
  getMenuProps: function getMenuProps(_temp) {
@@ -2629,8 +2646,8 @@ function useSelect(userProps) {
2629
2646
 
2630
2647
  return _extends((_extends2 = {}, _extends2[refKey] = handleRefs(ref, function (menuNode) {
2631
2648
  menuRef.current = menuNode;
2632
- }), _extends2.id = elementIds.current.menuId, _extends2.role = 'listbox', _extends2['aria-labelledby'] = elementIds.current.labelId, _extends2.tabIndex = -1, _extends2), isOpen && highlightedIndex > -1 && {
2633
- 'aria-activedescendant': elementIds.current.getItemId(highlightedIndex)
2649
+ }), _extends2.id = elementIdsRef.current.menuId, _extends2.role = 'listbox', _extends2['aria-labelledby'] = elementIdsRef.current.labelId, _extends2.tabIndex = -1, _extends2), isOpen && highlightedIndex > -1 && {
2650
+ 'aria-activedescendant': elementIdsRef.current.getItemId(highlightedIndex)
2634
2651
  }, {
2635
2652
  onMouseLeave: callAllEventHandlers(onMouseLeave, menuHandleMouseLeave),
2636
2653
  onKeyDown: callAllEventHandlers(onKeyDown, menuHandleKeyDown),
@@ -2654,7 +2671,7 @@ function useSelect(userProps) {
2654
2671
  var itemProps = _extends({
2655
2672
  role: 'option',
2656
2673
  'aria-selected': "" + (itemIndex === highlightedIndex),
2657
- id: elementIds.current.getItemId(itemIndex)
2674
+ id: elementIdsRef.current.getItemId(itemIndex)
2658
2675
  }, rest);
2659
2676
 
2660
2677
  if (!rest.disabled) {
@@ -2267,25 +2267,26 @@ function useSelect(userProps) {
2267
2267
 
2268
2268
  var toggleButtonRef = useRef(null);
2269
2269
  var menuRef = useRef(null);
2270
- var isInitialMount = useRef(true);
2271
- var shouldScroll = useRef(true);
2272
- var clearTimeout = useRef(null);
2273
- var mouseAndTouchTrackers = useRef({
2270
+ var isInitialMountRef = useRef(true);
2271
+ var shouldScrollRef = useRef(true);
2272
+ var shouldBlurRef = useRef(true);
2273
+ var clearTimeoutRef = useRef(null);
2274
+ var mouseAndTouchTrackersRef = useRef({
2274
2275
  isMouseDown: false,
2275
2276
  isTouchMove: false
2276
2277
  });
2277
- var elementIds = useRef(getElementIds(props));
2278
+ var elementIdsRef = useRef(getElementIds(props));
2278
2279
  var previousResultCountRef = useRef(); // Some utils.
2279
2280
 
2280
2281
  var getItemNodeFromIndex = function (index) {
2281
- return environment.document.getElementById(elementIds.current.getItemId(index));
2282
+ return environment.document.getElementById(elementIdsRef.current.getItemId(index));
2282
2283
  }; // Effects.
2283
2284
 
2284
2285
  /* Sets a11y status message on changes in state. */
2285
2286
 
2286
2287
 
2287
2288
  useEffect(function () {
2288
- if (isInitialMount.current) {
2289
+ if (isInitialMountRef.current) {
2289
2290
  return;
2290
2291
  }
2291
2292
 
@@ -2306,7 +2307,7 @@ function useSelect(userProps) {
2306
2307
  /* Sets a11y status message on changes in selectedItem. */
2307
2308
 
2308
2309
  useEffect(function () {
2309
- if (isInitialMount.current) {
2310
+ if (isInitialMountRef.current) {
2310
2311
  return;
2311
2312
  }
2312
2313
 
@@ -2328,8 +2329,8 @@ function useSelect(userProps) {
2328
2329
 
2329
2330
  useEffect(function () {
2330
2331
  // init the clean function here as we need access to dispatch.
2331
- if (isInitialMount.current) {
2332
- clearTimeout.current = debounce(function (outerDispatch) {
2332
+ if (isInitialMountRef.current) {
2333
+ clearTimeoutRef.current = debounce(function (outerDispatch) {
2333
2334
  outerDispatch({
2334
2335
  type: FunctionSetInputValue,
2335
2336
  inputValue: ''
@@ -2341,13 +2342,13 @@ function useSelect(userProps) {
2341
2342
  return;
2342
2343
  }
2343
2344
 
2344
- clearTimeout.current(dispatch); // eslint-disable-next-line react-hooks/exhaustive-deps
2345
+ clearTimeoutRef.current(dispatch); // eslint-disable-next-line react-hooks/exhaustive-deps
2345
2346
  }, [inputValue]);
2346
2347
  /* Controls the focus on the menu or the toggle button. */
2347
2348
 
2348
2349
  useEffect(function () {
2349
2350
  // Don't focus menu on first render.
2350
- if (isInitialMount.current) {
2351
+ if (isInitialMountRef.current) {
2351
2352
  // Unless it was initialised as open.
2352
2353
  if ((initialIsOpen || defaultIsOpen || isOpen) && menuRef.current) {
2353
2354
  menuRef.current.focus();
@@ -2355,13 +2356,23 @@ function useSelect(userProps) {
2355
2356
 
2356
2357
  return;
2357
2358
  } // Focus menu on open.
2358
- // istanbul ignore next
2359
2359
 
2360
2360
 
2361
- if (isOpen && menuRef.current) {
2362
- menuRef.current.focus(); // Focus toggleButton on close.
2363
- } else if (environment.document.activeElement === menuRef.current && toggleButtonRef.current) {
2364
- toggleButtonRef.current.focus();
2361
+ if (isOpen) {
2362
+ // istanbul ignore else
2363
+ if (menuRef.current) {
2364
+ menuRef.current.focus();
2365
+ return;
2366
+ }
2367
+ } // Focus toggleButton on close, but on if was closed with (Shift+)Tab.
2368
+
2369
+
2370
+ if (environment.document.activeElement === menuRef.current) {
2371
+ // istanbul ignore else
2372
+ if (toggleButtonRef.current) {
2373
+ shouldBlurRef.current = false;
2374
+ toggleButtonRef.current.focus();
2375
+ }
2365
2376
  } // eslint-disable-next-line react-hooks/exhaustive-deps
2366
2377
 
2367
2378
  }, [isOpen]);
@@ -2372,15 +2383,15 @@ function useSelect(userProps) {
2372
2383
  return;
2373
2384
  }
2374
2385
 
2375
- if (shouldScroll.current === false) {
2376
- shouldScroll.current = true;
2386
+ if (shouldScrollRef.current === false) {
2387
+ shouldScrollRef.current = true;
2377
2388
  } else {
2378
2389
  scrollIntoView(getItemNodeFromIndex(highlightedIndex), menuRef.current);
2379
2390
  } // eslint-disable-next-line react-hooks/exhaustive-deps
2380
2391
 
2381
2392
  }, [highlightedIndex]);
2382
2393
  useEffect(function () {
2383
- if (isInitialMount.current) {
2394
+ if (isInitialMountRef.current) {
2384
2395
  return;
2385
2396
  }
2386
2397
 
@@ -2389,7 +2400,7 @@ function useSelect(userProps) {
2389
2400
  /* Make initial ref false. */
2390
2401
 
2391
2402
  useEffect(function () {
2392
- isInitialMount.current = false;
2403
+ isInitialMountRef.current = false;
2393
2404
  }, []);
2394
2405
  /* Add mouse/touch events to document. */
2395
2406
 
@@ -2397,11 +2408,11 @@ function useSelect(userProps) {
2397
2408
  // The same strategy for checking if a click occurred inside or outside downsift
2398
2409
  // as in downshift.js.
2399
2410
  var onMouseDown = function () {
2400
- mouseAndTouchTrackers.current.isMouseDown = true;
2411
+ mouseAndTouchTrackersRef.current.isMouseDown = true;
2401
2412
  };
2402
2413
 
2403
2414
  var onMouseUp = function (event) {
2404
- mouseAndTouchTrackers.current.isMouseDown = false;
2415
+ mouseAndTouchTrackersRef.current.isMouseDown = false;
2405
2416
 
2406
2417
  if (isOpen && !targetWithinDownshift(event.target, [toggleButtonRef.current, menuRef.current], environment.document)) {
2407
2418
  dispatch({
@@ -2411,15 +2422,15 @@ function useSelect(userProps) {
2411
2422
  };
2412
2423
 
2413
2424
  var onTouchStart = function () {
2414
- mouseAndTouchTrackers.current.isTouchMove = false;
2425
+ mouseAndTouchTrackersRef.current.isTouchMove = false;
2415
2426
  };
2416
2427
 
2417
2428
  var onTouchMove = function () {
2418
- mouseAndTouchTrackers.current.isTouchMove = true;
2429
+ mouseAndTouchTrackersRef.current.isTouchMove = true;
2419
2430
  };
2420
2431
 
2421
2432
  var onTouchEnd = function (event) {
2422
- if (isOpen && !mouseAndTouchTrackers.current.isTouchMove && !targetWithinDownshift(event.target, [toggleButtonRef.current, menuRef.current], environment.document, false)) {
2433
+ if (isOpen && !mouseAndTouchTrackersRef.current.isTouchMove && !targetWithinDownshift(event.target, [toggleButtonRef.current, menuRef.current], environment.document, false)) {
2423
2434
  dispatch({
2424
2435
  type: MenuBlur
2425
2436
  });
@@ -2523,7 +2534,13 @@ function useSelect(userProps) {
2523
2534
  };
2524
2535
 
2525
2536
  var menuHandleBlur = function () {
2526
- var shouldBlur = !mouseAndTouchTrackers.current.isMouseDown;
2537
+ // if the blur was a result of selection, we don't trigger this action.
2538
+ if (shouldBlurRef.current === false) {
2539
+ shouldBlurRef.current = true;
2540
+ return;
2541
+ }
2542
+
2543
+ var shouldBlur = !mouseAndTouchTrackersRef.current.isMouseDown;
2527
2544
  /* istanbul ignore else */
2528
2545
 
2529
2546
  if (shouldBlur) {
@@ -2564,7 +2581,7 @@ function useSelect(userProps) {
2564
2581
  return;
2565
2582
  }
2566
2583
 
2567
- shouldScroll.current = false;
2584
+ shouldScrollRef.current = false;
2568
2585
  dispatch({
2569
2586
  type: ItemMouseMove,
2570
2587
  index: index
@@ -2594,7 +2611,7 @@ function useSelect(userProps) {
2594
2611
 
2595
2612
  var toggleProps = _extends((_extends3 = {}, _extends3[refKey] = handleRefs(ref, function (toggleButtonNode) {
2596
2613
  toggleButtonRef.current = toggleButtonNode;
2597
- }), _extends3.id = elementIds.current.toggleButtonId, _extends3['aria-haspopup'] = 'listbox', _extends3['aria-expanded'] = isOpen, _extends3['aria-labelledby'] = elementIds.current.labelId + " " + elementIds.current.toggleButtonId, _extends3), rest);
2614
+ }), _extends3.id = elementIdsRef.current.toggleButtonId, _extends3['aria-haspopup'] = 'listbox', _extends3['aria-expanded'] = isOpen, _extends3['aria-labelledby'] = elementIdsRef.current.labelId + " " + elementIdsRef.current.toggleButtonId, _extends3), rest);
2598
2615
 
2599
2616
  if (!rest.disabled) {
2600
2617
  toggleProps.onClick = callAllEventHandlers(onClick, toggleButtonHandleClick);
@@ -2605,8 +2622,8 @@ function useSelect(userProps) {
2605
2622
  },
2606
2623
  getLabelProps: function getLabelProps(labelProps) {
2607
2624
  return _extends({
2608
- id: elementIds.current.labelId,
2609
- htmlFor: elementIds.current.toggleButtonId
2625
+ id: elementIdsRef.current.labelId,
2626
+ htmlFor: elementIdsRef.current.toggleButtonId
2610
2627
  }, labelProps);
2611
2628
  },
2612
2629
  getMenuProps: function getMenuProps(_temp) {
@@ -2623,8 +2640,8 @@ function useSelect(userProps) {
2623
2640
 
2624
2641
  return _extends((_extends2 = {}, _extends2[refKey] = handleRefs(ref, function (menuNode) {
2625
2642
  menuRef.current = menuNode;
2626
- }), _extends2.id = elementIds.current.menuId, _extends2.role = 'listbox', _extends2['aria-labelledby'] = elementIds.current.labelId, _extends2.tabIndex = -1, _extends2), isOpen && highlightedIndex > -1 && {
2627
- 'aria-activedescendant': elementIds.current.getItemId(highlightedIndex)
2643
+ }), _extends2.id = elementIdsRef.current.menuId, _extends2.role = 'listbox', _extends2['aria-labelledby'] = elementIdsRef.current.labelId, _extends2.tabIndex = -1, _extends2), isOpen && highlightedIndex > -1 && {
2644
+ 'aria-activedescendant': elementIdsRef.current.getItemId(highlightedIndex)
2628
2645
  }, {
2629
2646
  onMouseLeave: callAllEventHandlers(onMouseLeave, menuHandleMouseLeave),
2630
2647
  onKeyDown: callAllEventHandlers(onKeyDown, menuHandleKeyDown),
@@ -2648,7 +2665,7 @@ function useSelect(userProps) {
2648
2665
  var itemProps = _extends({
2649
2666
  role: 'option',
2650
2667
  'aria-selected': "" + (itemIndex === highlightedIndex),
2651
- id: elementIds.current.getItemId(itemIndex)
2668
+ id: elementIdsRef.current.getItemId(itemIndex)
2652
2669
  }, rest);
2653
2670
 
2654
2671
  if (!rest.disabled) {
@@ -2206,25 +2206,26 @@ function useSelect(userProps) {
2206
2206
 
2207
2207
  var toggleButtonRef = react.useRef(null);
2208
2208
  var menuRef = react.useRef(null);
2209
- var isInitialMount = react.useRef(true);
2210
- var shouldScroll = react.useRef(true);
2211
- var clearTimeout = react.useRef(null);
2212
- var mouseAndTouchTrackers = react.useRef({
2209
+ var isInitialMountRef = react.useRef(true);
2210
+ var shouldScrollRef = react.useRef(true);
2211
+ var shouldBlurRef = react.useRef(true);
2212
+ var clearTimeoutRef = react.useRef(null);
2213
+ var mouseAndTouchTrackersRef = react.useRef({
2213
2214
  isMouseDown: false,
2214
2215
  isTouchMove: false
2215
2216
  });
2216
- var elementIds = react.useRef(getElementIds(props));
2217
+ var elementIdsRef = react.useRef(getElementIds(props));
2217
2218
  var previousResultCountRef = react.useRef(); // Some utils.
2218
2219
 
2219
2220
  var getItemNodeFromIndex = function (index) {
2220
- return environment.document.getElementById(elementIds.current.getItemId(index));
2221
+ return environment.document.getElementById(elementIdsRef.current.getItemId(index));
2221
2222
  }; // Effects.
2222
2223
 
2223
2224
  /* Sets a11y status message on changes in state. */
2224
2225
 
2225
2226
 
2226
2227
  react.useEffect(function () {
2227
- if (isInitialMount.current) {
2228
+ if (isInitialMountRef.current) {
2228
2229
  return;
2229
2230
  }
2230
2231
 
@@ -2245,7 +2246,7 @@ function useSelect(userProps) {
2245
2246
  /* Sets a11y status message on changes in selectedItem. */
2246
2247
 
2247
2248
  react.useEffect(function () {
2248
- if (isInitialMount.current) {
2249
+ if (isInitialMountRef.current) {
2249
2250
  return;
2250
2251
  }
2251
2252
 
@@ -2267,8 +2268,8 @@ function useSelect(userProps) {
2267
2268
 
2268
2269
  react.useEffect(function () {
2269
2270
  // init the clean function here as we need access to dispatch.
2270
- if (isInitialMount.current) {
2271
- clearTimeout.current = debounce(function (outerDispatch) {
2271
+ if (isInitialMountRef.current) {
2272
+ clearTimeoutRef.current = debounce(function (outerDispatch) {
2272
2273
  outerDispatch({
2273
2274
  type: FunctionSetInputValue,
2274
2275
  inputValue: ''
@@ -2280,13 +2281,13 @@ function useSelect(userProps) {
2280
2281
  return;
2281
2282
  }
2282
2283
 
2283
- clearTimeout.current(dispatch); // eslint-disable-next-line react-hooks/exhaustive-deps
2284
+ clearTimeoutRef.current(dispatch); // eslint-disable-next-line react-hooks/exhaustive-deps
2284
2285
  }, [inputValue]);
2285
2286
  /* Controls the focus on the menu or the toggle button. */
2286
2287
 
2287
2288
  react.useEffect(function () {
2288
2289
  // Don't focus menu on first render.
2289
- if (isInitialMount.current) {
2290
+ if (isInitialMountRef.current) {
2290
2291
  // Unless it was initialised as open.
2291
2292
  if ((initialIsOpen || defaultIsOpen || isOpen) && menuRef.current) {
2292
2293
  menuRef.current.focus();
@@ -2294,13 +2295,23 @@ function useSelect(userProps) {
2294
2295
 
2295
2296
  return;
2296
2297
  } // Focus menu on open.
2297
- // istanbul ignore next
2298
2298
 
2299
2299
 
2300
- if (isOpen && menuRef.current) {
2301
- menuRef.current.focus(); // Focus toggleButton on close.
2302
- } else if (environment.document.activeElement === menuRef.current && toggleButtonRef.current) {
2303
- toggleButtonRef.current.focus();
2300
+ if (isOpen) {
2301
+ // istanbul ignore else
2302
+ if (menuRef.current) {
2303
+ menuRef.current.focus();
2304
+ return;
2305
+ }
2306
+ } // Focus toggleButton on close, but on if was closed with (Shift+)Tab.
2307
+
2308
+
2309
+ if (environment.document.activeElement === menuRef.current) {
2310
+ // istanbul ignore else
2311
+ if (toggleButtonRef.current) {
2312
+ shouldBlurRef.current = false;
2313
+ toggleButtonRef.current.focus();
2314
+ }
2304
2315
  } // eslint-disable-next-line react-hooks/exhaustive-deps
2305
2316
 
2306
2317
  }, [isOpen]);
@@ -2311,15 +2322,15 @@ function useSelect(userProps) {
2311
2322
  return;
2312
2323
  }
2313
2324
 
2314
- if (shouldScroll.current === false) {
2315
- shouldScroll.current = true;
2325
+ if (shouldScrollRef.current === false) {
2326
+ shouldScrollRef.current = true;
2316
2327
  } else {
2317
2328
  scrollIntoView(getItemNodeFromIndex(highlightedIndex), menuRef.current);
2318
2329
  } // eslint-disable-next-line react-hooks/exhaustive-deps
2319
2330
 
2320
2331
  }, [highlightedIndex]);
2321
2332
  react.useEffect(function () {
2322
- if (isInitialMount.current) {
2333
+ if (isInitialMountRef.current) {
2323
2334
  return;
2324
2335
  }
2325
2336
 
@@ -2328,7 +2339,7 @@ function useSelect(userProps) {
2328
2339
  /* Make initial ref false. */
2329
2340
 
2330
2341
  react.useEffect(function () {
2331
- isInitialMount.current = false;
2342
+ isInitialMountRef.current = false;
2332
2343
  }, []);
2333
2344
  /* Add mouse/touch events to document. */
2334
2345
 
@@ -2336,11 +2347,11 @@ function useSelect(userProps) {
2336
2347
  // The same strategy for checking if a click occurred inside or outside downsift
2337
2348
  // as in downshift.js.
2338
2349
  var onMouseDown = function () {
2339
- mouseAndTouchTrackers.current.isMouseDown = true;
2350
+ mouseAndTouchTrackersRef.current.isMouseDown = true;
2340
2351
  };
2341
2352
 
2342
2353
  var onMouseUp = function (event) {
2343
- mouseAndTouchTrackers.current.isMouseDown = false;
2354
+ mouseAndTouchTrackersRef.current.isMouseDown = false;
2344
2355
 
2345
2356
  if (isOpen && !targetWithinDownshift(event.target, [toggleButtonRef.current, menuRef.current], environment.document)) {
2346
2357
  dispatch({
@@ -2350,15 +2361,15 @@ function useSelect(userProps) {
2350
2361
  };
2351
2362
 
2352
2363
  var onTouchStart = function () {
2353
- mouseAndTouchTrackers.current.isTouchMove = false;
2364
+ mouseAndTouchTrackersRef.current.isTouchMove = false;
2354
2365
  };
2355
2366
 
2356
2367
  var onTouchMove = function () {
2357
- mouseAndTouchTrackers.current.isTouchMove = true;
2368
+ mouseAndTouchTrackersRef.current.isTouchMove = true;
2358
2369
  };
2359
2370
 
2360
2371
  var onTouchEnd = function (event) {
2361
- if (isOpen && !mouseAndTouchTrackers.current.isTouchMove && !targetWithinDownshift(event.target, [toggleButtonRef.current, menuRef.current], environment.document, false)) {
2372
+ if (isOpen && !mouseAndTouchTrackersRef.current.isTouchMove && !targetWithinDownshift(event.target, [toggleButtonRef.current, menuRef.current], environment.document, false)) {
2362
2373
  dispatch({
2363
2374
  type: MenuBlur
2364
2375
  });
@@ -2462,7 +2473,13 @@ function useSelect(userProps) {
2462
2473
  };
2463
2474
 
2464
2475
  var menuHandleBlur = function () {
2465
- var shouldBlur = !mouseAndTouchTrackers.current.isMouseDown;
2476
+ // if the blur was a result of selection, we don't trigger this action.
2477
+ if (shouldBlurRef.current === false) {
2478
+ shouldBlurRef.current = true;
2479
+ return;
2480
+ }
2481
+
2482
+ var shouldBlur = !mouseAndTouchTrackersRef.current.isMouseDown;
2466
2483
  /* istanbul ignore else */
2467
2484
 
2468
2485
  if (shouldBlur) {
@@ -2503,7 +2520,7 @@ function useSelect(userProps) {
2503
2520
  return;
2504
2521
  }
2505
2522
 
2506
- shouldScroll.current = false;
2523
+ shouldScrollRef.current = false;
2507
2524
  dispatch({
2508
2525
  type: ItemMouseMove,
2509
2526
  index: index
@@ -2533,7 +2550,7 @@ function useSelect(userProps) {
2533
2550
 
2534
2551
  var toggleProps = _extends((_extends3 = {}, _extends3[refKey] = handleRefs(ref, function (toggleButtonNode) {
2535
2552
  toggleButtonRef.current = toggleButtonNode;
2536
- }), _extends3.id = elementIds.current.toggleButtonId, _extends3['aria-haspopup'] = 'listbox', _extends3['aria-expanded'] = isOpen, _extends3['aria-labelledby'] = elementIds.current.labelId + " " + elementIds.current.toggleButtonId, _extends3), rest);
2553
+ }), _extends3.id = elementIdsRef.current.toggleButtonId, _extends3['aria-haspopup'] = 'listbox', _extends3['aria-expanded'] = isOpen, _extends3['aria-labelledby'] = elementIdsRef.current.labelId + " " + elementIdsRef.current.toggleButtonId, _extends3), rest);
2537
2554
 
2538
2555
  if (!rest.disabled) {
2539
2556
  toggleProps.onClick = callAllEventHandlers(onClick, toggleButtonHandleClick);
@@ -2544,8 +2561,8 @@ function useSelect(userProps) {
2544
2561
  },
2545
2562
  getLabelProps: function getLabelProps(labelProps) {
2546
2563
  return _extends({
2547
- id: elementIds.current.labelId,
2548
- htmlFor: elementIds.current.toggleButtonId
2564
+ id: elementIdsRef.current.labelId,
2565
+ htmlFor: elementIdsRef.current.toggleButtonId
2549
2566
  }, labelProps);
2550
2567
  },
2551
2568
  getMenuProps: function getMenuProps(_temp) {
@@ -2562,8 +2579,8 @@ function useSelect(userProps) {
2562
2579
 
2563
2580
  return _extends((_extends2 = {}, _extends2[refKey] = handleRefs(ref, function (menuNode) {
2564
2581
  menuRef.current = menuNode;
2565
- }), _extends2.id = elementIds.current.menuId, _extends2.role = 'listbox', _extends2['aria-labelledby'] = elementIds.current.labelId, _extends2.tabIndex = -1, _extends2), isOpen && highlightedIndex > -1 && {
2566
- 'aria-activedescendant': elementIds.current.getItemId(highlightedIndex)
2582
+ }), _extends2.id = elementIdsRef.current.menuId, _extends2.role = 'listbox', _extends2['aria-labelledby'] = elementIdsRef.current.labelId, _extends2.tabIndex = -1, _extends2), isOpen && highlightedIndex > -1 && {
2583
+ 'aria-activedescendant': elementIdsRef.current.getItemId(highlightedIndex)
2567
2584
  }, {
2568
2585
  onMouseLeave: callAllEventHandlers(onMouseLeave, menuHandleMouseLeave),
2569
2586
  onKeyDown: callAllEventHandlers(onKeyDown, menuHandleKeyDown),
@@ -2587,7 +2604,7 @@ function useSelect(userProps) {
2587
2604
  var itemProps = _extends({
2588
2605
  role: 'option',
2589
2606
  'aria-selected': "" + (itemIndex === highlightedIndex),
2590
- id: elementIds.current.getItemId(itemIndex)
2607
+ id: elementIdsRef.current.getItemId(itemIndex)
2591
2608
  }, rest);
2592
2609
 
2593
2610
  if (!rest.disabled) {