@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/lib/DropList.js CHANGED
@@ -688,9 +688,8 @@ class DropList {
688
688
  }
689
689
 
690
690
  if (p.multi) {
691
- if (p.focusItemIndex >= 0) {
691
+ if (this.hasFocusedItem()) {
692
692
  this.toggleFocusedItem();
693
- event.preventDefault();
694
693
  }
695
694
  } else {
696
695
  this._setSingleSelectedItemEl(p.focusItemEl);
@@ -2480,8 +2479,6 @@ class DropList {
2480
2479
  if (this[DestroyedSymbol])
2481
2480
  return;
2482
2481
 
2483
- this.toggleFocusedItem();
2484
-
2485
2482
  if (document.activeElement !== this.el && p.focusItemEl) {
2486
2483
  clearTimeout(p.blurTimer);
2487
2484
  }
package/lib/SelectBox.js CHANGED
@@ -179,6 +179,42 @@ const defaultMultiPlaceholderFormatter = (items, labelProp) => {
179
179
  return title;
180
180
  };
181
181
 
182
+
183
+ function getFocusState(element) {
184
+ if (!element)
185
+ return null;
186
+
187
+ const state = { element };
188
+
189
+ if (typeof element.selectionStart === 'number' &&
190
+ typeof element.selectionEnd === 'number') {
191
+ state.selectionStart = element.selectionStart;
192
+ state.selectionEnd = element.selectionEnd;
193
+ state.selectionDirection = element.selectionDirection;
194
+ }
195
+
196
+ return state;
197
+ }
198
+
199
+ function restoreFocusState(state) {
200
+ if (!state?.element || !document.body.contains(state.element))
201
+ return false;
202
+
203
+ state.element.focus();
204
+
205
+ if (typeof state.selectionStart === 'number' &&
206
+ typeof state.selectionEnd === 'number' &&
207
+ typeof state.element.setSelectionRange === 'function') {
208
+ state.element.setSelectionRange(
209
+ state.selectionStart,
210
+ state.selectionEnd,
211
+ state.selectionDirection ?? 'none',
212
+ );
213
+ }
214
+
215
+ return true;
216
+ }
217
+
182
218
  /*
183
219
  Emits the following events:
184
220
  ---------------------------
@@ -1443,6 +1479,8 @@ class SelectBox {
1443
1479
 
1444
1480
  this._trigger('open:before', { list: p.dropList });
1445
1481
 
1482
+ p.lastActiveElement = document.activeElement === document.body ? null : getFocusState(document.activeElement);
1483
+
1446
1484
  // Propagate direction to droplist
1447
1485
  p.dropList.setDirection(getComputedStyle(p.el).direction);
1448
1486
 
@@ -1477,6 +1515,11 @@ class SelectBox {
1477
1515
 
1478
1516
  p.dropList.hide();
1479
1517
 
1518
+ if (p.lastActiveElement) {
1519
+ restoreFocusState(p.lastActiveElement);
1520
+ p.lastActiveElement = null;
1521
+ }
1522
+
1480
1523
  return this;
1481
1524
  }
1482
1525
 
@@ -2069,9 +2112,10 @@ class SelectBox {
2069
2112
 
2070
2113
  this.closeList();
2071
2114
 
2072
- if (p.blurOnSingleSelection === 'touch' && hasTouchCapability ||
2073
- p.blurOnSingleSelection !== 'touch' && p.blurOnSingleSelection) {
2074
- p.input && p.input.blur();
2115
+ if ((p.blurOnSingleSelection === 'touch' && hasTouchCapability ||
2116
+ p.blurOnSingleSelection !== 'touch' && p.blurOnSingleSelection) &&
2117
+ p.input && document.activeElement === p.input) {
2118
+ p.input.blur();
2075
2119
  }
2076
2120
  }
2077
2121
  break;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@danielgindi/selectbox",
3
- "version": "2.0.47",
3
+ "version": "2.0.49",
4
4
  "description": "A collection of dom utilities. So you can work natively with the dom without dom frameworks.",
5
5
  "main": "dist/lib.cjs.min.js",
6
6
  "module": "lib/index.js",