slim-select 3.1.0 → 3.1.1

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.
@@ -74,7 +74,7 @@ function T(p, e) {
74
74
  }
75
75
  return t(p, e) || s(p, e);
76
76
  }
77
- function O(p, e = 50, t = !1) {
77
+ function S(p, e = 50, t = !1) {
78
78
  let s;
79
79
  return function(...i) {
80
80
  const l = self, a = () => {
@@ -108,7 +108,7 @@ class u {
108
108
  this.id = !e.id || e.id === "" ? C() : e.id, this.value = e.value === void 0 ? e.text || "" : e.value || "", this.text = e.text || "", this.html = e.html || "", this.defaultSelected = e.defaultSelected !== void 0 ? e.defaultSelected : !1, this.selected = e.selected !== void 0 ? e.selected : !1, this.display = e.display !== void 0 ? e.display : !0, this.disabled = e.disabled !== void 0 ? e.disabled : !1, this.mandatory = e.mandatory !== void 0 ? e.mandatory : !1, this.placeholder = e.placeholder !== void 0 ? e.placeholder : !1, this.class = e.class || "", this.style = e.style || "", this.data = e.data || {};
109
109
  }
110
110
  }
111
- class m {
111
+ class g {
112
112
  id;
113
113
  label;
114
114
  selectAll;
@@ -135,7 +135,7 @@ class k {
135
135
  return new Error("Data must be an array");
136
136
  for (let t of e)
137
137
  if (t)
138
- if (t instanceof m || "label" in t) {
138
+ if (t instanceof g || "label" in t) {
139
139
  if (!("label" in t))
140
140
  return new Error("Optgroup must have a label");
141
141
  if ("options" in t && t.options)
@@ -160,11 +160,11 @@ class k {
160
160
  let t = [];
161
161
  return e.forEach((s) => {
162
162
  if (s) {
163
- if (s instanceof m || "label" in s) {
163
+ if (s instanceof g || "label" in s) {
164
164
  let i = [];
165
165
  "options" in s && s.options && s.options.forEach((l) => {
166
166
  i.push(new u(l));
167
- }), i.length > 0 && t.push(new m(s));
167
+ }), i.length > 0 && t.push(new g(s));
168
168
  }
169
169
  (s instanceof u || "text" in s) && t.push(new u(s));
170
170
  }
@@ -181,7 +181,7 @@ class k {
181
181
  n = !0;
182
182
  break;
183
183
  }
184
- if (o instanceof m) {
184
+ if (o instanceof g) {
185
185
  for (const c of o.options)
186
186
  if (c.id === a.id) {
187
187
  n = !0;
@@ -218,7 +218,7 @@ class k {
218
218
  let s = null, i = !1;
219
219
  const l = [];
220
220
  for (let n of this.data) {
221
- if (n instanceof m)
221
+ if (n instanceof g)
222
222
  for (let o of n.options) {
223
223
  s || (s = o);
224
224
  let c = o[e] || "";
@@ -241,7 +241,7 @@ class k {
241
241
  }
242
242
  getOptgroupByID(e) {
243
243
  for (let t of this.data)
244
- if (t instanceof m && t.id === e)
244
+ if (t instanceof g && t.id === e)
245
245
  return t;
246
246
  return null;
247
247
  }
@@ -255,7 +255,7 @@ class k {
255
255
  getFirstOption() {
256
256
  let e = null;
257
257
  for (let t of this.data)
258
- if (t instanceof m ? e = t.options[0] : t instanceof u && (e = t), e)
258
+ if (t instanceof g ? e = t.options[0] : t instanceof u && (e = t), e)
259
259
  break;
260
260
  return e;
261
261
  }
@@ -268,12 +268,12 @@ class k {
268
268
  filter(e, t) {
269
269
  const s = [];
270
270
  return this.data.forEach((i) => {
271
- if (i instanceof m) {
271
+ if (i instanceof g) {
272
272
  let l = [];
273
273
  if (i.options.forEach((n) => {
274
274
  (!e || e(n)) && (t ? l.push(new u(n)) : s.push(new u(n)));
275
275
  }), l.length > 0) {
276
- let n = new m(i);
276
+ let n = new g(i);
277
277
  n.options = l, s.push(n);
278
278
  }
279
279
  }
@@ -380,8 +380,8 @@ class N {
380
380
  if (this.settings.isMultiple)
381
381
  this.callbacks.setSelected([], !1), this.updateDeselectAll();
382
382
  else {
383
- const d = this.store.getFirstOption(), g = d ? d.id : "";
384
- this.callbacks.setSelected(g, !1);
383
+ const d = this.store.getFirstOption(), m = d ? d.id : "";
384
+ this.callbacks.setSelected(m, !1);
385
385
  }
386
386
  this.settings.closeOnSelect && this.callbacks.close(), this.callbacks.afterChange && this.callbacks.afterChange(this.store.getSelectedOptions());
387
387
  }
@@ -485,7 +485,7 @@ class N {
485
485
  if (!(this.settings.minSelected && r.length < this.settings.minSelected) && (this.callbacks.beforeChange && (o = this.callbacks.beforeChange(r, c) === !0), o)) {
486
486
  let h = [];
487
487
  for (const f of r) {
488
- if (f instanceof m)
488
+ if (f instanceof g)
489
489
  for (const d of f.options)
490
490
  d.id && h.push(d.id);
491
491
  f instanceof u && h.push(f.id);
@@ -535,7 +535,7 @@ class N {
535
535
  main: e,
536
536
  input: t
537
537
  };
538
- if (this.settings.showSearch || (e.classList.add(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 = O((l) => {
538
+ if (this.settings.showSearch || (e.classList.add(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((l) => {
539
539
  this.callbacks.search(l.target.value);
540
540
  }, 100), t.onkeydown = (l) => {
541
541
  switch (l.key) {
@@ -678,7 +678,7 @@ class N {
678
678
  ));
679
679
  const t = document.createDocumentFragment();
680
680
  for (const s of e) {
681
- if (s instanceof m) {
681
+ if (s instanceof g) {
682
682
  const i = document.createElement("div");
683
683
  i.classList.add(this.classes.optgroup);
684
684
  const l = document.createElement("div");
@@ -690,8 +690,8 @@ class N {
690
690
  const o = document.createElement("div");
691
691
  o.classList.add(this.classes.optgroupSelectAll);
692
692
  let c = !0;
693
- for (const g of s.options)
694
- if (!g.selected) {
693
+ for (const m of s.options)
694
+ if (!m.selected) {
695
695
  c = !1;
696
696
  break;
697
697
  }
@@ -703,13 +703,13 @@ class N {
703
703
  const f = document.createElementNS("http://www.w3.org/2000/svg", "path");
704
704
  f.setAttribute("d", this.classes.optgroupSelectAllBox), h.appendChild(f);
705
705
  const d = document.createElementNS("http://www.w3.org/2000/svg", "path");
706
- d.setAttribute("d", this.classes.optgroupSelectAllCheck), h.appendChild(d), o.addEventListener("click", (g) => {
707
- g.preventDefault(), g.stopPropagation();
706
+ d.setAttribute("d", this.classes.optgroupSelectAllCheck), h.appendChild(d), o.addEventListener("click", (m) => {
707
+ m.preventDefault(), m.stopPropagation();
708
708
  const v = this.store.getSelected();
709
709
  if (c) {
710
- const w = v.filter((S) => {
710
+ const w = v.filter((O) => {
711
711
  for (const b of s.options)
712
- if (S === b.id)
712
+ if (O === b.id)
713
713
  return !1;
714
714
  return !0;
715
715
  });
@@ -717,10 +717,10 @@ class N {
717
717
  return;
718
718
  } else {
719
719
  let w = s.options.map((b) => b.id).filter((b) => b !== void 0);
720
- const S = v.concat(w);
720
+ const O = v.concat(w);
721
721
  for (const b of s.options)
722
722
  b.id && !this.store.getOptionByID(b.id) && this.callbacks.addOption(new u(b));
723
- this.callbacks.setSelected(S, !0);
723
+ this.callbacks.setSelected(O, !0);
724
724
  return;
725
725
  }
726
726
  }), n.appendChild(o);
@@ -767,9 +767,9 @@ class N {
767
767
  if (this.settings.isMultiple) {
768
768
  const h = c.some((d) => d.id === a);
769
769
  if (s.shiftKey && this.lastSelectedOption) {
770
- const d = this.store.getDataOptions(), g = d.findIndex((w) => w.id === this.lastSelectedOption.id), v = d.findIndex((w) => w.id === e.id);
771
- if (g >= 0 && v >= 0) {
772
- const w = Math.min(g, v), S = Math.max(g, v), y = d.slice(w, S + 1).filter((x) => !c.find((E) => E.id === x.id));
770
+ const d = this.store.getDataOptions(), m = d.findIndex((w) => w.id === this.lastSelectedOption.id), v = d.findIndex((w) => w.id === e.id);
771
+ if (m >= 0 && v >= 0) {
772
+ const w = Math.min(m, v), O = Math.max(m, v), y = d.slice(w, O + 1).filter((x) => !c.find((E) => E.id === x.id));
773
773
  c.length + y.length <= this.settings.maxSelected ? r = c.concat(y) : r = c;
774
774
  } else
775
775
  r = c;
@@ -799,7 +799,7 @@ class N {
799
799
  const c = o.textContent || "", r = new RegExp("(" + l + ")", "i");
800
800
  if (r.test(c)) {
801
801
  const h = document.createElement("span");
802
- c.split(r).forEach((d, g) => {
802
+ c.split(r).forEach((d, m) => {
803
803
  if (d && r.test(d)) {
804
804
  const v = document.createElement("mark");
805
805
  v.className = s, v.textContent = d, h.appendChild(v);
@@ -848,6 +848,8 @@ class M {
848
848
  // Change observers
849
849
  listen = !1;
850
850
  observer = null;
851
+ isUpdating = !1;
852
+ pendingOptionsChange = null;
851
853
  // Event handlers for preventing native select behavior (especially on iOS Safari)
852
854
  preventNativeSelect = null;
853
855
  preventNativeSelectMousedown = null;
@@ -906,7 +908,16 @@ class M {
906
908
  }
907
909
  (l.target.nodeName === "OPTGROUP" || l.target.nodeName === "OPTION") && (i = !0);
908
910
  }
909
- t && this.onClassChange && this.onClassChange(this.select.className.split(" ")), s && this.onDisabledChange && (this.changeListen(!1), this.onDisabledChange(this.select.disabled), this.changeListen(!0)), i && this.onOptionsChange && (this.changeListen(!1), this.onOptionsChange(this.getData()), this.changeListen(!0));
911
+ if (t && this.onClassChange && this.onClassChange(this.select.className.split(" ")), s && this.onDisabledChange && (this.changeListen(!1), this.onDisabledChange(this.select.disabled), this.changeListen(!0)), i && this.onOptionsChange) {
912
+ if (this.isUpdating) {
913
+ if (this.select.options.length > 0) {
914
+ const l = this.getData();
915
+ l.length > 0 && (this.pendingOptionsChange = l);
916
+ }
917
+ return;
918
+ }
919
+ this.changeListen(!1), this.onOptionsChange(this.getData()), this.changeListen(!0);
920
+ }
910
921
  }
911
922
  // From the select element pull optgroup and options into data
912
923
  getData() {
@@ -1018,10 +1029,15 @@ class M {
1018
1029
  })), this.changeListen(!0);
1019
1030
  }
1020
1031
  updateOptions(e) {
1021
- this.changeListen(!1), this.select.innerHTML = "";
1022
- for (const t of e)
1023
- t instanceof m && this.select.appendChild(this.createOptgroup(t)), t instanceof u && this.select.appendChild(this.createOption(t));
1024
- this.select.dispatchEvent(new Event("change", { bubbles: !0 })), this.changeListen(!0);
1032
+ if (!(!e || e.length === 0)) {
1033
+ this.isUpdating = !0, this.pendingOptionsChange = null, this.changeListen(!1), this.select.innerHTML = "";
1034
+ for (const t of e)
1035
+ t instanceof g && this.select.appendChild(this.createOptgroup(t)), t instanceof u && this.select.appendChild(this.createOption(t));
1036
+ if (this.select.dispatchEvent(new Event("change", { bubbles: !0 })), this.changeListen(!0), this.isUpdating = !1, this.pendingOptionsChange !== null) {
1037
+ const t = this.pendingOptionsChange;
1038
+ t.length > 0 && this.onOptionsChange ? (this.pendingOptionsChange = null, this.changeListen(!1), this.onOptionsChange(t), this.changeListen(!0)) : this.pendingOptionsChange = null;
1039
+ }
1040
+ }
1025
1041
  }
1026
1042
  createOptgroup(e) {
1027
1043
  const t = document.createElement("optgroup");
@@ -1158,7 +1174,7 @@ class I {
1158
1174
  this.selectEl.dataset.ssid && this.destroy(), this.settings = new P(e.settings), this.cssClasses = new A(e.cssClasses);
1159
1175
  const t = ["beforeOpen", "afterOpen", "beforeClose", "afterClose"];
1160
1176
  for (const n in e.events)
1161
- e.events.hasOwnProperty(n) && (t.indexOf(n) !== -1 ? this.events[n] = O(e.events[n], 100) : this.events[n] = e.events[n]);
1177
+ e.events.hasOwnProperty(n) && (t.indexOf(n) !== -1 ? this.events[n] = S(e.events[n], 100) : this.events[n] = e.events[n]);
1162
1178
  this.settings.disabled = e.settings?.disabled ? e.settings.disabled : this.selectEl.disabled, this.settings.isMultiple = this.selectEl.multiple, this.settings.style = this.selectEl.style.cssText, this.settings.class = this.selectEl.className.split(" "), this.select = new M(this.selectEl), this.selectEl.id || (this.selectEl.id = this.settings.id), this.select.updateSelect(this.settings.id, this.settings.style, this.settings.class), this.select.hideUI(), this.select.onValueChange = (n) => {
1163
1179
  this.setSelected(n.map((o) => o.id));
1164
1180
  }, this.select.onClassChange = (n) => {
@@ -1166,7 +1182,7 @@ class I {
1166
1182
  }, this.select.onDisabledChange = (n) => {
1167
1183
  n ? this.disable() : this.enable();
1168
1184
  }, this.select.onOptionsChange = (n) => {
1169
- this.setData(n);
1185
+ this.setData(n || []);
1170
1186
  }, this.select.onLabelClick = () => {
1171
1187
  this.settings.disabled || this.open();
1172
1188
  };
@@ -1269,11 +1285,11 @@ class I {
1269
1285
  destroy() {
1270
1286
  this.openTimeout && (clearTimeout(this.openTimeout), this.openTimeout = null), this.closeTimeout && (clearTimeout(this.closeTimeout), this.closeTimeout = null), this.settings.intervalMove && (clearInterval(this.settings.intervalMove), this.settings.intervalMove = null), document.removeEventListener("click", this.documentClick), window.removeEventListener("resize", this.windowResize, !1), this.settings.openPosition === "auto" && window.removeEventListener("scroll", this.windowScroll, !1), document.removeEventListener("visibilitychange", this.windowVisibilityChange), this.store.setData([]), this.render.destroy(), this.select.destroy();
1271
1287
  }
1272
- windowResize = O(() => {
1288
+ windowResize = S(() => {
1273
1289
  !this.settings.isOpen && !this.settings.isFullOpen || this.render.moveContent();
1274
1290
  });
1275
1291
  // Event listener for window scrolling
1276
- windowScroll = O(() => {
1292
+ windowScroll = S(() => {
1277
1293
  !this.settings.isOpen && !this.settings.isFullOpen || this.render.moveContent();
1278
1294
  });
1279
1295
  // Event listener for document click
@@ -1286,7 +1302,7 @@ class I {
1286
1302
  };
1287
1303
  }
1288
1304
  export {
1289
- m as Optgroup,
1305
+ g as Optgroup,
1290
1306
  u as Option,
1291
1307
  P as Settings,
1292
1308
  I as default