@danielgindi/selectbox 2.0.54 → 2.0.56

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
@@ -48,6 +48,7 @@ const hasOwnProperty = Object.prototype.hasOwnProperty;
48
48
  * @property {number} [autoItemBlurDelay=300] How long to wait before deciding to blur the focused item (when the droplist loses focus)?
49
49
  * @property {boolean} [capturesFocus=true] Should this DropList be added to the TAB-key stack?
50
50
  * @property {boolean} [multi=false] Does this DropList show checkboxes for multiple item selection?
51
+ * @property {*} [singleSelectedValue] Current selected value when using single selection.
51
52
  * @property {function} [keyDownHandler=null] An alternative "keydown" event handler. Return true to prevent default behaviour.
52
53
  * @property {boolean} [autoCheckGroupChildren=true] When a group is checked/unchecked - all items beneath it will update accordingly
53
54
  * @property {boolean} [useExactTargetWidth=false] Use the exact target's width, do not allow growing
@@ -122,6 +123,7 @@ export const DefaultOptions = {
122
123
  autoItemBlurDelay: 300,
123
124
  capturesFocus: true,
124
125
  multi: false,
126
+ singleSelectedValue: undefined,
125
127
  keyDownHandler: null,
126
128
  autoCheckGroupChildren: true,
127
129
  useExactTargetWidth: false,
@@ -193,6 +195,7 @@ class DropList {
193
195
  autoItemBlurDelay: o.autoItemBlurDelay,
194
196
  capturesFocus: o.capturesFocus,
195
197
  multi: o.multi,
198
+ singleSelectedValue: o.singleSelectedValue,
196
199
  keyDownHandler: o.keyDownHandler,
197
200
  autoCheckGroupChildren: o.autoCheckGroupChildren,
198
201
  useExactTargetWidth: o.useExactTargetWidth,
@@ -342,28 +345,37 @@ class DropList {
342
345
 
343
346
  const classList = itemEl.classList;
344
347
 
345
- if (p.multi) {
346
- if (item._nocheck) {
347
- classList.add(`${p.baseClassName}__item_multi_nocheck`);
348
+ if (item) {
349
+ if (p.multi) {
350
+ if (item._nocheck) {
351
+ classList.add(`${p.baseClassName}__item_multi_nocheck`);
352
+ } else {
353
+ classList.add(`${p.baseClassName}__item_multi`);
354
+
355
+ if (item._checked) {
356
+ classList.add(`${p.baseClassName}__item_checked`);
357
+ }
358
+ }
348
359
  } else {
349
- classList.add(`${p.baseClassName}__item_multi`);
360
+ classList.add(`${p.baseClassName}__item_single`);
350
361
 
351
- if (item._checked) {
352
- classList.add(`${p.baseClassName}__item_checked`);
362
+ if (p.singleSelectedValue !== undefined && item.value === p.singleSelectedValue) {
363
+ classList.add(`${p.baseClassName}__item_selected`);
364
+ p.singleSelectedItemEl = itemEl;
365
+ } else if (p.singleSelectedItemEl === itemEl) {
366
+ p.singleSelectedItemEl = null;
353
367
  }
354
368
  }
355
- } else {
356
- classList.add(`${p.baseClassName}__item_single`);
357
- }
358
369
 
359
- if (item._group)
360
- classList.add(`${p.baseClassName}__item_group`);
370
+ if (item._group)
371
+ classList.add(`${p.baseClassName}__item_group`);
361
372
 
362
- if (item._child)
363
- classList.add(`${p.baseClassName}__item_child`);
373
+ if (item._child)
374
+ classList.add(`${p.baseClassName}__item_child`);
364
375
 
365
- if (item._nointeraction)
366
- classList.add(`${p.baseClassName}__item_nointeraction`);
376
+ if (item._nointeraction)
377
+ classList.add(`${p.baseClassName}__item_nointeraction`);
378
+ }
367
379
 
368
380
  if (p.focusItemIndex === index) {
369
381
  p.focusItemEl = itemEl;
@@ -707,7 +719,7 @@ class DropList {
707
719
  this.toggleFocusedItem();
708
720
  }
709
721
  } else {
710
- this._setSingleSelectedItemEl(p.focusItemEl);
722
+ this._setSingleSelectedItemEl(p.focusItemEl, item ? item.value : undefined);
711
723
 
712
724
  this._trigger('select', {
713
725
  value: item ? item.value : undefined,
@@ -2187,6 +2199,7 @@ class DropList {
2187
2199
  estimatedItemHeight: p.estimatedItemHeight,
2188
2200
  estimateWidth: p.estimateWidth,
2189
2201
  virtualMinItems: p.virtualMinItems,
2202
+ singleSelectedValue: p.singleSelectedValue,
2190
2203
  labelProp: p.labelProp,
2191
2204
  valueProp: p.valueProp,
2192
2205
  renderItem: p.renderItem,
@@ -2322,16 +2335,19 @@ class DropList {
2322
2335
  return this.setFocusedItemAtIndex(this.itemIndexByValue(value));
2323
2336
  }
2324
2337
 
2325
- setSingleSelectedItemAtIndex(itemIndex) {
2338
+ setSingleSelectedItemAtIndex(itemIndex, value) {
2326
2339
  const p = this._p;
2327
2340
 
2328
2341
  this.rushRefilter();
2329
2342
 
2330
2343
  let itemEl = null;
2344
+ let item;
2331
2345
 
2332
2346
  if (itemIndex > -1 && !p.items[itemIndex]._nointeraction) {
2347
+ item = p.items[itemIndex];
2348
+ value = item.value;
2349
+
2333
2350
  if (p.filteredItems) {
2334
- const item = p.items[itemIndex];
2335
2351
  itemIndex = p.filteredItems.indexOf(item);
2336
2352
  }
2337
2353
 
@@ -2340,7 +2356,7 @@ class DropList {
2340
2356
  }
2341
2357
  }
2342
2358
 
2343
- this._setSingleSelectedItemEl(itemEl);
2359
+ this._setSingleSelectedItemEl(itemEl, value);
2344
2360
 
2345
2361
  return this;
2346
2362
  }
@@ -2358,7 +2374,7 @@ class DropList {
2358
2374
  }
2359
2375
 
2360
2376
  setSingleSelectedItemByValue(value) {
2361
- return this.setSingleSelectedItemAtIndex(this.itemIndexByValue(value));
2377
+ return this.setSingleSelectedItemAtIndex(this.itemIndexByValue(value), value);
2362
2378
  }
2363
2379
 
2364
2380
  next(event) {
@@ -2501,16 +2517,18 @@ class DropList {
2501
2517
  return itemIndex;
2502
2518
  }
2503
2519
 
2504
- _setSingleSelectedItemEl(itemEl) {
2520
+ _setSingleSelectedItemEl(itemEl, value) {
2505
2521
  const p = this._p;
2506
2522
 
2507
2523
  if (p.singleSelectedItemEl) {
2508
- p.singleSelectedItemEl.classList.remove(`${p.baseClassName}__item_checked`);
2524
+ p.singleSelectedItemEl.classList.remove(`${p.baseClassName}__item_selected`);
2509
2525
  p.singleSelectedItemEl = null;
2510
2526
  }
2511
2527
 
2512
- if (itemEl) {
2513
- itemEl.classList.add(`${p.baseClassName}__item_checked`);
2528
+ p.singleSelectedValue = value;
2529
+
2530
+ if (itemEl && value !== undefined) {
2531
+ itemEl.classList.add(`${p.baseClassName}__item_selected`);
2514
2532
  p.singleSelectedItemEl = itemEl;
2515
2533
  }
2516
2534
 
@@ -3374,8 +3392,11 @@ class DropList {
3374
3392
  setElementWidth(el, newOuterWidth, true, true);
3375
3393
 
3376
3394
  const elComputedStyle = getComputedStyle(el);
3377
- let bordersWidth = (parseFloat(elComputedStyle.borderLeftWidth) || 0) + (parseFloat(elComputedStyle.borderRightWidth) || 0);
3378
- let scrollWidth = el.scrollWidth + bordersWidth;
3395
+ let extraElWidth = (parseFloat(elComputedStyle.borderLeftWidth) || 0) +
3396
+ (parseFloat(elComputedStyle.borderRightWidth) || 0)+
3397
+ (parseFloat(elComputedStyle.paddingLeft) || 0)+
3398
+ (parseFloat(elComputedStyle.paddingRight) || 0);
3399
+ let scrollWidth = el.scrollWidth + extraElWidth;
3379
3400
  if (scrollWidth > newOuterWidth) {
3380
3401
  // consider scrollWidth delta
3381
3402
  let delta = scrollWidth - newOuterWidth;
package/lib/SelectBox.js CHANGED
@@ -1982,6 +1982,7 @@ class SelectBox {
1982
1982
  ...p.listOptions,
1983
1983
 
1984
1984
  multi: p.multi,
1985
+ singleSelectedValue: !p.multi ? this.getValue() : undefined,
1985
1986
  capturesFocus: false,
1986
1987
 
1987
1988
  labelProp: p.labelProp,
@@ -2565,11 +2566,12 @@ class SelectBox {
2565
2566
  if (p.multi) {
2566
2567
  p.dropList.setCheckedValues(p.selectedValues);
2567
2568
  } else {
2568
- const singleItemIndex = p.dropList.itemIndexByValue(this.getValue());
2569
+ const singleValue = this.getValue();
2570
+ const singleItemIndex = p.dropList.itemIndexByValue(singleValue);
2569
2571
 
2570
2572
  p.dropList
2571
2573
  .setFocusedItemAtIndex(singleItemIndex)
2572
- .setSingleSelectedItemAtIndex(singleItemIndex);
2574
+ .setSingleSelectedItemByValue(singleValue);
2573
2575
 
2574
2576
  hasRefocusedItem = p.dropList.hasFocusedItem();
2575
2577
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@danielgindi/selectbox",
3
- "version": "2.0.54",
3
+ "version": "2.0.56",
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",
@@ -39,7 +39,7 @@
39
39
  "eslint-plugin-vue": "^10.9.2",
40
40
  "globals": "^17.7.0",
41
41
  "husky": "^9.1.7",
42
- "magic-string": "^0.30.21",
42
+ "magic-string": "^1.0.0",
43
43
  "pinst": "^3.0.0",
44
44
  "sass": "^1.101.0",
45
45
  "terser": "^5.49.0",
@@ -32,17 +32,17 @@
32
32
  max-width: 100%;
33
33
  line-height: 1.3;
34
34
 
35
- .droplist_header,
36
- .droplist_footer {
37
- position: relative;
38
- padding: 0.5em;
39
- box-sizing: inherit;
40
- }
41
-
42
- .droplist_header {
43
- input[type=search] {
44
- box-sizing: inherit;
45
- width: 100%;
35
+ .droplist_header,
36
+ .droplist_footer {
37
+ position: relative;
38
+ padding: 0.5em;
39
+ box-sizing: inherit;
40
+ }
41
+
42
+ .droplist_header {
43
+ input[type=search] {
44
+ box-sizing: inherit;
45
+ width: 100%;
46
46
  }
47
47
  }
48
48
 
@@ -96,10 +96,16 @@
96
96
  overflow: hidden;
97
97
 
98
98
  &.droplist__item_focus {
99
- /* background-color: #4c8fbd; */
99
+ background-color: #c7c7c7;
100
+ }
101
+ &.droplist__item_selected {
100
102
  background-color: #3e3f48;
101
103
  color: #fff;
102
104
  }
105
+ &.droplist__item_selected.droplist__item_focus {
106
+ background-color: #2e3038;
107
+ color: #fff;
108
+ }
103
109
  }
104
110
 
105
111
  li {