@danielgindi/selectbox 2.0.52 → 2.0.54

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/eslint.config.mjs CHANGED
@@ -131,7 +131,7 @@ export default [
131
131
  },
132
132
  },
133
133
  {
134
- files: ["scripts/**/*.js", "**/eslint.config.mjs"],
134
+ files: ["scripts/**/*.js", "benchmarks/**/*.mjs", "**/eslint.config.mjs"],
135
135
 
136
136
  languageOptions: {
137
137
  globals: {
package/lib/DropList.js CHANGED
@@ -63,8 +63,8 @@ const hasOwnProperty = Object.prototype.hasOwnProperty;
63
63
  * @property {function(item: DropList.ItemBase, itemEl: Element):(*|false)} [renderNoResultsItem]
64
64
  * @property {function(item: DropList.ItemBase, itemEl: Element)} [unrenderNoResultsItem]
65
65
  * @property {function(name: string, data: *)} [on]
66
- * @property {boolean} [isHeaderVisible=false] show header element
67
- * @property {boolean} [isFooterVisible=false] show footer element
66
+ * @property {boolean} [isHeaderVisible=false] show header element
67
+ * @property {boolean} [isFooterVisible=false] show footer element
68
68
  * @property {boolean} [searchable=false] include inline search box
69
69
  * @property {string} [noResultsText='No matching results'] text for no results (empty for none)
70
70
  * @property {number} [filterThrottleWindow=300] throttle time (milliseconds) for filtering
@@ -137,8 +137,8 @@ export const DefaultOptions = {
137
137
 
138
138
  on: null,
139
139
 
140
- isHeaderVisible: false,
141
- isFooterVisible: false,
140
+ isHeaderVisible: false,
141
+ isFooterVisible: false,
142
142
  searchable: false,
143
143
  noResultsText: 'No matching results',
144
144
  filterThrottleWindow: 300,
@@ -163,7 +163,7 @@ Emits the following events:
163
163
  'groupcheck' {value, item, affectedCount: number}: item was selected (in multi mode).
164
164
  'blur' {event}: element lost focus
165
165
  'show_subitems {value, item, el, droplist: DropList}': subitems dropdown will show.
166
- 'hide_subitems {value, item, el}': subitems dropdown did hide.
166
+ 'hide_subitems {value, item, el, droplist: DropList}': subitems dropdown did hide.
167
167
  'subitems:select' {value, item, event?, el}: item was selected (in single mode).
168
168
  'subitems:blur' {event}: subitems element lost focus
169
169
  */
@@ -211,8 +211,8 @@ class DropList {
211
211
  positionOptionsProvider: o.positionOptionsProvider ?? null,
212
212
 
213
213
  searchable: o.searchable,
214
- isHeaderVisible: o.isHeaderVisible,
215
- isFooterVisible: o.isFooterVisible,
214
+ isHeaderVisible: o.isHeaderVisible,
215
+ isFooterVisible: o.isFooterVisible,
216
216
 
217
217
  silenceEvents: true,
218
218
  mitt: mitt(),
@@ -266,27 +266,27 @@ class DropList {
266
266
  menuEl.role = 'menu';
267
267
  p.menuEl = menuEl;
268
268
 
269
- p.headerEl = createElement('div', {
270
- class: p.baseClassName + '_header',
271
- });
272
-
273
- p.footerEl = createElement('div', {
274
- class: p.baseClassName + '_footer',
275
- });
276
-
277
- if (o.searchable) {
278
- this.setSearchable(true);
279
- }
280
-
281
- if (o.isHeaderVisible)
282
- this.setHeaderVisible(o.isHeaderVisible);
283
-
284
- wrapperEl.appendChild(menuEl);
285
-
286
- if (o.isFooterVisible)
287
- this.setFooterVisible(o.isFooterVisible);
288
-
289
- p.items = [];
269
+ p.headerEl = createElement('div', {
270
+ class: p.baseClassName + '_header',
271
+ });
272
+
273
+ p.footerEl = createElement('div', {
274
+ class: p.baseClassName + '_footer',
275
+ });
276
+
277
+ if (o.searchable) {
278
+ this.setSearchable(true);
279
+ }
280
+
281
+ if (o.isHeaderVisible)
282
+ this.setHeaderVisible(o.isHeaderVisible);
283
+
284
+ wrapperEl.appendChild(menuEl);
285
+
286
+ if (o.isFooterVisible)
287
+ this.setFooterVisible(o.isFooterVisible);
288
+
289
+ p.items = [];
290
290
  p.groupCount = 0; // This will keep state of how many `group` items we have
291
291
  p.mouseHandled = false;
292
292
 
@@ -445,7 +445,7 @@ class DropList {
445
445
  }
446
446
 
447
447
  get el() {
448
- return this._p.el;
448
+ return this._p?.el ?? null;
449
449
  }
450
450
 
451
451
  /**
@@ -697,6 +697,11 @@ class DropList {
697
697
  return false;
698
698
  }
699
699
 
700
+ if (item._subitems?.length) {
701
+ this._showSublist(item, p.focusItemEl);
702
+ return false;
703
+ }
704
+
700
705
  if (p.multi) {
701
706
  if (this.hasFocusedItem()) {
702
707
  this.toggleFocusedItem();
@@ -1537,23 +1542,23 @@ class DropList {
1537
1542
  let verticalBorderWidth = (parseFloat(elComputedStyle.borderTopWidth) || 0) +
1538
1543
  (parseFloat(elComputedStyle.borderBottomWidth) || 0);
1539
1544
 
1540
- let headerHeight = 0;
1541
- if (p.headerEl) {
1542
- headerHeight = getElementHeight(p.headerEl, true, true);
1543
- }
1544
-
1545
- let footerHeight = 0;
1546
- if (p.footerEl) {
1547
- footerHeight = getElementHeight(p.footerEl, true, true);
1548
- }
1549
-
1550
- if (p.virtualListHelper.isVirtual()) {
1551
- maxViewHeight =
1552
- p.virtualListHelper.estimateFullHeight() +
1553
- verticalPadding +
1554
- verticalBorderWidth +
1555
- headerHeight +
1556
- footerHeight;
1545
+ let headerHeight = 0;
1546
+ if (p.headerEl) {
1547
+ headerHeight = getElementHeight(p.headerEl, true, true);
1548
+ }
1549
+
1550
+ let footerHeight = 0;
1551
+ if (p.footerEl) {
1552
+ footerHeight = getElementHeight(p.footerEl, true, true);
1553
+ }
1554
+
1555
+ if (p.virtualListHelper.isVirtual()) {
1556
+ maxViewHeight =
1557
+ p.virtualListHelper.estimateFullHeight() +
1558
+ verticalPadding +
1559
+ verticalBorderWidth +
1560
+ headerHeight +
1561
+ footerHeight;
1557
1562
  } else {
1558
1563
  // Another method to calculate height is measuring the whole thing at once.
1559
1564
  // This causes relayout of course.
@@ -1694,12 +1699,12 @@ class DropList {
1694
1699
  viewCss.left += 'px';
1695
1700
  viewCss.top += 'px';
1696
1701
  setCssProps(el, viewCss);
1697
- setElementHeight(el, viewSize.height, true, true);
1698
-
1699
- if (menuEl !== el) {
1700
- let menuHeight = viewSize.height - headerHeight - footerHeight - verticalBorderWidth - verticalPadding;
1701
- setElementHeight(menuEl, menuHeight, true, true);
1702
- }
1702
+ setElementHeight(el, viewSize.height, true, true);
1703
+
1704
+ if (menuEl !== el) {
1705
+ let menuHeight = viewSize.height - headerHeight - footerHeight - verticalBorderWidth - verticalPadding;
1706
+ setElementHeight(menuEl, menuHeight, true, true);
1707
+ }
1703
1708
 
1704
1709
  // Update the scroll position for virtual lists
1705
1710
  p.virtualListHelper.render();
@@ -1865,7 +1870,7 @@ class DropList {
1865
1870
  if (!p.onDocumentMouseDown) {
1866
1871
  // Detect mouse tap outside of the droplist, to blur the focused item
1867
1872
  p.sink.add(document, 'mousedown', p.onDocumentMouseDown = (event) => {
1868
- if (!p.el.contains(event.target))
1873
+ if (!this.elContains(event.target, true))
1869
1874
  this._delayBlurItemOnBlur();
1870
1875
  });
1871
1876
  }
@@ -2029,46 +2034,46 @@ class DropList {
2029
2034
  * Get a reference to the header element in order to add custom content.
2030
2035
  * @returns {Element}
2031
2036
  */
2032
- getHeaderElement() {
2033
- return this._p.headerEl;
2034
- }
2035
-
2036
- /**
2037
- * Change visibility of the footer element
2038
- * You should probably call `relayout()` after this.
2039
- * @param {boolean} visible
2040
- */
2041
- setFooterVisible(visible) {
2042
- let isVisible = this.isFooterVisible();
2043
- if (isVisible === !!visible)
2044
- return;
2045
-
2046
- if (visible) {
2047
- this._p.el.appendChild(this._p.footerEl);
2048
- } else {
2049
- this._p.footerEl.remove();
2050
- }
2051
- }
2052
-
2053
- /**
2054
- * Is the footer element visible?
2055
- * @returns {boolean}
2056
- */
2057
- isFooterVisible() {
2058
- return !!this._p.footerEl.parentNode;
2059
- }
2060
-
2061
- /**
2062
- * Get a reference to the footer element in order to add custom content.
2063
- * @returns {Element}
2064
- */
2065
- getFooterElement() {
2066
- return this._p.footerEl;
2067
- }
2068
-
2069
- /**
2070
- * Set inline search visibility
2071
- * You should probably call `relayout()` after this.
2037
+ getHeaderElement() {
2038
+ return this._p.headerEl;
2039
+ }
2040
+
2041
+ /**
2042
+ * Change visibility of the footer element
2043
+ * You should probably call `relayout()` after this.
2044
+ * @param {boolean} visible
2045
+ */
2046
+ setFooterVisible(visible) {
2047
+ let isVisible = this.isFooterVisible();
2048
+ if (isVisible === !!visible)
2049
+ return;
2050
+
2051
+ if (visible) {
2052
+ this._p.el.appendChild(this._p.footerEl);
2053
+ } else {
2054
+ this._p.footerEl.remove();
2055
+ }
2056
+ }
2057
+
2058
+ /**
2059
+ * Is the footer element visible?
2060
+ * @returns {boolean}
2061
+ */
2062
+ isFooterVisible() {
2063
+ return !!this._p.footerEl.parentNode;
2064
+ }
2065
+
2066
+ /**
2067
+ * Get a reference to the footer element in order to add custom content.
2068
+ * @returns {Element}
2069
+ */
2070
+ getFooterElement() {
2071
+ return this._p.footerEl;
2072
+ }
2073
+
2074
+ /**
2075
+ * Set inline search visibility
2076
+ * You should probably call `relayout()` after this.
2072
2077
  * @param {boolean} searchable
2073
2078
  */
2074
2079
  setSearchable(searchable) {
@@ -2163,6 +2168,8 @@ class DropList {
2163
2168
  if (!item._subitems?.length) return;
2164
2169
 
2165
2170
  const p = this._p;
2171
+ if (p.currentSubDropList?.item === item)
2172
+ return;
2166
2173
 
2167
2174
  const droplist = new DropList({
2168
2175
  baseClassName: p.baseClassName,
@@ -2204,6 +2211,12 @@ class DropList {
2204
2211
  .on('subitems:select', event => {
2205
2212
  this._trigger('subitems:select', event);
2206
2213
  })
2214
+ .on('show_subitems', event => {
2215
+ this._trigger('show_subitems', event);
2216
+ })
2217
+ .on('hide_subitems', event => {
2218
+ this._trigger('hide_subitems', event);
2219
+ })
2207
2220
  .on('blur', onBlur)
2208
2221
  .on('subitems:blur', onBlur)
2209
2222
  .on('_back_key_pressed', () => {
@@ -2259,6 +2272,7 @@ class DropList {
2259
2272
  value: data.item.value,
2260
2273
  item: data.item,
2261
2274
  el: data.itemElement,
2275
+ droplist: data.droplist,
2262
2276
  });
2263
2277
  }
2264
2278
 
@@ -2923,7 +2937,7 @@ class DropList {
2923
2937
  _move(direction, event) {
2924
2938
  const p = this._p;
2925
2939
 
2926
- let next, nextIndex, directionUp = false;
2940
+ let next, nextIndex, directionUp;
2927
2941
  const items = p.filteredItems ?? p.items;
2928
2942
 
2929
2943
  if (direction === 'first') {
package/lib/SelectBox.js CHANGED
@@ -502,7 +502,7 @@ class SelectBox {
502
502
  }
503
503
 
504
504
  get el() {
505
- return this._p.el;
505
+ return this._p?.el ?? null;
506
506
  }
507
507
 
508
508
  get droplistInstance() {
@@ -624,7 +624,7 @@ class SelectBox {
624
624
  p.items = items.slice(0);
625
625
  p.itemsChanged = true;
626
626
 
627
- this._updateItemByValueMap();
627
+ this._refreshItemByValueMap();
628
628
 
629
629
  if (resetValues) {
630
630
  this.setSelectedValues(this.getSelectedValues());
@@ -655,10 +655,17 @@ class SelectBox {
655
655
  updateItemByValue(value, newItem) {
656
656
  const p = this._p;
657
657
 
658
- let existingItem = p.itemByValueMap.get(value);
659
- if (existingItem)
658
+ let existingItem = this._getItemByValue(value);
659
+ if (existingItem) {
660
+ const valueProp = existingItem[p.valueProp];
661
+ if (p.itemByValueMap?.has(valueProp))
662
+ p.itemByValueMap.delete(valueProp);
663
+
660
664
  Object.assign(existingItem, newItem);
661
665
 
666
+ p.itemByValueMap.set(existingItem[p.valueProp], existingItem);
667
+ }
668
+
662
669
  if (p.dropList) {
663
670
  p.dropList.updateItemByValue(value, newItem);
664
671
  }
@@ -1228,7 +1235,7 @@ class SelectBox {
1228
1235
  if (p.dropList)
1229
1236
  p.dropList.setValueProp(prop);
1230
1237
 
1231
- this._updateItemByValueMap();
1238
+ this._refreshItemByValueMap();
1232
1239
 
1233
1240
  return this;
1234
1241
  }
@@ -1414,7 +1421,7 @@ class SelectBox {
1414
1421
 
1415
1422
  selectedValues.push(value);
1416
1423
 
1417
- let item = p.itemByValueMap.get(value);
1424
+ let item = this._getItemByValue(value);
1418
1425
  if (item !== undefined) {
1419
1426
  selectedItems.push(item);
1420
1427
  } else {
@@ -1709,7 +1716,7 @@ class SelectBox {
1709
1716
  * This reduces search time greatly (i.e when checking items), especially when Vue proxies are in place.
1710
1717
  * @private
1711
1718
  */
1712
- _updateItemByValueMap() {
1719
+ _refreshItemByValueMap() {
1713
1720
  const p = this._p;
1714
1721
 
1715
1722
  const itemByValueMap = p.itemByValueMap = new Map();
@@ -1717,6 +1724,52 @@ class SelectBox {
1717
1724
  for (let item of p.items) {
1718
1725
  itemByValueMap.set(item[valueProp], item);
1719
1726
  }
1727
+
1728
+ p.subitemByValueMap = null;
1729
+ }
1730
+
1731
+ /**
1732
+ * @param {*} value
1733
+ * @returns {DropList.ItemBase|undefined}
1734
+ * @private
1735
+ */
1736
+ _getItemByValue(value) {
1737
+ const p = this._p;
1738
+ let item = p.itemByValueMap.get(value);
1739
+ if (item !== undefined)
1740
+ return item;
1741
+
1742
+ if (!p.subitemByValueMap)
1743
+ this._refreshSubitemByValueMap();
1744
+
1745
+ return p.subitemByValueMap.get(value);
1746
+ }
1747
+
1748
+ /** @private */
1749
+ _refreshSubitemByValueMap() {
1750
+ const p = this._p;
1751
+ const subitemByValueMap = p.subitemByValueMap = new Map();
1752
+ this._addSubitemsToValueMap(p.items, subitemByValueMap, p.valueProp);
1753
+ }
1754
+
1755
+ /**
1756
+ * @param {DropList.ItemBase[]} items
1757
+ * @param {Map<*, DropList.ItemBase>} itemByValueMap
1758
+ * @param {string} valueProp
1759
+ * @private
1760
+ */
1761
+ _addSubitemsToValueMap(items, itemByValueMap, valueProp) {
1762
+ for (let item of items) {
1763
+ if (!item._subitems?.length)
1764
+ continue;
1765
+
1766
+ for (let subitem of item._subitems) {
1767
+ if (!itemByValueMap.has(subitem[valueProp]))
1768
+ itemByValueMap.set(subitem[valueProp], subitem);
1769
+ }
1770
+
1771
+ this._addSubitemsToValueMap(item._subitems, itemByValueMap, valueProp);
1772
+ }
1720
1773
  }
1721
1774
 
1722
1775
  /** @private */
@@ -1916,6 +1969,12 @@ class SelectBox {
1916
1969
  p.resortBySelectionNeeded = true;
1917
1970
  }
1918
1971
 
1972
+ const preventDropListMouseDownBlur = (event) => {
1973
+ const li = closestUntil(event.target, 'li', event.currentTarget);
1974
+ if (!li) return;
1975
+
1976
+ event.preventDefault();
1977
+ };
1919
1978
  const dropList = p.dropList = new DropList({
1920
1979
  virtualMinItems: 10,
1921
1980
 
@@ -2101,7 +2160,8 @@ class SelectBox {
2101
2160
  }
2102
2161
  break;
2103
2162
 
2104
- case 'select': {
2163
+ case 'select':
2164
+ case 'subitems:select': {
2105
2165
  if (p.multi) return;
2106
2166
 
2107
2167
  const item = event.item;
@@ -2122,6 +2182,14 @@ class SelectBox {
2122
2182
  }
2123
2183
  break;
2124
2184
 
2185
+ case 'show_subitems':
2186
+ p.sink.add(event.droplist.el, 'mousedown.subitems', preventDropListMouseDownBlur);
2187
+ break;
2188
+
2189
+ case 'hide_subitems':
2190
+ p.sink.remove(event.droplist.el, '.subitems');
2191
+ break;
2192
+
2125
2193
  case 'blur':
2126
2194
  this._handleOnBlur();
2127
2195
  break;
@@ -2137,12 +2205,7 @@ class SelectBox {
2137
2205
  },
2138
2206
  });
2139
2207
 
2140
- p.sink.add(dropList.el, 'mousedown', (event) => {
2141
- const li = closestUntil(event.target, 'li', event.currentTarget);
2142
- if (!li) return;
2143
-
2144
- event.preventDefault();
2145
- });
2208
+ p.sink.add(dropList.el, 'mousedown', preventDropListMouseDownBlur);
2146
2209
 
2147
2210
  this._registerDropdownEvents();
2148
2211
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@danielgindi/selectbox",
3
- "version": "2.0.52",
3
+ "version": "2.0.54",
4
4
  "description": "A collection of dom utilities. So you can work natively with the dom without dom frameworks.",
5
5
  "main": "dist/lib.cjs.min.js",
6
6
  "module": "lib/index.js",
@@ -18,6 +18,7 @@
18
18
  "lib": "lib"
19
19
  },
20
20
  "scripts": {
21
+ "benchmark": "node ./benchmarks/selectbox-value-lookup.mjs",
21
22
  "build": "npm run lint && node ./scripts/build.js",
22
23
  "lint": "eslint -f codeframe ./",
23
24
  "lint-fix": "eslint -f codeframe --fix ./",
@@ -31,26 +32,20 @@
31
32
  "homepage": "https://github.com/danielgindi/selectbox#readme",
32
33
  "license": "MIT",
33
34
  "devDependencies": {
34
- "@babel/core": "^7.29.0",
35
- "@babel/preset-env": "^7.29.0",
36
- "@babel/runtime": "^7.28.6",
37
- "@eslint/eslintrc": "^3.3.4",
38
- "@eslint/js": "^9.39.3",
39
- "@rollup/plugin-babel": "^6.1.0",
40
- "@rollup/plugin-commonjs": "^29.0.0",
41
- "@rollup/plugin-node-resolve": "^16.0.3",
42
- "@rollup/plugin-terser": "^0.4.4",
43
- "core-js": "^3.48.0",
44
- "eslint": "^9.39.3",
35
+ "@eslint/eslintrc": "^3.3.6",
36
+ "@eslint/js": "^10.0.1",
37
+ "eslint": "^10.7.0",
45
38
  "eslint-formatter-codeframe": "^7.32.2",
46
- "eslint-plugin-vue": "^9.33.0",
47
- "fs-extra": "^11.3.3",
48
- "globals": "^17.4.0",
39
+ "eslint-plugin-vue": "^10.9.2",
40
+ "globals": "^17.7.0",
49
41
  "husky": "^9.1.7",
42
+ "magic-string": "^0.30.21",
50
43
  "pinst": "^3.0.0",
51
- "rollup": "^4.59.0",
52
- "sass": "^1.97.3",
53
- "vite": "^8.1.0"
44
+ "sass": "^1.101.0",
45
+ "terser": "^5.49.0",
46
+ "vite": "^8.1.5",
47
+ "vue": "^3.5.40",
48
+ "vue-eslint-parser": "^10.4.1"
54
49
  },
55
50
  "dependencies": {
56
51
  "@danielgindi/dom-utils": "^1.0.11",