@oslokommune/punkt-elements 12.41.0 → 12.42.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,514 @@
1
+ import { E as y, x as d, n as r, a as k } from "./element-gAd63VwC.js";
2
+ import { o as f } from "./if-defined-Cj-3Kdsr.js";
3
+ import { r as c } from "./state-BxrO4zNJ.js";
4
+ import { e as _, n as v } from "./ref-DnSbnXsj.js";
5
+ import { e as w } from "./class-map-m7ZCZ49w.js";
6
+ import { c as R } from "./repeat-lUFq9w5d.js";
7
+ import { P as V } from "./input-element-BEvaQK4g.js";
8
+ import { P as C } from "./pkt-options-controller-DepYQBqb.js";
9
+ import { P as A } from "./pkt-slot-controller-DtDaD9q_.js";
10
+ import "./input-wrapper-CDgnd3WJ.js";
11
+ import "./icon-CO1TLSs5.js";
12
+ import "./tag-CX-zbR-7.js";
13
+ import "./listbox-DCWdrjFp.js";
14
+ const S = {
15
+ displayValueAs: {
16
+ default: "label"
17
+ }
18
+ }, T = {
19
+ props: S
20
+ };
21
+ var M = Object.defineProperty, F = Object.getOwnPropertyDescriptor, o = (t, e, i, s) => {
22
+ for (var l = s > 1 ? void 0 : s ? F(e, i) : e, n = t.length - 1, a; n >= 0; n--)
23
+ (a = t[n]) && (l = (s ? a(e, i, l) : a(l)) || l);
24
+ return s && l && M(e, i, l), l;
25
+ };
26
+ let h = class extends V {
27
+ constructor() {
28
+ super(), this.helptextSlot = _(), this.value = "", this.options = [], this.defaultOptions = [], this.allowUserInput = !1, this.typeahead = !1, this.includeSearch = !1, this.searchPlaceholder = "", this.multiple = !1, this.maxlength = null, this.displayValueAs = T.props.displayValueAs.default, this.tagPlacement = null, this._options = [], this._isOptionsOpen = !1, this._value = [], this._userInfoMessage = "", this._addValueText = null, this._maxIsReached = !1, this._search = "", this._inputFocus = !1, this._editingSingleValue = !1, this.inputRef = _(), this.arrowRef = _(), this.listboxRef = _(), this.focusRef = _(), this.optionTagRef = _(), this.optionsController = new C(this), this.slotController = new A(this, this.helptextSlot), this.slotController.skipOptions = !0;
29
+ }
30
+ // Lifecycle methods
31
+ connectedCallback() {
32
+ var t;
33
+ if (super.connectedCallback(), document && document.body.addEventListener("click", (e) => {
34
+ this._isOptionsOpen && !this.contains(e.target) && this.handleFocusOut(e);
35
+ }), this._options = [], this.defaultOptions && this.defaultOptions.length) {
36
+ const e = ((t = this.options) == null ? void 0 : t.filter((i) => i.userAdded)) || [];
37
+ this.options = [...e, ...structuredClone(this.defaultOptions)], this._options = [...this.options];
38
+ }
39
+ if (this.optionsController.nodes.length) {
40
+ const e = [];
41
+ this.optionsController.nodes.forEach((i) => {
42
+ if (!i.textContent && !i.getAttribute("value")) return null;
43
+ const s = {
44
+ value: i.getAttribute("value") || i.textContent || "",
45
+ label: i.textContent || i.getAttribute("value") || ""
46
+ };
47
+ i.getAttribute("data-prefix") && (s.prefix = i.getAttribute("data-prefix") || void 0), i.getAttribute("tagskincolor") && (s.tagSkinColor = i.getAttribute("tagskincolor")), i.getAttribute("description") && (s.description = i.getAttribute("description") || void 0), s.fulltext = s.value + s.label + (s.prefix || ""), e.push(s);
48
+ }), e.length && (this.options = [...e], this._options = [...e]);
49
+ }
50
+ }
51
+ updated(t) {
52
+ var e;
53
+ if (t.has("_value") && this.valueChanged(this._value, t.get("_value")), t.has("value") && (this._value = Array.isArray(this.value) ? this.value : this.value ? this.value.split(",") : [], !this.multiple && this._value.length > 1 && (this._value = [this._value[0]]), this.isMaxItemsReached()), t.has("defaultOptions") && this.defaultOptions.length) {
54
+ const i = ((e = this.options) == null ? void 0 : e.filter((s) => s.userAdded)) || [];
55
+ this.options = [...i, ...structuredClone(this.defaultOptions)], this._options = [...this.options];
56
+ }
57
+ if (t.has("options") && this.options.length) {
58
+ const s = this._options.filter((l) => l.userAdded).filter(
59
+ (l) => !this.options.some((n) => n.value === l.value)
60
+ );
61
+ this._options = [...s, ...this.options], this._options.forEach((l) => {
62
+ if (l.value && !l.label && (l.label = l.value), l.selected && !this._value.includes(l.value)) {
63
+ const n = [...this._value];
64
+ this._value = [...this._value, l.value], this.valueChanged(this._value, n);
65
+ }
66
+ l.fulltext = l.value + l.label + (l.prefix || ""), l.selected = l.selected || this._value.includes(l.value);
67
+ });
68
+ }
69
+ t.has("_search") && this.dispatchEvent(
70
+ new CustomEvent("search", {
71
+ detail: this._search,
72
+ bubbles: !1
73
+ })
74
+ ), super.updated(t);
75
+ }
76
+ attributeChangedCallback(t, e, i) {
77
+ t === "value" && (this._value = Array.isArray(this.value) ? this.value : this.value ? this.value.split(",") : [], !this.multiple && this._value.length > 1 && (this._value = [this._value[0]])), t === "options" && (this._options = this.options, this._options.forEach((s) => {
78
+ s.value && !s.label && (s.label = s.value), s.selected && !this._value.includes(s.value) && (this._value = [...this._value, s.value]), s.fulltext = s.value + s.label + (s.prefix || "");
79
+ }), this._search = ""), super.attributeChangedCallback(t, e, i);
80
+ }
81
+ // Render methods
82
+ render() {
83
+ return d`
84
+ <pkt-input-wrapper
85
+ .label=${this.label}
86
+ .helptext=${this.helptext}
87
+ .helptextDropdown=${f(this.helptextDropdown)}
88
+ .helptextDropdownButton=${f(this.helptextDropdownButton)}
89
+ ?fullwidth=${this.fullwidth}
90
+ ?hasError=${this.hasError}
91
+ ?inline=${this.inline}
92
+ ?disabled=${this.disabled}
93
+ .errorMessage=${this.errorMessage}
94
+ ?optionalTag=${this.optionalTag}
95
+ .optionalText=${this.optionalText}
96
+ ?requiredTag=${this.requiredTag}
97
+ .requiredText=${this.requiredText}
98
+ ?useWrapper=${this.useWrapper}
99
+ .forId=${this.allowUserInput || this.typeahead ? this.id + "-input" : this.id + "-arrow"}
100
+ class="pkt-combobox__wrapper"
101
+ @labelClick=${this.handleInputClick}
102
+ >
103
+ <div class="pkt-contents" ${v(this.helptextSlot)} name="helptext" slot="helptext"></div>
104
+ <div class="pkt-combobox" @focusout=${this.handleFocusOut}>
105
+ <div
106
+ class=${w({
107
+ "pkt-combobox__input": !0,
108
+ "pkt-combobox__input--fullwidth": this.fullwidth,
109
+ "pkt-combobox__input--open": this._isOptionsOpen,
110
+ "pkt-combobox__input--error": this.hasError,
111
+ "pkt-combobox__input--disabled": this.disabled
112
+ })}
113
+ tabindex="-1"
114
+ @click=${this.handleInputClick}
115
+ >
116
+ ${this.placeholder && (!this._value.length || this.multiple && this.tagPlacement == "outside") && !this._inputFocus ? d`<span class="pkt-combobox__placeholder">${this.placeholder}</span>` : this.tagPlacement !== "outside" ? this.renderSingleOrMultipleValues() : y}
117
+ ${this.renderInputField()}
118
+ <div
119
+ class="pkt-btn pkt-btn--tertiary pkt-combobox__arrow"
120
+ @click=${this.handleArrowClick}
121
+ @keydown=${this.handleArrowClick}
122
+ id="${this.id}-arrow"
123
+ ${v(this.arrowRef)}
124
+ aria-expanded=${this._isOptionsOpen}
125
+ aria-controls="${this.id}-listbox"
126
+ aria-haspopup="listbox"
127
+ aria-label="Åpne liste"
128
+ ?disabled=${this.disabled}
129
+ ?data-disabled=${this.disabled}
130
+ role="button"
131
+ tabindex="${this.disabled ? "-1" : "0"}"
132
+ >
133
+ <pkt-icon
134
+ class=${w({
135
+ "pkt-combobox__arrow-icon": !0,
136
+ "pkt-combobox__arrow-icon--open": this._isOptionsOpen
137
+ })}
138
+ name="chevron-thin-down"
139
+ ></pkt-icon>
140
+ </div>
141
+ <div ${v(this.focusRef)} tabindex="-1" @keydown=${this.handleArrowClick}></div>
142
+ </div>
143
+
144
+ <pkt-listbox
145
+ id="${this.id}-listbox"
146
+ .options=${this._options}
147
+ .isOpen=${this._isOptionsOpen}
148
+ .searchPlaceholder=${this.searchPlaceholder}
149
+ .label="Liste: ${this.label || ""}"
150
+ ?includeSearch=${this.includeSearch}
151
+ ?isMultiSelect=${this.multiple}
152
+ ?allowUserInput=${this.allowUserInput && !this._maxIsReached}
153
+ ?maxIsReached=${this._maxIsReached}
154
+ .customUserInput=${f(this._addValueText)}
155
+ .userMessage=${this._userInfoMessage}
156
+ @search=${this.handleSearch}
157
+ @option-toggle=${this.handleOptionToggled}
158
+ @select-all=${this.addAllOptions}
159
+ @close-options=${() => this._isOptionsOpen = !1}
160
+ .searchValue=${this._search || null}
161
+ .maxLength=${this.maxlength || 0}
162
+ ${v(this.listboxRef)}
163
+ ></pkt-listbox>
164
+ </div>
165
+
166
+ ${this.tagPlacement === "outside" && this.multiple ? d`<div class="pkt-combobox__tags-outside">
167
+ ${this.renderSingleOrMultipleValues()}
168
+ </div>` : y}
169
+ </pkt-input-wrapper>
170
+ `;
171
+ }
172
+ renderInputField() {
173
+ return this.typeahead || this.allowUserInput ? d`
174
+ <div class="pkt-combobox__input-div combobox__input">
175
+ <input
176
+ type="text"
177
+ id="${this.id}-input"
178
+ name=${(this.name || this.id) + "-input"}
179
+ @input=${this.handleInput}
180
+ @keydown=${this.handleInputKeydown}
181
+ @focus=${this.handleFocus}
182
+ @blur=${this.handleBlur}
183
+ autocomplete="off"
184
+ role="combobox"
185
+ aria-label=${f(this.label)}
186
+ aria-autocomplete=${this.typeahead ? "both" : "list"}
187
+ aria-controls="${this.id}-listbox"
188
+ aria-multiselectable=${f(this.multiple ? "true" : void 0)}
189
+ aria-activedescendant=${f(
190
+ this._value[0] && this.findValueInOptions(this._value[0]) ? `${this.id}-listbox-${this.findIndexInOptions(this._value[0])}` : void 0
191
+ )}
192
+ ${v(this.inputRef)}
193
+ />
194
+ </div>
195
+ ` : d`
196
+ <input
197
+ type="hidden"
198
+ id="${this.id}-input"
199
+ name=${(this.name || this.id) + "-input"}
200
+ .value=${this._value.join(",")}
201
+ ${v(this.inputRef)}
202
+ />
203
+ `;
204
+ }
205
+ renderSingleOrMultipleValues() {
206
+ const t = !this.multiple, e = this._editingSingleValue ? null : this.renderValueTag(this.findValueInOptions(this._value[0])), i = R(
207
+ this._value,
208
+ (s) => s,
209
+ (s) => {
210
+ var a;
211
+ const l = this.findValueInOptions(s), n = (a = this.options.find((u) => u.value === s)) == null ? void 0 : a.tagSkinColor;
212
+ return d`
213
+ <pkt-tag
214
+ skin=${n || "blue-dark"}
215
+ ?closeTag=${!this.disabled}
216
+ @close=${() => this.handleTagRemove(s)}
217
+ >
218
+ ${this.renderValueTag(l)}
219
+ </pkt-tag>
220
+ `;
221
+ }
222
+ );
223
+ return t ? e : i;
224
+ }
225
+ renderValueTag(t) {
226
+ if (!t) return "";
227
+ switch (this.displayValueAs) {
228
+ case "prefixAndValue":
229
+ return d`<span data-focusfix=${this.id}>${t.prefix || ""} ${t.value}</span>`;
230
+ case "value":
231
+ return d`<span data-focusfix=${this.id}>${t.value}</span>`;
232
+ case "label":
233
+ default:
234
+ return d`<span data-focusfix=${this.id}>${t.label || t.value}</span>`;
235
+ }
236
+ }
237
+ // Event handlers
238
+ handleInput(t) {
239
+ if (t.stopPropagation(), t.stopImmediatePropagation(), this.disabled) return;
240
+ this.touched = !0;
241
+ const e = t.target;
242
+ if (this._search = e.value, this.checkForMatches(), this.typeahead)
243
+ if (this._search) {
244
+ if (this._options = this.options.filter(
245
+ (i) => {
246
+ var s;
247
+ return (s = i.fulltext) == null ? void 0 : s.toLowerCase().includes(this._search.toLowerCase());
248
+ }
249
+ ), t.inputType !== "deleteContentBackward") {
250
+ const i = this._options.filter(
251
+ (s) => {
252
+ var l;
253
+ return !s.selected && ((l = s.label) == null ? void 0 : l.toLowerCase().startsWith(this._search.toLowerCase()));
254
+ }
255
+ );
256
+ if (i.length > 0 && this.inputRef.value && this.inputRef.value.type !== "hidden") {
257
+ const s = i[0];
258
+ s != null && s.label && (e.value = s.label, window.setTimeout(
259
+ () => e.setSelectionRange(this._search.length, e.value.length),
260
+ 0
261
+ ), e.selectionDirection = "backward");
262
+ }
263
+ }
264
+ } else
265
+ this._options = [...this.options];
266
+ }
267
+ handleFocus() {
268
+ if (!this.disabled) {
269
+ if (!this.multiple && this._value[0] && this.inputRef.value && this.inputRef.value.type !== "hidden") {
270
+ const t = this.findValueInOptions(this._value[0]);
271
+ this._editingSingleValue = !0, this.inputRef.value.value = this.displayValueAs === "label" && (t != null && t.label) ? t.label : this._value[0];
272
+ }
273
+ this._inputFocus = !0, this._search = "", this._options = [...this.options], this._isOptionsOpen = !0, this.onFocus(), this.requestUpdate();
274
+ }
275
+ }
276
+ handleFocusOut(t) {
277
+ var e, i, s, l, n;
278
+ if (!this.disabled && ((i = (e = t.relatedTarget) == null ? void 0 : e.closest("pkt-combobox")) == null ? void 0 : i.id) !== this.id && ((l = (s = t.relatedTarget) == null ? void 0 : s.closest("pkt-combobox")) == null ? void 0 : l.id) !== this.id && ((n = t.target) == null ? void 0 : n.getAttribute("data-focusfix")) !== this.id && t.relatedTarget !== this.focusRef.value && t.relatedTarget !== this.inputRef.value && t.relatedTarget !== this.arrowRef.value && this._isOptionsOpen) {
279
+ if (this._inputFocus = !1, this._addValueText = null, this._userInfoMessage = "", this._search = "", this.inputRef.value && this.inputRef.value.type !== "hidden" && this.inputRef.value.value !== "") {
280
+ const a = this.inputRef.value.value, u = this.findValueInOptions(a);
281
+ !this._value.includes(a) && !u ? this.allowUserInput ? this.addNewUserValue(a) : this.multiple || this.removeValue(this._value[0]) : u && !this._value.includes(u.value) && this.setSelected(u.value), this.inputRef.value.value = "";
282
+ }
283
+ this._isOptionsOpen = !1, this.onBlur();
284
+ }
285
+ }
286
+ handleBlur() {
287
+ this._inputFocus = !1, this._editingSingleValue = !1, this.onBlur();
288
+ }
289
+ handleInputClick(t) {
290
+ var e, i;
291
+ this.disabled || (t.currentTarget && t.currentTarget !== this.arrowRef.value && ((e = this.inputRef.value) == null ? void 0 : e.type) !== "hidden" ? (i = this.inputRef.value) == null || i.focus() : this.handleArrowClick(t));
292
+ }
293
+ handleArrowClick(t) {
294
+ var e, i;
295
+ this.disabled || t instanceof KeyboardEvent && t.key && t.key !== "Enter" && t.key !== " " && t.key !== "ArrowDown" || (t.stopImmediatePropagation(), t.preventDefault(), this._isOptionsOpen = !this._isOptionsOpen, this._isOptionsOpen ? (e = this.listboxRef.value) == null || e.focusFirstOrSelectedOption() : (i = this.arrowRef.value) == null || i.focus());
296
+ }
297
+ handleOptionToggled(t) {
298
+ this.toggleValue(t.detail);
299
+ }
300
+ handleSearch(t) {
301
+ t.stopPropagation(), this._search = t.detail.toLowerCase();
302
+ }
303
+ handleInputKeydown(t) {
304
+ var e, i, s;
305
+ switch (t.key) {
306
+ case ",":
307
+ case "Enter":
308
+ t.preventDefault(), this.addValue();
309
+ break;
310
+ case "Backspace":
311
+ !this._search && ((e = this.inputRef.value) == null ? void 0 : e.type) === "hidden" && this.removeLastValue(t);
312
+ break;
313
+ case "Tab":
314
+ case "ArrowDown":
315
+ t.shiftKey || ((i = this.listboxRef.value) == null || i.focusFirstOrSelectedOption(), t.preventDefault());
316
+ break;
317
+ case "Escape":
318
+ this._isOptionsOpen = !1, (s = this.arrowRef.value) == null || s.focus(), t.preventDefault();
319
+ break;
320
+ }
321
+ }
322
+ handleTagRemove(t) {
323
+ this.removeSelected(t);
324
+ }
325
+ blurInput() {
326
+ this.inputRef.value && this.inputRef.value.matches(":focus") && this.inputRef.value.blur();
327
+ }
328
+ checkForMatches() {
329
+ var n;
330
+ const t = ((n = this.inputRef.value) == null ? void 0 : n.value) || this._search || "", e = t.trim().toLowerCase() || "";
331
+ if (!e) {
332
+ !this.multiple && this._value[0] && this.removeValue(this._value[0]), this.resetComboboxInput(!1);
333
+ return;
334
+ }
335
+ const i = this._value.find((a) => a.toLowerCase() === e), s = this._options.filter(
336
+ (a) => {
337
+ var u;
338
+ return ((u = a.label) == null ? void 0 : u.toLowerCase().includes(e)) ?? !1;
339
+ }
340
+ ), l = s.find(
341
+ (a) => {
342
+ var u;
343
+ return ((u = a.label) == null ? void 0 : u.toLowerCase()) === e || a.value.toLowerCase() === e;
344
+ }
345
+ );
346
+ switch (!0) {
347
+ case ((s.length === 0 || !l) && this.allowUserInput):
348
+ this._addValueText = t, this._userInfoMessage = "";
349
+ break;
350
+ case (s.length === 0 && !this.allowUserInput):
351
+ this._addValueText = null, this._userInfoMessage = "Ingen match i søket";
352
+ break;
353
+ case !!i:
354
+ this._addValueText = null, this._userInfoMessage = "Verdien er allerede valgt";
355
+ break;
356
+ case s.length > 1:
357
+ this._addValueText = null, this._userInfoMessage = "";
358
+ break;
359
+ default:
360
+ this._addValueText = null, this._userInfoMessage = "";
361
+ }
362
+ }
363
+ findValueInOptions(t) {
364
+ return this.options.find((e) => e.value === t || e.label === t) || null;
365
+ }
366
+ findIndexInOptions(t) {
367
+ return this._options.findIndex((e) => e.value === t || e.label === t);
368
+ }
369
+ isMaxItemsReached() {
370
+ const t = this.maxlength !== null && this._value.length >= this.maxlength;
371
+ return t ? this._maxIsReached = !0 : this._maxIsReached = !1, t;
372
+ }
373
+ toggleValue(t) {
374
+ var I, O;
375
+ if (this.disabled) return;
376
+ this.touched = !0, this._userInfoMessage = "", this._addValueText = null;
377
+ const e = ((I = this.findValueInOptions(t)) == null ? void 0 : I.value) || null, i = this._value.includes(t || e || ""), s = !!e, l = ((O = this._options.find((m) => m.value === t)) == null ? void 0 : O.disabled) || !1, n = !(t != null && t.trim()), a = !this.multiple, u = this.multiple, $ = this.isMaxItemsReached();
378
+ let p = !1, b = !0, g = "", x = "";
379
+ l || (!s && this.allowUserInput && !n ? (this.addNewUserValue(t), g = "Ny verdi lagt til", p = !u) : !s && !this.allowUserInput ? (a && this._value[0] && this.removeValue(this._value[0]), b = !1, p = !0, g = "Ingen treff i søket") : i ? (this.removeValue(e), p = !0) : n && a ? (this.removeAllSelected(), p = !0) : a ? (this._value[0] && this.removeSelected(this._value[0]), this.setSelected(e), p = !1, this.inputRef.value && this.inputRef.value.type !== "hidden" && (this.inputRef.value.value = "", this.inputRef.value.blur())) : u && !$ ? (this.setSelected(e), p = !0) : u && $ ? (this._userInfoMessage = "Maks antall valg nådd", b = !1, x = t) : (a && this.removeAllSelected(), this._userInfoMessage = "Ingen gyldig verdi valgt", b = !1, p = !0, x = t), this._isOptionsOpen = p, p || window.setTimeout(() => {
380
+ var m;
381
+ (m = this.focusRef.value) == null || m.focus();
382
+ }, 0), this._userInfoMessage = g, this._search = x || "", this.resetComboboxInput(b), u && this.isMaxItemsReached());
383
+ }
384
+ setSelected(t) {
385
+ if (!this._value.includes(t)) {
386
+ if (this.multiple && this.isMaxItemsReached()) {
387
+ this._userInfoMessage = "Maks antall valg nådd";
388
+ return;
389
+ }
390
+ !this.multiple && this.removeAllSelected(), this._value = t ? [...this._value, t] : this._value, this._options = this._options.map((e) => (e.value === t && (e.selected = !0), e)), this.resetComboboxInput(!0);
391
+ }
392
+ }
393
+ removeSelected(t) {
394
+ if (!t) return;
395
+ this._value = this._value.filter((i) => i !== t);
396
+ const e = this.findValueInOptions(t);
397
+ e ? (e.selected = !1, e.userAdded ? (this._options = [...this._options.filter((i) => i.value !== t)], this.options = [...this.options.filter((i) => i.value !== t)]) : this._options = [...this._options, e]) : !t && !this.multiple && (this._options = this._options.map((i) => (i.selected = !1, i)));
398
+ }
399
+ addAllOptions() {
400
+ if (this.multiple) {
401
+ if (this.maxlength && this._options.length > this.maxlength) {
402
+ this._userInfoMessage = "For mange valgt";
403
+ return;
404
+ }
405
+ this._value = this._options.map((t) => t.value), this._options = this._options.map((t) => (t.selected = !0, t)), this.requestUpdate();
406
+ }
407
+ }
408
+ removeAllSelected() {
409
+ this._value = [], this._options = this._options.map((t) => (t.selected = !1, t)), this._options = this._options.filter((t) => !t.userAdded), this.requestUpdate();
410
+ }
411
+ addValue() {
412
+ var e;
413
+ const t = ((e = this.inputRef.value) == null ? void 0 : e.value.trim()) || "";
414
+ this._search = t, this.toggleValue(t);
415
+ }
416
+ removeValue(t) {
417
+ this._value = this.multiple ? this._value.filter((e) => e !== t) : [], this.removeSelected(t);
418
+ }
419
+ addNewUserValue(t) {
420
+ if (!t || t.trim() === "") return;
421
+ if (!this.multiple)
422
+ this._value[0] && this.removeSelected(this._value[0]), this._value = [t], this._isOptionsOpen = !1, this.blurInput();
423
+ else if (!this.findValueInOptions(t)) {
424
+ if (this.isMaxItemsReached()) return;
425
+ this._value = [...this._value, t];
426
+ }
427
+ const e = { value: t, label: t, userAdded: !0 };
428
+ this.options = [e, ...this.options], this._options = [e, ...this._options], this.setSelected(t), this.requestUpdate();
429
+ }
430
+ resetComboboxInput(t = !0) {
431
+ if (this._addValueText = null, this.inputRef.value && this.inputRef.value.type !== "hidden" && t)
432
+ if (this._search = "", this.multiple)
433
+ this.inputRef.value.value = "";
434
+ else {
435
+ const e = this.findValueInOptions(this._value[0]);
436
+ window.setTimeout(() => {
437
+ !this.inputRef.value || this.inputRef.value.type === "hidden" || (this.inputRef.value.value = this.displayValueAs === "label" && (e != null && e.label) ? e.label : this._value[0] || "");
438
+ }, 0), this._userInfoMessage = "";
439
+ }
440
+ this._options = [...this.options];
441
+ }
442
+ removeLastValue(t) {
443
+ if (this._value.length === 0) return;
444
+ t.preventDefault();
445
+ const e = this._value[this._value.length - 1];
446
+ e && this.removeSelected(e), this.isMaxItemsReached();
447
+ }
448
+ };
449
+ o([
450
+ r({ type: String, reflect: !0 })
451
+ ], h.prototype, "value", 2);
452
+ o([
453
+ r({ type: Array })
454
+ ], h.prototype, "options", 2);
455
+ o([
456
+ r({ type: Array })
457
+ ], h.prototype, "defaultOptions", 2);
458
+ o([
459
+ r({ type: Boolean })
460
+ ], h.prototype, "allowUserInput", 2);
461
+ o([
462
+ r({ type: Boolean })
463
+ ], h.prototype, "typeahead", 2);
464
+ o([
465
+ r({ type: Boolean })
466
+ ], h.prototype, "includeSearch", 2);
467
+ o([
468
+ r({ type: String })
469
+ ], h.prototype, "searchPlaceholder", 2);
470
+ o([
471
+ r({ type: Boolean })
472
+ ], h.prototype, "multiple", 2);
473
+ o([
474
+ r({ type: Number })
475
+ ], h.prototype, "maxlength", 2);
476
+ o([
477
+ r({ type: String })
478
+ ], h.prototype, "displayValueAs", 2);
479
+ o([
480
+ r({ type: String })
481
+ ], h.prototype, "tagPlacement", 2);
482
+ o([
483
+ c()
484
+ ], h.prototype, "_options", 2);
485
+ o([
486
+ c()
487
+ ], h.prototype, "_isOptionsOpen", 2);
488
+ o([
489
+ c()
490
+ ], h.prototype, "_value", 2);
491
+ o([
492
+ c()
493
+ ], h.prototype, "_userInfoMessage", 2);
494
+ o([
495
+ c()
496
+ ], h.prototype, "_addValueText", 2);
497
+ o([
498
+ c()
499
+ ], h.prototype, "_maxIsReached", 2);
500
+ o([
501
+ c()
502
+ ], h.prototype, "_search", 2);
503
+ o([
504
+ c()
505
+ ], h.prototype, "_inputFocus", 2);
506
+ o([
507
+ c()
508
+ ], h.prototype, "_editingSingleValue", 2);
509
+ h = o([
510
+ k("pkt-combobox")
511
+ ], h);
512
+ export {
513
+ h as P
514
+ };
@@ -1,4 +1,4 @@
1
- "use strict";const l=require("./element-90YeMNbV.cjs"),c=require("./if-defined-C8eotHpL.cjs"),d=require("./state-D-Recv7U.cjs"),r=require("./ref-B-w1vCo8.cjs"),I=require("./class-map-DWDPOqjO.cjs"),$=require("./repeat-CxQA3AeF.cjs"),w=require("./input-element-CInrWeac.cjs"),O=require("./pkt-options-controller-Ay9ELZTV.cjs"),y=require("./pkt-slot-controller-Da-RgXfS.cjs");require("./input-wrapper-B_TtkGqo.cjs");require("./icon-B1_BRNqf.cjs");require("./tag-lU4vz70a.cjs");require("./listbox-AjDNhDsQ.cjs");const C={displayValueAs:{default:"label"}},R={props:C};var V=Object.defineProperty,S=Object.getOwnPropertyDescriptor,a=(b,e,t,i)=>{for(var s=i>1?void 0:i?S(e,t):e,n=b.length-1,u;n>=0;n--)(u=b[n])&&(s=(i?u(e,t,s):u(s))||s);return i&&s&&V(e,t,s),s};exports.PktCombobox=class extends w.PktInputElement{constructor(){super(),this.helptextSlot=r.e(),this.value="",this.options=[],this.allowUserInput=!1,this.typeahead=!1,this.includeSearch=!1,this.searchPlaceholder="",this.multiple=!1,this.maxlength=null,this.displayValueAs=R.props.displayValueAs.default,this.tagPlacement=null,this._options=[],this._isOptionsOpen=!1,this._value=[],this._userInfoMessage="",this._addValueText=null,this._maxIsReached=!1,this._search="",this._inputFocus=!1,this._editingSingleValue=!1,this.inputRef=r.e(),this.arrowRef=r.e(),this.listboxRef=r.e(),this.focusRef=r.e(),this.optionTagRef=r.e(),this.optionsController=new O.PktOptionsSlotController(this),this.slotController=new y.PktSlotController(this,this.helptextSlot),this.slotController.skipOptions=!0}connectedCallback(){if(super.connectedCallback(),document&&document.body.addEventListener("click",e=>{this._isOptionsOpen&&!this.contains(e.target)&&this.handleFocusOut(e)}),this._options=[],this.optionsController.nodes.length){const e=[];this.optionsController.nodes.forEach(t=>{if(!t.textContent&&!t.getAttribute("value"))return null;const i={value:t.getAttribute("value")||t.textContent||"",label:t.textContent||t.getAttribute("value")||""};t.getAttribute("data-prefix")&&(i.prefix=t.getAttribute("data-prefix")||void 0),t.getAttribute("tagskincolor")&&(i.tagSkinColor=t.getAttribute("tagskincolor")),t.getAttribute("description")&&(i.description=t.getAttribute("description")||void 0),i.fulltext=i.value+i.label+(i.prefix||""),e.push(i)}),e.length&&(this.options=[...e],this._options=[...e])}}updated(e){e.has("_value")&&this.valueChanged(this._value,e.get("_value")),e.has("value")&&(this._value=Array.isArray(this.value)?this.value:this.value?this.value.split(","):[],!this.multiple&&this._value.length>1&&(this._value=[this._value[0]]),this.isMaxItemsReached()),e.has("options")&&this.options.length&&(this._options=this.options,this._options.forEach(t=>{if(t.value&&!t.label&&(t.label=t.value),t.selected&&!this._value.includes(t.value)){const i=[...this._value];this._value=[...this._value,t.value],this.valueChanged(this._value,i)}t.fulltext=t.value+t.label+(t.prefix||""),t.selected=t.selected||this._value.includes(t.value)})),e.has("_search")&&this.dispatchEvent(new CustomEvent("search",{detail:this._search,bubbles:!1})),super.updated(e)}attributeChangedCallback(e,t,i){e==="value"&&(this._value=Array.isArray(this.value)?this.value:this.value?this.value.split(","):[],!this.multiple&&this._value.length>1&&(this._value=[this._value[0]])),e==="options"&&(this._options=this.options,this._options.forEach(s=>{s.value&&!s.label&&(s.label=s.value),s.selected&&!this._value.includes(s.value)&&(this._value=[...this._value,s.value]),s.fulltext=s.value+s.label+(s.prefix||"")}),this._search=""),super.attributeChangedCallback(e,t,i)}render(){return l.x`
1
+ "use strict";const a=require("./element-90YeMNbV.cjs"),c=require("./if-defined-C8eotHpL.cjs"),d=require("./state-D-Recv7U.cjs"),r=require("./ref-B-w1vCo8.cjs"),O=require("./class-map-DWDPOqjO.cjs"),I=require("./repeat-CxQA3AeF.cjs"),$=require("./input-element-CInrWeac.cjs"),y=require("./pkt-options-controller-Ay9ELZTV.cjs"),w=require("./pkt-slot-controller-Da-RgXfS.cjs");require("./input-wrapper-B_TtkGqo.cjs");require("./icon-B1_BRNqf.cjs");require("./tag-lU4vz70a.cjs");require("./listbox-AjDNhDsQ.cjs");const C={displayValueAs:{default:"label"}},R={props:C};var V=Object.defineProperty,S=Object.getOwnPropertyDescriptor,o=(b,e,t,i)=>{for(var s=i>1?void 0:i?S(e,t):e,l=b.length-1,n;l>=0;l--)(n=b[l])&&(s=(i?n(e,t,s):n(s))||s);return i&&s&&V(e,t,s),s};exports.PktCombobox=class extends $.PktInputElement{constructor(){super(),this.helptextSlot=r.e(),this.value="",this.options=[],this.defaultOptions=[],this.allowUserInput=!1,this.typeahead=!1,this.includeSearch=!1,this.searchPlaceholder="",this.multiple=!1,this.maxlength=null,this.displayValueAs=R.props.displayValueAs.default,this.tagPlacement=null,this._options=[],this._isOptionsOpen=!1,this._value=[],this._userInfoMessage="",this._addValueText=null,this._maxIsReached=!1,this._search="",this._inputFocus=!1,this._editingSingleValue=!1,this.inputRef=r.e(),this.arrowRef=r.e(),this.listboxRef=r.e(),this.focusRef=r.e(),this.optionTagRef=r.e(),this.optionsController=new y.PktOptionsSlotController(this),this.slotController=new w.PktSlotController(this,this.helptextSlot),this.slotController.skipOptions=!0}connectedCallback(){var e;if(super.connectedCallback(),document&&document.body.addEventListener("click",t=>{this._isOptionsOpen&&!this.contains(t.target)&&this.handleFocusOut(t)}),this._options=[],this.defaultOptions&&this.defaultOptions.length){const t=((e=this.options)==null?void 0:e.filter(i=>i.userAdded))||[];this.options=[...t,...structuredClone(this.defaultOptions)],this._options=[...this.options]}if(this.optionsController.nodes.length){const t=[];this.optionsController.nodes.forEach(i=>{if(!i.textContent&&!i.getAttribute("value"))return null;const s={value:i.getAttribute("value")||i.textContent||"",label:i.textContent||i.getAttribute("value")||""};i.getAttribute("data-prefix")&&(s.prefix=i.getAttribute("data-prefix")||void 0),i.getAttribute("tagskincolor")&&(s.tagSkinColor=i.getAttribute("tagskincolor")),i.getAttribute("description")&&(s.description=i.getAttribute("description")||void 0),s.fulltext=s.value+s.label+(s.prefix||""),t.push(s)}),t.length&&(this.options=[...t],this._options=[...t])}}updated(e){var t;if(e.has("_value")&&this.valueChanged(this._value,e.get("_value")),e.has("value")&&(this._value=Array.isArray(this.value)?this.value:this.value?this.value.split(","):[],!this.multiple&&this._value.length>1&&(this._value=[this._value[0]]),this.isMaxItemsReached()),e.has("defaultOptions")&&this.defaultOptions.length){const i=((t=this.options)==null?void 0:t.filter(s=>s.userAdded))||[];this.options=[...i,...structuredClone(this.defaultOptions)],this._options=[...this.options]}if(e.has("options")&&this.options.length){const s=this._options.filter(l=>l.userAdded).filter(l=>!this.options.some(n=>n.value===l.value));this._options=[...s,...this.options],this._options.forEach(l=>{if(l.value&&!l.label&&(l.label=l.value),l.selected&&!this._value.includes(l.value)){const n=[...this._value];this._value=[...this._value,l.value],this.valueChanged(this._value,n)}l.fulltext=l.value+l.label+(l.prefix||""),l.selected=l.selected||this._value.includes(l.value)})}e.has("_search")&&this.dispatchEvent(new CustomEvent("search",{detail:this._search,bubbles:!1})),super.updated(e)}attributeChangedCallback(e,t,i){e==="value"&&(this._value=Array.isArray(this.value)?this.value:this.value?this.value.split(","):[],!this.multiple&&this._value.length>1&&(this._value=[this._value[0]])),e==="options"&&(this._options=this.options,this._options.forEach(s=>{s.value&&!s.label&&(s.label=s.value),s.selected&&!this._value.includes(s.value)&&(this._value=[...this._value,s.value]),s.fulltext=s.value+s.label+(s.prefix||"")}),this._search=""),super.attributeChangedCallback(e,t,i)}render(){return a.x`
2
2
  <pkt-input-wrapper
3
3
  .label=${this.label}
4
4
  .helptext=${this.helptext}
@@ -21,11 +21,11 @@
21
21
  <div class="pkt-contents" ${r.n(this.helptextSlot)} name="helptext" slot="helptext"></div>
22
22
  <div class="pkt-combobox" @focusout=${this.handleFocusOut}>
23
23
  <div
24
- class=${I.e({"pkt-combobox__input":!0,"pkt-combobox__input--fullwidth":this.fullwidth,"pkt-combobox__input--open":this._isOptionsOpen,"pkt-combobox__input--error":this.hasError,"pkt-combobox__input--disabled":this.disabled})}
24
+ class=${O.e({"pkt-combobox__input":!0,"pkt-combobox__input--fullwidth":this.fullwidth,"pkt-combobox__input--open":this._isOptionsOpen,"pkt-combobox__input--error":this.hasError,"pkt-combobox__input--disabled":this.disabled})}
25
25
  tabindex="-1"
26
26
  @click=${this.handleInputClick}
27
27
  >
28
- ${this.placeholder&&(!this._value.length||this.multiple&&this.tagPlacement=="outside")&&!this._inputFocus?l.x`<span class="pkt-combobox__placeholder">${this.placeholder}</span>`:this.tagPlacement!=="outside"?this.renderSingleOrMultipleValues():l.E}
28
+ ${this.placeholder&&(!this._value.length||this.multiple&&this.tagPlacement=="outside")&&!this._inputFocus?a.x`<span class="pkt-combobox__placeholder">${this.placeholder}</span>`:this.tagPlacement!=="outside"?this.renderSingleOrMultipleValues():a.E}
29
29
  ${this.renderInputField()}
30
30
  <div
31
31
  class="pkt-btn pkt-btn--tertiary pkt-combobox__arrow"
@@ -43,7 +43,7 @@
43
43
  tabindex="${this.disabled?"-1":"0"}"
44
44
  >
45
45
  <pkt-icon
46
- class=${I.e({"pkt-combobox__arrow-icon":!0,"pkt-combobox__arrow-icon--open":this._isOptionsOpen})}
46
+ class=${O.e({"pkt-combobox__arrow-icon":!0,"pkt-combobox__arrow-icon--open":this._isOptionsOpen})}
47
47
  name="chevron-thin-down"
48
48
  ></pkt-icon>
49
49
  </div>
@@ -72,11 +72,11 @@
72
72
  ></pkt-listbox>
73
73
  </div>
74
74
 
75
- ${this.tagPlacement==="outside"&&this.multiple?l.x`<div class="pkt-combobox__tags-outside">
75
+ ${this.tagPlacement==="outside"&&this.multiple?a.x`<div class="pkt-combobox__tags-outside">
76
76
  ${this.renderSingleOrMultipleValues()}
77
- </div>`:l.E}
77
+ </div>`:a.E}
78
78
  </pkt-input-wrapper>
79
- `}renderInputField(){return this.typeahead||this.allowUserInput?l.x`
79
+ `}renderInputField(){return this.typeahead||this.allowUserInput?a.x`
80
80
  <div class="pkt-combobox__input-div combobox__input">
81
81
  <input
82
82
  type="text"
@@ -96,7 +96,7 @@
96
96
  ${r.n(this.inputRef)}
97
97
  />
98
98
  </div>
99
- `:l.x`
99
+ `:a.x`
100
100
  <input
101
101
  type="hidden"
102
102
  id="${this.id}-input"
@@ -104,12 +104,12 @@
104
104
  .value=${this._value.join(",")}
105
105
  ${r.n(this.inputRef)}
106
106
  />
107
- `}renderSingleOrMultipleValues(){const e=!this.multiple,t=this._editingSingleValue?null:this.renderValueTag(this.findValueInOptions(this._value[0])),i=$.c(this._value,s=>s,s=>{var o;const n=this.findValueInOptions(s),u=(o=this.options.find(h=>h.value===s))==null?void 0:o.tagSkinColor;return l.x`
107
+ `}renderSingleOrMultipleValues(){const e=!this.multiple,t=this._editingSingleValue?null:this.renderValueTag(this.findValueInOptions(this._value[0])),i=I.c(this._value,s=>s,s=>{var h;const l=this.findValueInOptions(s),n=(h=this.options.find(u=>u.value===s))==null?void 0:h.tagSkinColor;return a.x`
108
108
  <pkt-tag
109
- skin=${u||"blue-dark"}
109
+ skin=${n||"blue-dark"}
110
110
  ?closeTag=${!this.disabled}
111
111
  @close=${()=>this.handleTagRemove(s)}
112
112
  >
113
- ${this.renderValueTag(n)}
113
+ ${this.renderValueTag(l)}
114
114
  </pkt-tag>
115
- `});return e?t:i}renderValueTag(e){if(!e)return"";switch(this.displayValueAs){case"prefixAndValue":return l.x`<span data-focusfix=${this.id}>${e.prefix||""} ${e.value}</span>`;case"value":return l.x`<span data-focusfix=${this.id}>${e.value}</span>`;case"label":default:return l.x`<span data-focusfix=${this.id}>${e.label||e.value}</span>`}}handleInput(e){if(e.stopPropagation(),e.stopImmediatePropagation(),this.disabled)return;this.touched=!0;const t=e.target;if(this._search=t.value,this.checkForMatches(),this.typeahead)if(this._search){if(this._options=this.options.filter(i=>{var s;return(s=i.fulltext)==null?void 0:s.toLowerCase().includes(this._search.toLowerCase())}),e.inputType!=="deleteContentBackward"){const i=this._options.filter(s=>{var n;return!s.selected&&((n=s.label)==null?void 0:n.toLowerCase().startsWith(this._search.toLowerCase()))});if(i.length>0&&this.inputRef.value&&this.inputRef.value.type!=="hidden"){const s=i[0];s!=null&&s.label&&(t.value=s.label,window.setTimeout(()=>t.setSelectionRange(this._search.length,t.value.length),0),t.selectionDirection="backward")}}}else this._options=[...this.options]}handleFocus(){if(!this.disabled){if(!this.multiple&&this._value[0]&&this.inputRef.value&&this.inputRef.value.type!=="hidden"){const e=this.findValueInOptions(this._value[0]);this._editingSingleValue=!0,this.inputRef.value.value=this.displayValueAs==="label"&&(e!=null&&e.label)?e.label:this._value[0]}this._inputFocus=!0,this._search="",this._options=[...this.options],this._isOptionsOpen=!0,this.onFocus(),this.requestUpdate()}}handleFocusOut(e){var t,i,s,n,u;if(!this.disabled&&((i=(t=e.relatedTarget)==null?void 0:t.closest("pkt-combobox"))==null?void 0:i.id)!==this.id&&((n=(s=e.relatedTarget)==null?void 0:s.closest("pkt-combobox"))==null?void 0:n.id)!==this.id&&((u=e.target)==null?void 0:u.getAttribute("data-focusfix"))!==this.id&&e.relatedTarget!==this.focusRef.value&&e.relatedTarget!==this.inputRef.value&&e.relatedTarget!==this.arrowRef.value&&this._isOptionsOpen){if(this._inputFocus=!1,this._addValueText=null,this._userInfoMessage="",this._search="",this.inputRef.value&&this.inputRef.value.type!=="hidden"&&this.inputRef.value.value!==""){const o=this.inputRef.value.value,h=this.findValueInOptions(o);!this._value.includes(o)&&!h?this.allowUserInput?this.addNewUserValue(o):this.multiple||this.removeValue(this._value[0]):h&&!this._value.includes(h.value)&&this.setSelected(h.value),this.inputRef.value.value=""}this._isOptionsOpen=!1,this.onBlur()}}handleBlur(){this._inputFocus=!1,this._editingSingleValue=!1,this.onBlur()}handleInputClick(e){var t,i;this.disabled||(e.currentTarget&&e.currentTarget!==this.arrowRef.value&&((t=this.inputRef.value)==null?void 0:t.type)!=="hidden"?(i=this.inputRef.value)==null||i.focus():this.handleArrowClick(e))}handleArrowClick(e){var t,i;this.disabled||e instanceof KeyboardEvent&&e.key&&e.key!=="Enter"&&e.key!==" "&&e.key!=="ArrowDown"||(e.stopImmediatePropagation(),e.preventDefault(),this._isOptionsOpen=!this._isOptionsOpen,this._isOptionsOpen?(t=this.listboxRef.value)==null||t.focusFirstOrSelectedOption():(i=this.arrowRef.value)==null||i.focus())}handleOptionToggled(e){this.toggleValue(e.detail)}handleSearch(e){e.stopPropagation(),this._search=e.detail.toLowerCase()}handleInputKeydown(e){var t,i,s;switch(e.key){case",":case"Enter":e.preventDefault(),this.addValue();break;case"Backspace":!this._search&&((t=this.inputRef.value)==null?void 0:t.type)==="hidden"&&this.removeLastValue(e);break;case"Tab":case"ArrowDown":e.shiftKey||((i=this.listboxRef.value)==null||i.focusFirstOrSelectedOption(),e.preventDefault());break;case"Escape":this._isOptionsOpen=!1,(s=this.arrowRef.value)==null||s.focus(),e.preventDefault();break}}handleTagRemove(e){this.removeSelected(e)}blurInput(){this.inputRef.value&&this.inputRef.value.matches(":focus")&&this.inputRef.value.blur()}checkForMatches(){var u;const e=((u=this.inputRef.value)==null?void 0:u.value)||this._search||"",t=e.trim().toLowerCase()||"";if(!t){!this.multiple&&this._value[0]&&this.removeValue(this._value[0]),this.resetComboboxInput(!1);return}const i=this._value.find(o=>o.toLowerCase()===t),s=this._options.filter(o=>{var h;return((h=o.label)==null?void 0:h.toLowerCase().includes(t))??!1}),n=s.find(o=>{var h;return((h=o.label)==null?void 0:h.toLowerCase())===t||o.value.toLowerCase()===t});switch(!0){case((s.length===0||!n)&&this.allowUserInput):this._addValueText=e,this._userInfoMessage="";break;case(s.length===0&&!this.allowUserInput):this._addValueText=null,this._userInfoMessage="Ingen match i søket";break;case!!i:this._addValueText=null,this._userInfoMessage="Verdien er allerede valgt";break;case s.length>1:this._addValueText=null,this._userInfoMessage="";break;default:this._addValueText=null,this._userInfoMessage=""}}findValueInOptions(e){return this.options.find(t=>t.value===e||t.label===e)||null}findIndexInOptions(e){return this._options.findIndex(t=>t.value===e||t.label===e)}isMaxItemsReached(){const e=this.maxlength!==null&&this._value.length>=this.maxlength;return e?this._maxIsReached=!0:this._maxIsReached=!1,e}toggleValue(e){var g,k;if(this.disabled)return;this.touched=!0,this._userInfoMessage="",this._addValueText=null;const t=((g=this.findValueInOptions(e))==null?void 0:g.value)||null,i=this._value.includes(e||t||""),s=!!t,n=((k=this._options.find(_=>_.value===e))==null?void 0:k.disabled)||!1,u=!(e!=null&&e.trim()),o=!this.multiple,h=this.multiple,x=this.isMaxItemsReached();let p=!1,f=!0,v="",m="";n||(!s&&this.allowUserInput&&!u?(this.addNewUserValue(e),v="Ny verdi lagt til",p=!h):!s&&!this.allowUserInput?(o&&this._value[0]&&this.removeValue(this._value[0]),f=!1,p=!0,v="Ingen treff i søket"):i?(this.removeValue(t),p=!0):u&&o?(this.removeAllSelected(),p=!0):o?(this._value[0]&&this.removeSelected(this._value[0]),this.setSelected(t),p=!1,this.inputRef.value&&this.inputRef.value.type!=="hidden"&&(this.inputRef.value.value="",this.inputRef.value.blur())):h&&!x?(this.setSelected(t),p=!0):h&&x?(this._userInfoMessage="Maks antall valg nådd",f=!1,m=e):(o&&this.removeAllSelected(),this._userInfoMessage="Ingen gyldig verdi valgt",f=!1,p=!0,m=e),this._isOptionsOpen=p,p||window.setTimeout(()=>{var _;(_=this.focusRef.value)==null||_.focus()},0),this._userInfoMessage=v,this._search=m||"",this.resetComboboxInput(f),h&&this.isMaxItemsReached())}setSelected(e){if(!this._value.includes(e)){if(this.multiple&&this.isMaxItemsReached()){this._userInfoMessage="Maks antall valg nådd";return}!this.multiple&&this.removeAllSelected(),this._value=e?[...this._value,e]:this._value,this._options=this._options.map(t=>(t.value===e&&(t.selected=!0),t)),this.resetComboboxInput(!0)}}removeSelected(e){if(!e)return;this._value=this._value.filter(i=>i!==e);const t=this.findValueInOptions(e);t?(t.selected=!1,t.userAdded?(this._options=[...this._options.filter(i=>i.value!==e)],this.options=[...this.options.filter(i=>i.value!==e)]):this._options=[...this._options,t]):!e&&!this.multiple&&(this._options=this._options.map(i=>(i.selected=!1,i)))}addAllOptions(){if(this.multiple){if(this.maxlength&&this._options.length>this.maxlength){this._userInfoMessage="For mange valgt";return}this._value=this._options.map(e=>e.value),this._options=this._options.map(e=>(e.selected=!0,e)),this.requestUpdate()}}removeAllSelected(){this._value=[],this._options=this._options.map(e=>(e.selected=!1,e)),this._options=this._options.filter(e=>!e.userAdded),this.requestUpdate()}addValue(){var t;const e=((t=this.inputRef.value)==null?void 0:t.value.trim())||"";this._search=e,this.toggleValue(e)}removeValue(e){this._value=this.multiple?this._value.filter(t=>t!==e):[],this.removeSelected(e)}addNewUserValue(e){if(!e||e.trim()==="")return;if(!this.multiple)this._value[0]&&this.removeSelected(this._value[0]),this._value=[e],this._isOptionsOpen=!1,this.blurInput();else if(!this.findValueInOptions(e)){if(this.isMaxItemsReached())return;this._value=[...this._value,e]}const t={value:e,label:e,userAdded:!0};this.options=[t,...this.options],this._options=[t,...this._options],this.setSelected(e),this.requestUpdate()}resetComboboxInput(e=!0){if(this._addValueText=null,this.inputRef.value&&this.inputRef.value.type!=="hidden"&&e)if(this._search="",this.multiple)this.inputRef.value.value="";else{const t=this.findValueInOptions(this._value[0]);window.setTimeout(()=>{!this.inputRef.value||this.inputRef.value.type==="hidden"||(this.inputRef.value.value=this.displayValueAs==="label"&&(t!=null&&t.label)?t.label:this._value[0]||"")},0),this._userInfoMessage=""}this._options=[...this.options]}removeLastValue(e){if(this._value.length===0)return;e.preventDefault();const t=this._value[this._value.length-1];t&&this.removeSelected(t),this.isMaxItemsReached()}};a([l.n({type:String,reflect:!0})],exports.PktCombobox.prototype,"value",2);a([l.n({type:Array})],exports.PktCombobox.prototype,"options",2);a([l.n({type:Boolean})],exports.PktCombobox.prototype,"allowUserInput",2);a([l.n({type:Boolean})],exports.PktCombobox.prototype,"typeahead",2);a([l.n({type:Boolean})],exports.PktCombobox.prototype,"includeSearch",2);a([l.n({type:String})],exports.PktCombobox.prototype,"searchPlaceholder",2);a([l.n({type:Boolean})],exports.PktCombobox.prototype,"multiple",2);a([l.n({type:Number})],exports.PktCombobox.prototype,"maxlength",2);a([l.n({type:String})],exports.PktCombobox.prototype,"displayValueAs",2);a([l.n({type:String})],exports.PktCombobox.prototype,"tagPlacement",2);a([d.r()],exports.PktCombobox.prototype,"_options",2);a([d.r()],exports.PktCombobox.prototype,"_isOptionsOpen",2);a([d.r()],exports.PktCombobox.prototype,"_value",2);a([d.r()],exports.PktCombobox.prototype,"_userInfoMessage",2);a([d.r()],exports.PktCombobox.prototype,"_addValueText",2);a([d.r()],exports.PktCombobox.prototype,"_maxIsReached",2);a([d.r()],exports.PktCombobox.prototype,"_search",2);a([d.r()],exports.PktCombobox.prototype,"_inputFocus",2);a([d.r()],exports.PktCombobox.prototype,"_editingSingleValue",2);exports.PktCombobox=a([l.t("pkt-combobox")],exports.PktCombobox);
115
+ `});return e?t:i}renderValueTag(e){if(!e)return"";switch(this.displayValueAs){case"prefixAndValue":return a.x`<span data-focusfix=${this.id}>${e.prefix||""} ${e.value}</span>`;case"value":return a.x`<span data-focusfix=${this.id}>${e.value}</span>`;case"label":default:return a.x`<span data-focusfix=${this.id}>${e.label||e.value}</span>`}}handleInput(e){if(e.stopPropagation(),e.stopImmediatePropagation(),this.disabled)return;this.touched=!0;const t=e.target;if(this._search=t.value,this.checkForMatches(),this.typeahead)if(this._search){if(this._options=this.options.filter(i=>{var s;return(s=i.fulltext)==null?void 0:s.toLowerCase().includes(this._search.toLowerCase())}),e.inputType!=="deleteContentBackward"){const i=this._options.filter(s=>{var l;return!s.selected&&((l=s.label)==null?void 0:l.toLowerCase().startsWith(this._search.toLowerCase()))});if(i.length>0&&this.inputRef.value&&this.inputRef.value.type!=="hidden"){const s=i[0];s!=null&&s.label&&(t.value=s.label,window.setTimeout(()=>t.setSelectionRange(this._search.length,t.value.length),0),t.selectionDirection="backward")}}}else this._options=[...this.options]}handleFocus(){if(!this.disabled){if(!this.multiple&&this._value[0]&&this.inputRef.value&&this.inputRef.value.type!=="hidden"){const e=this.findValueInOptions(this._value[0]);this._editingSingleValue=!0,this.inputRef.value.value=this.displayValueAs==="label"&&(e!=null&&e.label)?e.label:this._value[0]}this._inputFocus=!0,this._search="",this._options=[...this.options],this._isOptionsOpen=!0,this.onFocus(),this.requestUpdate()}}handleFocusOut(e){var t,i,s,l,n;if(!this.disabled&&((i=(t=e.relatedTarget)==null?void 0:t.closest("pkt-combobox"))==null?void 0:i.id)!==this.id&&((l=(s=e.relatedTarget)==null?void 0:s.closest("pkt-combobox"))==null?void 0:l.id)!==this.id&&((n=e.target)==null?void 0:n.getAttribute("data-focusfix"))!==this.id&&e.relatedTarget!==this.focusRef.value&&e.relatedTarget!==this.inputRef.value&&e.relatedTarget!==this.arrowRef.value&&this._isOptionsOpen){if(this._inputFocus=!1,this._addValueText=null,this._userInfoMessage="",this._search="",this.inputRef.value&&this.inputRef.value.type!=="hidden"&&this.inputRef.value.value!==""){const h=this.inputRef.value.value,u=this.findValueInOptions(h);!this._value.includes(h)&&!u?this.allowUserInput?this.addNewUserValue(h):this.multiple||this.removeValue(this._value[0]):u&&!this._value.includes(u.value)&&this.setSelected(u.value),this.inputRef.value.value=""}this._isOptionsOpen=!1,this.onBlur()}}handleBlur(){this._inputFocus=!1,this._editingSingleValue=!1,this.onBlur()}handleInputClick(e){var t,i;this.disabled||(e.currentTarget&&e.currentTarget!==this.arrowRef.value&&((t=this.inputRef.value)==null?void 0:t.type)!=="hidden"?(i=this.inputRef.value)==null||i.focus():this.handleArrowClick(e))}handleArrowClick(e){var t,i;this.disabled||e instanceof KeyboardEvent&&e.key&&e.key!=="Enter"&&e.key!==" "&&e.key!=="ArrowDown"||(e.stopImmediatePropagation(),e.preventDefault(),this._isOptionsOpen=!this._isOptionsOpen,this._isOptionsOpen?(t=this.listboxRef.value)==null||t.focusFirstOrSelectedOption():(i=this.arrowRef.value)==null||i.focus())}handleOptionToggled(e){this.toggleValue(e.detail)}handleSearch(e){e.stopPropagation(),this._search=e.detail.toLowerCase()}handleInputKeydown(e){var t,i,s;switch(e.key){case",":case"Enter":e.preventDefault(),this.addValue();break;case"Backspace":!this._search&&((t=this.inputRef.value)==null?void 0:t.type)==="hidden"&&this.removeLastValue(e);break;case"Tab":case"ArrowDown":e.shiftKey||((i=this.listboxRef.value)==null||i.focusFirstOrSelectedOption(),e.preventDefault());break;case"Escape":this._isOptionsOpen=!1,(s=this.arrowRef.value)==null||s.focus(),e.preventDefault();break}}handleTagRemove(e){this.removeSelected(e)}blurInput(){this.inputRef.value&&this.inputRef.value.matches(":focus")&&this.inputRef.value.blur()}checkForMatches(){var n;const e=((n=this.inputRef.value)==null?void 0:n.value)||this._search||"",t=e.trim().toLowerCase()||"";if(!t){!this.multiple&&this._value[0]&&this.removeValue(this._value[0]),this.resetComboboxInput(!1);return}const i=this._value.find(h=>h.toLowerCase()===t),s=this._options.filter(h=>{var u;return((u=h.label)==null?void 0:u.toLowerCase().includes(t))??!1}),l=s.find(h=>{var u;return((u=h.label)==null?void 0:u.toLowerCase())===t||h.value.toLowerCase()===t});switch(!0){case((s.length===0||!l)&&this.allowUserInput):this._addValueText=e,this._userInfoMessage="";break;case(s.length===0&&!this.allowUserInput):this._addValueText=null,this._userInfoMessage="Ingen match i søket";break;case!!i:this._addValueText=null,this._userInfoMessage="Verdien er allerede valgt";break;case s.length>1:this._addValueText=null,this._userInfoMessage="";break;default:this._addValueText=null,this._userInfoMessage=""}}findValueInOptions(e){return this.options.find(t=>t.value===e||t.label===e)||null}findIndexInOptions(e){return this._options.findIndex(t=>t.value===e||t.label===e)}isMaxItemsReached(){const e=this.maxlength!==null&&this._value.length>=this.maxlength;return e?this._maxIsReached=!0:this._maxIsReached=!1,e}toggleValue(e){var g,k;if(this.disabled)return;this.touched=!0,this._userInfoMessage="",this._addValueText=null;const t=((g=this.findValueInOptions(e))==null?void 0:g.value)||null,i=this._value.includes(e||t||""),s=!!t,l=((k=this._options.find(_=>_.value===e))==null?void 0:k.disabled)||!1,n=!(e!=null&&e.trim()),h=!this.multiple,u=this.multiple,x=this.isMaxItemsReached();let p=!1,f=!0,v="",m="";l||(!s&&this.allowUserInput&&!n?(this.addNewUserValue(e),v="Ny verdi lagt til",p=!u):!s&&!this.allowUserInput?(h&&this._value[0]&&this.removeValue(this._value[0]),f=!1,p=!0,v="Ingen treff i søket"):i?(this.removeValue(t),p=!0):n&&h?(this.removeAllSelected(),p=!0):h?(this._value[0]&&this.removeSelected(this._value[0]),this.setSelected(t),p=!1,this.inputRef.value&&this.inputRef.value.type!=="hidden"&&(this.inputRef.value.value="",this.inputRef.value.blur())):u&&!x?(this.setSelected(t),p=!0):u&&x?(this._userInfoMessage="Maks antall valg nådd",f=!1,m=e):(h&&this.removeAllSelected(),this._userInfoMessage="Ingen gyldig verdi valgt",f=!1,p=!0,m=e),this._isOptionsOpen=p,p||window.setTimeout(()=>{var _;(_=this.focusRef.value)==null||_.focus()},0),this._userInfoMessage=v,this._search=m||"",this.resetComboboxInput(f),u&&this.isMaxItemsReached())}setSelected(e){if(!this._value.includes(e)){if(this.multiple&&this.isMaxItemsReached()){this._userInfoMessage="Maks antall valg nådd";return}!this.multiple&&this.removeAllSelected(),this._value=e?[...this._value,e]:this._value,this._options=this._options.map(t=>(t.value===e&&(t.selected=!0),t)),this.resetComboboxInput(!0)}}removeSelected(e){if(!e)return;this._value=this._value.filter(i=>i!==e);const t=this.findValueInOptions(e);t?(t.selected=!1,t.userAdded?(this._options=[...this._options.filter(i=>i.value!==e)],this.options=[...this.options.filter(i=>i.value!==e)]):this._options=[...this._options,t]):!e&&!this.multiple&&(this._options=this._options.map(i=>(i.selected=!1,i)))}addAllOptions(){if(this.multiple){if(this.maxlength&&this._options.length>this.maxlength){this._userInfoMessage="For mange valgt";return}this._value=this._options.map(e=>e.value),this._options=this._options.map(e=>(e.selected=!0,e)),this.requestUpdate()}}removeAllSelected(){this._value=[],this._options=this._options.map(e=>(e.selected=!1,e)),this._options=this._options.filter(e=>!e.userAdded),this.requestUpdate()}addValue(){var t;const e=((t=this.inputRef.value)==null?void 0:t.value.trim())||"";this._search=e,this.toggleValue(e)}removeValue(e){this._value=this.multiple?this._value.filter(t=>t!==e):[],this.removeSelected(e)}addNewUserValue(e){if(!e||e.trim()==="")return;if(!this.multiple)this._value[0]&&this.removeSelected(this._value[0]),this._value=[e],this._isOptionsOpen=!1,this.blurInput();else if(!this.findValueInOptions(e)){if(this.isMaxItemsReached())return;this._value=[...this._value,e]}const t={value:e,label:e,userAdded:!0};this.options=[t,...this.options],this._options=[t,...this._options],this.setSelected(e),this.requestUpdate()}resetComboboxInput(e=!0){if(this._addValueText=null,this.inputRef.value&&this.inputRef.value.type!=="hidden"&&e)if(this._search="",this.multiple)this.inputRef.value.value="";else{const t=this.findValueInOptions(this._value[0]);window.setTimeout(()=>{!this.inputRef.value||this.inputRef.value.type==="hidden"||(this.inputRef.value.value=this.displayValueAs==="label"&&(t!=null&&t.label)?t.label:this._value[0]||"")},0),this._userInfoMessage=""}this._options=[...this.options]}removeLastValue(e){if(this._value.length===0)return;e.preventDefault();const t=this._value[this._value.length-1];t&&this.removeSelected(t),this.isMaxItemsReached()}};o([a.n({type:String,reflect:!0})],exports.PktCombobox.prototype,"value",2);o([a.n({type:Array})],exports.PktCombobox.prototype,"options",2);o([a.n({type:Array})],exports.PktCombobox.prototype,"defaultOptions",2);o([a.n({type:Boolean})],exports.PktCombobox.prototype,"allowUserInput",2);o([a.n({type:Boolean})],exports.PktCombobox.prototype,"typeahead",2);o([a.n({type:Boolean})],exports.PktCombobox.prototype,"includeSearch",2);o([a.n({type:String})],exports.PktCombobox.prototype,"searchPlaceholder",2);o([a.n({type:Boolean})],exports.PktCombobox.prototype,"multiple",2);o([a.n({type:Number})],exports.PktCombobox.prototype,"maxlength",2);o([a.n({type:String})],exports.PktCombobox.prototype,"displayValueAs",2);o([a.n({type:String})],exports.PktCombobox.prototype,"tagPlacement",2);o([d.r()],exports.PktCombobox.prototype,"_options",2);o([d.r()],exports.PktCombobox.prototype,"_isOptionsOpen",2);o([d.r()],exports.PktCombobox.prototype,"_value",2);o([d.r()],exports.PktCombobox.prototype,"_userInfoMessage",2);o([d.r()],exports.PktCombobox.prototype,"_addValueText",2);o([d.r()],exports.PktCombobox.prototype,"_maxIsReached",2);o([d.r()],exports.PktCombobox.prototype,"_search",2);o([d.r()],exports.PktCombobox.prototype,"_inputFocus",2);o([d.r()],exports.PktCombobox.prototype,"_editingSingleValue",2);exports.PktCombobox=o([a.t("pkt-combobox")],exports.PktCombobox);