slim-select 3.4.1 → 3.4.3

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.
@@ -70,7 +70,7 @@ class T {
70
70
  function y() {
71
71
  return Math.random().toString(36).substring(2, 10);
72
72
  }
73
- function E(f, e) {
73
+ function x(f, e) {
74
74
  function t(i, n) {
75
75
  return n && i && i.classList && i.classList.contains(n) || n && i && i.dataset && i.dataset.id && i.dataset.id === e ? i : null;
76
76
  }
@@ -177,8 +177,10 @@ class k {
177
177
  }
178
178
  setData(e, t = !1) {
179
179
  const s = this.partialToFullData(e);
180
+ let i = [];
180
181
  if (t) {
181
- const i = this.getSelectedOptions(), n = [];
182
+ i = this.getSelectedOptions();
183
+ const n = [];
182
184
  i.forEach((l) => {
183
185
  let a = !1;
184
186
  for (const o of s) {
@@ -198,7 +200,10 @@ class k {
198
200
  }), this.data = [...n, ...s];
199
201
  } else
200
202
  this.data = s;
201
- this.selectType === "single" && this.setSelectedBy("id", this.getSelected());
203
+ if (this.selectType === "single") {
204
+ const n = t && i.length === 0;
205
+ this.setSelectedBy("id", this.getSelected(), n);
206
+ }
202
207
  }
203
208
  // Get data will return all the data
204
209
  getData() {
@@ -219,21 +224,22 @@ class k {
219
224
  // Pass in an array of id that will loop through
220
225
  // each option and set the selected property to true
221
226
  // but also clean selected by determining selectType
222
- setSelectedBy(e, t) {
223
- let s = null, i = !1;
224
- const n = [];
225
- for (let a of this.data) {
226
- if (a instanceof m)
227
- for (let o of a.options) {
228
- s || (s = o);
229
- let h = o[e] || "";
230
- o.selected = i ? !1 : t.includes(h), o.selected && (n.push(o), this.selectType === "single" && (i = !0));
227
+ // When allowEmptySelection is true (e.g. after search with no prior selection), single select will not auto-select the first option
228
+ setSelectedBy(e, t, s = !1) {
229
+ let i = null, n = !1;
230
+ const l = [];
231
+ for (let o of this.data) {
232
+ if (o instanceof m)
233
+ for (let h of o.options) {
234
+ i || (i = h);
235
+ let r = h[e] || "";
236
+ h.selected = n ? !1 : t.includes(r), h.selected && (l.push(h), this.selectType === "single" && (n = !0));
231
237
  }
232
- a instanceof u && (s || (s = a), a.selected = i ? !1 : t.includes(a[e]), a.selected && (n.push(a), this.selectType === "single" && (i = !0)));
238
+ o instanceof u && (i || (i = o), o.selected = n ? !1 : t.includes(o[e]), o.selected && (l.push(o), this.selectType === "single" && (n = !0)));
233
239
  }
234
- this.selectType === "single" && s && !i && (s.selected = !0, n.push(s));
235
- const l = t.map((a) => n.find((o) => o[e] === a)?.id || "");
236
- this.selectedOrder = l;
240
+ this.selectType === "single" && i && !n && !s && (i.selected = !0, l.push(i));
241
+ const a = t.map((o) => l.find((h) => h[e] === o)?.id || "");
242
+ this.selectedOrder = a;
237
243
  }
238
244
  getSelected() {
239
245
  return this.getSelectedOptions().map((e) => e.id);
@@ -308,6 +314,7 @@ class N {
308
314
  callbacks;
309
315
  // Used to compute the range selection
310
316
  lastSelectedOption;
317
+ lastRenderedOptions;
311
318
  // Timeout tracking for cleanup
312
319
  closeAnimationTimeout = null;
313
320
  // Elements
@@ -316,7 +323,7 @@ class N {
316
323
  // Classes
317
324
  classes;
318
325
  constructor(e, t, s, i) {
319
- this.store = s, this.settings = e, this.classes = t, this.callbacks = i, this.lastSelectedOption = null, this.main = this.mainDiv(), this.content = this.contentDiv(), this.updateClassStyles(), this.updateAriaAttributes(), this.settings.contentPosition !== "relative" && (this.content.main.style.top = "-9999px", this.content.main.style.left = "-9999px", this.content.main.style.margin = "0", this.content.main.style.width = "auto"), this.settings.contentLocation && this.settings.contentLocation.appendChild(this.content.main);
326
+ this.store = s, this.settings = e, this.classes = t, this.callbacks = i, this.lastSelectedOption = null, this.lastRenderedOptions = [], this.main = this.mainDiv(), this.content = this.contentDiv(), this.updateClassStyles(), this.updateAriaAttributes(), this.settings.contentPosition !== "relative" && (this.content.main.style.top = "-9999px", this.content.main.style.left = "-9999px", this.content.main.style.margin = "0", this.content.main.style.width = "auto"), this.settings.contentLocation && this.settings.contentLocation.appendChild(this.content.main);
320
327
  }
321
328
  // Helper method to add classes that may contain spaces
322
329
  // Splits by spaces and adds each class individually to avoid DOMException
@@ -706,7 +713,7 @@ class N {
706
713
  }
707
714
  // Take in data and add options to
708
715
  renderOptions(e) {
709
- if (this.content.list.innerHTML = "", e.length === 0) {
716
+ if (this.lastRenderedOptions = e.map((s) => s instanceof u ? [s] : s.options.map((i) => new u(i))).flat(), this.content.list.innerHTML = "", e.length === 0) {
710
717
  const s = document.createElement("div");
711
718
  this.addClasses(s, this.classes.search), this.callbacks.addable ? s.innerHTML = this.settings.addableText.replace("{value}", this.content.search.input.value) : s.innerHTML = this.settings.searchText, this.content.list.appendChild(s);
712
719
  return;
@@ -811,10 +818,10 @@ class N {
811
818
  if (this.settings.isMultiple) {
812
819
  const c = h.some((d) => d.id === l);
813
820
  if (s.shiftKey && this.lastSelectedOption) {
814
- const d = this.store.getDataOptions(), g = d.findIndex((C) => C.id === this.lastSelectedOption.id), v = d.findIndex((C) => C.id === e.id);
821
+ const d = this.lastRenderedOptions, g = d.findIndex((C) => C.id === this.lastSelectedOption.id), v = d.findIndex((C) => C.id === e.id);
815
822
  if (g >= 0 && v >= 0) {
816
- const C = Math.min(g, v), w = Math.max(g, v), x = d.slice(C, w + 1).filter((A) => !h.find((L) => L.id === A.id));
817
- h.length + x.length <= this.settings.maxSelected ? r = h.concat(x) : r = h;
823
+ const C = Math.min(g, v), w = Math.max(g, v), E = d.slice(C, w + 1).filter((A) => !h.find((L) => L.id === A.id));
824
+ h.length + E.length <= this.settings.maxSelected ? r = h.concat(E) : r = h;
818
825
  } else
819
826
  r = h;
820
827
  } else a ? (c ? r = h.filter((d) => d.id !== l) : r = h.concat(e), this.lastSelectedOption = e) : (c ? r = h.filter((d) => d.id !== l) : r = h.concat(e), this.lastSelectedOption = e);
@@ -1143,7 +1150,7 @@ class M {
1143
1150
  if (n.__slimSelectLabelHandler)
1144
1151
  return;
1145
1152
  const l = (a) => {
1146
- const o = a.target, h = E(o, this.select.dataset.id);
1153
+ const o = a.target, h = x(o, this.select.dataset.id);
1147
1154
  a.preventDefault(), !h && this.onLabelClick && this.onLabelClick();
1148
1155
  };
1149
1156
  n.__slimSelectLabelHandler = l, n.addEventListener("click", l, { capture: !0, passive: !1 });
@@ -1368,7 +1375,7 @@ class F {
1368
1375
  });
1369
1376
  // Event listener for document click
1370
1377
  documentClick = (e) => {
1371
- this.settings.isOpen && e.target && !E(e.target, this.settings.id) && this.close(e.type);
1378
+ this.settings.isOpen && e.target && !x(e.target, this.settings.id) && this.close(e.type);
1372
1379
  };
1373
1380
  // Event Listener for window visibility change
1374
1381
  windowVisibilityChange = () => {