@nectary/components 0.13.0 → 0.14.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.
@@ -0,0 +1,11 @@
1
+ import type { TSinchIconElement, TSinchIconReact } from '../types';
2
+ declare global {
3
+ namespace JSX {
4
+ interface IntrinsicElements {
5
+ 'sinch-icon-search': TSinchIconReact;
6
+ }
7
+ }
8
+ interface HTMLElementTagNameMap {
9
+ 'sinch-icon-search': TSinchIconElement;
10
+ }
11
+ }
@@ -0,0 +1,4 @@
1
+ import { defineCustomElement } from '../../utils';
2
+ import { createIconClass } from '../create-icon-class';
3
+ const templateHTML = '<svg viewBox="0 0 24 24" aria-hidden="true"><path d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 0 0 1.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 0 0-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 0 0 5.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14Zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14Z"/></svg>';
4
+ defineCustomElement('sinch-icon-search', createIconClass(templateHTML));
package/index.d.ts CHANGED
@@ -48,6 +48,7 @@ import './icon/north';
48
48
  import './icon/north-east';
49
49
  import './icon/north-west';
50
50
  import './icon/open-in-new';
51
+ import './icon/search';
51
52
  import './icon/south';
52
53
  import './icon/south-east';
53
54
  import './icon/south-west';
@@ -79,3 +80,5 @@ import './dialog';
79
80
  import './grid';
80
81
  import './grid-item';
81
82
  import './icon-button';
83
+ import './search';
84
+ import './search-option';
package/index.js CHANGED
@@ -48,6 +48,7 @@ import './icon/north';
48
48
  import './icon/north-east';
49
49
  import './icon/north-west';
50
50
  import './icon/open-in-new';
51
+ import './icon/search';
51
52
  import './icon/south';
52
53
  import './icon/south-east';
53
54
  import './icon/south-west';
@@ -78,4 +79,6 @@ import './icon-branded/users';
78
79
  import './dialog';
79
80
  import './grid';
80
81
  import './grid-item';
81
- import './icon-button';
82
+ import './icon-button';
83
+ import './search';
84
+ import './search-option';
package/input/index.d.ts CHANGED
@@ -1,6 +1,9 @@
1
1
  import type { TSinchElementReact } from '../types';
2
2
  import type { DOMAttributes, FocusEvent, SyntheticEvent } from 'react';
3
+ declare const inputTypes: readonly ["text", "password"];
4
+ export declare type TTextInputType = typeof inputTypes[number];
3
5
  export declare type TSinchInputElement = HTMLElement & {
6
+ type: TTextInputType;
4
7
  value: string;
5
8
  label: string;
6
9
  placeholder: string | null;
@@ -12,6 +15,7 @@ export declare type TSinchInputElement = HTMLElement & {
12
15
  blur(): void;
13
16
  };
14
17
  export declare type TSinchInputReact = TSinchElementReact<TSinchInputElement> & {
18
+ type?: TTextInputType;
15
19
  value: string;
16
20
  label: string;
17
21
  placeholder?: string;
@@ -35,3 +39,4 @@ declare global {
35
39
  'sinch-input': TSinchInputElement;
36
40
  }
37
41
  }
42
+ export {};
package/input/index.js CHANGED
@@ -7,8 +7,9 @@ function _classPrivateFieldInitSpec(obj, privateMap, value) { _checkPrivateRedec
7
7
 
8
8
  function _checkPrivateRedeclaration(obj, privateCollection) { if (privateCollection.has(obj)) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } }
9
9
 
10
- import { defineCustomElement, getAttribute, getBooleanAttribute, isAttrTrue, updateAttribute, updateBooleanAttribute } from '../utils';
11
- const templateHTML = '<style>:host{display:inline-block;vertical-align:middle;outline:0;--sinch-color-icon:var(--sinch-color-stormy-500)}#wrapper{width:100%;box-sizing:border-box}#input{all:initial;border:1px solid var(--sinch-color-stormy-200);box-sizing:border-box;border-radius:4px;width:100%;height:48px;margin:2px 0;padding:0 12px;font:var(--sinch-font-body);color:var(--sinch-color-text-default);caret-color:var(--sinch-caret-color,auto)}#input::placeholder{font:var(--sinch-font-body);color:var(--sinch-color-text-muted)}#input:disabled{border-color:var(--sinch-color-snow-500);color:var(--sinch-color-stormy-100)}#input:disabled::placeholder{color:var(--sinch-color-snow-500)}#input:focus{border-color:var(--sinch-color-stormy-600)}:host([invalidtext]:not([invalidtext=""])) #input:not(:disabled){border-color:var(--sinch-color-text-invalid)}#bottom,#top{display:flex;align-items:baseline}#top{height:24px}#bottom{height:20px}#additional,#invalid,#label,#optional{overflow:hidden;text-overflow:ellipsis;white-space:nowrap}#label{font:var(--sinch-font-title-4);color:var(--sinch-color-text-default)}#optional{flex:1;text-align:right;font:var(--sinch-font-small-text);color:var(--sinch-color-text-muted)}#additional{flex:1;text-align:right;font:var(--sinch-font-extra-small-text);color:var(--sinch-color-text-muted)}#invalid{font:var(--sinch-font-extra-small-text);color:var(--sinch-color-text-invalid)}::slotted(sinch-help-tooltip){align-self:center;margin:0 8px}:host([disabled]:not([disabled=false])) :is(#label,#additional,#optional,#invalid){color:var(--sinch-color-stormy-100)}:host([disabled]:not([disabled=false])){--sinch-color-icon:var(--sinch-color-stormy-100)}</style><div id="wrapper"><div id="top"><label id="label" for="input"></label><slot name="tooltip"></slot><span id="optional"></span></div><input id="input" type="text"/><div id="bottom"><span id="invalid"></span> <span id="additional"></span></div></div>';
10
+ import { defineCustomElement, getAttribute, getBooleanAttribute, getLiteralAttribute, isAttrTrue, updateAttribute, updateBooleanAttribute, updateLiteralAttribute } from '../utils';
11
+ const templateHTML = '<style>:host{display:inline-block;vertical-align:middle;outline:0;--sinch-color-icon:var(--sinch-color-stormy-500)}#wrapper{width:100%;box-sizing:border-box}#input{all:initial;border:1px solid var(--sinch-color-stormy-200);box-sizing:border-box;border-radius:4px;width:100%;height:48px;margin:2px 0;padding:0 12px;font:var(--sinch-font-body);color:var(--sinch-color-text-default);caret-color:var(--sinch-caret-color,auto)}#input::placeholder{font:var(--sinch-font-body);color:var(--sinch-color-text-muted)}#input:disabled{border-color:var(--sinch-color-snow-500);color:var(--sinch-color-stormy-100)}#input:disabled::placeholder{color:var(--sinch-color-snow-500)}#input:focus{border-color:var(--sinch-color-stormy-600)}#input[type=password]{font-size:1.5em;letter-spacing:.1em}:host([invalidtext]:not([invalidtext=""])) #input:not(:disabled){border-color:var(--sinch-color-text-invalid)}#bottom,#top{display:flex;align-items:baseline}#top{height:24px}#bottom{height:20px}#additional,#invalid,#label,#optional{overflow:hidden;text-overflow:ellipsis;white-space:nowrap}#label{font:var(--sinch-font-title-4);color:var(--sinch-color-text-default)}#optional{flex:1;text-align:right;font:var(--sinch-font-small-text);color:var(--sinch-color-text-muted)}#additional{flex:1;text-align:right;font:var(--sinch-font-extra-small-text);color:var(--sinch-color-text-muted)}#invalid{font:var(--sinch-font-extra-small-text);color:var(--sinch-color-text-invalid)}::slotted(sinch-help-tooltip){align-self:center;margin:0 8px}:host([disabled]:not([disabled=false])) :is(#label,#additional,#optional,#invalid){color:var(--sinch-color-stormy-100)}:host([disabled]:not([disabled=false])){--sinch-color-icon:var(--sinch-color-stormy-100)}</style><div id="wrapper"><div id="top"><label id="label" for="input"></label><slot name="tooltip"></slot><span id="optional"></span></div><input id="input" type="text"/><div id="bottom"><span id="invalid"></span> <span id="additional"></span></div></div>';
12
+ const inputTypes = ['text', 'password'];
12
13
  const template = document.createElement('template');
13
14
  template.innerHTML = templateHTML;
14
15
  defineCustomElement('sinch-input', (_$input = new WeakMap(), _$label = new WeakMap(), _$optionalText = new WeakMap(), _$additionalText = new WeakMap(), _$invalidText = new WeakMap(), _selectionStart = new WeakMap(), _selectionEnd = new WeakMap(), _onInput = new WeakMap(), class extends HTMLElement {
@@ -115,17 +116,21 @@ defineCustomElement('sinch-input', (_$input = new WeakMap(), _$label = new WeakM
115
116
  }
116
117
 
117
118
  static get observedAttributes() {
118
- return ['value', 'placeholder', 'label', 'optionaltext', 'additionaltext', 'invalidtext', 'disabled'];
119
- }
120
-
121
- get type() {
122
- return 'text';
119
+ return ['type', 'value', 'placeholder', 'label', 'optionaltext', 'additionaltext', 'invalidtext', 'disabled'];
123
120
  }
124
121
 
125
122
  get nodeName() {
126
123
  return 'input';
127
124
  }
128
125
 
126
+ set type(value) {
127
+ updateAttribute(this, 'type', value);
128
+ }
129
+
130
+ get type() {
131
+ return getLiteralAttribute(this, inputTypes, 'type', 'text');
132
+ }
133
+
129
134
  set value(value) {
130
135
  updateAttribute(this, 'value', value);
131
136
  }
@@ -184,6 +189,12 @@ defineCustomElement('sinch-input', (_$input = new WeakMap(), _$label = new WeakM
184
189
 
185
190
  attributeChangedCallback(name, _, newVal) {
186
191
  switch (name) {
192
+ case 'type':
193
+ {
194
+ updateLiteralAttribute(_classPrivateFieldGet(this, _$input), inputTypes, 'type', newVal);
195
+ break;
196
+ }
197
+
187
198
  case 'value':
188
199
  {
189
200
  const nextVal = newVal ?? '';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nectary/components",
3
- "version": "0.13.0",
3
+ "version": "0.14.0",
4
4
  "files": [
5
5
  "theme.css",
6
6
  "**/*/*.js",
@@ -0,0 +1,34 @@
1
+ import '../icon/search';
2
+ import '../icon/close';
3
+ import type { TRect, TSinchElementReact } from '../types';
4
+ import type { FocusEvent, SyntheticEvent } from 'react';
5
+ export declare type TSinchSearchElement = HTMLElement & {
6
+ value: string;
7
+ label: string | null;
8
+ placeholder: string | null;
9
+ maxVisibleItems: number | null;
10
+ readonly dropdownRect: TRect;
11
+ focus(): void;
12
+ blur(): void;
13
+ };
14
+ export declare type TSinchSearchReact = TSinchElementReact<TSinchSearchElement> & {
15
+ value: string;
16
+ label?: string;
17
+ placeholder?: string;
18
+ maxVisibleItems?: number;
19
+ 'aria-label': string;
20
+ 'clear-aria-label': string;
21
+ onChange: (e: SyntheticEvent<TSinchSearchElement, CustomEvent<string>>) => void;
22
+ onFocus?: (e: FocusEvent<TSinchSearchElement>) => void;
23
+ onBlur?: (e: FocusEvent<TSinchSearchElement>) => void;
24
+ };
25
+ declare global {
26
+ namespace JSX {
27
+ interface IntrinsicElements {
28
+ 'sinch-search': TSinchSearchReact;
29
+ }
30
+ }
31
+ interface HTMLElementTagNameMap {
32
+ 'sinch-search': TSinchSearchElement;
33
+ }
34
+ }
@@ -0,0 +1,470 @@
1
+ import _classPrivateFieldGet from '@babel/runtime/helpers/classPrivateFieldGet';
2
+ import _classPrivateFieldSet from '@babel/runtime/helpers/classPrivateFieldSet';
3
+
4
+ var _$label, _$input, _selectionStart, _selectionEnd, _$optionSlot, _$listbox, _$clear, _onInput, _onValueChange, _onClearMouseDown, _onClear, _onInputFocus, _onInputBlur, _onListboxClick, _onListboxKeyPress, _onListboxKeyDown, _onOptionSlotChange, _onExpand, _onCollapse, _getFirstOption, _getLastOption, _getNextOption, _getPrevOption, _selectOption, _getOptionElements;
5
+
6
+ function _classPrivateMethodInitSpec(obj, privateSet) { _checkPrivateRedeclaration(obj, privateSet); privateSet.add(obj); }
7
+
8
+ function _classPrivateFieldInitSpec(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); }
9
+
10
+ function _checkPrivateRedeclaration(obj, privateCollection) { if (privateCollection.has(obj)) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } }
11
+
12
+ function _classPrivateMethodGet(receiver, privateSet, fn) { if (!privateSet.has(receiver)) { throw new TypeError("attempted to get private field on non-instance"); } return fn; }
13
+
14
+ import { isSearchOptionElement } from '../search-option';
15
+ import { attrValueToPixels, defineCustomElement, getAttribute, getIntegerAttribute, getRect, updateAttribute, updateIntegerAttribute } from '../utils';
16
+ const templateHTML = '<style>:host{display:inline-block;vertical-align:middle;outline:0}#wrapper{position:relative;width:100%;box-sizing:border-box;--sinch-size-icon:24px}#label{display:none;font:var(--sinch-font-title-4);color:var(--sinch-color-text-default);overflow:hidden;text-overflow:ellipsis;white-space:nowrap;width:100%;height:24px;margin-bottom:2px}#input{all:initial;display:block;border:1px solid var(--sinch-color-stormy-200);box-sizing:border-box;border-radius:4px;width:100%;height:48px;padding:0 44px;font:var(--sinch-font-body);color:var(--sinch-color-text-default);caret-color:var(--sinch-caret-color,auto)}#input::placeholder{font:var(--sinch-font-body);color:var(--sinch-color-text-muted)}#input:focus{border-color:var(--sinch-color-stormy-600)}#icon-search{position:absolute;left:12px;top:12px;pointer-events:none;--sinch-color-icon:var(--sinch-color-stormy-500)}#clear{all:initial;position:absolute;right:8px;top:8px;width:32px;height:32px;display:flex;align-items:center;justify-content:center;cursor:pointer;--sinch-color-icon:var(--sinch-color-stormy-300)}#clear:focus{--sinch-color-icon:var(--sinch-color-stormy-500)}:host([value=""]) #clear{display:none}:host([value=""]) #input{padding-right:12px}#listbox{display:none;position:absolute;z-index:1;left:0;top:calc(100% + 8px);width:100%;box-sizing:border-box;font:var(--sinch-font-body);color:var(--sinch-color-text-default);background-color:var(--sinch-color-snow-100);box-shadow:1px 2px 4px rgb(0 0 0 / 15%);overflow-y:auto;contain:content}:host([aria-expanded=true]) #listbox{display:block}:host([label]:not([label=""])) #label{display:block}</style><label id="label" for="input"></label><div id="wrapper"><input id="input" type="text"/><sinch-icon-search id="icon-search"></sinch-icon-search><button id="clear"><sinch-icon-close id="icon-close"></sinch-icon-close></button><div id="listbox"><slot name="option"></slot></div></div>';
17
+ import '../icon/search';
18
+ import '../icon/close';
19
+ const ITEM_HEIGHT = 40;
20
+
21
+ const findSelectedOption = elements => {
22
+ for (const el of elements) {
23
+ if (el.selected) {
24
+ return el;
25
+ }
26
+ }
27
+
28
+ return null;
29
+ };
30
+
31
+ const template = document.createElement('template');
32
+ template.innerHTML = templateHTML;
33
+ defineCustomElement('sinch-search', (_$label = new WeakMap(), _$input = new WeakMap(), _selectionStart = new WeakMap(), _selectionEnd = new WeakMap(), _$optionSlot = new WeakMap(), _$listbox = new WeakMap(), _$clear = new WeakMap(), _onInput = new WeakMap(), _onValueChange = new WeakSet(), _onClearMouseDown = new WeakMap(), _onClear = new WeakMap(), _onInputFocus = new WeakMap(), _onInputBlur = new WeakMap(), _onListboxClick = new WeakMap(), _onListboxKeyPress = new WeakMap(), _onListboxKeyDown = new WeakMap(), _onOptionSlotChange = new WeakMap(), _onExpand = new WeakSet(), _onCollapse = new WeakSet(), _getFirstOption = new WeakSet(), _getLastOption = new WeakSet(), _getNextOption = new WeakSet(), _getPrevOption = new WeakSet(), _selectOption = new WeakSet(), _getOptionElements = new WeakSet(), class extends HTMLElement {
34
+ constructor() {
35
+ super();
36
+
37
+ _classPrivateMethodInitSpec(this, _getOptionElements);
38
+
39
+ _classPrivateMethodInitSpec(this, _selectOption);
40
+
41
+ _classPrivateMethodInitSpec(this, _getPrevOption);
42
+
43
+ _classPrivateMethodInitSpec(this, _getNextOption);
44
+
45
+ _classPrivateMethodInitSpec(this, _getLastOption);
46
+
47
+ _classPrivateMethodInitSpec(this, _getFirstOption);
48
+
49
+ _classPrivateMethodInitSpec(this, _onCollapse);
50
+
51
+ _classPrivateMethodInitSpec(this, _onExpand);
52
+
53
+ _classPrivateMethodInitSpec(this, _onValueChange);
54
+
55
+ _classPrivateFieldInitSpec(this, _$label, {
56
+ writable: true,
57
+ value: void 0
58
+ });
59
+
60
+ _classPrivateFieldInitSpec(this, _$input, {
61
+ writable: true,
62
+ value: void 0
63
+ });
64
+
65
+ _classPrivateFieldInitSpec(this, _selectionStart, {
66
+ writable: true,
67
+ value: null
68
+ });
69
+
70
+ _classPrivateFieldInitSpec(this, _selectionEnd, {
71
+ writable: true,
72
+ value: null
73
+ });
74
+
75
+ _classPrivateFieldInitSpec(this, _$optionSlot, {
76
+ writable: true,
77
+ value: void 0
78
+ });
79
+
80
+ _classPrivateFieldInitSpec(this, _$listbox, {
81
+ writable: true,
82
+ value: void 0
83
+ });
84
+
85
+ _classPrivateFieldInitSpec(this, _$clear, {
86
+ writable: true,
87
+ value: void 0
88
+ });
89
+
90
+ _classPrivateFieldInitSpec(this, _onInput, {
91
+ writable: true,
92
+ value: e => {
93
+ e.stopPropagation();
94
+
95
+ _classPrivateMethodGet(this, _onValueChange, _onValueChange2).call(this, _classPrivateFieldGet(this, _$input).value, true);
96
+ }
97
+ });
98
+
99
+ _classPrivateFieldInitSpec(this, _onClearMouseDown, {
100
+ writable: true,
101
+ value: e => {
102
+ e.preventDefault();
103
+ }
104
+ });
105
+
106
+ _classPrivateFieldInitSpec(this, _onClear, {
107
+ writable: true,
108
+ value: e => {
109
+ e.stopPropagation();
110
+
111
+ _classPrivateMethodGet(this, _onValueChange, _onValueChange2).call(this, '');
112
+
113
+ _classPrivateFieldGet(this, _$input).focus();
114
+ }
115
+ });
116
+
117
+ _classPrivateFieldInitSpec(this, _onInputFocus, {
118
+ writable: true,
119
+ value: () => {
120
+ _classPrivateMethodGet(this, _onExpand, _onExpand2).call(this);
121
+ }
122
+ });
123
+
124
+ _classPrivateFieldInitSpec(this, _onInputBlur, {
125
+ writable: true,
126
+ value: () => {
127
+ _classPrivateMethodGet(this, _onCollapse, _onCollapse2).call(this);
128
+ }
129
+ });
130
+
131
+ _classPrivateFieldInitSpec(this, _onListboxClick, {
132
+ writable: true,
133
+ value: e => {
134
+ e.stopPropagation();
135
+ const $elem = e.target;
136
+
137
+ if (isSearchOptionElement($elem)) {
138
+ _classPrivateMethodGet(this, _onValueChange, _onValueChange2).call(this, $elem.text);
139
+ }
140
+
141
+ _classPrivateMethodGet(this, _onCollapse, _onCollapse2).call(this);
142
+ }
143
+ });
144
+
145
+ _classPrivateFieldInitSpec(this, _onListboxKeyPress, {
146
+ writable: true,
147
+ value: e => {
148
+ switch (e.code) {
149
+ case 'Enter':
150
+ {
151
+ e.preventDefault();
152
+ const $elem = findSelectedOption(_classPrivateMethodGet(this, _getOptionElements, _getOptionElements2).call(this));
153
+
154
+ if ($elem != null) {
155
+ _classPrivateMethodGet(this, _onValueChange, _onValueChange2).call(this, $elem.text);
156
+ }
157
+
158
+ _classPrivateMethodGet(this, _onCollapse, _onCollapse2).call(this);
159
+
160
+ break;
161
+ }
162
+ }
163
+ }
164
+ });
165
+
166
+ _classPrivateFieldInitSpec(this, _onListboxKeyDown, {
167
+ writable: true,
168
+ value: e => {
169
+ switch (e.code) {
170
+ case 'ArrowUp':
171
+ {
172
+ e.preventDefault();
173
+
174
+ _classPrivateMethodGet(this, _selectOption, _selectOption2).call(this, _classPrivateMethodGet(this, _getPrevOption, _getPrevOption2).call(this));
175
+
176
+ break;
177
+ }
178
+
179
+ case 'ArrowDown':
180
+ {
181
+ e.preventDefault();
182
+
183
+ _classPrivateMethodGet(this, _selectOption, _selectOption2).call(this, _classPrivateMethodGet(this, _getNextOption, _getNextOption2).call(this));
184
+
185
+ break;
186
+ }
187
+
188
+ case 'Escape':
189
+ {
190
+ e.preventDefault();
191
+
192
+ _classPrivateMethodGet(this, _onCollapse, _onCollapse2).call(this);
193
+
194
+ break;
195
+ }
196
+ }
197
+ }
198
+ });
199
+
200
+ _classPrivateFieldInitSpec(this, _onOptionSlotChange, {
201
+ writable: true,
202
+ value: () => {
203
+ _classPrivateMethodGet(this, _onExpand, _onExpand2).call(this);
204
+ }
205
+ });
206
+
207
+ const shadowRoot = this.attachShadow({
208
+ mode: 'closed',
209
+ delegatesFocus: true
210
+ });
211
+ shadowRoot.appendChild(template.content.cloneNode(true));
212
+
213
+ _classPrivateFieldSet(this, _$input, shadowRoot.querySelector('#input'));
214
+
215
+ _classPrivateFieldSet(this, _$label, shadowRoot.querySelector('#label'));
216
+
217
+ _classPrivateFieldSet(this, _$listbox, shadowRoot.querySelector('#listbox'));
218
+
219
+ _classPrivateFieldSet(this, _$optionSlot, shadowRoot.querySelector('slot[name="option"]'));
220
+
221
+ _classPrivateFieldSet(this, _$clear, shadowRoot.querySelector('#clear'));
222
+ }
223
+
224
+ connectedCallback() {
225
+ this.setAttribute('role', 'listbox');
226
+ this.setAttribute('autocomplete', 'off');
227
+ this.setAttribute('aria-expanded', 'false');
228
+
229
+ _classPrivateFieldGet(this, _$input).addEventListener('input', _classPrivateFieldGet(this, _onInput));
230
+
231
+ _classPrivateFieldGet(this, _$input).addEventListener('focus', _classPrivateFieldGet(this, _onInputFocus));
232
+
233
+ _classPrivateFieldGet(this, _$input).addEventListener('blur', _classPrivateFieldGet(this, _onInputBlur));
234
+
235
+ _classPrivateFieldGet(this, _$input).addEventListener('keydown', _classPrivateFieldGet(this, _onListboxKeyDown));
236
+
237
+ _classPrivateFieldGet(this, _$input).addEventListener('keypress', _classPrivateFieldGet(this, _onListboxKeyPress));
238
+
239
+ _classPrivateFieldGet(this, _$listbox).addEventListener('click', _classPrivateFieldGet(this, _onListboxClick));
240
+
241
+ _classPrivateFieldGet(this, _$optionSlot).addEventListener('slotchange', _classPrivateFieldGet(this, _onOptionSlotChange));
242
+
243
+ _classPrivateFieldGet(this, _$clear).addEventListener('mousedown', _classPrivateFieldGet(this, _onClearMouseDown));
244
+
245
+ _classPrivateFieldGet(this, _$clear).addEventListener('click', _classPrivateFieldGet(this, _onClear));
246
+ }
247
+
248
+ disconnectedCallback() {
249
+ _classPrivateFieldGet(this, _$input).removeEventListener('input', _classPrivateFieldGet(this, _onInput));
250
+
251
+ _classPrivateFieldGet(this, _$input).removeEventListener('focus', _classPrivateFieldGet(this, _onInputFocus));
252
+
253
+ _classPrivateFieldGet(this, _$input).removeEventListener('blur', _classPrivateFieldGet(this, _onInputBlur));
254
+
255
+ _classPrivateFieldGet(this, _$input).removeEventListener('keydown', _classPrivateFieldGet(this, _onListboxKeyDown));
256
+
257
+ _classPrivateFieldGet(this, _$input).removeEventListener('keypress', _classPrivateFieldGet(this, _onListboxKeyPress));
258
+
259
+ _classPrivateFieldGet(this, _$listbox).removeEventListener('click', _classPrivateFieldGet(this, _onListboxClick));
260
+
261
+ _classPrivateFieldGet(this, _$optionSlot).removeEventListener('slotchange', _classPrivateFieldGet(this, _onOptionSlotChange));
262
+
263
+ _classPrivateFieldGet(this, _$clear).removeEventListener('mousedown', _classPrivateFieldGet(this, _onClearMouseDown));
264
+
265
+ _classPrivateFieldGet(this, _$clear).removeEventListener('click', _classPrivateFieldGet(this, _onClear));
266
+ }
267
+
268
+ static get observedAttributes() {
269
+ return ['label', 'value', 'placeholder', 'maxvisibleitems', 'clear-aria-label'];
270
+ }
271
+
272
+ get type() {
273
+ return 'text';
274
+ }
275
+
276
+ get nodeName() {
277
+ return 'input';
278
+ }
279
+
280
+ set value(value) {
281
+ updateAttribute(this, 'value', value);
282
+ }
283
+
284
+ get value() {
285
+ return getAttribute(this, 'value', '');
286
+ }
287
+
288
+ set placeholder(value) {
289
+ updateAttribute(this, 'placeholder', value);
290
+ }
291
+
292
+ get placeholder() {
293
+ return getAttribute(this, 'placeholder', null);
294
+ }
295
+
296
+ set label(value) {
297
+ updateAttribute(this, 'label', value);
298
+ }
299
+
300
+ get label() {
301
+ return getAttribute(this, 'label', null);
302
+ }
303
+
304
+ set maxVisibleItems(value) {
305
+ updateIntegerAttribute(this, 'maxvisibleitems', value);
306
+ }
307
+
308
+ get maxVisibleItems() {
309
+ return getIntegerAttribute(this, 'maxvisibleitems', null);
310
+ }
311
+
312
+ get dropdownRect() {
313
+ return getRect(_classPrivateFieldGet(this, _$listbox));
314
+ }
315
+
316
+ attributeChangedCallback(name, oldVal, newVal) {
317
+ switch (name) {
318
+ case 'value':
319
+ {
320
+ const nextVal = newVal ?? '';
321
+
322
+ if (nextVal !== _classPrivateFieldGet(this, _$input).value) {
323
+ _classPrivateFieldGet(this, _$input).value = nextVal;
324
+ const isNextCursorEnd = _classPrivateFieldGet(this, _selectionStart) === null || _classPrivateFieldGet(this, _selectionStart) === nextVal.length;
325
+
326
+ if (!isNextCursorEnd) {
327
+ _classPrivateFieldGet(this, _$input).setSelectionRange(_classPrivateFieldGet(this, _selectionStart), _classPrivateFieldGet(this, _selectionEnd));
328
+ }
329
+ }
330
+
331
+ break;
332
+ }
333
+
334
+ case 'label':
335
+ {
336
+ _classPrivateFieldGet(this, _$label).textContent = newVal;
337
+ break;
338
+ }
339
+
340
+ case 'placeholder':
341
+ {
342
+ _classPrivateFieldGet(this, _$input).placeholder = newVal ?? '';
343
+ updateAttribute(this, 'aria-placeholder', newVal);
344
+ break;
345
+ }
346
+
347
+ case 'maxvisibleitems':
348
+ {
349
+ _classPrivateFieldGet(this, _$listbox).style.maxHeight = attrValueToPixels(newVal, {
350
+ min: 2,
351
+ multiplier: ITEM_HEIGHT
352
+ });
353
+ break;
354
+ }
355
+
356
+ case 'clear-aria-label':
357
+ {
358
+ updateAttribute(_classPrivateFieldGet(this, _$clear), 'aria-label', newVal);
359
+ }
360
+ }
361
+ }
362
+
363
+ focus() {
364
+ _classPrivateFieldGet(this, _$input).focus();
365
+ }
366
+
367
+ blur() {
368
+ _classPrivateFieldGet(this, _$input).blur();
369
+ }
370
+
371
+ }));
372
+
373
+ function _onValueChange2(nextValue) {
374
+ let preserveCursorPosition = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
375
+ const prevValue = this.value;
376
+
377
+ if (prevValue !== nextValue) {
378
+ const nextSelectionStart = preserveCursorPosition ? _classPrivateFieldGet(this, _$input).selectionStart : nextValue.length;
379
+ const nextSelectionEnd = preserveCursorPosition ? _classPrivateFieldGet(this, _$input).selectionEnd : nextValue.length;
380
+
381
+ const prevSelectionStart = _classPrivateFieldGet(this, _selectionStart);
382
+
383
+ const prevSelectionEnd = _classPrivateFieldGet(this, _selectionEnd);
384
+
385
+ const isPrevCursorEnd = prevSelectionStart === prevSelectionEnd && prevSelectionStart === prevValue.length;
386
+ _classPrivateFieldGet(this, _$input).value = prevValue;
387
+
388
+ if (!isPrevCursorEnd) {
389
+ _classPrivateFieldGet(this, _$input).setSelectionRange(prevSelectionStart, prevSelectionEnd);
390
+ }
391
+
392
+ _classPrivateFieldSet(this, _selectionStart, nextSelectionStart);
393
+
394
+ _classPrivateFieldSet(this, _selectionEnd, nextSelectionEnd);
395
+
396
+ this.dispatchEvent(new CustomEvent('change', {
397
+ detail: nextValue,
398
+ bubbles: true
399
+ }));
400
+ }
401
+ }
402
+
403
+ function _onExpand2() {
404
+ const elems = _classPrivateMethodGet(this, _getOptionElements, _getOptionElements2).call(this);
405
+
406
+ if (elems.length === 0 || document.activeElement !== this) {
407
+ _classPrivateMethodGet(this, _onCollapse, _onCollapse2).call(this);
408
+
409
+ return;
410
+ }
411
+
412
+ this.setAttribute('aria-expanded', 'true');
413
+
414
+ _classPrivateMethodGet(this, _selectOption, _selectOption2).call(this, elems[0]);
415
+ }
416
+
417
+ function _onCollapse2() {
418
+ this.setAttribute('aria-expanded', 'false');
419
+ }
420
+
421
+ function _getFirstOption2() {
422
+ return _classPrivateMethodGet(this, _getOptionElements, _getOptionElements2).call(this)[0] ?? null;
423
+ }
424
+
425
+ function _getLastOption2() {
426
+ return _classPrivateMethodGet(this, _getOptionElements, _getOptionElements2).call(this).reverse()[0] ?? null;
427
+ }
428
+
429
+ function _getNextOption2() {
430
+ const $options = _classPrivateMethodGet(this, _getOptionElements, _getOptionElements2).call(this);
431
+
432
+ const $selectedOption = findSelectedOption($options);
433
+ const currentIndex = $selectedOption !== null ? $options.indexOf($selectedOption) : -1;
434
+
435
+ if (currentIndex < 0) {
436
+ return _classPrivateMethodGet(this, _getFirstOption, _getFirstOption2).call(this);
437
+ }
438
+
439
+ return $options[(currentIndex + 1) % $options.length];
440
+ }
441
+
442
+ function _getPrevOption2() {
443
+ const $options = _classPrivateMethodGet(this, _getOptionElements, _getOptionElements2).call(this);
444
+
445
+ const $selectedOption = findSelectedOption($options);
446
+ const currentIndex = $selectedOption !== null ? $options.indexOf($selectedOption) : -1;
447
+
448
+ if (currentIndex < 0) {
449
+ return _classPrivateMethodGet(this, _getLastOption, _getLastOption2).call(this);
450
+ }
451
+
452
+ return $options[(currentIndex - 1 + $options.length) % $options.length];
453
+ }
454
+
455
+ function _selectOption2($option) {
456
+ for (const $op of _classPrivateMethodGet(this, _getOptionElements, _getOptionElements2).call(this)) {
457
+ const isSelected = $op === $option;
458
+ $op.selected = isSelected;
459
+
460
+ if (isSelected) {
461
+ $op.scrollIntoView?.({
462
+ block: 'nearest'
463
+ });
464
+ }
465
+ }
466
+ }
467
+
468
+ function _getOptionElements2() {
469
+ return _classPrivateFieldGet(this, _$optionSlot).assignedElements().filter(isSearchOptionElement);
470
+ }
@@ -0,0 +1,20 @@
1
+ import type { TSinchElementReact } from '../types';
2
+ export declare const isSearchOptionElement: (element: EventTarget | Element | null) => element is TSinchSearchOptionElement;
3
+ export declare type TSinchSearchOptionElement = HTMLElement & {
4
+ text: string;
5
+ selected: boolean;
6
+ };
7
+ export declare type TSinchSearchOptionReact = TSinchElementReact<TSinchSearchOptionElement> & {
8
+ text: string;
9
+ 'aria-label': string;
10
+ };
11
+ declare global {
12
+ namespace JSX {
13
+ interface IntrinsicElements {
14
+ 'sinch-search-option': TSinchSearchOptionReact;
15
+ }
16
+ }
17
+ interface HTMLElementTagNameMap {
18
+ 'sinch-search-option': TSinchSearchOptionElement;
19
+ }
20
+ }
@@ -0,0 +1,76 @@
1
+ import _classPrivateFieldGet from '@babel/runtime/helpers/classPrivateFieldGet';
2
+ import _classPrivateFieldSet from '@babel/runtime/helpers/classPrivateFieldSet';
3
+
4
+ function _classPrivateFieldInitSpec(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); }
5
+
6
+ function _checkPrivateRedeclaration(obj, privateCollection) { if (privateCollection.has(obj)) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } }
7
+
8
+ import { defineCustomElement, getAttribute, getBooleanAttribute, updateAttribute, updateBooleanAttribute, updateExplicitBooleanAttribute } from '../utils';
9
+ const templateHTML = '<style>:host{display:block}#wrapper{display:block;box-sizing:border-box;line-height:40px;padding:0 12px;width:100%;overflow:hidden;white-space:nowrap;text-overflow:ellipsis}:host(:hover)>#wrapper,:host([data-selected])>#wrapper{background-color:var(--sinch-color-snow-500)}</style><div id="wrapper"></div>';
10
+ const template = document.createElement('template');
11
+ template.innerHTML = templateHTML;
12
+
13
+ var _$content = new WeakMap();
14
+
15
+ class SearchOption extends HTMLElement {
16
+ constructor() {
17
+ super();
18
+
19
+ _classPrivateFieldInitSpec(this, _$content, {
20
+ writable: true,
21
+ value: void 0
22
+ });
23
+
24
+ const shadowRoot = this.attachShadow({
25
+ mode: 'closed'
26
+ });
27
+ shadowRoot.appendChild(template.content.cloneNode(true));
28
+
29
+ _classPrivateFieldSet(this, _$content, shadowRoot.querySelector('#wrapper'));
30
+ }
31
+
32
+ connectedCallback() {
33
+ this.setAttribute('role', 'option');
34
+ }
35
+
36
+ static get observedAttributes() {
37
+ return ['text'];
38
+ }
39
+
40
+ attributeChangedCallback(name, oldVal, newVal) {
41
+ if (oldVal === newVal) {
42
+ return;
43
+ }
44
+
45
+ switch (name) {
46
+ case 'text':
47
+ {
48
+ _classPrivateFieldGet(this, _$content).textContent = newVal;
49
+ break;
50
+ }
51
+ }
52
+ }
53
+
54
+ set text(value) {
55
+ updateAttribute(this, 'text', value);
56
+ }
57
+
58
+ get text() {
59
+ return getAttribute(this, 'text', '');
60
+ }
61
+
62
+ set selected(isSelected) {
63
+ updateBooleanAttribute(this, 'data-selected', isSelected);
64
+ updateExplicitBooleanAttribute(this, 'aria-selected', isSelected);
65
+ }
66
+
67
+ get selected() {
68
+ return getBooleanAttribute(this, 'data-selected');
69
+ }
70
+
71
+ }
72
+
73
+ defineCustomElement('sinch-search-option', SearchOption);
74
+ export const isSearchOptionElement = element => {
75
+ return element instanceof SearchOption;
76
+ };
package/utils.d.ts CHANGED
@@ -3,6 +3,7 @@ export declare const defineCustomElement: (name: string, constructor: CustomElem
3
3
  export declare const defineNectaryElements: (registry: CustomElementRegistry) => void;
4
4
  export declare type TEventHandler = (arg?: any) => void;
5
5
  export declare const updateBooleanAttribute: ($element: Element, attrName: string, attrValue: boolean | null | undefined) => void;
6
+ export declare const updateExplicitBooleanAttribute: ($element: Element, attrName: string, attrValue: boolean | null | undefined) => void;
6
7
  export declare const isAttrTrue: (attrValue: string | null) => boolean;
7
8
  export declare const getBooleanAttribute: ($element: Element, attrName: string) => boolean;
8
9
  export declare const updateAttribute: ($element: Element, attrName: string, attrValue: string | number | boolean | null | undefined) => void;
@@ -10,7 +11,7 @@ export declare function getAttribute($element: Element, attrName: string): strin
10
11
  export declare function getAttribute($element: Element, attrName: string, defaultValue: null): string | null;
11
12
  export declare function getAttribute($element: Element, attrName: string, defaultValue: string): string;
12
13
  export declare const isLiteralValue: <T extends readonly string[]>(literals: T, value: string | null | undefined) => value is T[number];
13
- export declare const updateLiteralAttribute: <T extends readonly string[]>($element: Element, literals: T, attrName: string, attrValue: T[number] | null | undefined) => void;
14
+ export declare const updateLiteralAttribute: <T extends readonly string[]>($element: Element, literals: T, attrName: string, attrValue: string | null | undefined) => void;
14
15
  export declare function getLiteralAttribute<T extends readonly string[]>($element: Element, literals: T, attrName: string): T[number] | undefined;
15
16
  export declare function getLiteralAttribute<T extends readonly string[]>($element: Element, literals: T, attrName: string, defaultValue: null): T[number] | null;
16
17
  export declare function getLiteralAttribute<T extends readonly string[]>($element: Element, literals: T, attrName: string, defaultValue: T[number]): T[number];
package/utils.js CHANGED
@@ -35,6 +35,9 @@ export const updateBooleanAttribute = ($element, attrName, attrValue) => {
35
35
  $element.removeAttribute(attrName);
36
36
  }
37
37
  };
38
+ export const updateExplicitBooleanAttribute = ($element, attrName, attrValue) => {
39
+ $element.setAttribute(attrName, attrValue === true ? 'true' : 'false');
40
+ };
38
41
  export const isAttrTrue = attrValue => {
39
42
  return attrValue === '' || attrValue !== 'false' && attrValue !== null;
40
43
  };