slim-select 4.0.6 → 4.1.0

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.
@@ -342,7 +342,7 @@ var N = class {
342
342
  if (this.state === "closed" || this.state === "closing" || this.handlers.beforeClose && this.handlers.beforeClose(e) === !1) return !1;
343
343
  this.cancelPending();
344
344
  let t = ++this.generation;
345
- return this.handlers.onClose && this.handlers.onClose(e), this.state = "closing", await this.waitForPhase("close", t), t === this.generation ? (this.state = "closed", this.handlers.afterClose && this.handlers.afterClose(), this.handlers.onCloseReady && this.handlers.onCloseReady(), !0) : !1;
345
+ return this.handlers.onClose && this.handlers.onClose(e), this.state = "closing", await this.waitForPhase("close", t), t === this.generation && (this.state = "closed", this.handlers.afterClose && this.handlers.afterClose(), this.handlers.onCloseReady && this.handlers.onCloseReady(), !0);
346
346
  }
347
347
  cancelPending() {
348
348
  this.generation++, this.animationAbort?.abort(), this.animationAbort = null, this.pendingTimer &&= (clearTimeout(this.pendingTimer), null);
@@ -408,16 +408,18 @@ var N = class {
408
408
  }
409
409
  validateDataArray(e) {
410
410
  if (!Array.isArray(e)) return /* @__PURE__ */ Error("Data must be an array");
411
- for (let t of e) if (t) if (t instanceof I || "label" in t) {
412
- if (!("label" in t)) return /* @__PURE__ */ Error("Optgroup must have a label");
413
- if ("options" in t && t.options) for (let e of t.options) {
414
- let t = this.validateOption(e);
415
- if (t) return t;
416
- }
417
- } else if (t instanceof F || "text" in t) {
418
- let e = this.validateOption(t);
419
- if (e) return e;
420
- } else return /* @__PURE__ */ Error("Data object must be a valid optgroup or option");
411
+ for (let t of e) if (t) {
412
+ if (t instanceof I || "label" in t) {
413
+ if (!("label" in t)) return /* @__PURE__ */ Error("Optgroup must have a label");
414
+ if ("options" in t && t.options) for (let e of t.options) {
415
+ let t = this.validateOption(e);
416
+ if (t) return t;
417
+ }
418
+ } else if (t instanceof F || "text" in t) {
419
+ let e = this.validateOption(t);
420
+ if (e) return e;
421
+ } else return /* @__PURE__ */ Error("Data object must be a valid optgroup or option");
422
+ }
421
423
  return null;
422
424
  }
423
425
  validateOption(e) {
@@ -557,10 +559,12 @@ var N = class {
557
559
  let a = [];
558
560
  if (i.options.forEach((i) => {
559
561
  (!e || e(i)) && (t ? a.push(n ? new F(i) : i) : r.push(n ? new F(i) : i));
560
- }), a.length > 0) if (n) {
561
- let e = new I(i);
562
- e.options = a, r.push(e);
563
- } else r.push(this.createOptgroupView(i, a));
562
+ }), a.length > 0) {
563
+ if (n) {
564
+ let e = new I(i);
565
+ e.options = a, r.push(e);
566
+ } else r.push(this.createOptgroupView(i, a));
567
+ }
564
568
  }
565
569
  i instanceof F && (!e || e(i)) && r.push(n ? new F(i) : i);
566
570
  }), r;
@@ -702,7 +706,7 @@ var N = class {
702
706
  close() {
703
707
  this.main.main.setAttribute("aria-expanded", "false"), this.main.arrow.path.setAttribute("d", this.classes.arrowClose);
704
708
  let e = this.modalSessionActive === !0;
705
- this.removeClasses(this.content.main, this.classes.contentOpen), e || (this.removeClasses(this.main.main, this.classes.dirAbove), this.removeClasses(this.main.main, this.classes.dirBelow)), e && this.modalElements && this.removeClasses(this.modalElements.overlay, this.classes.contentOpen), this.modalSessionActive = null, this.content.search.input.setAttribute("aria-hidden", "true"), this.main.main.removeAttribute("aria-activedescendant");
709
+ this.removeClasses(this.content.main, this.classes.contentOpen), e || (this.removeClasses(this.main.main, this.classes.dirAbove), this.removeClasses(this.main.main, this.classes.dirBelow)), e && this.modalElements && this.removeClasses(this.modalElements.overlay, this.classes.contentOpen), this.modalSessionActive = null, this.content.search.input.setAttribute("aria-hidden", "true"), this.main.main.removeAttribute("aria-activedescendant"), this.clearValueChipHighlight();
706
710
  }
707
711
  clearDirectionClasses() {
708
712
  this.removeClasses(this.main.main, this.classes.dirAbove), this.removeClasses(this.main.main, this.classes.dirBelow), this.removeClasses(this.content.main, this.classes.dirAbove), this.removeClasses(this.content.main, this.classes.dirBelow);
@@ -809,7 +813,8 @@ var N = class {
809
813
  if (t.length === 0) {
810
814
  this.main.values.innerHTML = this.placeholder().outerHTML;
811
815
  return;
812
- } else {
816
+ }
817
+ {
813
818
  let e = this.main.values.querySelector("." + this.classes.getFirst("placeholder"));
814
819
  e && e.remove();
815
820
  }
@@ -824,7 +829,8 @@ var N = class {
824
829
  let e = document.createElement("div");
825
830
  this.addClasses(e, this.classes.max), e.textContent = this.settings.maxValuesMessage.replace("{number}", t.length.toString()), this.main.values.innerHTML = e.outerHTML;
826
831
  return;
827
- } else {
832
+ }
833
+ {
828
834
  let e = this.main.values.querySelector("." + this.classes.getFirst("max"));
829
835
  e && e.remove();
830
836
  }
@@ -853,6 +859,38 @@ var N = class {
853
859
  let e = this.store.getSelectedOptions();
854
860
  return this.settings.keepOrder && (e = this.store.selectedOrderOptions(e)), e.slice(0, this.settings.minSelected).map((e) => e.id);
855
861
  }
862
+ getValueChips() {
863
+ return !this.settings.isMultiple || this.settings.multiString ? [] : Array.from(this.main.values.querySelectorAll("." + this.classes.getFirst("value"))).filter((e) => {
864
+ if (e.classList.contains(this.classes.getFirst("valueOut"))) return !1;
865
+ let t = e.querySelector("." + this.classes.getFirst("valueDelete"));
866
+ return !!t && !t.classList.contains(this.classes.getFirst("hide"));
867
+ });
868
+ }
869
+ getHighlightedValueChip() {
870
+ return this.getValueChips().find((e) => e.classList.contains(this.classes.getFirst("highlighted"))) ?? null;
871
+ }
872
+ clearValueChipHighlight() {
873
+ for (let e of Array.from(this.main.values.querySelectorAll("." + this.classes.getFirst("value")))) this.removeClasses(e, this.classes.highlighted);
874
+ }
875
+ highlightValueChip(e) {
876
+ this.clearValueChipHighlight(), e && this.addClasses(e, this.classes.highlighted);
877
+ }
878
+ navigateValueChips(e) {
879
+ let t = this.getValueChips();
880
+ if (t.length === 0) return !1;
881
+ let n = this.getHighlightedValueChip(), r = n ? t.indexOf(n) : -1;
882
+ return e === "left" ? (r === -1 ? this.highlightValueChip(t[t.length - 1]) : r > 0 && this.highlightValueChip(t[r - 1]), !0) : r !== -1 && (r < t.length - 1 ? this.highlightValueChip(t[r + 1]) : this.clearValueChipHighlight(), !0);
883
+ }
884
+ deleteValueChip(e = !1) {
885
+ let t = this.getValueChips();
886
+ if (t.length === 0) return !1;
887
+ let n = this.getHighlightedValueChip();
888
+ if (e && !n) return !1;
889
+ let r = n ?? t[t.length - 1], i = r.querySelector("." + this.classes.getFirst("valueDelete"));
890
+ if (!i) return !1;
891
+ let a = t.indexOf(r), o = n ? t[a - 1] ?? t[a + 1] ?? null : null;
892
+ return i.click(), o && o !== r && this.main.values.contains(o) ? this.highlightValueChip(o) : this.clearValueChipHighlight(), !0;
893
+ }
856
894
  updateMultipleValueDeleteVisibility() {
857
895
  if (!this.settings.isMultiple || this.settings.multiString) return;
858
896
  let e = !this.isAtMinSelected(), t = this.main.values.querySelectorAll("." + this.classes.getFirst("valueDelete"));
@@ -912,7 +950,8 @@ var N = class {
912
950
  if (this.settings.openPosition === "down") {
913
951
  this.moveContentBelow();
914
952
  return;
915
- } else if (this.settings.openPosition === "up") {
953
+ }
954
+ if (this.settings.openPosition === "up") {
916
955
  this.moveContentAbove();
917
956
  return;
918
957
  }
@@ -945,19 +984,27 @@ var N = class {
945
984
  main: e,
946
985
  input: t
947
986
  };
948
- if (this.settings.showSearch || (this.addClasses(e, this.classes.hide), t.readOnly = !0), t.type = "search", t.placeholder = this.settings.searchPlaceholder, t.tabIndex = -1, t.setAttribute("aria-label", this.settings.searchPlaceholder), t.setAttribute("aria-autocomplete", "list"), t.setAttribute("autocapitalize", "off"), t.setAttribute("autocomplete", "off"), t.setAttribute("autocorrect", "off"), t.setAttribute("aria-hidden", "true"), t.oninput = S((e) => {
949
- this.callbacks.search(e.target.value);
950
- }, 100), t.onkeydown = (e) => {
987
+ this.settings.showSearch || (this.addClasses(e, this.classes.hide), t.readOnly = !0), t.type = "search", t.placeholder = this.settings.searchPlaceholder, t.tabIndex = -1, t.setAttribute("aria-label", this.settings.searchPlaceholder), t.setAttribute("aria-autocomplete", "list"), t.setAttribute("autocapitalize", "off"), t.setAttribute("autocomplete", "off"), t.setAttribute("autocorrect", "off"), t.setAttribute("aria-hidden", "true");
988
+ let i = S(() => {
989
+ this.callbacks.search(t.value);
990
+ }, 100);
991
+ if (t.oninput = (e) => {
992
+ e.target.value !== "" && this.clearValueChipHighlight(), i();
993
+ }, t.onkeydown = (e) => {
951
994
  switch (e.key) {
952
995
  case "ArrowUp":
953
- case "ArrowDown": return e.key === "ArrowDown" ? this.highlight("down") : this.highlight("up"), !1;
996
+ case "ArrowDown": return this.clearValueChipHighlight(), e.key === "ArrowDown" ? this.highlight("down") : this.highlight("up"), !1;
997
+ case "ArrowLeft":
998
+ case "ArrowRight": return t.value !== "" || !this.navigateValueChips(e.key === "ArrowLeft" ? "left" : "right") || (e.preventDefault(), !1);
999
+ case "Backspace": return t.value !== "" || !this.deleteValueChip() || (e.preventDefault(), !1);
1000
+ case "Delete": return t.value !== "" || !this.getHighlightedValueChip() || !this.deleteValueChip(!0) || (e.preventDefault(), !1);
954
1001
  case "Tab": return this.requestClose("tab"), !0;
955
1002
  case "Escape": return this.requestClose("escape"), !1;
956
1003
  case "Enter":
957
- let t = this.content.list.querySelector("." + this.classes.getFirst("highlighted"));
958
- return t ? (t.click(), !1) : this.callbacks.addable ? (n.click(), !1) : !0;
1004
+ let r = this.content.list.querySelector("." + this.classes.getFirst("highlighted"));
1005
+ return r ? (r.click(), !1) : !this.callbacks.addable || (n.click(), !1);
959
1006
  }
960
- return !0;
1007
+ return e.key.length === 1 && this.clearValueChipHighlight(), !0;
961
1008
  }, e.appendChild(t), this.callbacks.addable) {
962
1009
  this.addClasses(n, this.classes.addable);
963
1010
  let t = document.createElementNS("http://www.w3.org/2000/svg", "svg");
@@ -983,7 +1030,7 @@ var N = class {
983
1030
  }, e);
984
1031
  }
985
1032
  }, r = this.callbacks.addable(t);
986
- r === !1 || r == null || (r instanceof Promise ? r.then((e) => {
1033
+ r !== !1 && r != null && (r instanceof Promise ? r.then((e) => {
987
1034
  typeof e == "string" ? n({
988
1035
  text: e,
989
1036
  value: e
@@ -1107,7 +1154,8 @@ var N = class {
1107
1154
  });
1108
1155
  this.callbacks.setSelected(e, !0);
1109
1156
  return;
1110
- } else {
1157
+ }
1158
+ {
1111
1159
  let e = n.options.map((e) => e.id).filter((e) => e !== void 0), r = t.concat(e);
1112
1160
  for (let e of n.options) e.id && !this.store.getOptionByID(e.id) && this.callbacks.addOption(new F(e));
1113
1161
  this.callbacks.setSelected(r, !0);
@@ -1551,6 +1599,11 @@ var B = class {
1551
1599
  getSelectedValues() {
1552
1600
  return this.getSelectedOptions().map((e) => e.value);
1553
1601
  }
1602
+ hasOptionValues(e) {
1603
+ if (e.length === 0) return !0;
1604
+ let t = new Set(Array.from(this.select.options).map((e) => e.value));
1605
+ return e.every((e) => t.has(e));
1606
+ }
1554
1607
  setSelected(e) {
1555
1608
  this.changeListen(!1);
1556
1609
  let t = this.select.childNodes;
@@ -1595,13 +1648,14 @@ var B = class {
1595
1648
  })), this.changeListen(!0);
1596
1649
  }
1597
1650
  updateOptions(e) {
1598
- if (!(!e || e.length === 0)) {
1599
- this.isUpdating = !0, this.pendingOptionsChange = null, this.changeListen(!1), this.select.innerHTML = "";
1600
- for (let t of e) t instanceof I && this.select.appendChild(this.createOptgroup(t)), t instanceof F && this.select.appendChild(this.createOption(t));
1601
- if (this.select.dispatchEvent(new Event("change", { bubbles: !0 })), this.changeListen(!0), this.isUpdating = !1, this.pendingOptionsChange !== null) {
1602
- let e = this.pendingOptionsChange;
1603
- e.length > 0 && this.onOptionsChange ? (this.pendingOptionsChange = null, this.changeListen(!1), this.onOptionsChange(e), this.changeListen(!0)) : this.pendingOptionsChange = null;
1604
- }
1651
+ if (!e || e.length === 0) return;
1652
+ this.isUpdating = !0, this.pendingOptionsChange = null, this.changeListen(!1);
1653
+ let t = this.getSelectedValues();
1654
+ this.select.innerHTML = "";
1655
+ for (let t of e) t instanceof I && this.select.appendChild(this.createOptgroup(t)), t instanceof F && this.select.appendChild(this.createOption(t));
1656
+ if (C(t, this.getSelectedValues()) || this.select.dispatchEvent(new Event("change", { bubbles: !0 })), this.changeListen(!0), this.isUpdating = !1, this.pendingOptionsChange !== null) {
1657
+ let e = this.pendingOptionsChange;
1658
+ e.length > 0 && this.onOptionsChange ? (this.pendingOptionsChange = null, this.changeListen(!1), this.onOptionsChange(e), this.changeListen(!0)) : this.pendingOptionsChange = null;
1605
1659
  }
1606
1660
  }
1607
1661
  createOptgroup(e) {
@@ -1718,9 +1772,7 @@ var U = class {
1718
1772
  case "selection":
1719
1773
  this.applySelection(e.values, e.source, e.runAfterChange !== !1);
1720
1774
  break;
1721
- case "addOption":
1722
- this.applyAddOption(e.option);
1723
- break;
1775
+ case "addOption": this.applyAddOption(e.option);
1724
1776
  }
1725
1777
  }
1726
1778
  applyStructure(e, t, n = !1, r = !1) {
@@ -1733,12 +1785,30 @@ var U = class {
1733
1785
  }
1734
1786
  i.setData(e, n);
1735
1787
  let u = i.getData(!1);
1736
- r || i.snapshotCatalog(), a.updateOptions(u), o.renderValues(), o.renderOptions(u), s.afterChange && !C(c, i.getSelected()) && s.afterChange(i.getSelectedOptions());
1788
+ r || i.snapshotCatalog(), r || a.updateOptions(u), o.renderValues(), o.renderOptions(u), s.afterChange && !C(c, i.getSelected()) && s.afterChange(i.getSelectedOptions());
1789
+ }
1790
+ syncNativeSelection() {
1791
+ let { store: e, select: t } = this.deps, n = e.getSelectedOptions(), r = e.getSelectedValues();
1792
+ if (!t.hasOptionValues(r)) {
1793
+ if (n.length > 0) {
1794
+ t.updateOptions(n);
1795
+ return;
1796
+ }
1797
+ let r = e.getCatalogData().find((e) => e instanceof F && e.placeholder === !0);
1798
+ if (r) {
1799
+ t.updateOptions([new F({
1800
+ ...r,
1801
+ selected: !0
1802
+ })]);
1803
+ return;
1804
+ }
1805
+ }
1806
+ t.setSelectedByValue(r);
1737
1807
  }
1738
1808
  applySelection(e, t, n) {
1739
1809
  let { store: r, select: i, render: a, events: o } = this.deps, s = r.getSelected(), c = H(r, e);
1740
1810
  if (C(s, c)) return;
1741
- r.setSelectedBy("id", c), i.setSelectedByValue(r.getSelectedValues()), a.renderValues();
1811
+ r.setSelectedBy("id", c), this.syncNativeSelection(), a.renderValues();
1742
1812
  let l = a.content.search.input.value.trim();
1743
1813
  l === "" ? a.canUpdateOptionSelectionInPlace() ? a.updateOptionSelection() : a.renderOptions(r.getData(!1)) : a.hasRenderedOptions() ? a.updateOptionSelection() : this.deps.search && this.deps.search(l), n && o.afterChange && !C(s, r.getSelected()) && o.afterChange(r.getSelectedOptions());
1744
1814
  }
@@ -1753,12 +1823,18 @@ var U = class {
1753
1823
  }
1754
1824
  return !1;
1755
1825
  };
1756
- if (!c(t.getData(!1))) if (o) {
1757
- let n = t.getCatalogData();
1758
- c(n) || t.setData([...n, new F(e)], !0), t.snapshotCatalog();
1759
- } else t.addOption(e), t.snapshotCatalog();
1826
+ if (!c(t.getData(!1))) {
1827
+ if (o) {
1828
+ let n = t.getCatalogData();
1829
+ c(n) || t.setData([...n, new F(e)], !0), t.snapshotCatalog();
1830
+ } else t.addOption(e), t.snapshotCatalog();
1831
+ }
1760
1832
  let l = t.getData(!1);
1761
- n.updateOptions(l), r.renderValues(), r.renderOptions(l), i.afterChange && !C(a, t.getSelected()) && i.afterChange(t.getSelectedOptions());
1833
+ if (o) {
1834
+ let e = t.getSelectedOptions();
1835
+ e.length > 0 && n.updateOptions(e);
1836
+ } else n.updateOptions(l);
1837
+ r.renderValues(), r.renderOptions(l), i.afterChange && !C(a, t.getSelected()) && i.afterChange(t.getSelectedOptions());
1762
1838
  }
1763
1839
  }, W = class {
1764
1840
  selectEl;