@kashi-ui/elements 2.22.0 → 2.23.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.
Files changed (79) hide show
  1. package/README.md +6 -1
  2. package/dist/base.js +4 -0
  3. package/dist/buttons.d.ts +82 -0
  4. package/dist/buttons.js +487 -0
  5. package/dist/cards.d.ts +30 -0
  6. package/dist/cards.js +183 -0
  7. package/dist/cdn/{appointment-wMGBoHw9.js → appointment-DOXBUwIU.js} +1 -1
  8. package/dist/cdn/{base-Dg1Mb8Jk.js → base-D4tCOysh.js} +1 -1
  9. package/dist/cdn/{basic-B5ZxgagA.js → basic-DuY4RslZ.js} +1 -1
  10. package/dist/cdn/buttons-zhSdiaXr.js +1 -0
  11. package/dist/cdn/calendar-BulPeVAJ.js +16 -0
  12. package/dist/cdn/card-BKiwJF34.js +1 -0
  13. package/dist/cdn/cards-DKarXHsC.js +1 -0
  14. package/dist/cdn/charts-ETYOAJyB.js +1 -0
  15. package/dist/cdn/chat-D5ZIMnNp.js +4 -0
  16. package/dist/cdn/choice-DBvC2Puo.js +1 -0
  17. package/dist/cdn/{combobox-CBT7yp94.js → combobox-BGuPAAt3.js} +1 -1
  18. package/dist/cdn/{commerce-CsyixnVK.js → commerce-SzWdpWJG.js} +1 -1
  19. package/dist/cdn/content-BgfBfy8j.js +1 -0
  20. package/dist/cdn/controls--OMZkJYe.js +1 -0
  21. package/dist/cdn/display-BfYWAjNu.js +1 -0
  22. package/dist/cdn/{fields-Cx84-qz1.js → fields-B-jTUQiI.js} +1 -1
  23. package/dist/cdn/forms-DgRiYmdV.js +1 -0
  24. package/dist/cdn/{holidays-DO82gXhM.js → holidays-Cu6IfbTL.js} +1 -1
  25. package/dist/cdn/hours-CeiVqPKD.js +1 -0
  26. package/dist/cdn/inputs-BkJ70Xkc.js +1 -0
  27. package/dist/cdn/kashi-elements.js +1 -1
  28. package/dist/cdn/legal-id-Mt8FX0fV.js +1 -0
  29. package/dist/cdn/{map-DlpA916P.js → map-TfSt3CjL.js} +2 -2
  30. package/dist/cdn/media-BRBINvzU.js +1 -0
  31. package/dist/cdn/{menu-CqTvk8Ok.js → menu-DTNVFXrb.js} +1 -1
  32. package/dist/cdn/money-CjGI2VIQ.js +1 -0
  33. package/dist/cdn/{nav-BHv7H1Fy.js → nav-CT5RVOzy.js} +1 -1
  34. package/dist/cdn/{overlay-BIEEJ4Sh.js → overlay-DC5-I6hM.js} +1 -1
  35. package/dist/cdn/parts-5fVaWtRe.js +1 -0
  36. package/dist/cdn/pickers-BwDRsDRo.js +1 -0
  37. package/dist/cdn/relative-OC1fV3Xh.js +1 -0
  38. package/dist/cdn/rich-text-CS6xEim6.js +1 -0
  39. package/dist/cdn/sheets-eVMx4d-5.js +1 -0
  40. package/dist/cdn/{table-B5XwDDS9.js → table-tgB4iwcM.js} +1 -1
  41. package/dist/cdn/{text-Bml-pVjo.js → text-C6fzEBBh.js} +1 -1
  42. package/dist/cdn/{time-DrXgFeQy.js → time-CkqqUKJv.js} +1 -1
  43. package/dist/cdn/{time-BQsTSvql.js → time-CwOYrtK-.js} +1 -1
  44. package/dist/cdn/validate-Dr2f6i6s.js +1 -0
  45. package/dist/cdn/work-DoNAKgVU.js +1 -0
  46. package/dist/cdn.js +11 -0
  47. package/dist/chat.d.ts +127 -0
  48. package/dist/chat.js +734 -0
  49. package/dist/choice.d.ts +98 -0
  50. package/dist/choice.js +597 -0
  51. package/dist/content.d.ts +164 -0
  52. package/dist/content.js +726 -0
  53. package/dist/hours.d.ts +29 -0
  54. package/dist/hours.js +179 -0
  55. package/dist/index.d.ts +64 -2
  56. package/dist/index.js +22 -1
  57. package/dist/manifest.json +843 -1
  58. package/dist/media.d.ts +51 -0
  59. package/dist/media.js +363 -0
  60. package/dist/parts.d.ts +31 -0
  61. package/dist/parts.js +114 -0
  62. package/dist/qr.d.ts +7 -0
  63. package/dist/qr.js +271 -0
  64. package/dist/rich-text.d.ts +40 -0
  65. package/dist/rich-text.js +375 -0
  66. package/dist/sheets.d.ts +128 -0
  67. package/dist/sheets.js +661 -0
  68. package/dist/validate.d.ts +40 -0
  69. package/dist/validate.js +224 -0
  70. package/dist/work.d.ts +117 -0
  71. package/dist/work.js +895 -0
  72. package/package.json +4 -4
  73. package/dist/cdn/calendar-Cjp5Rqer.js +0 -16
  74. package/dist/cdn/charts-DvQ4OwdS.js +0 -1
  75. package/dist/cdn/controls-B-s4IF_4.js +0 -1
  76. package/dist/cdn/display-MorJMDEF.js +0 -1
  77. package/dist/cdn/forms-CQeP9iiW.js +0 -1
  78. package/dist/cdn/inputs-Bqcs9Xy7.js +0 -1
  79. package/dist/cdn/pickers-BT9VPT4o.js +0 -1
package/dist/choice.js ADDED
@@ -0,0 +1,597 @@
1
+ // چک‌باکس، سوییچ، گروه رادیو و فهرست انتخاب.
2
+ // کنترل‌های بومی داخل عنصرند، پس name، required و ارسال فرم مثل HTML معمولی کار می‌کنند.
3
+ import { normalizePersian } from "@kashi-ui/persian";
4
+ import { afterParse, bool, define, emit, esc, fieldHTML, HTMLBase, msg, setMessages, uid } from "./base.js";
5
+ import { cx } from "./parts.js";
6
+ /** ویژگی‌هایی که روی input داخلی می‌روند */
7
+ const PASS = ["name", "value", "form", "aria-label", "aria-describedby"];
8
+ /** پایهٔ چک‌باکس و سوییچ */
9
+ class CheckBase extends HTMLBase {
10
+ constructor() {
11
+ super(...arguments);
12
+ this.input = null;
13
+ this.wrap = null;
14
+ this.initial = null;
15
+ this.key = uid("kashi-check");
16
+ }
17
+ connectedCallback() {
18
+ if (!this.input)
19
+ this.build();
20
+ this.update();
21
+ }
22
+ attributeChangedCallback(n, old, next) {
23
+ if (!this.input || old === next)
24
+ return;
25
+ if (n === "checked")
26
+ this.input.checked = next !== null && next !== "false";
27
+ else
28
+ this.update();
29
+ }
30
+ get checked() {
31
+ return this.input ? this.input.checked : (this.initial ?? bool(this, "checked"));
32
+ }
33
+ set checked(v) {
34
+ this.initial = !!v;
35
+ if (this.input)
36
+ this.input.checked = !!v;
37
+ }
38
+ focus(options) {
39
+ this.input?.focus(options);
40
+ }
41
+ build() {
42
+ const input = document.createElement("input");
43
+ input.type = "checkbox";
44
+ input.id = this.key;
45
+ if (this.kind === "switch")
46
+ input.setAttribute("role", "switch");
47
+ input.checked = this.initial ?? bool(this, "checked");
48
+ input.addEventListener("click", (e) => {
49
+ if (bool(this, "readonly"))
50
+ e.preventDefault();
51
+ });
52
+ input.addEventListener("change", () => {
53
+ this.removeAttribute("data-indeterminate");
54
+ emit(this, "kashi-change", { checked: input.checked, value: input.value });
55
+ });
56
+ this.input = input;
57
+ this.extra();
58
+ }
59
+ /** چیزهای خاص هر نوع پس از ساخت */
60
+ extra() { }
61
+ update() {
62
+ const input = this.input;
63
+ const label = this.getAttribute("label");
64
+ const desc = this.getAttribute("description");
65
+ const size = this.getAttribute("size") ?? "md";
66
+ input.className = this.kind === "switch"
67
+ ? cx("k-switch", size !== "md" && `k-switch--${size}`)
68
+ : cx("k-checkbox", size === "lg" && "k-checkbox--lg");
69
+ input.disabled = bool(this, "disabled");
70
+ input.required = bool(this, "required");
71
+ for (const a of PASS) {
72
+ const v = this.getAttribute(a);
73
+ if (v === null)
74
+ input.removeAttribute(a);
75
+ else
76
+ input.setAttribute(a, v);
77
+ }
78
+ if (bool(this, "invalid"))
79
+ input.setAttribute("aria-invalid", "true");
80
+ else
81
+ input.removeAttribute("aria-invalid");
82
+ if (bool(this, "readonly"))
83
+ input.setAttribute("aria-readonly", "true");
84
+ else
85
+ input.removeAttribute("aria-readonly");
86
+ if (desc)
87
+ input.setAttribute("aria-describedby", `${this.key}-desc`);
88
+ if (!label && !desc) {
89
+ this.wrap?.remove();
90
+ this.wrap = null;
91
+ if (input.parentElement !== this)
92
+ this.prepend(input);
93
+ return;
94
+ }
95
+ if (!this.wrap) {
96
+ this.wrap = document.createElement("label");
97
+ this.wrap.htmlFor = this.key;
98
+ this.wrap.innerHTML = '<span class="k-check__text"><span class="k-check__label"></span><span class="k-check__description"></span></span>';
99
+ this.wrap.prepend(input);
100
+ this.prepend(this.wrap);
101
+ }
102
+ const w = this.wrap;
103
+ w.className = cx("k-check", this.getAttribute("label-position") === "start" && "k-check--start");
104
+ w.toggleAttribute("data-readonly", bool(this, "readonly"));
105
+ const l = w.querySelector(".k-check__label");
106
+ l.textContent = label ?? "";
107
+ l.hidden = !label;
108
+ const d = w.querySelector(".k-check__description");
109
+ d.id = `${this.key}-desc`;
110
+ d.textContent = desc ?? "";
111
+ d.hidden = !desc;
112
+ }
113
+ }
114
+ CheckBase.observedAttributes = ["label", "description", "checked", "disabled", "required", "readonly", "invalid", "size", "label-position", ...PASS];
115
+ /** چک‌باکس با label و description؛ indeterminate برای «انتخاب همه»، readonly دیده و ارسال می‌شود ولی عوض نمی‌شود */
116
+ export class KashiCheckbox extends CheckBase {
117
+ constructor() {
118
+ super(...arguments);
119
+ this.kind = "checkbox";
120
+ }
121
+ attributeChangedCallback(n, old, next) {
122
+ super.attributeChangedCallback(n, old, next);
123
+ if (n === "indeterminate" && this.input)
124
+ this.input.indeterminate = next !== null && next !== "false";
125
+ }
126
+ extra() {
127
+ this.input.indeterminate = bool(this, "indeterminate");
128
+ }
129
+ get indeterminate() {
130
+ return this.input?.indeterminate ?? bool(this, "indeterminate");
131
+ }
132
+ set indeterminate(v) {
133
+ this.toggleAttribute("indeterminate", !!v);
134
+ }
135
+ }
136
+ KashiCheckbox.observedAttributes = [...CheckBase.observedAttributes, "indeterminate"];
137
+ /** سوییچ روشن و خاموش (role="switch")؛ size برای sm و lg */
138
+ export class KashiSwitch extends CheckBase {
139
+ constructor() {
140
+ super(...arguments);
141
+ this.kind = "switch";
142
+ }
143
+ }
144
+ // ---------- گروه رادیو ----------
145
+ /**
146
+ * گروه رادیو با fieldset و legend؛ گزینه‌ها از فرزندان <option> (data-description، data-badge، data-meta).
147
+ * variant: list، inline یا cards. value، name، hint، error، required، disabled و readonly.
148
+ */
149
+ export class KashiRadioGroup extends HTMLBase {
150
+ constructor() {
151
+ super(...arguments);
152
+ this.box = null;
153
+ this.current = null;
154
+ this.key = uid("kashi-radio");
155
+ }
156
+ connectedCallback() {
157
+ afterParse(() => {
158
+ if (!this.isConnected)
159
+ return;
160
+ if (!this.box)
161
+ this.build();
162
+ this.update();
163
+ });
164
+ }
165
+ attributeChangedCallback(n, old, next) {
166
+ if (!this.box || old === next)
167
+ return;
168
+ if (n === "value") {
169
+ this.current = next;
170
+ this.paint();
171
+ }
172
+ else
173
+ this.update();
174
+ }
175
+ get value() {
176
+ return this.current ?? "";
177
+ }
178
+ set value(v) {
179
+ this.current = v;
180
+ if (this.box)
181
+ this.paint();
182
+ }
183
+ focus(options) {
184
+ (this.box?.querySelector("input:checked") ?? this.box?.querySelector("input:not(:disabled)"))?.focus(options);
185
+ }
186
+ build() {
187
+ const opts = [...this.querySelectorAll(":scope > option")];
188
+ if (this.current === null)
189
+ this.current = this.getAttribute("value") ?? opts.find((o) => o.hasAttribute("selected"))?.value ?? null;
190
+ const box = document.createElement("fieldset");
191
+ // نقش radiogroup تا aria-required و aria-readonly مجاز باشند؛ نام از همان legend
192
+ box.setAttribute("role", "radiogroup");
193
+ box.setAttribute("aria-labelledby", `${this.key}-legend`);
194
+ box.innerHTML = `<legend class="k-label" id="${this.key}-legend"></legend>`;
195
+ opts.forEach((o, i) => {
196
+ const id = `${this.key}-${i}`;
197
+ const desc = o.dataset.description, badge = o.dataset.badge, meta = o.dataset.meta;
198
+ const label = esc(o.textContent?.trim() ?? o.value);
199
+ box.insertAdjacentHTML("beforeend", `<label class="k-check" for="${id}"><input type="radio" class="k-radio" id="${id}" value="${esc(o.value)}"${o.disabled ? " disabled data-off" : ""}${desc ? ` aria-describedby="${id}-desc"` : ""}><span class="k-check__text">${badge ? `<span class="k-check__head"><span class="k-check__label">${label}</span><span class="k-badge k-badge--brand k-badge--sm">${esc(badge)}</span></span>` : `<span class="k-check__label">${label}</span>`}${desc ? `<span class="k-check__description" id="${id}-desc">${esc(desc)}</span>` : ""}</span>${meta ? `<span class="k-check__meta">${esc(meta)}</span>` : ""}</label>`);
200
+ });
201
+ for (const o of opts)
202
+ o.remove();
203
+ box.addEventListener("click", (e) => {
204
+ if (bool(this, "readonly") && e.target.closest("input"))
205
+ e.preventDefault();
206
+ });
207
+ box.addEventListener("change", (e) => {
208
+ const input = e.target;
209
+ if (bool(this, "readonly"))
210
+ return this.paint();
211
+ this.current = input.value;
212
+ emit(this, "kashi-change", { value: input.value });
213
+ });
214
+ this.prepend(box);
215
+ this.box = box;
216
+ }
217
+ update() {
218
+ const box = this.box;
219
+ const variant = this.getAttribute("variant") ?? "list";
220
+ const error = this.getAttribute("error"), hint = this.getAttribute("hint");
221
+ const ro = bool(this, "readonly");
222
+ box.className = cx("k-radio-group", variant !== "list" && `k-radio-group--${variant}`);
223
+ box.disabled = bool(this, "disabled");
224
+ box.toggleAttribute("data-readonly", ro);
225
+ for (const [k, on] of [["aria-readonly", ro], ["aria-required", bool(this, "required")], ["aria-invalid", !!error]]) {
226
+ if (on)
227
+ box.setAttribute(k, "true");
228
+ else
229
+ box.removeAttribute(k);
230
+ }
231
+ const legend = box.querySelector("legend");
232
+ legend.innerHTML = `${esc(this.getAttribute("label") ?? "")}${bool(this, "required") ? '<span class="k-label__required" aria-hidden="true">*</span>' : ""}`;
233
+ box.querySelectorAll(":scope > .k-hint").forEach((n) => n.remove());
234
+ if (error)
235
+ box.insertAdjacentHTML("beforeend", `<p class="k-hint k-hint--error" id="${this.key}-error">${esc(error)}</p>`);
236
+ if (hint)
237
+ box.insertAdjacentHTML("beforeend", `<p class="k-hint" id="${this.key}-hint">${esc(hint)}</p>`);
238
+ const by = [error && `${this.key}-error`, hint && `${this.key}-hint`].filter(Boolean).join(" ");
239
+ if (by)
240
+ box.setAttribute("aria-describedby", by);
241
+ else
242
+ box.removeAttribute("aria-describedby");
243
+ const name = this.getAttribute("name") ?? this.key;
244
+ const inputs = [...box.querySelectorAll("input")];
245
+ inputs.forEach((input, i) => {
246
+ input.name = name;
247
+ // required روی اولی کافی است؛ مرورگر کل گروه را بررسی می‌کند
248
+ input.required = bool(this, "required") && i === 0;
249
+ if (ro)
250
+ input.setAttribute("aria-readonly", "true");
251
+ else
252
+ input.removeAttribute("aria-readonly");
253
+ if (error)
254
+ input.setAttribute("aria-invalid", "true");
255
+ else
256
+ input.removeAttribute("aria-invalid");
257
+ });
258
+ this.paint();
259
+ }
260
+ paint() {
261
+ for (const input of this.box.querySelectorAll("input"))
262
+ input.checked = input.value === this.current;
263
+ }
264
+ }
265
+ KashiRadioGroup.observedAttributes = ["label", "name", "value", "variant", "hint", "error", "required", "disabled", "readonly"];
266
+ const CHEVRON = '<svg class="k-select__chevron" viewBox="0 0 20 20" width="18" height="18" fill="none" stroke="currentColor" stroke-width="1.7" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="m6 8 4 4 4-4"/></svg>';
267
+ const CHECK = '<svg class="k-option__check" viewBox="0 0 20 20" width="18" height="18" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="m4.5 10.5 3.5 3.5 7.5-8"/></svg>';
268
+ const norm = (s) => normalizePersian(s, { digits: "latin" }).replace(/‌/g, " ").toLowerCase();
269
+ /**
270
+ * انتخاب از فهرست با ظاهر کاشی: فلش‌ها، Home/End، Enter، Escape و تایپ حرف اول؛ گزینه‌ها از <option> و <optgroup> یا element.options.
271
+ * یک <select> بومی پنهان زیرش است تا name، required و ارسال فرم کار کنند. با native انتخاب‌گر خود سیستم.
272
+ */
273
+ export class KashiSelect extends HTMLBase {
274
+ constructor() {
275
+ super(...arguments);
276
+ this.list = null;
277
+ this.current = null;
278
+ this.rendered = false;
279
+ this.key = uid("kashi-select");
280
+ this.isOpen = false;
281
+ this.active = -1;
282
+ this.typed = { text: "", at: 0 };
283
+ this.trigger = null;
284
+ this.listbox = null;
285
+ /** فهرست در لایهٔ بالای صفحه و با position: fixed کنار فیلد؛ اگر پایین جا نیست، بالای فیلد */
286
+ this.place = () => {
287
+ const t = this.trigger, lb = this.listbox;
288
+ if (!t || !lb)
289
+ return;
290
+ const r = t.getBoundingClientRect();
291
+ const need = Math.min(288, lb.scrollHeight + 8);
292
+ const below = innerHeight - r.bottom;
293
+ const up = below < need && r.top > below;
294
+ lb.classList.toggle("k-select__list--up", up);
295
+ Object.assign(lb.style, { position: "fixed", insetBlock: "auto", right: "auto", left: `${r.left}px`, width: `${r.width}px`, top: up ? "auto" : `${r.bottom + 6}px`, bottom: up ? `${innerHeight - r.top + 6}px` : "auto" });
296
+ };
297
+ this.onOutside = (e) => {
298
+ const t = e.target;
299
+ if (!this.listbox?.contains(t) && !this.trigger?.contains(t))
300
+ this.close(false);
301
+ };
302
+ }
303
+ connectedCallback() {
304
+ afterParse(() => {
305
+ if (!this.isConnected)
306
+ return;
307
+ if (!this.rendered) {
308
+ if (this.list === null)
309
+ this.list = this.readOptions();
310
+ if (this.current === null)
311
+ this.current = this.getAttribute("value") ?? (this.hasAttribute("placeholder") ? "" : this.enabled()[0]?.value ?? "");
312
+ }
313
+ this.render();
314
+ });
315
+ }
316
+ disconnectedCallback() {
317
+ this.close(false);
318
+ }
319
+ attributeChangedCallback(n, old, next) {
320
+ if (!this.rendered || old === next)
321
+ return;
322
+ if (n === "value")
323
+ this.value = next ?? "";
324
+ else if (n === "error" || n === "hint" || n === "placeholder")
325
+ this.sync();
326
+ else
327
+ this.render();
328
+ }
329
+ get options() {
330
+ return this.list ?? [];
331
+ }
332
+ set options(v) {
333
+ this.list = [...(v ?? [])];
334
+ if (this.rendered)
335
+ this.render();
336
+ }
337
+ get value() {
338
+ return this.current ?? "";
339
+ }
340
+ set value(v) {
341
+ this.current = String(v ?? "");
342
+ if (this.rendered)
343
+ this.sync();
344
+ }
345
+ focus(options) {
346
+ (this.trigger ?? this.nativeEl)?.focus(options);
347
+ }
348
+ readOptions() {
349
+ const out = [];
350
+ for (const el of this.querySelectorAll(":scope > option, :scope > optgroup > option")) {
351
+ if (el.value === "" && !el.textContent?.trim())
352
+ continue;
353
+ const group = el.parentElement?.tagName === "OPTGROUP" ? el.parentElement.getAttribute("label") ?? undefined : undefined;
354
+ out.push({ value: el.value, label: el.textContent?.trim() ?? el.value, disabled: el.disabled, group });
355
+ }
356
+ for (const el of [...this.querySelectorAll(":scope > option, :scope > optgroup")])
357
+ el.remove();
358
+ return out;
359
+ }
360
+ enabled() {
361
+ return this.options.filter((o) => !o.disabled);
362
+ }
363
+ render() {
364
+ this.close(false);
365
+ const id = this.key;
366
+ const label = this.getAttribute("label");
367
+ const ariaLabel = this.getAttribute("aria-label");
368
+ const size = this.getAttribute("size") ?? "md";
369
+ const required = bool(this, "required"), disabled = bool(this, "disabled"), readonly = bool(this, "readonly");
370
+ const native = bool(this, "native");
371
+ const placeholder = this.getAttribute("placeholder");
372
+ const opts = this.options;
373
+ let last;
374
+ const optionTags = opts.map((o) => {
375
+ const head = o.group !== last ? `${last !== undefined ? "</optgroup>" : ""}${o.group !== undefined ? `<optgroup label="${esc(o.group)}">` : ""}` : "";
376
+ last = o.group;
377
+ return `${head}<option value="${esc(o.value)}"${o.disabled ? " disabled" : ""}>${esc(o.label)}</option>`;
378
+ }).join("") + (last !== undefined ? "</optgroup>" : "");
379
+ const nameAttr = this.getAttribute("name") ? ` name="${esc(this.getAttribute("name"))}"` : "";
380
+ let control;
381
+ if (native) {
382
+ control = `<div class="${cx("k-select", size !== "md" && `k-select--${size}`)}"><select id="${id}" class="k-select__control"${nameAttr}${required ? " required" : ""}${disabled ? " disabled" : ""}${!label && ariaLabel ? ` aria-label="${esc(ariaLabel)}"` : ""}>${placeholder !== null ? `<option value="" disabled>${esc(placeholder)}</option>` : ""}${optionTags}</select></div>`;
383
+ }
384
+ else {
385
+ const listId = `${id}-list`;
386
+ control = `<div class="${cx("k-select", "k-select--custom", size !== "md" && `k-select--${size}`)}"${readonly ? " data-readonly" : ""}${bool(this, "loading") ? ' aria-busy="true"' : ""}><button id="${id}" type="button" role="combobox" class="k-select__control" aria-haspopup="listbox" aria-expanded="false" aria-controls="${listId}"${required ? ' aria-required="true"' : ""}${readonly ? ' aria-readonly="true"' : ""}${disabled ? " disabled" : ""}${!label && ariaLabel ? ` aria-label="${esc(ariaLabel)}"` : ""}><span class="k-select__value"></span>${CHEVRON}</button><ul id="${listId}" role="listbox" aria-labelledby="${id}" class="k-listbox k-select__list" hidden tabindex="-1" popover="manual"></ul><select class="k-select__native" tabindex="-1" aria-hidden="true"${nameAttr}${required ? " required" : ""}${disabled ? " disabled" : ""}><option value=""></option>${opts.map((o) => `<option value="${esc(o.value)}"${o.disabled ? " disabled" : ""}>${esc(o.label)}</option>`).join("")}</select></div>`;
387
+ }
388
+ this.querySelector(":scope > .k-field")?.remove();
389
+ this.insertAdjacentHTML("afterbegin", fieldHTML({ id, label, required, control }));
390
+ this.nativeEl = this.querySelector(native ? "select.k-select__control" : "select.k-select__native");
391
+ this.trigger = native ? null : this.querySelector("button.k-select__control");
392
+ this.listbox = native ? null : this.querySelector(".k-select__list");
393
+ this.nativeEl.addEventListener("change", () => {
394
+ this.current = this.nativeEl.value;
395
+ this.sync();
396
+ emit(this, "kashi-change", { value: this.current });
397
+ });
398
+ if (this.trigger) {
399
+ const t = this.trigger;
400
+ this.nativeEl.addEventListener("focus", () => t.focus());
401
+ this.nativeEl.addEventListener("invalid", () => t.focus());
402
+ t.addEventListener("click", () => (this.isOpen ? this.close() : this.open()));
403
+ t.addEventListener("keydown", (e) => this.onKey(e));
404
+ t.addEventListener("blur", (e) => {
405
+ if (!this.listbox?.contains(e.relatedTarget))
406
+ this.close(false);
407
+ });
408
+ const lb = this.listbox;
409
+ lb.addEventListener("mousedown", (e) => e.preventDefault());
410
+ lb.addEventListener("click", (e) => {
411
+ const li = e.target.closest('[role="option"]');
412
+ if (!li || li.getAttribute("aria-disabled") === "true")
413
+ return;
414
+ this.choose(this.options[Number(li.dataset.index)].value);
415
+ this.close();
416
+ });
417
+ lb.addEventListener("pointermove", (e) => {
418
+ const li = e.target.closest('[role="option"]');
419
+ if (li && li.getAttribute("aria-disabled") !== "true")
420
+ this.setActive(Number(li.dataset.index));
421
+ });
422
+ }
423
+ this.rendered = true;
424
+ this.sync();
425
+ }
426
+ sync() {
427
+ const opts = this.options;
428
+ const sel = opts.find((o) => o.value === this.current);
429
+ this.nativeEl.value = sel ? sel.value : "";
430
+ if (this.trigger) {
431
+ const v = this.trigger.querySelector(".k-select__value");
432
+ v.textContent = sel ? sel.label : (this.getAttribute("placeholder") ?? " ");
433
+ v.className = cx("k-select__value", !sel && "k-select__value--placeholder");
434
+ }
435
+ const control = (this.trigger ?? this.nativeEl);
436
+ setMessages(this.querySelector(":scope > .k-field"), control, this.key, this.getAttribute("error"), this.getAttribute("hint"));
437
+ if (this.isOpen)
438
+ this.renderList();
439
+ }
440
+ choose(v) {
441
+ if (v === this.current)
442
+ return;
443
+ this.current = v;
444
+ this.sync();
445
+ emit(this, "kashi-change", { value: v });
446
+ }
447
+ renderList() {
448
+ const lb = this.listbox;
449
+ const opts = this.options;
450
+ const listId = `${this.key}-list`;
451
+ let last;
452
+ const rows = opts.map((o, i) => {
453
+ const head = o.group && o.group !== last ? `<li role="presentation" class="k-listbox__group">${esc(o.group)}</li>` : "";
454
+ last = o.group;
455
+ const on = o.value === this.current;
456
+ return `${head}<li id="${listId}-${i}" data-index="${i}" role="option" class="k-option" aria-selected="${on}"${o.disabled ? ' aria-disabled="true"' : ""}><span class="k-option__label">${esc(o.label)}</span>${on ? CHECK : ""}</li>`;
457
+ });
458
+ if (!opts.length)
459
+ rows.push(bool(this, "loading") ? '<li class="k-listbox__loading" role="presentation"></li>'.repeat(3) : `<li class="k-listbox__empty" role="presentation">${esc(this.getAttribute("empty-message") ?? msg().common.noOptions)}</li>`);
460
+ lb.innerHTML = rows.join("");
461
+ this.setActive(this.active);
462
+ }
463
+ setActive(i) {
464
+ this.active = i;
465
+ const lb = this.listbox;
466
+ lb.querySelector("[data-active]")?.removeAttribute("data-active");
467
+ const li = i >= 0 ? lb.querySelector(`[data-index="${i}"]`) : null;
468
+ if (li) {
469
+ li.setAttribute("data-active", "");
470
+ li.scrollIntoView?.({ block: "nearest" });
471
+ if (this.isOpen)
472
+ this.trigger.setAttribute("aria-activedescendant", li.id);
473
+ }
474
+ else
475
+ this.trigger.removeAttribute("aria-activedescendant");
476
+ }
477
+ open(at) {
478
+ if (bool(this, "disabled") || bool(this, "readonly") || !this.trigger)
479
+ return;
480
+ this.isOpen = true;
481
+ const lb = this.listbox;
482
+ lb.hidden = false;
483
+ this.trigger.setAttribute("aria-expanded", "true");
484
+ this.querySelector(".k-select").setAttribute("data-open", "");
485
+ const sel = this.options.findIndex((o) => o.value === this.current);
486
+ this.active = at ?? (sel >= 0 ? sel : this.step(-1, 1));
487
+ this.renderList();
488
+ try {
489
+ const p = lb;
490
+ if (p.showPopover && !lb.matches(":popover-open"))
491
+ p.showPopover();
492
+ }
493
+ catch { /* بدون Popover API */ }
494
+ this.place();
495
+ addEventListener("resize", this.place);
496
+ addEventListener("scroll", this.place, true);
497
+ document.addEventListener("pointerdown", this.onOutside);
498
+ }
499
+ close(focus = true) {
500
+ if (!this.isOpen || !this.listbox)
501
+ return;
502
+ this.isOpen = false;
503
+ const lb = this.listbox;
504
+ try {
505
+ const p = lb;
506
+ if (p.hidePopover && lb.matches(":popover-open"))
507
+ p.hidePopover();
508
+ }
509
+ catch { /* */ }
510
+ lb.hidden = true;
511
+ this.trigger.setAttribute("aria-expanded", "false");
512
+ this.trigger.removeAttribute("aria-activedescendant");
513
+ this.querySelector(".k-select")?.removeAttribute("data-open");
514
+ removeEventListener("resize", this.place);
515
+ removeEventListener("scroll", this.place, true);
516
+ document.removeEventListener("pointerdown", this.onOutside);
517
+ if (focus)
518
+ this.trigger.focus();
519
+ }
520
+ step(from, dir) {
521
+ const opts = this.options;
522
+ for (let i = from + dir; i >= 0 && i < opts.length; i += dir)
523
+ if (!opts[i].disabled)
524
+ return i;
525
+ return from;
526
+ }
527
+ typeahead(key) {
528
+ const opts = this.options;
529
+ if (!opts.length)
530
+ return;
531
+ const now = Date.now();
532
+ this.typed.text = now - this.typed.at > 700 ? key : this.typed.text + key;
533
+ this.typed.at = now;
534
+ const q = norm(this.typed.text);
535
+ const start = this.isOpen ? this.active : opts.findIndex((o) => o.value === this.current);
536
+ const order = [...opts.keys()].map((k) => (k + start + 1 + opts.length) % opts.length);
537
+ const hit = order.find((i) => !opts[i].disabled && norm(opts[i].label).startsWith(q));
538
+ if (hit === undefined)
539
+ return;
540
+ if (this.isOpen)
541
+ this.setActive(hit);
542
+ else
543
+ this.choose(opts[hit].value);
544
+ }
545
+ onKey(e) {
546
+ if (bool(this, "disabled") || bool(this, "readonly"))
547
+ return;
548
+ const k = e.key;
549
+ const opts = this.options;
550
+ const typing = k.length === 1 && !e.ctrlKey && !e.metaKey && !e.altKey;
551
+ if (!this.isOpen) {
552
+ if (["ArrowDown", "ArrowUp", "Enter", " "].includes(k)) {
553
+ e.preventDefault();
554
+ this.open();
555
+ }
556
+ else if (k === "Home" || k === "End") {
557
+ e.preventDefault();
558
+ this.open(k === "Home" ? this.step(-1, 1) : this.step(opts.length, -1));
559
+ }
560
+ else if (typing)
561
+ this.typeahead(k);
562
+ return;
563
+ }
564
+ if (k === "ArrowDown" || k === "ArrowUp") {
565
+ e.preventDefault();
566
+ this.setActive(this.step(this.active, k === "ArrowDown" ? 1 : -1));
567
+ }
568
+ else if (k === "Home" || k === "PageUp") {
569
+ e.preventDefault();
570
+ this.setActive(this.step(-1, 1));
571
+ }
572
+ else if (k === "End" || k === "PageDown") {
573
+ e.preventDefault();
574
+ this.setActive(this.step(opts.length, -1));
575
+ }
576
+ else if (k === "Enter" || k === " ") {
577
+ e.preventDefault();
578
+ const o = opts[this.active];
579
+ if (o && !o.disabled)
580
+ this.choose(o.value);
581
+ this.close();
582
+ }
583
+ else if (k === "Escape") {
584
+ e.preventDefault();
585
+ this.close();
586
+ }
587
+ else if (k === "Tab")
588
+ this.close(false);
589
+ else if (typing)
590
+ this.typeahead(k);
591
+ }
592
+ }
593
+ KashiSelect.observedAttributes = ["label", "hint", "error", "placeholder", "size", "required", "disabled", "readonly", "loading", "empty-message", "name", "value", "native", "aria-label"];
594
+ define("kashi-checkbox", KashiCheckbox);
595
+ define("kashi-switch", KashiSwitch);
596
+ define("kashi-radio-group", KashiRadioGroup, "block");
597
+ define("kashi-select", KashiSelect, "block");