@nectary/components 4.2.0 → 4.3.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nectary/components",
3
- "version": "4.2.0",
3
+ "version": "4.3.0",
4
4
  "files": [
5
5
  "**/*/*.css",
6
6
  "**/*/*.json",
@@ -1,8 +1,9 @@
1
1
  import '../input';
2
2
  import '../icon';
3
3
  import '../text';
4
+ import { isSelectMenuOption } from '../select-menu-option/utils';
4
5
  import { attrValueToPixels, defineCustomElement, getAttribute, getBooleanAttribute, unpackCsv, getFirstCsvValue, getIntegerAttribute, getReactEventHandler, isAttrTrue, NectaryElement, updateAttribute, updateBooleanAttribute, updateCsv, updateExplicitBooleanAttribute, updateIntegerAttribute, debounceTimeout, setClass, subscribeContext, hasClass, isTargetEqual } from '../utils';
5
- const templateHTML = '<style>:host{display:block;outline:0}#listbox{overflow-y:auto}#search{display:none;margin:10px}#search.active{display:block}#search-clear:not(.active){display:none}#not-found{display:flex;align-items:center;justify-content:center;width:100%;height:30px;margin-bottom:10px;pointer-events:none;user-select:none;--sinch-comp-text-font:var(--sinch-comp-select-menu-font-not-found-text);--sinch-global-color-text:var(--sinch-comp-select-menu-color-default-not-found-text-initial)}#not-found:not(.active){display:none}::slotted(.hidden){display:none}</style><sinch-input id="search" size="s" placeholder="Search"><sinch-icon icons-version="2" name="magnifying-glass" id="icon-search" slot="icon"></sinch-icon><sinch-button id="search-clear" slot="right"><sinch-icon icons-version="2" name="fa-xmark" slot="icon"></sinch-icon></sinch-button></sinch-input><div id="not-found"><sinch-text type="m">No results</sinch-text></div><div id="listbox" role="presentation"><slot></slot></div>';
6
+ const templateHTML = '<style>:host{display:block;outline:0}#listbox{overflow-y:auto}#search{display:none;margin:10px}#search.active{display:block}#search-clear:not(.active){display:none}#not-found{display:flex;align-items:center;justify-content:center;width:100%;height:30px;margin-bottom:10px;pointer-events:none;user-select:none;--sinch-comp-text-font:var(--sinch-comp-select-menu-font-not-found-text);--sinch-global-color-text:var(--sinch-comp-select-menu-color-default-not-found-text-initial)}#not-found:not(.active){display:none}::slotted(.hidden){display:none}::slotted(sinch-title){padding:8px 16px;--sinch-global-color-text:var(--sinch-comp-select-menu-color-default-title-initial)}</style><sinch-input id="search" size="s" placeholder="Search"><sinch-icon icons-version="2" name="magnifying-glass" id="icon-search" slot="icon"></sinch-icon><sinch-button id="search-clear" slot="right"><sinch-icon icons-version="2" name="fa-xmark" slot="icon"></sinch-icon></sinch-button></sinch-input><div id="not-found"><sinch-text type="m">No results</sinch-text></div><div id="listbox" role="presentation"><slot></slot></div>';
6
7
  const ITEM_HEIGHT = 40;
7
8
  const NUM_ITEMS_SEARCH = 7;
8
9
  const template = document.createElement('template');
@@ -70,9 +71,12 @@ defineCustomElement('sinch-select-menu', class extends NectaryElement {
70
71
  }
71
72
  case 'rows':
72
73
  {
74
+ const numberOfItems = this.#$optionSlot.assignedElements().length;
75
+ const maxNumberOfRows = parseInt(newVal ?? '0', 10);
73
76
  this.#$listbox.style.maxHeight = attrValueToPixels(newVal, {
74
77
  min: 2,
75
- itemSizeMultiplier: ITEM_HEIGHT
78
+ itemSizeMultiplier: ITEM_HEIGHT,
79
+ addExtraSpace: numberOfItems > maxNumberOfRows
76
80
  });
77
81
  break;
78
82
  }
@@ -142,6 +146,9 @@ defineCustomElement('sinch-select-menu', class extends NectaryElement {
142
146
  };
143
147
  #onListboxClick = e => {
144
148
  const $elem = e.target;
149
+ if ($elem == null || !($elem instanceof Element) || !isSelectMenuOption($elem)) {
150
+ return;
151
+ }
145
152
  this.focus();
146
153
  if (!isTargetEqual(e, this.#$listbox) && !getBooleanAttribute($elem, 'disabled')) {
147
154
  this.#selectOption($elem);
@@ -231,7 +238,7 @@ defineCustomElement('sinch-select-menu', class extends NectaryElement {
231
238
  }
232
239
  #onOptionSlotChange = () => {
233
240
  const searchable = this.searchable;
234
- const options = this.#$optionSlot.assignedElements();
241
+ const options = this.#getOptionElements();
235
242
  const isEnoughOptions = options.length >= NUM_ITEMS_SEARCH;
236
243
  const isSearchActive = isEnoughOptions && searchable !== false || Boolean(searchable);
237
244
  if (!isSearchActive) {
@@ -314,7 +321,8 @@ defineCustomElement('sinch-select-menu', class extends NectaryElement {
314
321
  }
315
322
  }
316
323
  #getOptionElements() {
317
- return this.#$optionSlot.assignedElements();
324
+ const assignedElements = this.#$optionSlot.assignedElements();
325
+ return assignedElements.filter(isSelectMenuOption);
318
326
  }
319
327
  #getSelectedOptionIndex() {
320
328
  const elements = this.#getOptionElements();
@@ -0,0 +1,2 @@
1
+ import type { TSinchSelectMenuOptionElement } from './types';
2
+ export declare const isSelectMenuOption: (el: Element) => el is TSinchSelectMenuOptionElement;
@@ -0,0 +1 @@
1
+ export const isSelectMenuOption = el => el.localName === 'sinch-select-menu-option';
package/utils/dom.d.ts CHANGED
@@ -17,6 +17,7 @@ type IntegerOptions = {
17
17
  defaultValue?: number | null;
18
18
  itemSizeMultiplier?: number;
19
19
  itemSpaceBetween?: number;
20
+ addExtraSpace?: boolean;
20
21
  };
21
22
  export declare const attrValueToInteger: (value: string | null, options?: IntegerOptions) => number | null;
22
23
  export declare const attrValueToPixels: (value: string | null, options?: IntegerOptions) => string;
package/utils/dom.js CHANGED
@@ -72,7 +72,8 @@ export const attrValueToInteger = function (value) {
72
72
  const {
73
73
  defaultValue = null,
74
74
  itemSizeMultiplier = 1,
75
- itemSpaceBetween = 0
75
+ itemSpaceBetween = 0,
76
+ addExtraSpace = false
76
77
  } = options;
77
78
  let intValue = defaultValue;
78
79
  if (value !== null) {
@@ -82,7 +83,7 @@ export const attrValueToInteger = function (value) {
82
83
  }
83
84
  }
84
85
  if (intValue !== null) {
85
- return intValue * itemSizeMultiplier + Math.max(intValue - 1, 0) * itemSpaceBetween;
86
+ return intValue * itemSizeMultiplier + Math.max(intValue - 1, 0) * itemSpaceBetween + (addExtraSpace ? 0.5 * itemSizeMultiplier : 0);
86
87
  }
87
88
  return null;
88
89
  };