@universal-material/web 3.0.45 → 3.0.47
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/chip/chip.d.ts +1 -0
- package/chip/chip.d.ts.map +1 -1
- package/chip/chip.js +4 -0
- package/chip/chip.js.map +1 -1
- package/chip-field/chip-field.d.ts.map +1 -1
- package/chip-field/chip-field.js +1 -4
- package/chip-field/chip-field.js.map +1 -1
- package/custom-elements.json +2874 -2857
- package/package.json +1 -1
- package/typeahead/typeahead.d.ts +4 -0
- package/typeahead/typeahead.d.ts.map +1 -1
- package/typeahead/typeahead.js +28 -8
- package/typeahead/typeahead.js.map +1 -1
package/package.json
CHANGED
package/typeahead/typeahead.d.ts
CHANGED
|
@@ -13,6 +13,7 @@ export declare class UmTypeahead extends LitElement {
|
|
|
13
13
|
results: Data[];
|
|
14
14
|
source: (any[] | ((term: string) => Promise<any[]>)) | undefined;
|
|
15
15
|
formatter: ((value: any) => string) | undefined;
|
|
16
|
+
template: ((value: any) => string) | undefined;
|
|
16
17
|
debounce: number;
|
|
17
18
|
limit: number;
|
|
18
19
|
minLength: number;
|
|
@@ -21,6 +22,8 @@ export declare class UmTypeahead extends LitElement {
|
|
|
21
22
|
get form(): HTMLFormElement | null;
|
|
22
23
|
get value(): any;
|
|
23
24
|
set value(value: any);
|
|
25
|
+
focus(): void;
|
|
26
|
+
clear(): void;
|
|
24
27
|
get targetId(): string | undefined;
|
|
25
28
|
set targetId(value: string | undefined);
|
|
26
29
|
menu: UmMenu;
|
|
@@ -29,6 +32,7 @@ export declare class UmTypeahead extends LitElement {
|
|
|
29
32
|
disconnectedCallback(): void;
|
|
30
33
|
protected render(): HTMLTemplateResult;
|
|
31
34
|
private getTargetValue;
|
|
35
|
+
private setTargetValue;
|
|
32
36
|
}
|
|
33
37
|
declare global {
|
|
34
38
|
interface HTMLElementTagNameMap {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"typeahead.d.ts","sourceRoot":"","sources":["../../src/typeahead/typeahead.ts"],"names":[],"mappings":"AAAA,OAAO,EAAQ,kBAAkB,EAAE,UAAU,EAAE,MAAM,KAAK,CAAC;
|
|
1
|
+
{"version":3,"file":"typeahead.d.ts","sourceRoot":"","sources":["../../src/typeahead/typeahead.ts"],"names":[],"mappings":"AAAA,OAAO,EAAQ,kBAAkB,EAAE,UAAU,EAAE,MAAM,KAAK,CAAC;AAM3D,OAAO,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAC;AAGzC,OAAO,gBAAgB,CAAC;AAExB,MAAM,WAAW,IAAI;IACnB,KAAK,EAAE,MAAM,CAAC;IACd,KAAK,EAAE,GAAG,CAAC;CACZ;AAED,qBACa,WAAY,SAAQ,UAAU;;IACzC,MAAM,CAAC,QAAQ,CAAC,cAAc,QAAQ;IAEtC,OAAgB,MAAM,0BAAU;IAKhC,OAAO,CAAC,MAAM,CAAwE;IAQ7E,OAAO,EAAE,IAAI,EAAE,CAAC;IAEb,MAAM,EAAE,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC,IAAI,EAAE,MAAM,KAAK,OAAO,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,GAAG,SAAS,CAAC;IAC7E,SAAS,EAAE,CAAC,CAAC,KAAK,EAAE,GAAG,KAAK,MAAM,CAAC,GAAG,SAAS,CAAC;IAChD,QAAQ,EAAE,CAAC,CAAC,KAAK,EAAE,GAAG,KAAK,MAAM,CAAC,GAAG,SAAS,CAAC;IAEN,QAAQ,SAAO;IACf,KAAK,SAAM;IACX,SAAS,SAAK;IACe,WAAW,UAAS;IAE1F,QAAQ,UAAS;IAEjB,IAAI,IAAI,IAAI,eAAe,GAAG,IAAI,CAEjC;IAED,IAAI,KAAK,IAAI,GAAG,CAEf;IACD,IAAI,KAAK,CAAC,KAAK,EAAE,GAAG,EAOnB;IAEQ,KAAK;IAId,KAAK;IASL,IACI,QAAQ,IAAI,MAAM,GAAG,SAAS,CAEjC;IACD,IAAI,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,SAAS,EAMrC;IAEgB,IAAI,EAAG,MAAM,CAAC;;IAOtB,iBAAiB;IAQjB,oBAAoB;cA0FV,MAAM,IAAI,kBAAkB;IA0F/C,OAAO,CAAC,cAAc;IAItB,OAAO,CAAC,cAAc;CAOvB;AAED,OAAO,CAAC,MAAM,CAAC;IACb,UAAU,qBAAqB;QAC7B,aAAa,EAAE,WAAW,CAAC;KAC5B;CACF"}
|
package/typeahead/typeahead.js
CHANGED
|
@@ -17,6 +17,7 @@ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (
|
|
|
17
17
|
};
|
|
18
18
|
var _UmTypeahead_instances, _UmTypeahead_targetId, _UmTypeahead_connected, _UmTypeahead_documentMutationObserver, _UmTypeahead_termNormalized, _UmTypeahead_debounceTimeout, _UmTypeahead_value, _UmTypeahead_elementInternals, _UmTypeahead_attach, _UmTypeahead_detach, _UmTypeahead_handleFocus, _UmTypeahead_handleInput, _UmTypeahead_getItemClickHandler, _UmTypeahead_setValueAndDispatchEvents, _UmTypeahead_updateResults, _UmTypeahead_getData, _UmTypeahead_setValueOnTarget;
|
|
19
19
|
import { html, LitElement } from 'lit';
|
|
20
|
+
import { unsafeHTML } from 'lit-html/directives/unsafe-html.js';
|
|
20
21
|
import { customElement, property, query, state } from 'lit/decorators.js';
|
|
21
22
|
import { styles } from './typeahead.styles.js';
|
|
22
23
|
import { normalizeText } from '../shared/normalize-text.js';
|
|
@@ -35,6 +36,16 @@ let UmTypeahead = class UmTypeahead extends LitElement {
|
|
|
35
36
|
__classPrivateFieldGet(this, _UmTypeahead_instances, "m", _UmTypeahead_setValueOnTarget).call(this);
|
|
36
37
|
}
|
|
37
38
|
}
|
|
39
|
+
focus() {
|
|
40
|
+
this.target?.focus();
|
|
41
|
+
}
|
|
42
|
+
clear() {
|
|
43
|
+
if (!this.target) {
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
__classPrivateFieldSet(this, _UmTypeahead_termNormalized, '', "f");
|
|
47
|
+
this.setTargetValue('');
|
|
48
|
+
}
|
|
38
49
|
get targetId() {
|
|
39
50
|
return __classPrivateFieldGet(this, _UmTypeahead_targetId, "f");
|
|
40
51
|
}
|
|
@@ -66,6 +77,7 @@ let UmTypeahead = class UmTypeahead extends LitElement {
|
|
|
66
77
|
}
|
|
67
78
|
});
|
|
68
79
|
_UmTypeahead_handleInput.set(this, () => {
|
|
80
|
+
console.log('input');
|
|
69
81
|
if (__classPrivateFieldGet(this, _UmTypeahead_debounceTimeout, "f")) {
|
|
70
82
|
clearTimeout(__classPrivateFieldGet(this, _UmTypeahead_debounceTimeout, "f"));
|
|
71
83
|
}
|
|
@@ -92,20 +104,29 @@ let UmTypeahead = class UmTypeahead extends LitElement {
|
|
|
92
104
|
if (!this.results?.length) {
|
|
93
105
|
return html ``;
|
|
94
106
|
}
|
|
95
|
-
setTimeout(() => this.menu.open = true
|
|
107
|
+
setTimeout(() => this.menu.open = true);
|
|
96
108
|
return html `
|
|
97
109
|
<u-menu manualFocus>
|
|
98
110
|
${this.results
|
|
99
|
-
.map(result =>
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
111
|
+
.map(result => {
|
|
112
|
+
const content = this.template
|
|
113
|
+
? unsafeHTML(this.template(result.value))
|
|
114
|
+
: html `<u-highlight .term=${__classPrivateFieldGet(this, _UmTypeahead_termNormalized, "f")} .result=${result.label}></u-highlight>`;
|
|
115
|
+
return html `
|
|
116
|
+
<u-menu-item @click=${__classPrivateFieldGet(this, _UmTypeahead_instances, "m", _UmTypeahead_getItemClickHandler).call(this, result)}>${content}</u-menu-item>`;
|
|
117
|
+
})}
|
|
103
118
|
</u-menu>
|
|
104
119
|
`;
|
|
105
120
|
}
|
|
106
121
|
getTargetValue() {
|
|
107
122
|
return this.target.input?.value ?? this.target.value;
|
|
108
123
|
}
|
|
124
|
+
setTargetValue(value) {
|
|
125
|
+
const targetInput = this.target?.input ?? this.target;
|
|
126
|
+
if (targetInput) {
|
|
127
|
+
targetInput.value = value;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
109
130
|
};
|
|
110
131
|
_UmTypeahead_targetId = new WeakMap();
|
|
111
132
|
_UmTypeahead_connected = new WeakMap();
|
|
@@ -175,6 +196,7 @@ _UmTypeahead_updateResults = async function _UmTypeahead_updateResults(lazy = fa
|
|
|
175
196
|
__classPrivateFieldSet(this, _UmTypeahead_termNormalized, termNormalized, "f");
|
|
176
197
|
if (termNormalized.length < this.minLength) {
|
|
177
198
|
this.results = [];
|
|
199
|
+
console.log(this.results);
|
|
178
200
|
return;
|
|
179
201
|
}
|
|
180
202
|
const data = await __classPrivateFieldGet(this, _UmTypeahead_instances, "m", _UmTypeahead_getData).call(this);
|
|
@@ -183,6 +205,7 @@ _UmTypeahead_updateResults = async function _UmTypeahead_updateResults(lazy = fa
|
|
|
183
205
|
.toLowerCase()
|
|
184
206
|
.includes(termNormalized));
|
|
185
207
|
this.results = this.results.slice(0, this.limit || this.results.length);
|
|
208
|
+
console.log(this.results);
|
|
186
209
|
};
|
|
187
210
|
_UmTypeahead_getData = async function _UmTypeahead_getData() {
|
|
188
211
|
if (!this.source) {
|
|
@@ -227,9 +250,6 @@ __decorate([
|
|
|
227
250
|
__decorate([
|
|
228
251
|
property()
|
|
229
252
|
], UmTypeahead.prototype, "source", void 0);
|
|
230
|
-
__decorate([
|
|
231
|
-
property()
|
|
232
|
-
], UmTypeahead.prototype, "formatter", void 0);
|
|
233
253
|
__decorate([
|
|
234
254
|
property({ type: Number, reflect: true })
|
|
235
255
|
], UmTypeahead.prototype, "debounce", void 0);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"typeahead.js","sourceRoot":"","sources":["../../src/typeahead/typeahead.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAAA,OAAO,EAAE,IAAI,EAAsB,UAAU,EAAE,MAAM,KAAK,CAAC;AAC3D,OAAO,EAAE,aAAa,EAAE,QAAQ,EAAE,KAAK,EAAE,KAAK,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EAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,IAAI,EAAC,CAAC;0CAAY;AACX;IAAxC,QAAQ,CAAC,EAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,IAAI,EAAC,CAAC;8CAAe;AACe;IAArE,QAAQ,CAAC,EAAC,IAAI,EAAE,OAAO,EAAE,SAAS,EAAE,eAAe,EAAE,OAAO,EAAE,IAAI,EAAC,CAAC;gDAAqB;AAE1F;IADC,QAAQ,CAAC,EAAC,IAAI,EAAE,OAAO,EAAE,OAAO,EAAE,IAAI,EAAC,CAAC;6CACxB;AAmBjB;IADC,QAAQ,CAAC,EAAC,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,WAAW,EAAC,CAAC;2CAGjD;AASgB;IAAhB,KAAK,CAAC,QAAQ,CAAC;yCAAe;AAxDpB,WAAW;IADvB,aAAa,CAAC,aAAa,CAAC;GAChB,WAAW,CAsPvB","sourcesContent":["import { html, HTMLTemplateResult, LitElement } from 'lit';\nimport { customElement, property, query, state } from 'lit/decorators.js';\n\nimport { styles } from './typeahead.styles.js';\n\nimport { UmMenu } from '../menu/menu.js';\nimport { normalizeText } from '../shared/normalize-text.js';\n\nimport './highlight.js';\n\nexport interface Data {\n label: string;\n value: any;\n}\n\n@customElement('u-typeahead')\nexport class UmTypeahead extends LitElement {\n static readonly formAssociated = true;\n\n static override styles = styles;\n\n #targetId: string | undefined;\n\n #connected = false;\n private target: HTMLElement & {input?: HTMLInputElement; value: string} | null = null;\n #documentMutationObserver: MutationObserver | null = null;\n #termNormalized: string = '';\n #debounceTimeout: number | null = null;\n #value: any;\n readonly #elementInternals: ElementInternals;\n\n // @ts-ignore\n @state() results: Data[];\n\n @property() source: (any[] | ((term: string) => Promise<any[]>)) | undefined;\n @property() formatter: ((value: any) => string) | undefined;\n\n @property({type: Number, reflect: true}) debounce = 300;\n @property({type: Number, reflect: true}) limit = 10;\n @property({type: Number, reflect: true}) minLength = 2;\n @property({type: Boolean, attribute: 'open-on-focus', reflect: true}) openOnFocus = false;\n @property({type: Boolean, reflect: true})\n editable = false;\n\n get form(): HTMLFormElement | null {\n return this.#elementInternals.form;\n }\n\n get value(): any {\n return this.#value;\n }\n set value(value: any) {\n this.#value = value;\n this.#elementInternals.setFormValue(value);\n\n if (this.#connected) {\n this.#setValueOnTarget();\n }\n }\n\n @property({reflect: true, attribute: \"target-id\"})\n get targetId(): string | undefined {\n return this.#targetId;\n }\n set targetId(value: string | undefined) {\n this.#targetId = value;\n\n if (this.#connected) {\n this.#attach();\n }\n }\n\n @query('u-menu') menu!: UmMenu;\n\n constructor() {\n super();\n this.#elementInternals = this.attachInternals();\n }\n\n override connectedCallback() {\n super.connectedCallback();\n this.#connected = true;\n this.#attach();\n this.#documentMutationObserver = new MutationObserver(() => this.#attach())\n this.#documentMutationObserver.observe(document, {attributes: true, childList: true});\n }\n\n override disconnectedCallback() {\n super.disconnectedCallback();\n this.#connected = false;\n this.#detach();\n this.#documentMutationObserver!.disconnect();\n this.#documentMutationObserver = null;\n }\n\n #attach() {\n if (!this.targetId) {\n this.#detach();\n return;\n }\n\n const newTarget = document.getElementById(this.targetId) as HTMLInputElement;\n\n if (newTarget === this.target) {\n return;\n }\n\n this.#detach();\n\n if (!newTarget) {\n return;\n }\n\n // @ts-ignore\n this.target = newTarget;\n\n newTarget.addEventListener('input', this.#handleInput);\n newTarget.addEventListener('focus', this.#handleFocus);\n\n if (this.value) {\n this.#setValueOnTarget();\n }\n }\n\n #detach() {\n this.target?.removeEventListener('input', this.#handleInput);\n this.target?.removeEventListener('focus', this.#handleFocus);\n }\n\n #handleFocus = async () => {\n if (this.openOnFocus) {\n await this.#updateResults();\n }\n }\n\n #handleInput = () => {\n if (this.#debounceTimeout) {\n clearTimeout(this.#debounceTimeout);\n }\n\n this.#setValueAndDispatchEvents(this.editable ? this.getTargetValue() : null, true);\n\n this.#debounceTimeout = setTimeout(async () => await this.#updateResults(true), this.debounce);\n }\n\n #getItemClickHandler(data: Data) {\n return () => {\n const selectedEvent = new CustomEvent('selected', {\n cancelable: true,\n detail: data.value\n });\n\n this.dispatchEvent(selectedEvent);\n\n if (selectedEvent.defaultPrevented) {\n return;\n }\n\n this.#setValueAndDispatchEvents(data.value);\n }\n }\n\n #setValueAndDispatchEvents(value: any, direct = false) {\n\n if (!direct) {\n this.value = value;\n } else {\n this.#value = value;\n this.#elementInternals.setFormValue(value);\n }\n\n this.dispatchEvent(new InputEvent('input', {bubbles: true, composed: true}));\n this.dispatchEvent(new Event('change', {bubbles: true}));\n }\n\n protected override render(): HTMLTemplateResult {\n\n if (!this.results?.length) {\n return html``;\n }\n\n setTimeout(() => this.menu.open = true, 200);\n return html`\n <u-menu manualFocus>\n ${this.results\n .map(result => html`\n <u-menu-item @click=${this.#getItemClickHandler(result)}>\n <u-highlight .term=${this.#termNormalized} .result=${result.label}></u-highlight>\n </u-menu-item>`)}\n </u-menu>\n `;\n }\n\n async #updateResults(lazy = false) {\n\n const term = this.getTargetValue();\n\n const termNormalized = normalizeText(term).toLowerCase();\n\n if (lazy && termNormalized === this.#termNormalized) {\n return;\n }\n\n this.#termNormalized = termNormalized;\n\n if (termNormalized.length < this.minLength) {\n this.results = [];\n return;\n }\n\n const data = await this.#getData();\n this.results = data\n .filter(t => normalizeText(t.label)\n .toLowerCase()\n .includes(termNormalized));\n this.results = this.results.slice(0, this.limit || this.results.length);\n }\n\n async #getData(): Promise<Data[]> {\n if (!this.source) {\n return [];\n }\n\n let values: any[]\n\n if (this.source instanceof Array) {\n values = this.source;\n } else {\n const source = this.source as ((term: string) => Promise<any[]>);\n values = await source(this.#termNormalized);\n }\n\n return values\n .map(source => ({\n label: this.formatter\n ? this.formatter(source)\n : source.toString(),\n value: source\n }));\n }\n\n #setValueOnTarget() {\n if (!this.target) {\n return;\n }\n\n const value = this.formatter\n ? this.formatter(this.value)\n : this.value;\n\n if (this.target!.input) {\n this.target.input.value = value;\n } else {\n this.target.value = value;\n }\n\n this.#termNormalized = normalizeText(value)?.toLowerCase();\n }\n\n private getTargetValue(): string {\n return this.target!.input?.value ?? this.target!.value;\n }\n}\n\ndeclare global {\n interface HTMLElementTagNameMap {\n 'u-typeahead': UmTypeahead;\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"typeahead.js","sourceRoot":"","sources":["../../src/typeahead/typeahead.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAAA,OAAO,EAAE,IAAI,EAAsB,UAAU,EAAE,MAAM,KAAK,CAAC;AAC3D,OAAO,EAAE,UAAU,EAAE,MAAM,oCAAoC,CAAC;AAChE,OAAO,EAAE,aAAa,EAAE,QAAQ,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,mBAAmB,CAAC;AAE1E,OAAO,EAAE,MAAM,EAAE,MAAM,uBAAuB,CAAC;AAG/C,OAAO,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAC;AAE5D,OAAO,gBAAgB,CAAC;AAQjB,IAAM,WAAW,GAAjB,MAAM,WAAY,SAAQ,UAAU;IA6BzC,IAAI,IAAI;QACN,OAAO,uBAAA,IAAI,qCAAkB,CAAC,IAAI,CAAC;IACrC,CAAC;IAED,IAAI,KAAK;QACP,OAAO,uBAAA,IAAI,0BAAO,CAAC;IACrB,CAAC;IACD,IAAI,KAAK,CAAC,KAAU;QAClB,uBAAA,IAAI,sBAAU,KAAK,MAAA,CAAC;QACpB,uBAAA,IAAI,qCAAkB,CAAC,YAAY,CAAC,KAAK,CAAC,CAAC;QAE3C,IAAI,uBAAA,IAAI,8BAAW,EAAE,CAAC;YACpB,uBAAA,IAAI,6DAAkB,MAAtB,IAAI,CAAoB,CAAC;QAC3B,CAAC;IACH,CAAC;IAEQ,KAAK;QACZ,IAAI,CAAC,MAAM,EAAE,KAAK,EAAE,CAAC;IACvB,CAAC;IAED,KAAK;QACH,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE,CAAC;YACjB,OAAO;QACT,CAAC;QAED,uBAAA,IAAI,+BAAmB,EAAE,MAAA,CAAC;QAC1B,IAAI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,OAAO,EAAE,IAAI,EAAC,CAAC;0CAAY;AACX;IAAxC,QAAQ,CAAC,EAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,IAAI,EAAC,CAAC;8CAAe;AACe;IAArE,QAAQ,CAAC,EAAC,IAAI,EAAE,OAAO,EAAE,SAAS,EAAE,eAAe,EAAE,OAAO,EAAE,IAAI,EAAC,CAAC;gDAAqB;AAE1F;IADC,QAAQ,CAAC,EAAC,IAAI,EAAE,OAAO,EAAE,OAAO,EAAE,IAAI,EAAC,CAAC;6CACxB;AAgCjB;IADC,QAAQ,CAAC,EAAC,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,WAAW,EAAC,CAAC;2CAGjD;AASgB;IAAhB,KAAK,CAAC,QAAQ,CAAC;yCAAe;AAtEpB,WAAW;IADvB,aAAa,CAAC,aAAa,CAAC;GAChB,WAAW,CAoRvB","sourcesContent":["import { html, HTMLTemplateResult, LitElement } from 'lit';\nimport { unsafeHTML } from 'lit-html/directives/unsafe-html.js';\nimport { customElement, property, query, state } from 'lit/decorators.js';\n\nimport { styles } from './typeahead.styles.js';\n\nimport { UmMenu } from '../menu/menu.js';\nimport { normalizeText } from '../shared/normalize-text.js';\n\nimport './highlight.js';\n\nexport interface Data {\n label: string;\n value: any;\n}\n\n@customElement('u-typeahead')\nexport class UmTypeahead extends LitElement {\n static readonly formAssociated = true;\n\n static override styles = styles;\n\n #targetId: string | undefined;\n\n #connected = false;\n private target: HTMLElement & {input?: HTMLInputElement; value: string} | null = null;\n #documentMutationObserver: MutationObserver | null = null;\n #termNormalized: string = '';\n #debounceTimeout: number | null = null;\n #value: any;\n readonly #elementInternals: ElementInternals;\n\n // @ts-ignore\n @state() results: Data[];\n\n @property() source: (any[] | ((term: string) => Promise<any[]>)) | undefined;\n formatter: ((value: any) => string) | undefined;\n template: ((value: any) => string) | undefined;\n\n @property({type: Number, reflect: true}) debounce = 300;\n @property({type: Number, reflect: true}) limit = 10;\n @property({type: Number, reflect: true}) minLength = 2;\n @property({type: Boolean, attribute: 'open-on-focus', reflect: true}) openOnFocus = false;\n @property({type: Boolean, reflect: true})\n editable = false;\n\n get form(): HTMLFormElement | null {\n return this.#elementInternals.form;\n }\n\n get value(): any {\n return this.#value;\n }\n set value(value: any) {\n this.#value = value;\n this.#elementInternals.setFormValue(value);\n\n if (this.#connected) {\n this.#setValueOnTarget();\n }\n }\n\n override focus() {\n this.target?.focus();\n }\n\n clear() {\n if (!this.target) {\n return;\n }\n\n this.#termNormalized = '';\n this.setTargetValue('');\n }\n\n @property({reflect: true, attribute: \"target-id\"})\n get targetId(): string | undefined {\n return this.#targetId;\n }\n set targetId(value: string | undefined) {\n this.#targetId = value;\n\n if (this.#connected) {\n this.#attach();\n }\n }\n\n @query('u-menu') menu!: UmMenu;\n\n constructor() {\n super();\n this.#elementInternals = this.attachInternals();\n }\n\n override connectedCallback() {\n super.connectedCallback();\n this.#connected = true;\n this.#attach();\n this.#documentMutationObserver = new MutationObserver(() => this.#attach())\n this.#documentMutationObserver.observe(document, {attributes: true, childList: true});\n }\n\n override disconnectedCallback() {\n super.disconnectedCallback();\n this.#connected = false;\n this.#detach();\n this.#documentMutationObserver!.disconnect();\n this.#documentMutationObserver = null;\n }\n\n #attach() {\n if (!this.targetId) {\n this.#detach();\n return;\n }\n\n const newTarget = document.getElementById(this.targetId) as HTMLInputElement;\n\n if (newTarget === this.target) {\n return;\n }\n\n this.#detach();\n\n if (!newTarget) {\n return;\n }\n\n // @ts-ignore\n this.target = newTarget;\n\n newTarget.addEventListener('input', this.#handleInput);\n newTarget.addEventListener('focus', this.#handleFocus);\n\n if (this.value) {\n this.#setValueOnTarget();\n }\n }\n\n #detach() {\n this.target?.removeEventListener('input', this.#handleInput);\n this.target?.removeEventListener('focus', this.#handleFocus);\n }\n\n #handleFocus = async () => {\n if (this.openOnFocus) {\n await this.#updateResults();\n }\n }\n\n #handleInput = () => {\n console.log('input');\n\n if (this.#debounceTimeout) {\n clearTimeout(this.#debounceTimeout);\n }\n\n this.#setValueAndDispatchEvents(this.editable ? this.getTargetValue() : null, true);\n\n this.#debounceTimeout = setTimeout(async () => await this.#updateResults(true), this.debounce);\n }\n\n #getItemClickHandler(data: Data) {\n return () => {\n const selectedEvent = new CustomEvent('selected', {\n cancelable: true,\n detail: data.value\n });\n\n this.dispatchEvent(selectedEvent);\n\n if (selectedEvent.defaultPrevented) {\n return;\n }\n\n this.#setValueAndDispatchEvents(data.value);\n }\n }\n\n #setValueAndDispatchEvents(value: any, direct = false) {\n\n if (!direct) {\n this.value = value;\n } else {\n this.#value = value;\n this.#elementInternals.setFormValue(value);\n }\n\n this.dispatchEvent(new InputEvent('input', {bubbles: true, composed: true}));\n this.dispatchEvent(new Event('change', {bubbles: true}));\n }\n\n protected override render(): HTMLTemplateResult {\n\n if (!this.results?.length) {\n return html``;\n }\n\n setTimeout(() => this.menu.open = true);\n return html`\n <u-menu manualFocus>\n ${this.results\n .map(result => {\n const content = this.template\n ? unsafeHTML(this.template(result.value))\n : html`<u-highlight .term=${this.#termNormalized} .result=${result.label}></u-highlight>`;\n \n return html`\n <u-menu-item @click=${this.#getItemClickHandler(result)}>${content}</u-menu-item>`;\n })}\n </u-menu>\n `;\n }\n\n async #updateResults(lazy = false) {\n\n const term = this.getTargetValue();\n\n const termNormalized = normalizeText(term).toLowerCase();\n\n if (lazy && termNormalized === this.#termNormalized) {\n return;\n }\n\n this.#termNormalized = termNormalized;\n\n if (termNormalized.length < this.minLength) {\n this.results = [];\n console.log(this.results);\n return;\n }\n\n const data = await this.#getData();\n this.results = data\n .filter(t => normalizeText(t.label)\n .toLowerCase()\n .includes(termNormalized));\n this.results = this.results.slice(0, this.limit || this.results.length);\n console.log(this.results);\n }\n\n async #getData(): Promise<Data[]> {\n if (!this.source) {\n return [];\n }\n\n let values: any[]\n\n if (this.source instanceof Array) {\n values = this.source;\n } else {\n const source = this.source as ((term: string) => Promise<any[]>);\n values = await source(this.#termNormalized);\n }\n\n return values\n .map(source => ({\n label: this.formatter\n ? this.formatter(source)\n : source.toString(),\n value: source\n }));\n }\n\n #setValueOnTarget() {\n if (!this.target) {\n return;\n }\n\n const value = this.formatter\n ? this.formatter(this.value)\n : this.value;\n\n if (this.target!.input) {\n this.target.input.value = value;\n } else {\n this.target.value = value;\n }\n\n this.#termNormalized = normalizeText(value)?.toLowerCase();\n }\n\n private getTargetValue(): string {\n return this.target!.input?.value ?? this.target!.value;\n }\n\n private setTargetValue(value: string): void {\n const targetInput = this.target?.input ?? this.target;\n\n if (targetInput) {\n targetInput.value = value;\n }\n }\n}\n\ndeclare global {\n interface HTMLElementTagNameMap {\n 'u-typeahead': UmTypeahead;\n }\n}\n"]}
|