@danielgindi/selectbox 2.0.47 → 2.0.49

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.
package/dist/lib.es6.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * @danielgindi/selectbox 2.0.47
2
+ * @danielgindi/selectbox 2.0.49
3
3
  * git://github.com/danielgindi/selectbox.git
4
4
  */
5
5
  import { createElement, closestUntil, setElementAttrs, next, prev } from '@danielgindi/dom-utils/lib/Dom';
@@ -716,9 +716,8 @@ class DropList {
716
716
  }
717
717
 
718
718
  if (p.multi) {
719
- if (p.focusItemIndex >= 0) {
719
+ if (this.hasFocusedItem()) {
720
720
  this.toggleFocusedItem();
721
- event.preventDefault();
722
721
  }
723
722
  } else {
724
723
  this._setSingleSelectedItemEl(p.focusItemEl);
@@ -2508,8 +2507,6 @@ class DropList {
2508
2507
  if (this[DestroyedSymbol$1])
2509
2508
  return;
2510
2509
 
2511
- this.toggleFocusedItem();
2512
-
2513
2510
  if (document.activeElement !== this.el && p.focusItemEl) {
2514
2511
  clearTimeout(p.blurTimer);
2515
2512
  }
@@ -3499,6 +3496,42 @@ const defaultMultiPlaceholderFormatter = (items, labelProp) => {
3499
3496
  return title;
3500
3497
  };
3501
3498
 
3499
+
3500
+ function getFocusState(element) {
3501
+ if (!element)
3502
+ return null;
3503
+
3504
+ const state = { element };
3505
+
3506
+ if (typeof element.selectionStart === 'number' &&
3507
+ typeof element.selectionEnd === 'number') {
3508
+ state.selectionStart = element.selectionStart;
3509
+ state.selectionEnd = element.selectionEnd;
3510
+ state.selectionDirection = element.selectionDirection;
3511
+ }
3512
+
3513
+ return state;
3514
+ }
3515
+
3516
+ function restoreFocusState(state) {
3517
+ if (!state?.element || !document.body.contains(state.element))
3518
+ return false;
3519
+
3520
+ state.element.focus();
3521
+
3522
+ if (typeof state.selectionStart === 'number' &&
3523
+ typeof state.selectionEnd === 'number' &&
3524
+ typeof state.element.setSelectionRange === 'function') {
3525
+ state.element.setSelectionRange(
3526
+ state.selectionStart,
3527
+ state.selectionEnd,
3528
+ state.selectionDirection ?? 'none',
3529
+ );
3530
+ }
3531
+
3532
+ return true;
3533
+ }
3534
+
3502
3535
  /*
3503
3536
  Emits the following events:
3504
3537
  ---------------------------
@@ -4763,6 +4796,8 @@ class SelectBox {
4763
4796
 
4764
4797
  this._trigger('open:before', { list: p.dropList });
4765
4798
 
4799
+ p.lastActiveElement = document.activeElement === document.body ? null : getFocusState(document.activeElement);
4800
+
4766
4801
  // Propagate direction to droplist
4767
4802
  p.dropList.setDirection(getComputedStyle(p.el).direction);
4768
4803
 
@@ -4797,6 +4832,11 @@ class SelectBox {
4797
4832
 
4798
4833
  p.dropList.hide();
4799
4834
 
4835
+ if (p.lastActiveElement) {
4836
+ restoreFocusState(p.lastActiveElement);
4837
+ p.lastActiveElement = null;
4838
+ }
4839
+
4800
4840
  return this;
4801
4841
  }
4802
4842
 
@@ -5389,9 +5429,10 @@ class SelectBox {
5389
5429
 
5390
5430
  this.closeList();
5391
5431
 
5392
- if (p.blurOnSingleSelection === 'touch' && hasTouchCapability ||
5393
- p.blurOnSingleSelection !== 'touch' && p.blurOnSingleSelection) {
5394
- p.input && p.input.blur();
5432
+ if ((p.blurOnSingleSelection === 'touch' && hasTouchCapability ||
5433
+ p.blurOnSingleSelection !== 'touch' && p.blurOnSingleSelection) &&
5434
+ p.input && document.activeElement === p.input) {
5435
+ p.input.blur();
5395
5436
  }
5396
5437
  }
5397
5438
  break;