@danielgindi/selectbox 1.0.39 → 1.0.46

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/lib/DropList.js CHANGED
@@ -131,7 +131,7 @@ Emits the following events:
131
131
  'hide:before': the drop list will hide.
132
132
  'hide': the drop list was hidden.
133
133
  'hide:after': emitted after the hide css transition has ended, or immediately after 'hide'.
134
- 'check' {value, item, isCheckingGroup}: item was selected (in multi mode).
134
+ 'check' {value, item, checked: boolean, isGroup: boolean, isCheckingGroup: boolean}: item was selected (in multi mode).
135
135
  'groupcheck' {value, item, affectedItems}: item was selected (in multi mode).
136
136
  */
137
137
 
@@ -181,7 +181,7 @@ class DropList {
181
181
  let classes = [p.baseClassName];
182
182
 
183
183
  if (p.additionalClasses) {
184
- classes = classes.concat(p.additionalClasses);
184
+ classes = classes.concat((p.additionalClasses + '').split('').filter(x => x));
185
185
  }
186
186
 
187
187
  const initialCss = {
@@ -230,8 +230,13 @@ class DropList {
230
230
 
231
231
  if ((/**@type any*/index) === GhostSymbol) {
232
232
  item = {
233
- [p.labelProp]: p.lastMeasureLongestLabelText,
234
- [p.valueProp]: 'Measure',
233
+ label: p.lastMeasureLongestLabelText,
234
+ value: 'Measure',
235
+
236
+ [ItemSymbol]: {
237
+ [p.labelProp]: p.lastMeasureLongestLabelText,
238
+ [p.valueProp]: 'Measure',
239
+ },
235
240
  };
236
241
  itemEl.setAttribute('aria-hidden', 'true');
237
242
  } else {
@@ -285,7 +290,7 @@ class DropList {
285
290
  const fn = p.unrenderItem;
286
291
  p.virtualListHelper.setOnItemUnrender(el => {
287
292
  try {
288
- fn(el[ItemSymbol], el);
293
+ fn(el[ItemSymbol][ItemSymbol], el);
289
294
  } catch (err) { console.error(err); } // eslint-disable-line no-console
290
295
  delete el[ItemSymbol];
291
296
 
@@ -425,7 +430,7 @@ class DropList {
425
430
  if (!item)
426
431
  return;
427
432
 
428
- this._trigger('itemblur', { value: item[p.valueProp], item: item });
433
+ this._trigger('itemblur', { value: item.value, item: item[ItemSymbol] ?? item });
429
434
  }
430
435
 
431
436
  nextPage(event) {
@@ -447,7 +452,13 @@ class DropList {
447
452
  if (p.focusItemEl) {
448
453
  toggleClass(p.focusItemEl, `${p.baseClassName}__item_checked`, item._checked);
449
454
  }
450
- this._trigger('check', { value: item[p.valueProp], item: item });
455
+ this._trigger('check', {
456
+ value: item.value,
457
+ item: item[ItemSymbol] ?? item,
458
+ checked: item._checked,
459
+ isGroup: item._group,
460
+ isCheckingGroup: false,
461
+ });
451
462
 
452
463
  this._updateGroupStateForItem(item);
453
464
  }
@@ -472,7 +483,7 @@ class DropList {
472
483
  this._setSingleSelectedItemEl(p.focusItemEl);
473
484
  }
474
485
 
475
- this._trigger('select', { value: item ? item[p.valueProp] : undefined, item: item });
486
+ this._trigger('select', { value: item ? item.value : undefined, item: item[ItemSymbol] ?? item });
476
487
 
477
488
  return true;
478
489
  }
@@ -511,8 +522,8 @@ class DropList {
511
522
  //noinspection PointlessBooleanExpressionJS
512
523
  let item = {
513
524
  [ItemSymbol]: oitem,
514
- [labelProp]: oitem[labelProp],
515
- [valueProp]: oitem[valueProp],
525
+ label: oitem[labelProp],
526
+ value: oitem[valueProp],
516
527
  _nocheck: !!oitem._nocheck,
517
528
  _nointeraction: !!oitem._nointeraction,
518
529
  };
@@ -563,10 +574,10 @@ class DropList {
563
574
  item[ItemSymbol] = newItem;
564
575
 
565
576
  if (hasOwnProperty.call(newItem, p.labelProp))
566
- item[p.labelProp] = newItem[p.labelProp];
577
+ item.label = newItem[p.labelProp];
567
578
 
568
579
  if (hasOwnProperty.call(newItem, p.valueProp))
569
- item[p.valueProp] = newItem[p.valueProp];
580
+ item.value = newItem[p.valueProp];
570
581
 
571
582
  if (hasOwnProperty.call(newItem, '_nocheck'))
572
583
  item._nocheck = !!newItem._nocheck;
@@ -634,12 +645,12 @@ class DropList {
634
645
  }
635
646
 
636
647
  itemDataByValue(value) {
637
- const p = this._p, valueProp = p.valueProp;
648
+ const p = this._p;
638
649
 
639
650
  for (let i = 0, count = p.items.length; i < count; i++) {
640
651
  let item = p.items[i];
641
- if (item[valueProp] === value) {
642
- return item;
652
+ if (item.value === value) {
653
+ return item[ItemSymbol];
643
654
  }
644
655
  }
645
656
 
@@ -647,11 +658,11 @@ class DropList {
647
658
  }
648
659
 
649
660
  itemIndexByValue(value) {
650
- const p = this._p, valueProp = p.valueProp;
661
+ const p = this._p;
651
662
 
652
663
  for (let i = 0, count = p.items.length; i < count; i++) {
653
664
  let item = p.items[i];
654
- if (item[valueProp] === value) {
665
+ if (item.value === value) {
655
666
  return i;
656
667
  }
657
668
  }
@@ -660,11 +671,11 @@ class DropList {
660
671
  }
661
672
 
662
673
  itemIndexByValueOrLabel(value, label) {
663
- const p = this._p, labelProp = p.labelProp, valueProp = p.valueProp;
674
+ const p = this._p;
664
675
 
665
676
  for (let i = 0, count = p.items.length; i < count; i++) {
666
677
  let item = p.items[i];
667
- if (item[valueProp] === value || item[labelProp] === label) {
678
+ if (item.value === value || item.label === label) {
668
679
  return i;
669
680
  }
670
681
  }
@@ -673,7 +684,7 @@ class DropList {
673
684
  }
674
685
 
675
686
  items() {
676
- return this._p.items.slice(0);
687
+ return this._p.items.map(x => x[ItemSymbol]);
677
688
  }
678
689
 
679
690
  itemsReference() {
@@ -685,7 +696,7 @@ class DropList {
685
696
  }
686
697
 
687
698
  itemAtIndex(index) {
688
- return this._p.items[index];
699
+ return this._p.items[index]?.[ItemSymbol];
689
700
  }
690
701
 
691
702
  /**
@@ -965,13 +976,13 @@ class DropList {
965
976
  * @returns {DropList} self
966
977
  */
967
978
  setCheckedValues(values) {
968
- const p = this._p, valueProp = p.valueProp;
979
+ const p = this._p;
969
980
 
970
981
  let groupIndexes = [];
971
982
 
972
983
  for (let i = 0, count = p.items.length; i < count; i++) {
973
984
  let item = p.items[i];
974
- let checked = !item._nocheck && values.indexOf(item[valueProp]) !== -1;
985
+ let checked = !item._nocheck && values.indexOf(item.value) !== -1;
975
986
 
976
987
  if (item._group) {
977
988
  groupIndexes.push(i);
@@ -1003,7 +1014,7 @@ class DropList {
1003
1014
  * @returns {Array<*>} self
1004
1015
  */
1005
1016
  getCheckedValues(excludeGroups) {
1006
- const p = this._p, valueProp = p.valueProp;
1017
+ const p = this._p;
1007
1018
 
1008
1019
  excludeGroups = excludeGroups && p.groupCount > 0;
1009
1020
 
@@ -1013,7 +1024,7 @@ class DropList {
1013
1024
  let item = p.items[i];
1014
1025
  if (!item._checked) continue;
1015
1026
  if (excludeGroups && item._group) continue;
1016
- values.push(item[valueProp]);
1027
+ values.push(item.value);
1017
1028
  }
1018
1029
 
1019
1030
  return values;
@@ -1036,7 +1047,7 @@ class DropList {
1036
1047
  let item = p.items[i];
1037
1048
  if (!item._checked) continue;
1038
1049
  if (excludeGroups && item._group) continue;
1039
- items.push(item);
1050
+ items.push(item[ItemSymbol]);
1040
1051
  }
1041
1052
 
1042
1053
  return items;
@@ -1215,7 +1226,7 @@ class DropList {
1215
1226
  itemElement.classList.add(`${p.baseClassName}__item_focus`);
1216
1227
  p.focusItemEl = itemElement;
1217
1228
 
1218
- this._trigger('itemfocus', { value: itemElement[p.valueProp], item: itemElement[ItemSymbol], event: null });
1229
+ this._trigger('itemfocus', { value: itemElement.value, item: itemElement[ItemSymbol], event: null });
1219
1230
  }
1220
1231
  }
1221
1232
 
@@ -1372,15 +1383,15 @@ class DropList {
1372
1383
  }
1373
1384
 
1374
1385
  _getItemIndex(item) {
1375
- const p = this._p, labelProp = p.labelProp, valueProp = p.valueProp;
1386
+ const p = this._p;
1376
1387
 
1377
1388
  let itemIndex = -1;
1378
1389
 
1379
1390
  if (item) {
1380
1391
  itemIndex = p.items.indexOf(item);
1381
1392
  if (itemIndex === -1) {
1382
- let value = (item && item[valueProp] !== undefined) ? item[valueProp] : item;
1383
- let label = (item && item[labelProp]) ? item[labelProp] : value;
1393
+ let value = (item && item.value !== undefined) ? item.value : item;
1394
+ let label = (item && item.label) ? item.label : value;
1384
1395
  itemIndex = this.itemIndexByValueOrLabel(value, label);
1385
1396
  }
1386
1397
  }
@@ -1616,7 +1627,7 @@ class DropList {
1616
1627
  }
1617
1628
 
1618
1629
  _keydownFreeType(evt) {
1619
- const p = this._p, labelProp = p.labelProp;
1630
+ const p = this._p;
1620
1631
 
1621
1632
  // noinspection JSDeprecatedSymbols
1622
1633
  let character = evt.key || String.fromCharCode(evt.keyCode);
@@ -1635,7 +1646,7 @@ class DropList {
1635
1646
  // These are all the possible matches
1636
1647
  for (let i = 0, count = p.items.length; i < count; i++) {
1637
1648
  item = p.items[i];
1638
- if (regex.test(item[labelProp])) {
1649
+ if (regex.test(item.label)) {
1639
1650
  matchIndices.push(i);
1640
1651
  }
1641
1652
  }
@@ -1648,7 +1659,7 @@ class DropList {
1648
1659
 
1649
1660
  for (let i = 0, count = p.items.length; i < count; i++) {
1650
1661
  item = p.items[i];
1651
- if (regex.test(item[labelProp])) {
1662
+ if (regex.test(item.label)) {
1652
1663
  matchIndices.push(i);
1653
1664
  }
1654
1665
  }
@@ -1722,7 +1733,7 @@ class DropList {
1722
1733
  p.focusItemIndex = itemIndex;
1723
1734
 
1724
1735
  const item = p.items[itemIndex];
1725
- this._trigger('itemfocus', { value: item[p.valueProp], item: item, event: event });
1736
+ this._trigger('itemfocus', { value: item.value, item: item[ItemSymbol] ?? item, event: event });
1726
1737
  }
1727
1738
 
1728
1739
  _delayBlur() {
@@ -1875,7 +1886,7 @@ class DropList {
1875
1886
  }
1876
1887
 
1877
1888
  _updateGroupStateForItem(item) {
1878
- const p = this._p, valueProp = p.valueProp;
1889
+ const p = this._p;
1879
1890
 
1880
1891
  if (p.multi && p.autoCheckGroupChildren) {
1881
1892
 
@@ -1912,11 +1923,17 @@ class DropList {
1912
1923
  }
1913
1924
 
1914
1925
  // Fire event
1915
- this._trigger('check', { value: next[valueProp], item: next, isCheckingGroup: true });
1926
+ this._trigger('check', {
1927
+ value: next.value,
1928
+ item: next[ItemSymbol] ?? next,
1929
+ checked: next._checked,
1930
+ isGroup: next._group,
1931
+ isCheckingGroup: true,
1932
+ });
1916
1933
  }
1917
1934
 
1918
1935
  // Fire event
1919
- this._trigger('groupcheck', { value: item[valueProp], item: item, affectedItems: affectedItems });
1936
+ this._trigger('groupcheck', { value: item.value, item: item[ItemSymbol] ?? item, affectedItems: affectedItems });
1920
1937
  } else if (p.groupCount > 0) {
1921
1938
  items = p.items;
1922
1939
  itemIndex = items.indexOf(item);
@@ -1978,7 +1995,13 @@ class DropList {
1978
1995
 
1979
1996
  if (fireEvents) {
1980
1997
  // Fire event
1981
- this._trigger('check', { value: groupItem[p.valueProp], item: groupItem });
1998
+ this._trigger('check', {
1999
+ value: groupItem.value,
2000
+ item: groupItem[ItemSymbol] ?? groupItem,
2001
+ checked: groupItem._checked,
2002
+ isGroup: groupItem._group,
2003
+ isCheckingGroup: false,
2004
+ });
1982
2005
  }
1983
2006
  }
1984
2007
  }
@@ -1994,7 +2017,6 @@ class DropList {
1994
2017
  const p = this._p;
1995
2018
 
1996
2019
  if (p.lastMeasureItemCount !== p.items.length) {
1997
- const labelProp = p.labelProp, valueProp = p.valueProp;
1998
2020
  let longestLabel = p.lastMeasureLongestLabel || 1,
1999
2021
  longestLabelText = p.lastMeasureLongestLabelText || '';
2000
2022
 
@@ -2003,9 +2025,9 @@ class DropList {
2003
2025
  i++) {
2004
2026
 
2005
2027
  const item = items[i];
2006
- let text = item[labelProp];
2028
+ let text = item.label;
2007
2029
  if (text == null)
2008
- text = item[valueProp];
2030
+ text = item.value;
2009
2031
  if (text == null)
2010
2032
  text = '';
2011
2033
 
@@ -2054,7 +2076,7 @@ class DropList {
2054
2076
  }
2055
2077
 
2056
2078
  _renderItemContent(item, itemEl) {
2057
- const p = this._p, labelProp = p.labelProp;
2079
+ const p = this._p;
2058
2080
 
2059
2081
  // NOTE: a "measure" item will not have full data of original item.
2060
2082
  // so for a custom renderer - we try to send original item, and fallback to our private list item.
@@ -2062,7 +2084,7 @@ class DropList {
2062
2084
  if (!p.renderItem || p.renderItem(item[ItemSymbol] || item, itemEl) === false) {
2063
2085
  itemEl.appendChild(createElement('span', {
2064
2086
  class: `${p.baseClassName}__item_label`,
2065
- textContent: item[labelProp],
2087
+ textContent: item.label,
2066
2088
  }));
2067
2089
 
2068
2090
  if (p.multi) {
package/lib/SelectBox.js CHANGED
@@ -83,7 +83,7 @@ const inputBackbufferCssProps = [
83
83
  * @property {boolean} [hasOpenIndicator=true] has open/close indicator?
84
84
  * @property {string} [placeholder=''] Placeholder text
85
85
  * @property {boolean} [sortSelectedItems=true] Should the selected items be sorted?
86
- * @property {boolean} [sortListItems=true] Sort list items
86
+ * @property {boolean} [sortListItems=false] Sort list items
87
87
  * @property {boolean} [sortListCheckedFirst=true] When sorting - put checked items first (applicable to `multi` mode only)
88
88
  * @property {*[]} [stickyValues]
89
89
  * @property {function(a: DropList.ItemBase, b: DropList.ItemBase):number} [sortItemComparator]
@@ -125,7 +125,7 @@ const defaultOptions = {
125
125
  hasOpenIndicator: true,
126
126
  placeholder: '',
127
127
  sortSelectedItems: true,
128
- sortListItems: true,
128
+ sortListItems: false,
129
129
  sortListCheckedFirst: true,
130
130
  stickyValues: null,
131
131
  sortItemComparator: null,
@@ -253,6 +253,7 @@ class SelectBox {
253
253
 
254
254
  items: [],
255
255
  filteredItems: null,
256
+ currentItemsView: [], // contains the final version of items sent to DropList
256
257
  itemsChanged: true,
257
258
 
258
259
  sink: new DomEventsSink(),
@@ -1729,8 +1730,8 @@ class SelectBox {
1729
1730
  const item = /**@type DropList.Item*/event.item;
1730
1731
  const value = event.value;
1731
1732
 
1732
- let checked = item._checked;
1733
- if (item._group) return; // Ignore groups
1733
+ let checked = event.checked;
1734
+ if (event.isGroup) return; // Ignore groups
1734
1735
 
1735
1736
  let selEvt = { value: value, item: item, cancel: false };
1736
1737
  this._trigger((checked ? 'addsel' : 'removesel') + ':before', selEvt);
@@ -2092,8 +2093,10 @@ class SelectBox {
2092
2093
 
2093
2094
  let selectedItems = this.getSelectedItems();
2094
2095
  let items = p.filteredItems ?? p.items;
2096
+ if (p.currentItemsView && p.currentItemsView.length === items.length)
2097
+ items = p.currentItemsView;
2095
2098
  if ((items.length + (p.clearable ? 1 : 0)) > 1) {
2096
- let nextIndex = selectedItems.length > 0 ? items.indexOf(selectedItems[0]) - 1 : (selectedItems.length - 1);
2099
+ let nextIndex = selectedItems.length > 0 ? items.indexOf(selectedItems[0]) - 1 : (items.length - 1);
2097
2100
  if (nextIndex === -1 && !p.clearable)
2098
2101
  nextIndex = items.length - 1;
2099
2102
  this.setSelectedItems(nextIndex === -1 ? [] : [items[nextIndex]]);
@@ -2107,9 +2110,11 @@ class SelectBox {
2107
2110
 
2108
2111
  let selectedItems = this.getSelectedItems();
2109
2112
  let items = p.filteredItems ?? p.items;
2113
+ if (p.currentItemsView && p.currentItemsView.length === items.length)
2114
+ items = p.currentItemsView;
2110
2115
  if ((items.length + (p.clearable ? 1 : 0)) > 1) {
2111
2116
  let nextIndex = selectedItems.length > 0 ? items.indexOf(selectedItems[0]) + 1 : 0;
2112
- if (nextIndex === selectedItems.length)
2117
+ if (nextIndex === items.length)
2113
2118
  nextIndex = p.clearable ? -1 : 0;
2114
2119
  this.setSelectedItems(nextIndex === -1 ? [] : [items[nextIndex]]);
2115
2120
  }
@@ -2153,6 +2158,7 @@ class SelectBox {
2153
2158
  }
2154
2159
 
2155
2160
  dropList.addItems(items);
2161
+ p.currentItemsView = items;
2156
2162
  p.itemsChanged = false;
2157
2163
  p.selectionChanged = true;
2158
2164
  p.resortBySelectionNeeded = false;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@danielgindi/selectbox",
3
- "version": "1.0.39",
3
+ "version": "1.0.46",
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",
@@ -31,27 +31,30 @@
31
31
  "homepage": "https://github.com/danielgindi/selectbox#readme",
32
32
  "license": "MIT",
33
33
  "devDependencies": {
34
- "@babel/core": "^7.16.0",
35
- "@babel/preset-env": "^7.16.0",
36
- "@babel/runtime": "^7.16.3",
34
+ "@babel/core": "^7.16.7",
35
+ "@babel/preset-env": "^7.16.8",
36
+ "@babel/runtime": "^7.16.7",
37
37
  "@rollup/plugin-babel": "^5.3.0",
38
38
  "@rollup/plugin-commonjs": "^21.0.1",
39
- "@rollup/plugin-node-resolve": "^13.0.6",
40
- "core-js": "^3.19.1",
41
- "eslint": "^8.2.0",
39
+ "@rollup/plugin-node-resolve": "^13.1.3",
40
+ "core-js": "^3.20.3",
41
+ "eslint": "^8.7.0",
42
42
  "eslint-formatter-codeframe": "^7.32.1",
43
- "eslint-plugin-vue": "^8.0.3",
43
+ "eslint-plugin-vue": "^8.3.0",
44
44
  "fs-extra": "^10.0.0",
45
45
  "husky": "^7.0.4",
46
46
  "pinst": "^2.1.6",
47
- "rollup": "^2.59.0",
47
+ "rollup": "^2.64.0",
48
48
  "rollup-plugin-terser": "^7.0.2",
49
- "sass": "^1.43.4"
49
+ "sass": "^1.48.0"
50
50
  },
51
51
  "dependencies": {
52
- "@danielgindi/dom-utils": "^1.0.5",
52
+ "@danielgindi/dom-utils": "^1.0.6",
53
53
  "@danielgindi/virtual-list-helper": "^1.0.3",
54
54
  "keycode-js": "^3.1.0",
55
55
  "mitt": "^3.0.0"
56
+ },
57
+ "peerDependencies": {
58
+ "vue": "^2.*"
56
59
  }
57
60
  }
package/vue/SelectBox.vue CHANGED
@@ -54,7 +54,7 @@
54
54
  },
55
55
  sortListItems: {
56
56
  type: Boolean,
57
- default: true,
57
+ default: false,
58
58
  },
59
59
  sortListCheckedFirst: {
60
60
  type: Boolean,
@@ -1,4 +1,4 @@
1
- import Vue, { Component as VueComponent } from 'vue';
1
+ import Vue from 'vue';
2
2
 
3
3
  const generateVNodeRenderer = vnode => {
4
4
  return new Vue({
@@ -20,7 +20,7 @@ const VueInstanceSymbol = Symbol('vue_instance');
20
20
 
21
21
  /**
22
22
  *
23
- * @param {VueComponent} vue
23
+ * @param {import('vue').Component} vue
24
24
  * @param {string} slotName
25
25
  * @param {function(item: *)?} onAfterUpdate
26
26
  * @returns {(function(item: *, parent: Element): void)|undefined}
@@ -72,7 +72,7 @@ const createSlotBasedRenderFunc = (vue, slotName, onAfterUpdate) => {
72
72
 
73
73
  /**
74
74
  *
75
- * @param {VueComponent} vue
75
+ * @param {import('vue').Component} vue
76
76
  * @param {string} slotName
77
77
  * @returns {(function(parent: Element): void)|undefined}
78
78
  */