@danielgindi/selectbox 1.0.36 → 1.0.44

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,5 +1,5 @@
1
1
  /*!
2
- * @danielgindi/selectbox 1.0.36
2
+ * @danielgindi/selectbox 1.0.44
3
3
  * git://github.com/danielgindi/selectbox.git
4
4
  */
5
5
  import { createElement, closestUntil, setElementAttrs, next, prev } from '@danielgindi/dom-utils/lib/Dom';
@@ -114,7 +114,7 @@ Emits the following events:
114
114
  'hide:before': the drop list will hide.
115
115
  'hide': the drop list was hidden.
116
116
  'hide:after': emitted after the hide css transition has ended, or immediately after 'hide'.
117
- 'check' {value, item, isCheckingGroup}: item was selected (in multi mode).
117
+ 'check' {value, item, checked: boolean, isGroup: boolean, isCheckingGroup: boolean}: item was selected (in multi mode).
118
118
  'groupcheck' {value, item, affectedItems}: item was selected (in multi mode).
119
119
  */
120
120
 
@@ -213,8 +213,13 @@ class DropList {
213
213
 
214
214
  if ((/**@type any*/index) === GhostSymbol) {
215
215
  item = {
216
- [p.labelProp]: p.lastMeasureLongestLabelText,
217
- [p.valueProp]: 'Measure',
216
+ label: p.lastMeasureLongestLabelText,
217
+ value: 'Measure',
218
+
219
+ [ItemSymbol$1]: {
220
+ [p.labelProp]: p.lastMeasureLongestLabelText,
221
+ [p.valueProp]: 'Measure',
222
+ },
218
223
  };
219
224
  itemEl.setAttribute('aria-hidden', 'true');
220
225
  } else {
@@ -268,7 +273,7 @@ class DropList {
268
273
  const fn = p.unrenderItem;
269
274
  p.virtualListHelper.setOnItemUnrender(el => {
270
275
  try {
271
- fn(el[ItemSymbol$1], el);
276
+ fn(el[ItemSymbol$1][ItemSymbol$1], el);
272
277
  } catch (err) { console.error(err); } // eslint-disable-line no-console
273
278
  delete el[ItemSymbol$1];
274
279
 
@@ -408,7 +413,7 @@ class DropList {
408
413
  if (!item)
409
414
  return;
410
415
 
411
- this._trigger('itemblur', { value: item[p.valueProp], item: item });
416
+ this._trigger('itemblur', { value: item.value, item: item[ItemSymbol$1] ?? item });
412
417
  }
413
418
 
414
419
  nextPage(event) {
@@ -430,7 +435,13 @@ class DropList {
430
435
  if (p.focusItemEl) {
431
436
  toggleClass(p.focusItemEl, `${p.baseClassName}__item_checked`, item._checked);
432
437
  }
433
- this._trigger('check', { value: item[p.valueProp], item: item });
438
+ this._trigger('check', {
439
+ value: item.value,
440
+ item: item[ItemSymbol$1] ?? item,
441
+ checked: item._checked,
442
+ isGroup: item._group,
443
+ isCheckingGroup: false,
444
+ });
434
445
 
435
446
  this._updateGroupStateForItem(item);
436
447
  }
@@ -455,7 +466,7 @@ class DropList {
455
466
  this._setSingleSelectedItemEl(p.focusItemEl);
456
467
  }
457
468
 
458
- this._trigger('select', { value: item ? item[p.valueProp] : undefined, item: item });
469
+ this._trigger('select', { value: item ? item.value : undefined, item: item[ItemSymbol$1] ?? item });
459
470
 
460
471
  return true;
461
472
  }
@@ -494,8 +505,8 @@ class DropList {
494
505
  //noinspection PointlessBooleanExpressionJS
495
506
  let item = {
496
507
  [ItemSymbol$1]: oitem,
497
- [labelProp]: oitem[labelProp],
498
- [valueProp]: oitem[valueProp],
508
+ label: oitem[labelProp],
509
+ value: oitem[valueProp],
499
510
  _nocheck: !!oitem._nocheck,
500
511
  _nointeraction: !!oitem._nointeraction,
501
512
  };
@@ -546,10 +557,10 @@ class DropList {
546
557
  item[ItemSymbol$1] = newItem;
547
558
 
548
559
  if (hasOwnProperty.call(newItem, p.labelProp))
549
- item[p.labelProp] = newItem[p.labelProp];
560
+ item.label = newItem[p.labelProp];
550
561
 
551
562
  if (hasOwnProperty.call(newItem, p.valueProp))
552
- item[p.valueProp] = newItem[p.valueProp];
563
+ item.value = newItem[p.valueProp];
553
564
 
554
565
  if (hasOwnProperty.call(newItem, '_nocheck'))
555
566
  item._nocheck = !!newItem._nocheck;
@@ -617,12 +628,12 @@ class DropList {
617
628
  }
618
629
 
619
630
  itemDataByValue(value) {
620
- const p = this._p, valueProp = p.valueProp;
631
+ const p = this._p;
621
632
 
622
633
  for (let i = 0, count = p.items.length; i < count; i++) {
623
634
  let item = p.items[i];
624
- if (item[valueProp] === value) {
625
- return item;
635
+ if (item.value === value) {
636
+ return item[ItemSymbol$1];
626
637
  }
627
638
  }
628
639
 
@@ -630,11 +641,11 @@ class DropList {
630
641
  }
631
642
 
632
643
  itemIndexByValue(value) {
633
- const p = this._p, valueProp = p.valueProp;
644
+ const p = this._p;
634
645
 
635
646
  for (let i = 0, count = p.items.length; i < count; i++) {
636
647
  let item = p.items[i];
637
- if (item[valueProp] === value) {
648
+ if (item.value === value) {
638
649
  return i;
639
650
  }
640
651
  }
@@ -643,11 +654,11 @@ class DropList {
643
654
  }
644
655
 
645
656
  itemIndexByValueOrLabel(value, label) {
646
- const p = this._p, labelProp = p.labelProp, valueProp = p.valueProp;
657
+ const p = this._p;
647
658
 
648
659
  for (let i = 0, count = p.items.length; i < count; i++) {
649
660
  let item = p.items[i];
650
- if (item[valueProp] === value || item[labelProp] === label) {
661
+ if (item.value === value || item.label === label) {
651
662
  return i;
652
663
  }
653
664
  }
@@ -656,7 +667,7 @@ class DropList {
656
667
  }
657
668
 
658
669
  items() {
659
- return this._p.items.slice(0);
670
+ return this._p.items.map(x => x[ItemSymbol$1]);
660
671
  }
661
672
 
662
673
  itemsReference() {
@@ -668,7 +679,7 @@ class DropList {
668
679
  }
669
680
 
670
681
  itemAtIndex(index) {
671
- return this._p.items[index];
682
+ return this._p.items[index]?.[ItemSymbol$1];
672
683
  }
673
684
 
674
685
  /**
@@ -948,13 +959,13 @@ class DropList {
948
959
  * @returns {DropList} self
949
960
  */
950
961
  setCheckedValues(values) {
951
- const p = this._p, valueProp = p.valueProp;
962
+ const p = this._p;
952
963
 
953
964
  let groupIndexes = [];
954
965
 
955
966
  for (let i = 0, count = p.items.length; i < count; i++) {
956
967
  let item = p.items[i];
957
- let checked = !item._nocheck && values.indexOf(item[valueProp]) !== -1;
968
+ let checked = !item._nocheck && values.indexOf(item.value) !== -1;
958
969
 
959
970
  if (item._group) {
960
971
  groupIndexes.push(i);
@@ -986,7 +997,7 @@ class DropList {
986
997
  * @returns {Array<*>} self
987
998
  */
988
999
  getCheckedValues(excludeGroups) {
989
- const p = this._p, valueProp = p.valueProp;
1000
+ const p = this._p;
990
1001
 
991
1002
  excludeGroups = excludeGroups && p.groupCount > 0;
992
1003
 
@@ -996,7 +1007,7 @@ class DropList {
996
1007
  let item = p.items[i];
997
1008
  if (!item._checked) continue;
998
1009
  if (excludeGroups && item._group) continue;
999
- values.push(item[valueProp]);
1010
+ values.push(item.value);
1000
1011
  }
1001
1012
 
1002
1013
  return values;
@@ -1019,7 +1030,7 @@ class DropList {
1019
1030
  let item = p.items[i];
1020
1031
  if (!item._checked) continue;
1021
1032
  if (excludeGroups && item._group) continue;
1022
- items.push(item);
1033
+ items.push(item[ItemSymbol$1]);
1023
1034
  }
1024
1035
 
1025
1036
  return items;
@@ -1198,7 +1209,7 @@ class DropList {
1198
1209
  itemElement.classList.add(`${p.baseClassName}__item_focus`);
1199
1210
  p.focusItemEl = itemElement;
1200
1211
 
1201
- this._trigger('itemfocus', { value: itemElement[p.valueProp], item: itemElement[ItemSymbol$1], event: null });
1212
+ this._trigger('itemfocus', { value: itemElement.value, item: itemElement[ItemSymbol$1], event: null });
1202
1213
  }
1203
1214
  }
1204
1215
 
@@ -1355,15 +1366,15 @@ class DropList {
1355
1366
  }
1356
1367
 
1357
1368
  _getItemIndex(item) {
1358
- const p = this._p, labelProp = p.labelProp, valueProp = p.valueProp;
1369
+ const p = this._p;
1359
1370
 
1360
1371
  let itemIndex = -1;
1361
1372
 
1362
1373
  if (item) {
1363
1374
  itemIndex = p.items.indexOf(item);
1364
1375
  if (itemIndex === -1) {
1365
- let value = (item && item[valueProp] !== undefined) ? item[valueProp] : item;
1366
- let label = (item && item[labelProp]) ? item[labelProp] : value;
1376
+ let value = (item && item.value !== undefined) ? item.value : item;
1377
+ let label = (item && item.label) ? item.label : value;
1367
1378
  itemIndex = this.itemIndexByValueOrLabel(value, label);
1368
1379
  }
1369
1380
  }
@@ -1599,7 +1610,7 @@ class DropList {
1599
1610
  }
1600
1611
 
1601
1612
  _keydownFreeType(evt) {
1602
- const p = this._p, labelProp = p.labelProp;
1613
+ const p = this._p;
1603
1614
 
1604
1615
  // noinspection JSDeprecatedSymbols
1605
1616
  let character = evt.key || String.fromCharCode(evt.keyCode);
@@ -1618,7 +1629,7 @@ class DropList {
1618
1629
  // These are all the possible matches
1619
1630
  for (let i = 0, count = p.items.length; i < count; i++) {
1620
1631
  item = p.items[i];
1621
- if (regex.test(item[labelProp])) {
1632
+ if (regex.test(item.label)) {
1622
1633
  matchIndices.push(i);
1623
1634
  }
1624
1635
  }
@@ -1631,7 +1642,7 @@ class DropList {
1631
1642
 
1632
1643
  for (let i = 0, count = p.items.length; i < count; i++) {
1633
1644
  item = p.items[i];
1634
- if (regex.test(item[labelProp])) {
1645
+ if (regex.test(item.label)) {
1635
1646
  matchIndices.push(i);
1636
1647
  }
1637
1648
  }
@@ -1705,7 +1716,7 @@ class DropList {
1705
1716
  p.focusItemIndex = itemIndex;
1706
1717
 
1707
1718
  const item = p.items[itemIndex];
1708
- this._trigger('itemfocus', { value: item[p.valueProp], item: item, event: event });
1719
+ this._trigger('itemfocus', { value: item.value, item: item[ItemSymbol$1] ?? item, event: event });
1709
1720
  }
1710
1721
 
1711
1722
  _delayBlur() {
@@ -1858,7 +1869,7 @@ class DropList {
1858
1869
  }
1859
1870
 
1860
1871
  _updateGroupStateForItem(item) {
1861
- const p = this._p, valueProp = p.valueProp;
1872
+ const p = this._p;
1862
1873
 
1863
1874
  if (p.multi && p.autoCheckGroupChildren) {
1864
1875
 
@@ -1895,11 +1906,17 @@ class DropList {
1895
1906
  }
1896
1907
 
1897
1908
  // Fire event
1898
- this._trigger('check', { value: next[valueProp], item: next, isCheckingGroup: true });
1909
+ this._trigger('check', {
1910
+ value: next.value,
1911
+ item: next[ItemSymbol$1] ?? next,
1912
+ checked: item._checked,
1913
+ isGroup: item._group,
1914
+ isCheckingGroup: true,
1915
+ });
1899
1916
  }
1900
1917
 
1901
1918
  // Fire event
1902
- this._trigger('groupcheck', { value: item[valueProp], item: item, affectedItems: affectedItems });
1919
+ this._trigger('groupcheck', { value: item.value, item: item[ItemSymbol$1] ?? item, affectedItems: affectedItems });
1903
1920
  } else if (p.groupCount > 0) {
1904
1921
  items = p.items;
1905
1922
  itemIndex = items.indexOf(item);
@@ -1961,7 +1978,13 @@ class DropList {
1961
1978
 
1962
1979
  if (fireEvents) {
1963
1980
  // Fire event
1964
- this._trigger('check', { value: groupItem[p.valueProp], item: groupItem });
1981
+ this._trigger('check', {
1982
+ value: groupItem.value,
1983
+ item: groupItem[ItemSymbol$1] ?? groupItem,
1984
+ checked: item._checked,
1985
+ isGroup: item._group,
1986
+ isCheckingGroup: false,
1987
+ });
1965
1988
  }
1966
1989
  }
1967
1990
  }
@@ -1977,7 +2000,6 @@ class DropList {
1977
2000
  const p = this._p;
1978
2001
 
1979
2002
  if (p.lastMeasureItemCount !== p.items.length) {
1980
- const labelProp = p.labelProp, valueProp = p.valueProp;
1981
2003
  let longestLabel = p.lastMeasureLongestLabel || 1,
1982
2004
  longestLabelText = p.lastMeasureLongestLabelText || '';
1983
2005
 
@@ -1986,9 +2008,9 @@ class DropList {
1986
2008
  i++) {
1987
2009
 
1988
2010
  const item = items[i];
1989
- let text = item[labelProp];
2011
+ let text = item.label;
1990
2012
  if (text == null)
1991
- text = item[valueProp];
2013
+ text = item.value;
1992
2014
  if (text == null)
1993
2015
  text = '';
1994
2016
 
@@ -2037,7 +2059,7 @@ class DropList {
2037
2059
  }
2038
2060
 
2039
2061
  _renderItemContent(item, itemEl) {
2040
- const p = this._p, labelProp = p.labelProp;
2062
+ const p = this._p;
2041
2063
 
2042
2064
  // NOTE: a "measure" item will not have full data of original item.
2043
2065
  // so for a custom renderer - we try to send original item, and fallback to our private list item.
@@ -2045,7 +2067,7 @@ class DropList {
2045
2067
  if (!p.renderItem || p.renderItem(item[ItemSymbol$1] || item, itemEl) === false) {
2046
2068
  itemEl.appendChild(createElement('span', {
2047
2069
  class: `${p.baseClassName}__item_label`,
2048
- textContent: item[labelProp],
2070
+ textContent: item.label,
2049
2071
  }));
2050
2072
 
2051
2073
  if (p.multi) {
@@ -2200,7 +2222,7 @@ const inputBackbufferCssProps = [
2200
2222
  * @property {boolean} [hasOpenIndicator=true] has open/close indicator?
2201
2223
  * @property {string} [placeholder=''] Placeholder text
2202
2224
  * @property {boolean} [sortSelectedItems=true] Should the selected items be sorted?
2203
- * @property {boolean} [sortListItems=true] Sort list items
2225
+ * @property {boolean} [sortListItems=false] Sort list items
2204
2226
  * @property {boolean} [sortListCheckedFirst=true] When sorting - put checked items first (applicable to `multi` mode only)
2205
2227
  * @property {*[]} [stickyValues]
2206
2228
  * @property {function(a: DropList.ItemBase, b: DropList.ItemBase):number} [sortItemComparator]
@@ -2242,7 +2264,7 @@ const defaultOptions = {
2242
2264
  hasOpenIndicator: true,
2243
2265
  placeholder: '',
2244
2266
  sortSelectedItems: true,
2245
- sortListItems: true,
2267
+ sortListItems: false,
2246
2268
  sortListCheckedFirst: true,
2247
2269
  stickyValues: null,
2248
2270
  sortItemComparator: null,
@@ -2370,6 +2392,7 @@ class SelectBox {
2370
2392
 
2371
2393
  items: [],
2372
2394
  filteredItems: null,
2395
+ currentItemsView: [], // contains the final version of items sent to DropList
2373
2396
  itemsChanged: true,
2374
2397
 
2375
2398
  sink: new DomEventsSink(),
@@ -3846,8 +3869,8 @@ class SelectBox {
3846
3869
  const item = /**@type DropList.Item*/event.item;
3847
3870
  const value = event.value;
3848
3871
 
3849
- let checked = item._checked;
3850
- if (item._group) return; // Ignore groups
3872
+ let checked = event.checked;
3873
+ if (event.isGroup) return; // Ignore groups
3851
3874
 
3852
3875
  let selEvt = { value: value, item: item, cancel: false };
3853
3876
  this._trigger((checked ? 'addsel' : 'removesel') + ':before', selEvt);
@@ -3991,16 +4014,26 @@ class SelectBox {
3991
4014
 
3992
4015
  switch (evt.key) {
3993
4016
  case VALUE_PAGE_UP:
3994
- dropList.previousPage(evt);
4017
+ if (dropList.isVisible())
4018
+ dropList.previousPage(evt);
3995
4019
  break;
3996
4020
  case VALUE_PAGE_DOWN:
3997
- dropList.nextPage(evt);
4021
+ if (dropList.isVisible())
4022
+ dropList.nextPage(evt);
3998
4023
  break;
3999
4024
  case VALUE_UP:
4000
- dropList.previous(evt);
4025
+ if (dropList.isVisible()) {
4026
+ dropList.previous(evt);
4027
+ } else {
4028
+ this._movePrev();
4029
+ }
4001
4030
  break;
4002
4031
  case VALUE_DOWN:
4003
- dropList.next(evt);
4032
+ if (dropList.isVisible()) {
4033
+ dropList.next(evt);
4034
+ } else {
4035
+ this._moveNext();
4036
+ }
4004
4037
  break;
4005
4038
 
4006
4039
  }
@@ -4008,7 +4041,7 @@ class SelectBox {
4008
4041
 
4009
4042
  case VALUE_SPACE:
4010
4043
  if (p.lastKeyAllowsSpaceToggle) {
4011
- if (dropList.hasFocusedItem()) {
4044
+ if (dropList.isVisible() && dropList.hasFocusedItem()) {
4012
4045
  suppressKeyPress = true;
4013
4046
  suppressKeyPressRepeat = true;
4014
4047
  if (p.multi)
@@ -4030,7 +4063,7 @@ class SelectBox {
4030
4063
  break;
4031
4064
 
4032
4065
  case VALUE_TAB:
4033
- if (dropList.hasFocusedItem()) {
4066
+ if (dropList.isVisible() && dropList.hasFocusedItem()) {
4034
4067
  dropList.triggerItemSelection(evt);
4035
4068
  }
4036
4069
  break;
@@ -4087,18 +4120,27 @@ class SelectBox {
4087
4120
 
4088
4121
  switch (evt.key) {
4089
4122
  case VALUE_PAGE_UP:
4090
- dropList.previousPage(evt);
4123
+ if (dropList.isVisible())
4124
+ dropList.previousPage(evt);
4091
4125
  break;
4092
4126
  case VALUE_PAGE_DOWN:
4093
- dropList.nextPage(evt);
4127
+ if (dropList.isVisible())
4128
+ dropList.nextPage(evt);
4094
4129
  break;
4095
4130
  case VALUE_UP:
4096
- dropList.previous(evt);
4131
+ if (dropList.isVisible()) {
4132
+ dropList.previous(evt);
4133
+ } else {
4134
+ this._movePrev();
4135
+ }
4097
4136
  break;
4098
4137
  case VALUE_DOWN:
4099
- dropList.next(evt);
4138
+ if (dropList.isVisible()) {
4139
+ dropList.next(evt);
4140
+ } else {
4141
+ this._moveNext();
4142
+ }
4100
4143
  break;
4101
-
4102
4144
  }
4103
4145
  break;
4104
4146
  }
@@ -4183,6 +4225,40 @@ class SelectBox {
4183
4225
  });
4184
4226
  }
4185
4227
 
4228
+ _movePrev() {
4229
+ const p = this._p;
4230
+
4231
+ if (this.isMultiEnabled()) return;
4232
+
4233
+ let selectedItems = this.getSelectedItems();
4234
+ let items = p.filteredItems ?? p.items;
4235
+ if (p.currentItemsView && p.currentItemsView.length === items.length)
4236
+ items = p.currentItemsView;
4237
+ if ((items.length + (p.clearable ? 1 : 0)) > 1) {
4238
+ let nextIndex = selectedItems.length > 0 ? items.indexOf(selectedItems[0]) - 1 : (items.length - 1);
4239
+ if (nextIndex === -1 && !p.clearable)
4240
+ nextIndex = items.length - 1;
4241
+ this.setSelectedItems(nextIndex === -1 ? [] : [items[nextIndex]]);
4242
+ }
4243
+ }
4244
+
4245
+ _moveNext() {
4246
+ const p = this._p;
4247
+
4248
+ if (this.isMultiEnabled()) return;
4249
+
4250
+ let selectedItems = this.getSelectedItems();
4251
+ let items = p.filteredItems ?? p.items;
4252
+ if (p.currentItemsView && p.currentItemsView.length === items.length)
4253
+ items = p.currentItemsView;
4254
+ if ((items.length + (p.clearable ? 1 : 0)) > 1) {
4255
+ let nextIndex = selectedItems.length > 0 ? items.indexOf(selectedItems[0]) + 1 : 0;
4256
+ if (nextIndex === items.length)
4257
+ nextIndex = p.clearable ? -1 : 0;
4258
+ this.setSelectedItems(nextIndex === -1 ? [] : [items[nextIndex]]);
4259
+ }
4260
+ }
4261
+
4186
4262
  /** @private */
4187
4263
  _updateListItems() {
4188
4264
  const p = this._p;
@@ -4221,6 +4297,7 @@ class SelectBox {
4221
4297
  }
4222
4298
 
4223
4299
  dropList.addItems(items);
4300
+ p.currentItemsView = items;
4224
4301
  p.itemsChanged = false;
4225
4302
  p.selectionChanged = true;
4226
4303
  p.resortBySelectionNeeded = false;