@danielgindi/selectbox 2.0.66 → 3.0.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.
package/dist/lib.es6.js CHANGED
@@ -1,19 +1,18 @@
1
1
  /*!
2
- * @danielgindi/selectbox 2.0.66
2
+ * @danielgindi/selectbox 3.0.0
3
3
  * git://github.com/danielgindi/selectbox.git
4
4
  */
5
5
  import { closestUntil, createElement, next, prev, setElementAttrs } from "@danielgindi/dom-utils/lib/Dom";
6
- import { append, before, getRootNode, remove, toggleClass } from "@danielgindi/dom-utils/lib/DomCompat";
7
- import { anchoredPosition, getCssProps, getElementHeight, getElementOffset, getElementWidth, getPseudoElementWidth, parseTransition, setCssProps, setElementHeight, setElementWidth } from "@danielgindi/dom-utils/lib/Css";
6
+ import { before, getRootNode, remove, toggleClass } from "@danielgindi/dom-utils/lib/DomCompat";
7
+ import { anchoredPosition, getCssProps, getElementHeight, getElementOffset, getElementWidth, parseTransition, setCssProps, setElementHeight, setElementWidth } from "@danielgindi/dom-utils/lib/Css";
8
8
  import DomEventsSink from "@danielgindi/dom-utils/lib/DomEventsSink";
9
9
  import VirtualListHelper from "@danielgindi/virtual-list-helper";
10
10
  import { VALUE_BACK_SPACE, VALUE_DELETE, VALUE_DOWN, VALUE_END, VALUE_ENTER, VALUE_ESCAPE, VALUE_HOME, VALUE_LEFT, VALUE_PAGE_DOWN, VALUE_PAGE_UP, VALUE_RIGHT, VALUE_SPACE, VALUE_TAB, VALUE_UP } from "keycode-js";
11
11
  import mitt from "mitt";
12
- //#region src/lib/utils/escapeRegex.ts
12
+ // src/lib/utils/escapeRegex.ts
13
13
  /** @internal */
14
14
  var escapeRegex = (value) => value.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
15
- //#endregion
16
- //#region src/lib/utils/throttle.ts
15
+ // src/lib/utils/throttle.ts
17
16
  /** @internal */
18
17
  var throttle = (func, wait, options = {}) => {
19
18
  let timeout;
@@ -54,8 +53,7 @@ var throttle = (func, wait, options = {}) => {
54
53
  };
55
54
  return throttled;
56
55
  };
57
- //#endregion
58
- //#region src/lib/utils/reportError.ts
56
+ // src/lib/utils/reportError.ts
59
57
  /**
60
58
  * Routes an error/warning to `handler` if supplied, falling back to the
61
59
  * console (matching the previous hardcoded behaviour) if there is no
@@ -72,8 +70,7 @@ var reportError = (handler, error, context) => {
72
70
  if (context.level === "warn") console.warn(error);
73
71
  else console.error(error);
74
72
  };
75
- //#endregion
76
- //#region src/lib/DropList.ts
73
+ // src/lib/DropList.ts
77
74
  var ItemSymbol = Symbol("item");
78
75
  var DestroyedSymbol$1 = Symbol("destroyed");
79
76
  var GhostSymbol = Symbol("ghost");
@@ -562,7 +559,7 @@ var DropList = class DropList {
562
559
  const p = this._p;
563
560
  let itemIndex = this.itemIndexByValue(value);
564
561
  if (itemIndex === -1) return this;
565
- let item = this.itemAtIndex(itemIndex);
562
+ let item = p.items[itemIndex];
566
563
  item[ItemSymbol] = newItem;
567
564
  if (hasOwnProperty.call(newItem, p.labelProp)) item.label = newItem[p.labelProp];
568
565
  if (hasOwnProperty.call(newItem, p.valueProp)) item.value = newItem[p.valueProp];
@@ -582,10 +579,7 @@ var DropList = class DropList {
582
579
  if (hasOwnProperty.call(newItem, "_level")) item._level = newItem._level;
583
580
  if (hasOwnProperty.call(newItem, "_child")) item._child = !!newItem._child;
584
581
  let virtualItemIndex = itemIndex;
585
- if (p.filteredItems) {
586
- virtualItemIndex = p.filteredItems.indexOf(item);
587
- if (virtualItemIndex !== -1) p.filteredItems.splice(virtualItemIndex, 1);
588
- }
582
+ if (p.filteredItems) virtualItemIndex = p.filteredItems.indexOf(item);
589
583
  if (virtualItemIndex !== -1 && p.virtualListHelper.isItemRendered(virtualItemIndex)) p.virtualListHelper.refreshItemAt(virtualItemIndex).render();
590
584
  return this;
591
585
  }
@@ -603,7 +597,7 @@ var DropList = class DropList {
603
597
  if (virtualItemIndex !== -1) {
604
598
  p.hasNoResultsItem = (p.filteredItems ?? p.items).length === 0 && !!p.noResultsText;
605
599
  if (p.hasNoResultsItem) p.virtualListHelper.setCount(1).render();
606
- else p.virtualListHelper.removeItemsAt(virtualItemIndex, 1).render();
600
+ else p.virtualListHelper.removeItemsAt(1, virtualItemIndex).render();
607
601
  }
608
602
  return this;
609
603
  }
@@ -2025,14 +2019,16 @@ var DropList = class DropList {
2025
2019
  } else if (p.focusItemEl) {
2026
2020
  let base = getElementOffset(p.focusItemEl).top;
2027
2021
  let height = getElementHeight(p.el, true);
2028
- while (true) {
2029
- next = p.focusItemEl.nextElementSibling;
2030
- if (!next) return;
2031
- if (next.tagName !== "LI") continue;
2022
+ let candidate = p.focusItemEl;
2023
+ next = null;
2024
+ while (candidate = direction === "prev_page" ? candidate.previousElementSibling : candidate.nextElementSibling) {
2025
+ if (candidate.tagName !== "LI") continue;
2026
+ next = candidate;
2032
2027
  if (direction === "prev_page") {
2033
2028
  if (getElementOffset(next).top - base + height <= 0) break;
2034
2029
  } else if (getElementOffset(next).top - base - height >= 0) break;
2035
2030
  }
2031
+ if (!next) return;
2036
2032
  if (next) {
2037
2033
  nextIndex = p.virtualListHelper.getItemIndexFromElement(next);
2038
2034
  if (nextIndex === void 0) nextIndex = -1;
@@ -2251,8 +2247,7 @@ var DropList = class DropList {
2251
2247
  return newOuterWidth;
2252
2248
  }
2253
2249
  };
2254
- //#endregion
2255
- //#region src/lib/SelectBox.ts
2250
+ // src/lib/SelectBox.ts
2256
2251
  var DestroyedSymbol = Symbol("destroyed");
2257
2252
  var RestMultiItemsSymbol = Symbol("rest_multi_items");
2258
2253
  var hasTouchCapability = !!("ontouchstart" in window || window.DocumentTouch && window.document instanceof window.DocumentTouch || window.navigator.maxTouchPoints);
@@ -2500,9 +2495,12 @@ var SelectBox = class {
2500
2495
  remove(p.list);
2501
2496
  remove(p.singleWrapper);
2502
2497
  remove(p.input);
2498
+ remove(p.inputWrapper);
2503
2499
  remove(p.inputBackBuffer);
2504
2500
  remove(p.clearButtonWrapper);
2505
2501
  remove(p.openIndicator);
2502
+ remove(p.spinner);
2503
+ remove(p.controls);
2506
2504
  if (!p.ownsEl) {
2507
2505
  for (let name of Array.from(p.el.classList)) if (name.startsWith(p.baseClassName)) p.el.classList.remove(name);
2508
2506
  p.el.removeAttribute("role");
@@ -2588,6 +2586,7 @@ var SelectBox = class {
2588
2586
  p.itemsChanged = true;
2589
2587
  this._refreshItemByValueMap();
2590
2588
  if (resetValues) this.setSelectedValues(this.getSelectedValues());
2589
+ else this._scheduleSync("render_list");
2591
2590
  this._trigger("itemschanged", {
2592
2591
  term: null,
2593
2592
  mutated: true,
@@ -3243,6 +3242,10 @@ var SelectBox = class {
3243
3242
  top: "-9999px",
3244
3243
  whiteSpace: "pre"
3245
3244
  } });
3245
+ if (!p.controls) {
3246
+ p.controls = createElement("span", { class: `${p.baseClassName}__controls` });
3247
+ p.el.appendChild(p.controls);
3248
+ }
3246
3249
  if (!p.input) p.inputWrapper = createElement("span", { class: `${p.baseClassName}__search_wrapper` }, p.input = createElement("input", {
3247
3250
  class: `${p.baseClassName}__search_field`,
3248
3251
  type: "search",
@@ -3264,7 +3267,7 @@ var SelectBox = class {
3264
3267
  this._unregisterDropdownEvents();
3265
3268
  const lastActiveElement = document.activeElement;
3266
3269
  p.list = createElement("ul", { class: `${p.baseClassName}__list` });
3267
- p.el.appendChild(p.list);
3270
+ p.el.insertBefore(p.list, p.controls);
3268
3271
  p.list.appendChild(p.inputWrapper);
3269
3272
  p.el.classList.remove(`${p.baseClassName}__single`);
3270
3273
  p.el.classList.add(`${p.baseClassName}__multi`);
@@ -3293,8 +3296,8 @@ var SelectBox = class {
3293
3296
  this._unregisterDropdownEvents();
3294
3297
  const lastActiveElement = document.activeElement;
3295
3298
  p.singleWrapper = createElement("div", { class: `${p.baseClassName}__single_wrapper` });
3296
- p.el.appendChild(p.singleWrapper);
3297
- p.el.appendChild(p.inputWrapper);
3299
+ p.el.insertBefore(p.singleWrapper, p.controls);
3300
+ p.el.insertBefore(p.inputWrapper, p.controls);
3298
3301
  p.el.classList.remove(`${p.baseClassName}__multi`);
3299
3302
  p.el.classList.add(`${p.baseClassName}__single`);
3300
3303
  if (lastActiveElement === p.input) p.input.focus();
@@ -3304,7 +3307,7 @@ var SelectBox = class {
3304
3307
  if (p.hasOpenIndicator !== !!p.openIndicator) {
3305
3308
  if (p.hasOpenIndicator) {
3306
3309
  p.openIndicator = createElement("span", { class: `${p.baseClassName}__open_indicator` });
3307
- p.el.appendChild(p.openIndicator);
3310
+ this._insertControl(p.openIndicator);
3308
3311
  p.el.classList.add(`${p.baseClassName}__has_open_indicator`);
3309
3312
  } else {
3310
3313
  remove(p.openIndicator);
@@ -3315,7 +3318,7 @@ var SelectBox = class {
3315
3318
  if (p.isLoadingMode !== !!p.spinner) {
3316
3319
  if (p.isLoadingMode) {
3317
3320
  p.spinner = createElement("span", { class: `${p.baseClassName}__spinner` });
3318
- p.el.appendChild(p.spinner);
3321
+ this._insertControl(p.spinner);
3319
3322
  p.el.classList.add(`${p.baseClassName}__has_spinner`);
3320
3323
  } else {
3321
3324
  remove(p.spinner);
@@ -3325,6 +3328,21 @@ var SelectBox = class {
3325
3328
  }
3326
3329
  }
3327
3330
  /**
3331
+ * Inserts a control into the controls group, keeping the order: spinner, clear button, open indicator
3332
+ * @private
3333
+ * @internal
3334
+ */
3335
+ _insertControl(control) {
3336
+ const p = this._p;
3337
+ const order = [
3338
+ p.spinner,
3339
+ p.clearButtonWrapper,
3340
+ p.openIndicator
3341
+ ];
3342
+ const next = order.slice(order.indexOf(control) + 1).find((x) => x && x.parentNode === p.controls);
3343
+ p.controls.insertBefore(control, next ?? null);
3344
+ }
3345
+ /**
3328
3346
  * @private
3329
3347
  * @internal
3330
3348
  */
@@ -3344,7 +3362,10 @@ var SelectBox = class {
3344
3362
  if (p.searchable || p.multi) classes.push(`${p.baseClassName}__searchable`);
3345
3363
  if (p.direction === "ltr" || p.direction === "rtl") classes.push(`${p.baseClassName}__` + p.direction);
3346
3364
  if (p.additionalClasses) classes = classes.concat(p.additionalClasses);
3347
- el.className = classes.join(" ");
3365
+ classes = classes.join(" ").split(/\s+/).filter(Boolean);
3366
+ const previousClasses = p.appliedClasses ?? [];
3367
+ el.className = Array.from(el.classList).filter((name) => !name.startsWith(p.baseClassName) && !previousClasses.includes(name)).concat(classes).join(" ");
3368
+ p.appliedClasses = classes;
3348
3369
  }
3349
3370
  /** @private */
3350
3371
  /** @internal */
@@ -3861,9 +3882,8 @@ var SelectBox = class {
3861
3882
  const p = this._p, multiItemLabelProp = p.multiItemLabelProp;
3862
3883
  if (p.selectedItems.length > 0 && p.selectedItems.some((x) => x[multiItemLabelProp] !== false) && p.clearable && p.showSelection) {
3863
3884
  if (!p.clearButtonWrapper) {
3864
- p.clearButtonWrapper = createElement(p.multi ? "li" : "span", { class: `${p.baseClassName}__clear` }, p.clearButton = createElement("button"));
3865
- if (p.multi) append(p.list, p.clearButtonWrapper);
3866
- else append(p.el, p.clearButtonWrapper);
3885
+ p.clearButtonWrapper = createElement("span", { class: `${p.baseClassName}__clear` }, p.clearButton = createElement("button"));
3886
+ this._insertControl(p.clearButtonWrapper);
3867
3887
  p.el.classList.add(`${p.baseClassName}__has_clear`);
3868
3888
  p.sink.add(p.clearButton, "click", () => {
3869
3889
  if (this.isDisabled() || this.isReadOnly()) return;
@@ -4110,14 +4130,6 @@ var SelectBox = class {
4110
4130
  let hasResize = false;
4111
4131
  if (p.multi && p.multiItemEls.length === 0 && p.showSelection) {
4112
4132
  input.style.width = "";
4113
- let beforeWidth = 0, afterWidth = 0;
4114
- let beforeStyle = getComputedStyle(p.list || el, "::before");
4115
- if (beforeStyle.content && beforeStyle.content !== "none" && beforeStyle.position !== "absolute" && beforeStyle.float !== "none") beforeWidth = getPseudoElementWidth(p.list || el, "::before", true, true, true);
4116
- let afterStyle = getComputedStyle(p.list || el, "::after");
4117
- if (afterStyle.content && afterStyle.content !== "none" && afterStyle.position !== "absolute" && afterStyle.float !== "none") afterWidth = getPseudoElementWidth(p.list || el, "::after", true, true, true);
4118
- let contentWidth = getElementWidth(p.list || el);
4119
- contentWidth -= beforeWidth + afterWidth;
4120
- input.style.width = `${contentWidth}px`;
4121
4133
  hasResize = true;
4122
4134
  } else {
4123
4135
  setCssProps(backBufferEl, getCssProps(input, inputBackbufferCssProps));
@@ -4402,7 +4414,6 @@ var SelectBox = class {
4402
4414
  return joined;
4403
4415
  }
4404
4416
  };
4405
- //#endregion
4406
4417
  export { DropList, DefaultOptions as DropListDefaultOptions, ItemSymbol, SelectBox, DefaultOptions$1 as SelectBoxDefaultOptions };
4407
4418
 
4408
4419
  //# sourceMappingURL=lib.es6.js.map