@danielgindi/selectbox 2.0.62 → 2.0.64

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.62
2
+ * @danielgindi/selectbox 2.0.64
3
3
  * git://github.com/danielgindi/selectbox.git
4
4
  */
5
5
  import { closestUntil, createElement, next, prev, setElementAttrs } from "@danielgindi/dom-utils/lib/Dom";
@@ -164,6 +164,7 @@ var DropList = class DropList {
164
164
  singleSelectedItemEl: null,
165
165
  onDocumentMouseDown: null,
166
166
  lastKeyWasChar: false,
167
+ lastInputKeyAllowsNonTypeKeys: false,
167
168
  currentSubDropList: null,
168
169
  _isFocusingItem: false
169
170
  };
@@ -1731,6 +1732,100 @@ var DropList = class DropList {
1731
1732
  if (!p.searchable) this._keydownFreeType(event, !this.isVisible());
1732
1733
  }
1733
1734
  }
1735
+ /**
1736
+ * Handles a keydown event from an external input using the same navigation,
1737
+ * selection, and visibility behavior used by SelectBox.
1738
+ */
1739
+ handleInputKeydown(event, options = {}) {
1740
+ const p = this._p;
1741
+ const currentTarget = event.currentTarget;
1742
+ if (currentTarget?.readOnly) return;
1743
+ let suppressEnterSpaceToggle = false;
1744
+ const lastKeyAllowsNonTypeKeys = p.lastInputKeyAllowsNonTypeKeys;
1745
+ p.lastInputKeyAllowsNonTypeKeys = false;
1746
+ const target = event.target;
1747
+ const input = currentTarget?.value !== void 0 ? currentTarget : target;
1748
+ const hasInputText = options.hasInputText ?? !!input?.value?.length;
1749
+ switch (event.key) {
1750
+ case VALUE_PAGE_UP:
1751
+ case VALUE_PAGE_DOWN:
1752
+ case VALUE_UP:
1753
+ case VALUE_DOWN:
1754
+ case VALUE_HOME:
1755
+ case VALUE_END:
1756
+ if ((event.key === VALUE_HOME || event.key === VALUE_END) && hasInputText && !lastKeyAllowsNonTypeKeys) {
1757
+ this._keydownFreeType(event);
1758
+ break;
1759
+ }
1760
+ p.lastInputKeyAllowsNonTypeKeys = true;
1761
+ event.preventDefault();
1762
+ switch (event.key) {
1763
+ case VALUE_PAGE_UP:
1764
+ if (this.isVisible()) this.previousPage(event);
1765
+ break;
1766
+ case VALUE_PAGE_DOWN:
1767
+ if (this.isVisible()) this.nextPage(event);
1768
+ break;
1769
+ case VALUE_UP:
1770
+ if (this.isVisible()) this.previous(event);
1771
+ else options.movePrevious?.(event);
1772
+ break;
1773
+ case VALUE_DOWN:
1774
+ if (this.isVisible()) this.next(event);
1775
+ else options.moveNext?.(event);
1776
+ break;
1777
+ case VALUE_HOME:
1778
+ this.goToFirst(event);
1779
+ break;
1780
+ case VALUE_END: this.goToLast(event);
1781
+ }
1782
+ break;
1783
+ case VALUE_SPACE:
1784
+ if (lastKeyAllowsNonTypeKeys) {
1785
+ p.lastInputKeyAllowsNonTypeKeys = true;
1786
+ if (this.isVisible() && this.hasFocusedItem()) {
1787
+ suppressEnterSpaceToggle = true;
1788
+ if (p.multi) this.toggleFocusedItem(event);
1789
+ else this.triggerItemSelection(null, event);
1790
+ event.preventDefault();
1791
+ }
1792
+ }
1793
+ break;
1794
+ case VALUE_ENTER:
1795
+ if (this.isVisible() && this.hasFocusedItem()) {
1796
+ suppressEnterSpaceToggle = true;
1797
+ event.preventDefault();
1798
+ this.triggerItemSelection(null, event);
1799
+ }
1800
+ break;
1801
+ case VALUE_TAB:
1802
+ if (this.isVisible() && this.hasFocusedItem()) this.triggerItemSelection(null, event);
1803
+ break;
1804
+ case VALUE_ESCAPE:
1805
+ if (this.isVisible()) {
1806
+ this.hide(event);
1807
+ event.preventDefault();
1808
+ }
1809
+ break;
1810
+ default: if (this.isVisible()) this._keydownFreeType(event, false);
1811
+ else if (options.allowTypeToSelect) this._keydownFreeType(event, true);
1812
+ else {
1813
+ if (options.open) options.open(event);
1814
+ else this.show();
1815
+ setTimeout(() => {
1816
+ if (this[DestroyedSymbol$1]) return;
1817
+ this._keydownFreeType(event, false);
1818
+ });
1819
+ }
1820
+ }
1821
+ if (!suppressEnterSpaceToggle && (event.key === VALUE_ENTER || event.key === VALUE_SPACE && p.lastInputKeyAllowsNonTypeKeys && !p.multi && !this.hasFocusedItem() && !options.disabled && !options.readOnly)) {
1822
+ if (options.toggle) options.toggle(event);
1823
+ else if (this.isVisible()) this.hide(event);
1824
+ else this.show();
1825
+ event.preventDefault();
1826
+ event.stopPropagation();
1827
+ }
1828
+ }
1734
1829
  /** @internal */
1735
1830
  _keydownFreeType(evt, autoSelect) {
1736
1831
  const p = this._p;
@@ -2227,7 +2322,6 @@ var SelectBox = class {
2227
2322
  filterTerm: "",
2228
2323
  dropListVisible: false,
2229
2324
  lastActiveElement: null,
2230
- lastKeyAllowsNonTypeKeys: false,
2231
2325
  presenceInt: null
2232
2326
  };
2233
2327
  let el = o.el;
@@ -3343,89 +3437,16 @@ var SelectBox = class {
3343
3437
  let avoidToggleFromClick = false, currentTouchId = null;
3344
3438
  const keyEventsTarget = p.multi || p.searchable ? p.input : p.el;
3345
3439
  p.sink.add(keyEventsTarget, "keydown.dropdown", (evt) => {
3346
- if (evt.currentTarget.readOnly) return;
3347
- let suppressEnterSpaceToggle = false;
3348
- let lastKeyAllowsNonTypeKeys = p.lastKeyAllowsNonTypeKeys;
3349
- p.lastKeyAllowsNonTypeKeys = false;
3350
- let hasInputText = p.input && p.input.value.length > 0;
3351
- switch (evt.key) {
3352
- case VALUE_PAGE_UP:
3353
- case VALUE_PAGE_DOWN:
3354
- case VALUE_UP:
3355
- case VALUE_DOWN:
3356
- case VALUE_HOME:
3357
- case VALUE_END:
3358
- if ((evt.key === VALUE_HOME || evt.key === VALUE_END) && hasInputText && !lastKeyAllowsNonTypeKeys) {
3359
- dropList._keydownFreeType(evt);
3360
- break;
3361
- }
3362
- p.lastKeyAllowsNonTypeKeys = true;
3363
- evt.preventDefault();
3364
- switch (evt.key) {
3365
- case VALUE_PAGE_UP:
3366
- if (dropList.isVisible()) dropList.previousPage(evt);
3367
- break;
3368
- case VALUE_PAGE_DOWN:
3369
- if (dropList.isVisible()) dropList.nextPage(evt);
3370
- break;
3371
- case VALUE_UP:
3372
- if (dropList.isVisible()) dropList.previous(evt);
3373
- else this._movePrev();
3374
- break;
3375
- case VALUE_DOWN:
3376
- if (dropList.isVisible()) dropList.next(evt);
3377
- else this._moveNext();
3378
- break;
3379
- case VALUE_HOME:
3380
- dropList.goToFirst(evt);
3381
- break;
3382
- case VALUE_END: dropList.goToLast(evt);
3383
- }
3384
- break;
3385
- case VALUE_SPACE:
3386
- if (lastKeyAllowsNonTypeKeys) {
3387
- p.lastKeyAllowsNonTypeKeys = true;
3388
- if (dropList.isVisible() && dropList.hasFocusedItem()) {
3389
- suppressEnterSpaceToggle = true;
3390
- if (p.multi) dropList.toggleFocusedItem(evt);
3391
- else dropList.triggerItemSelection(null, evt);
3392
- evt.preventDefault();
3393
- }
3394
- }
3395
- break;
3396
- case VALUE_ENTER:
3397
- if (dropList.isVisible() && dropList.hasFocusedItem()) {
3398
- suppressEnterSpaceToggle = true;
3399
- evt.preventDefault();
3400
- dropList.triggerItemSelection(null, evt);
3401
- }
3402
- break;
3403
- case VALUE_TAB:
3404
- if (dropList.isVisible() && dropList.hasFocusedItem()) dropList.triggerItemSelection(null, evt);
3405
- break;
3406
- case VALUE_ESCAPE:
3407
- if (dropList.isVisible()) {
3408
- dropList.hide(evt);
3409
- evt.preventDefault();
3410
- }
3411
- break;
3412
- default: if (dropList.isVisible()) dropList._keydownFreeType(evt, false);
3413
- else if (p.allowTypeToSelect) dropList._keydownFreeType(evt, true);
3414
- else {
3415
- this.openList();
3416
- setTimeout(() => {
3417
- if (this[DestroyedSymbol]) return;
3418
- dropList._keydownFreeType(evt, false);
3419
- });
3420
- }
3421
- }
3422
- if (!suppressEnterSpaceToggle) {
3423
- if (evt.key === VALUE_ENTER || evt.key === VALUE_SPACE && p.lastKeyAllowsNonTypeKeys && !p.multi && !dropList.hasFocusedItem() && !p.disabled && !p.readOnly) {
3424
- this.toggleList();
3425
- evt.preventDefault();
3426
- evt.stopPropagation();
3427
- }
3428
- }
3440
+ dropList.handleInputKeydown(evt, {
3441
+ allowTypeToSelect: p.allowTypeToSelect,
3442
+ disabled: p.disabled,
3443
+ readOnly: p.readOnly,
3444
+ hasInputText: p.input?.value.length > 0,
3445
+ movePrevious: () => this._movePrev(),
3446
+ moveNext: () => this._moveNext(),
3447
+ open: () => this.openList(),
3448
+ toggle: () => this.toggleList()
3449
+ });
3429
3450
  });
3430
3451
  if (p.input) p.sink.add(p.input, "input.dropdown", () => {
3431
3452
  if (p.disabled || p.readOnly) return;