tyrell-components 1.0.0-RC14 → 1.0.0-RC16

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 (287) hide show
  1. package/README.md +53 -53
  2. package/css/tyrell-theme.css +32 -4
  3. package/css/tyrell.css +10 -15
  4. package/dist/tyrell-colors-static.css +1 -316
  5. package/dist/tyrell-theme.css +1 -1443
  6. package/dist/tyrell.css +1 -1390
  7. package/dist/tyrell.js +1 -1
  8. package/lib/base/ty-component.d.ts +14 -0
  9. package/lib/base/ty-component.d.ts.map +1 -0
  10. package/lib/base/ty-component.js +223 -196
  11. package/lib/base/ty-component.js.map +1 -0
  12. package/lib/components/button.d.ts +1 -1
  13. package/lib/components/button.d.ts.map +1 -0
  14. package/lib/components/button.js +267 -259
  15. package/lib/components/button.js.map +1 -0
  16. package/lib/components/calendar-month.d.ts +2 -2
  17. package/lib/components/calendar-month.d.ts.map +1 -0
  18. package/lib/components/calendar-month.js +440 -416
  19. package/lib/components/calendar-month.js.map +1 -0
  20. package/lib/components/calendar-navigation.d.ts +11 -0
  21. package/lib/components/calendar-navigation.d.ts.map +1 -0
  22. package/lib/components/calendar-navigation.js +272 -261
  23. package/lib/components/calendar-navigation.js.map +1 -0
  24. package/lib/components/calendar.d.ts +31 -3
  25. package/lib/components/calendar.d.ts.map +1 -0
  26. package/lib/components/calendar.js +785 -603
  27. package/lib/components/calendar.js.map +1 -0
  28. package/lib/components/checkbox.d.ts +1 -1
  29. package/lib/components/checkbox.d.ts.map +1 -0
  30. package/lib/components/checkbox.js +273 -272
  31. package/lib/components/checkbox.js.map +1 -0
  32. package/lib/components/copy.d.ts +1 -1
  33. package/lib/components/copy.d.ts.map +1 -0
  34. package/lib/components/copy.js +287 -278
  35. package/lib/components/copy.js.map +1 -0
  36. package/lib/components/date-picker.d.ts +16 -10
  37. package/lib/components/date-picker.d.ts.map +1 -0
  38. package/lib/components/date-picker.js +1068 -1122
  39. package/lib/components/date-picker.js.map +1 -0
  40. package/lib/components/dropdown.d.ts +403 -0
  41. package/lib/components/dropdown.d.ts.map +1 -0
  42. package/lib/components/dropdown.js +1315 -0
  43. package/lib/components/dropdown.js.map +1 -0
  44. package/lib/components/file-upload.d.ts +4 -0
  45. package/lib/components/file-upload.js +418 -404
  46. package/lib/components/icon.d.ts.map +1 -0
  47. package/lib/components/icon.js +141 -141
  48. package/lib/components/icon.js.map +1 -0
  49. package/lib/components/input.d.ts +1 -1
  50. package/lib/components/input.d.ts.map +1 -0
  51. package/lib/components/input.js +593 -596
  52. package/lib/components/input.js.map +1 -0
  53. package/lib/components/modal.d.ts +2 -8
  54. package/lib/components/modal.d.ts.map +1 -0
  55. package/lib/components/modal.js +309 -319
  56. package/lib/components/modal.js.map +1 -0
  57. package/lib/components/multiselect.d.ts +389 -0
  58. package/lib/components/multiselect.d.ts.map +1 -0
  59. package/lib/components/multiselect.js +1301 -0
  60. package/lib/components/multiselect.js.map +1 -0
  61. package/lib/components/option.d.ts.map +1 -0
  62. package/lib/components/option.js +167 -167
  63. package/lib/components/option.js.map +1 -0
  64. package/lib/components/popup.d.ts.map +1 -0
  65. package/lib/components/popup.js +236 -233
  66. package/lib/components/popup.js.map +1 -0
  67. package/lib/components/radio.d.ts +17 -2
  68. package/lib/components/radio.js +437 -406
  69. package/lib/components/resize-observer.d.ts.map +1 -0
  70. package/lib/components/resize-observer.js +67 -67
  71. package/lib/components/resize-observer.js.map +1 -0
  72. package/lib/components/scroll-container.d.ts +3 -2
  73. package/lib/components/scroll-container.d.ts.map +1 -0
  74. package/lib/components/scroll-container.js +289 -294
  75. package/lib/components/scroll-container.js.map +1 -0
  76. package/lib/components/select.d.ts +85 -18
  77. package/lib/components/select.js +1647 -1529
  78. package/lib/components/selected-tags.d.ts +1 -1
  79. package/lib/components/selected-tags.js +188 -182
  80. package/lib/components/step.d.ts.map +1 -0
  81. package/lib/components/step.js +62 -62
  82. package/lib/components/step.js.map +1 -0
  83. package/lib/components/switch.d.ts +1 -1
  84. package/lib/components/switch.js +214 -213
  85. package/lib/components/tab.d.ts +49 -1
  86. package/lib/components/tab.d.ts.map +1 -0
  87. package/lib/components/tab.js +203 -62
  88. package/lib/components/tab.js.map +1 -0
  89. package/lib/components/tabs.d.ts +15 -2
  90. package/lib/components/tabs.d.ts.map +1 -0
  91. package/lib/components/tabs.js +576 -496
  92. package/lib/components/tabs.js.map +1 -0
  93. package/lib/components/tag.d.ts +5 -8
  94. package/lib/components/tag.d.ts.map +1 -0
  95. package/lib/components/tag.js +235 -235
  96. package/lib/components/tag.js.map +1 -0
  97. package/lib/components/textarea.d.ts +5 -4
  98. package/lib/components/textarea.d.ts.map +1 -0
  99. package/lib/components/textarea.js +458 -456
  100. package/lib/components/textarea.js.map +1 -0
  101. package/lib/components/tooltip.d.ts +4 -3
  102. package/lib/components/tooltip.d.ts.map +1 -0
  103. package/lib/components/tooltip.js +293 -283
  104. package/lib/components/tooltip.js.map +1 -0
  105. package/lib/components/wizard.d.ts +5 -4
  106. package/lib/components/wizard.d.ts.map +1 -0
  107. package/lib/components/wizard.js +359 -395
  108. package/lib/components/wizard.js.map +1 -0
  109. package/lib/icons/fontawesome/brands.d.ts.map +1 -0
  110. package/lib/icons/fontawesome/brands.js.map +1 -0
  111. package/lib/icons/fontawesome/regular.d.ts.map +1 -0
  112. package/lib/icons/fontawesome/regular.js.map +1 -0
  113. package/lib/icons/fontawesome/solid.d.ts.map +1 -0
  114. package/lib/icons/fontawesome/solid.js.map +1 -0
  115. package/lib/icons/heroicons/micro.d.ts.map +1 -0
  116. package/lib/icons/heroicons/micro.js +392 -392
  117. package/lib/icons/heroicons/micro.js.map +1 -0
  118. package/lib/icons/heroicons/mini.d.ts.map +1 -0
  119. package/lib/icons/heroicons/mini.js +382 -382
  120. package/lib/icons/heroicons/mini.js.map +1 -0
  121. package/lib/icons/heroicons/outline.d.ts.map +1 -0
  122. package/lib/icons/heroicons/outline.js +337 -337
  123. package/lib/icons/heroicons/outline.js.map +1 -0
  124. package/lib/icons/heroicons/solid.d.ts.map +1 -0
  125. package/lib/icons/heroicons/solid.js +407 -407
  126. package/lib/icons/heroicons/solid.js.map +1 -0
  127. package/lib/icons/lucide.d.ts.map +1 -0
  128. package/lib/icons/lucide.js +22843 -22843
  129. package/lib/icons/lucide.js.map +1 -0
  130. package/lib/icons/material/filled.d.ts.map +1 -0
  131. package/lib/icons/material/filled.js +9653 -9653
  132. package/lib/icons/material/filled.js.map +1 -0
  133. package/lib/icons/material/outlined.d.ts.map +1 -0
  134. package/lib/icons/material/outlined.js +10271 -10271
  135. package/lib/icons/material/outlined.js.map +1 -0
  136. package/lib/icons/material/round.d.ts.map +1 -0
  137. package/lib/icons/material/round.js +10495 -10495
  138. package/lib/icons/material/round.js.map +1 -0
  139. package/lib/icons/material/sharp.d.ts.map +1 -0
  140. package/lib/icons/material/sharp.js +9905 -9905
  141. package/lib/icons/material/sharp.js.map +1 -0
  142. package/lib/icons/material/two-tone.d.ts.map +1 -0
  143. package/lib/icons/material/two-tone.js +12792 -12792
  144. package/lib/icons/material/two-tone.js.map +1 -0
  145. package/lib/index.d.ts +1 -1
  146. package/lib/index.d.ts.map +1 -0
  147. package/lib/index.js +21 -21
  148. package/lib/index.js.map +1 -0
  149. package/lib/styles/button.d.ts.map +1 -0
  150. package/lib/styles/button.js +200 -302
  151. package/lib/styles/button.js.map +1 -0
  152. package/lib/styles/calendar-month.d.ts +3 -0
  153. package/lib/styles/calendar-month.d.ts.map +1 -0
  154. package/lib/styles/calendar-month.js +141 -185
  155. package/lib/styles/calendar-month.js.map +1 -0
  156. package/lib/styles/calendar-navigation.d.ts +1 -1
  157. package/lib/styles/calendar-navigation.d.ts.map +1 -0
  158. package/lib/styles/calendar-navigation.js +100 -86
  159. package/lib/styles/calendar-navigation.js.map +1 -0
  160. package/lib/styles/calendar.d.ts +3 -2
  161. package/lib/styles/calendar.d.ts.map +1 -0
  162. package/lib/styles/calendar.js +83 -9
  163. package/lib/styles/calendar.js.map +1 -0
  164. package/lib/styles/checkbox.js +62 -77
  165. package/lib/styles/copy.d.ts.map +1 -0
  166. package/lib/styles/copy.js +71 -91
  167. package/lib/styles/copy.js.map +1 -0
  168. package/lib/styles/custom-scrollbar.js +80 -100
  169. package/lib/styles/date-picker.d.ts.map +1 -0
  170. package/lib/styles/date-picker.js +295 -335
  171. package/lib/styles/date-picker.js.map +1 -0
  172. package/lib/styles/dropdown.d.ts +12 -0
  173. package/lib/styles/dropdown.d.ts.map +1 -0
  174. package/lib/styles/dropdown.js +725 -0
  175. package/lib/styles/dropdown.js.map +1 -0
  176. package/lib/styles/field-size.d.ts +6 -0
  177. package/lib/styles/field-size.js +91 -0
  178. package/lib/styles/file-upload.js +139 -169
  179. package/lib/styles/icon.d.ts.map +1 -0
  180. package/lib/styles/icon.js +92 -156
  181. package/lib/styles/icon.js.map +1 -0
  182. package/lib/styles/input.d.ts.map +1 -0
  183. package/lib/styles/input.js +125 -241
  184. package/lib/styles/input.js.map +1 -0
  185. package/lib/styles/modal.d.ts.map +1 -0
  186. package/lib/styles/modal.js +67 -102
  187. package/lib/styles/modal.js.map +1 -0
  188. package/lib/styles/multiselect.d.ts +6 -0
  189. package/lib/styles/multiselect.d.ts.map +1 -0
  190. package/lib/styles/multiselect.js +568 -0
  191. package/lib/styles/multiselect.js.map +1 -0
  192. package/lib/styles/option.d.ts +1 -1
  193. package/lib/styles/option.d.ts.map +1 -0
  194. package/lib/styles/option.js +47 -67
  195. package/lib/styles/option.js.map +1 -0
  196. package/lib/styles/popup.d.ts +1 -1
  197. package/lib/styles/popup.d.ts.map +1 -0
  198. package/lib/styles/popup.js +37 -65
  199. package/lib/styles/popup.js.map +1 -0
  200. package/lib/styles/radio.js +75 -96
  201. package/lib/styles/resize-observer.d.ts.map +1 -0
  202. package/lib/styles/resize-observer.js +2 -5
  203. package/lib/styles/resize-observer.js.map +1 -0
  204. package/lib/styles/scroll-container.d.ts +1 -1
  205. package/lib/styles/scroll-container.d.ts.map +1 -0
  206. package/lib/styles/scroll-container.js +101 -131
  207. package/lib/styles/scroll-container.js.map +1 -0
  208. package/lib/styles/select-base.js +596 -773
  209. package/lib/styles/select.d.ts +1 -1
  210. package/lib/styles/select.js +146 -228
  211. package/lib/styles/step.d.ts +1 -1
  212. package/lib/styles/step.d.ts.map +1 -0
  213. package/lib/styles/step.js +15 -30
  214. package/lib/styles/step.js.map +1 -0
  215. package/lib/styles/switch.js +37 -55
  216. package/lib/styles/tab.d.ts +1 -1
  217. package/lib/styles/tab.d.ts.map +1 -0
  218. package/lib/styles/tab.js +86 -29
  219. package/lib/styles/tab.js.map +1 -0
  220. package/lib/styles/tabs.d.ts +1 -1
  221. package/lib/styles/tabs.d.ts.map +1 -0
  222. package/lib/styles/tabs.js +190 -180
  223. package/lib/styles/tabs.js.map +1 -0
  224. package/lib/styles/tag.d.ts.map +1 -0
  225. package/lib/styles/tag.js +137 -191
  226. package/lib/styles/tag.js.map +1 -0
  227. package/lib/styles/textarea.d.ts +1 -1
  228. package/lib/styles/textarea.d.ts.map +1 -0
  229. package/lib/styles/textarea.js +205 -271
  230. package/lib/styles/textarea.js.map +1 -0
  231. package/lib/styles/tooltip.d.ts.map +1 -0
  232. package/lib/styles/tooltip.js +3 -3
  233. package/lib/styles/tooltip.js.map +1 -0
  234. package/lib/styles/wizard.d.ts +9 -5
  235. package/lib/styles/wizard.d.ts.map +1 -0
  236. package/lib/styles/wizard.js +301 -245
  237. package/lib/styles/wizard.js.map +1 -0
  238. package/lib/types/common.d.ts +4 -2
  239. package/lib/types/common.d.ts.map +1 -0
  240. package/lib/types/common.js +6 -5
  241. package/lib/types/common.js.map +1 -0
  242. package/lib/utils/calendar-utils.d.ts.map +1 -0
  243. package/lib/utils/calendar-utils.js +100 -100
  244. package/lib/utils/calendar-utils.js.map +1 -0
  245. package/lib/utils/custom-scrollbar.js +272 -272
  246. package/lib/utils/flavor-sheet.js +14 -14
  247. package/lib/utils/icon-registry.d.ts.map +1 -0
  248. package/lib/utils/icon-registry.js +127 -127
  249. package/lib/utils/icon-registry.js.map +1 -0
  250. package/lib/utils/loader-registry.js +3 -3
  251. package/lib/utils/locale.d.ts.map +1 -0
  252. package/lib/utils/locale.js +34 -34
  253. package/lib/utils/locale.js.map +1 -0
  254. package/lib/utils/mobile.js +1 -1
  255. package/lib/utils/number-format.d.ts.map +1 -0
  256. package/lib/utils/number-format.js +54 -54
  257. package/lib/utils/number-format.js.map +1 -0
  258. package/lib/utils/parse-boolean.d.ts +39 -0
  259. package/lib/utils/parse-boolean.d.ts.map +1 -0
  260. package/lib/utils/parse-boolean.js +23 -0
  261. package/lib/utils/parse-boolean.js.map +1 -0
  262. package/lib/utils/positioning.d.ts.map +1 -0
  263. package/lib/utils/positioning.js +212 -212
  264. package/lib/utils/positioning.js.map +1 -0
  265. package/lib/utils/property-capture.d.ts.map +1 -0
  266. package/lib/utils/property-capture.js +36 -36
  267. package/lib/utils/property-capture.js.map +1 -0
  268. package/lib/utils/property-manager.d.ts +1 -1
  269. package/lib/utils/property-manager.d.ts.map +1 -0
  270. package/lib/utils/property-manager.js +127 -125
  271. package/lib/utils/property-manager.js.map +1 -0
  272. package/lib/utils/resize-observer.d.ts.map +1 -0
  273. package/lib/utils/resize-observer.js +23 -23
  274. package/lib/utils/resize-observer.js.map +1 -0
  275. package/lib/utils/scroll-lock.d.ts.map +1 -0
  276. package/lib/utils/scroll-lock.js +57 -57
  277. package/lib/utils/scroll-lock.js.map +1 -0
  278. package/lib/utils/styles.d.ts.map +1 -0
  279. package/lib/utils/styles.js +17 -17
  280. package/lib/utils/styles.js.map +1 -0
  281. package/lib/utils/validity.d.ts +26 -0
  282. package/lib/utils/validity.js +53 -0
  283. package/lib/version.d.ts +1 -1
  284. package/lib/version.d.ts.map +1 -0
  285. package/lib/version.js +1 -1
  286. package/lib/version.js.map +1 -0
  287. package/package.json +28 -7
@@ -1,3 +1,4 @@
1
+ import { DEFAULT_SIZE } from "../types/common.js";
1
2
  import { ensureStyles } from "../utils/styles.js";
2
3
  import { syncCustomFlavorSheet } from "../utils/flavor-sheet.js";
3
4
  import { selectBaseStyles, selectCustomFlavorCss } from "../styles/select-base.js";
@@ -11,1548 +12,1665 @@ import { CustomScrollbar, isCustomScrollbarEnabled, } from "../utils/custom-scro
11
12
  let elementIdCounter = 0;
12
13
  const elementIds = new WeakMap();
13
14
  function getElementHash(element) {
14
- let id = elementIds.get(element);
15
- if (id === undefined) {
16
- id = ++elementIdCounter;
17
- elementIds.set(element, id);
18
- }
19
- return id;
15
+ let id = elementIds.get(element);
16
+ if (id === undefined) {
17
+ id = ++elementIdCounter;
18
+ elementIds.set(element, id);
19
+ }
20
+ return id;
20
21
  }
21
22
  import { REQUIRED_ICON_SVG } from "../utils/icons.js";
23
+ const SEARCH_AUTO_THRESHOLD = 7;
22
24
  const CHEVRON_DOWN_SVG = `<svg viewBox="0 0 20 20" fill="currentColor">
23
- <path fill-rule="evenodd" d="M5.293 7.293a1 1 0 011.414 0L10 10.586l3.293-3.293a1 1 0 111.414 1.414l-4 4a1 1 0 01-1.414 0l-4-4a1 1 0 010-1.414z" clip-rule="evenodd" />
25
+ <path fill-rule="evenodd" d="M5.293 7.293a1 1 0 011.414 0L10 10.586l3.293-3.293a1 1 0 111.414 1.414l-4 4a1 1 0 01-1.414 0l-4-4a1 1 0 010-1.414z" clip-rule="evenodd" />
24
26
  </svg>`;
25
- const SEARCH_AUTO_THRESHOLD = 7;
26
27
  function slugify(text) {
27
- return text
28
- .trim()
29
- .toLowerCase()
30
- .replace(/\s+/g, "_")
31
- .replace(/[^a-z0-9_]/g, "")
32
- .replace(/_+/g, "_")
33
- .replace(/^_|_$/g, "");
28
+ return text
29
+ .trim()
30
+ .toLowerCase()
31
+ .replace(/\s+/g, "_")
32
+ .replace(/[^a-z0-9_]/g, "")
33
+ .replace(/_+/g, "_")
34
+ .replace(/^_|_$/g, "");
34
35
  }
35
36
  const SEARCH_ICON_SVG = `<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
36
- <circle cx="11" cy="11" r="8"/><path d="m21 21-4.3-4.3"/>
37
+ <circle cx="11" cy="11" r="8"/><path d="m21 21-4.3-4.3"/>
37
38
  </svg>`;
38
39
  const CLEAR_ICON_SVG = `<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
39
- <line x1="18" y1="6" x2="6" y2="18"></line>
40
- <line x1="6" y1="6" x2="18" y2="18"></line>
40
+ <line x1="18" y1="6" x2="6" y2="18"></line>
41
+ <line x1="6" y1="6" x2="18" y2="18"></line>
41
42
  </svg>`;
42
43
  export class TySelect extends TyComponent {
43
- constructor() {
44
- super();
45
- this._name = "";
46
- this._multiple = false;
47
- this._compact = false;
48
- this._placeholder = "Select...";
49
- this._label = "";
50
- this._disabled = false;
51
- this._readonly = false;
52
- this._required = false;
53
- this._clearable = true;
54
- this._externalSearch = false;
55
- this._allowCreate = false;
56
- this._createTransform = "none";
57
- this._searchable = "auto";
58
- this._loading = false;
59
- this._scrollLockId = null;
60
- this._size = "md";
61
- this._availableLabel = "Available";
62
- this._noOptionsMessage = "No options available";
63
- this._state = {
64
- open: false,
65
- search: "",
66
- highlightedIndex: -1,
67
- filteredTags: [],
68
- selectedValues: [],
69
- mode: "desktop",
70
- };
71
- this._stubClickHandler = null;
72
- this._tagClickHandler = null;
73
- this._searchInputHandler = null;
74
- this._blockSearchClick = null;
75
- this._keyboardHandler = null;
76
- this._debounce = 0;
77
- this._searchDebounceTimer = null;
78
- this._optionsScrollbar = null;
79
- this._childObserver = null;
80
- this._selectedClone = null;
81
- this._customFlavorSheet = null;
82
- const shadow = this.shadowRoot;
83
- ensureStyles(shadow, { css: selectBaseStyles, id: "ty-select-base" });
84
- ensureStyles(shadow, { css: selectStyles, id: "ty-select" });
85
- }
86
- onConnect() {
87
- const shadow = this.shadowRoot;
88
- const dialogs = shadow.querySelectorAll("dialog");
89
- dialogs.forEach((dialog) => {
90
- if (dialog.open) {
91
- console.warn("⚠️ Found open dialog on connect, closing it");
92
- dialog.close();
93
- }
94
- });
95
- this.render();
96
- requestAnimationFrame(() => {
97
- this.initializeState();
98
- });
99
- this._childObserver = new MutationObserver(() => {
100
- this.ensureOptionRoles();
101
- if (this._state.selectedValues.length > 0) {
102
- this.syncSelectedTags(this._state.selectedValues);
103
- }
104
- this.updateSearchVisibility();
105
- this.updateSelectionDisplay();
106
- this.updateMobileSelectedState();
107
- });
108
- this._childObserver.observe(this, { childList: true });
109
- this.ensureOptionRoles();
110
- this._syncCustomFlavor();
111
- }
112
- _syncCustomFlavor() {
113
- this._customFlavorSheet = syncCustomFlavorSheet(this.shadowRoot, this._customFlavorSheet, this.getProperty("flavor"), ({ base }) => selectCustomFlavorCss(base));
114
- }
115
- onDisconnect() {
116
- const shadow = this.shadowRoot;
117
- const dialogs = shadow.querySelectorAll("dialog");
118
- dialogs.forEach((dialog) => {
119
- if (dialog.open) {
120
- dialog.close();
121
- }
122
- });
123
- if (this._keyboardHandler) {
124
- document.removeEventListener("keydown", this._keyboardHandler);
125
- this._keyboardHandler = null;
126
- }
127
- if (this._searchDebounceTimer !== null) {
128
- clearTimeout(this._searchDebounceTimer);
129
- this._searchDebounceTimer = null;
130
- }
131
- this._destroyOptionsScrollbar();
132
- if (this._childObserver) {
133
- this._childObserver.disconnect();
134
- this._childObserver = null;
135
- }
136
- }
137
- onPropertiesChanged(changes) {
138
- for (const { name, newValue } of changes) {
139
- switch (name) {
140
- case "value":
141
- const selectedValues = this.parseValue(newValue);
142
- this._state.selectedValues = selectedValues;
143
- if (this.isConnected && this.shadowRoot) {
144
- this.syncSelectedTags(selectedValues);
145
- this.updateSelectionDisplay();
146
- this.updateMobileSelectedState();
147
- }
148
- break;
149
- case "name":
150
- this._name = newValue || "";
151
- break;
152
- case "flavor":
153
- this._syncCustomFlavor();
154
- break;
155
- case "multiple":
156
- this._multiple = newValue;
157
- if (!newValue && this._state.selectedValues.length > 1) {
158
- this.updateComponentValue([this._state.selectedValues[0]], false, "set", null);
159
- }
160
- break;
161
- case "compact":
162
- this._compact = newValue;
163
- break;
164
- case "placeholder":
165
- this._placeholder = newValue || "Select...";
166
- if (this.isConnected && this.shadowRoot)
167
- this.updateSelectionDisplay();
168
- break;
169
- case "label":
170
- this._label = newValue || "";
171
- break;
172
- case "disabled":
173
- this._disabled = newValue;
174
- break;
175
- case "readonly":
176
- this._readonly = newValue;
177
- break;
178
- case "required":
179
- this._required = newValue;
180
- break;
181
- case "clearable":
182
- this._clearable = newValue;
183
- if (this.isConnected && this.shadowRoot)
184
- this.updateSelectionDisplay();
185
- break;
186
- case "externalSearch":
187
- this._externalSearch = newValue;
188
- break;
189
- case "searchable":
190
- this._searchable = newValue;
191
- if (this.isConnected && this.shadowRoot)
192
- this.updateSearchVisibility();
193
- break;
194
- case "allowCreate":
195
- this._allowCreate = newValue;
196
- if (this.isConnected && this.shadowRoot)
197
- this.updateSearchVisibility();
198
- break;
199
- case "createTransform":
200
- this._createTransform = newValue;
201
- break;
202
- case "size":
203
- this._size = newValue;
204
- break;
205
- case "debounce":
206
- this._debounce = newValue;
207
- break;
208
- case "available-label":
209
- this._availableLabel = newValue || "Available";
210
- break;
211
- case "no-options-message":
212
- this._noOptionsMessage = newValue || "No options available";
213
- break;
214
- case "loading":
215
- this._loading = newValue;
216
- this.applyLoadingState();
217
- break;
218
- }
219
- }
220
- }
221
- applyLoadingState() {
222
- const shadow = this.shadowRoot;
223
- if (!shadow)
224
- return;
225
- const svg = this._loading ? getLoaderSvg() : null;
226
- shadow.querySelectorAll(".dropdown-options-wrapper").forEach((wrapper) => {
227
- wrapper.classList.toggle("loading", this._loading);
228
- if (this._loading) {
229
- wrapper.setAttribute("aria-busy", "true");
230
- const spinner = wrapper.querySelector(".dropdown-loading-spinner");
231
- if (spinner && svg)
232
- spinner.innerHTML = svg;
233
- }
234
- else {
235
- wrapper.removeAttribute("aria-busy");
236
- }
237
- });
238
- }
239
- getFormValue() {
240
- const selectedValues = this._state.selectedValues;
241
- if (this._name && selectedValues.length > 0) {
242
- if (!this._multiple)
243
- return selectedValues[0];
244
- const formData = new FormData();
245
- selectedValues.forEach((value) => {
246
- formData.append(this._name, value);
247
- });
248
- return formData;
249
- }
250
- return null;
251
- }
252
- parseValue(value) {
253
- if (!value || typeof value !== "string" || value.trim() === "")
254
- return [];
255
- return value
256
- .split(",")
257
- .map((v) => v.trim())
258
- .filter((v) => v !== "");
259
- }
260
- initializeState() {
261
- const initialValue = this.getProperty("value") || "";
262
- if (initialValue) {
263
- const selectedValues = this.parseValue(initialValue);
264
- this._state.selectedValues = selectedValues;
265
- this.syncSelectedTags(selectedValues);
266
- this.updateSelectionDisplay();
267
- this.updateMobileSelectedState();
268
- }
269
- else {
270
- const allTags = this.getTagElements();
271
- const preSelectedTags = allTags
272
- .filter((tag) => tag.hasAttribute("selected"))
273
- .map((tag) => this.getTagData(tag).value);
274
- if (preSelectedTags.length > 0) {
275
- this.updateComponentValue(preSelectedTags, false);
276
- }
277
- }
278
- }
279
- getTagElements() {
280
- return Array.from(this.querySelectorAll("ty-option:not([cloned]), ty-tag:not([cloned])"));
281
- }
282
- getTagData(element) {
283
- const value = element.value ||
284
- element.getAttribute("value") ||
285
- element.textContent ||
286
- "";
287
- const text = element.getAttribute("label") || element.textContent || "";
288
- return { value, text, element };
289
- }
290
- selectTag(tag) {
291
- tag.setAttribute("selected", "");
292
- }
293
- deselectTag(tag) {
294
- tag.removeAttribute("selected");
295
- }
296
- getSelectedValues() {
297
- return this.getTagElements()
298
- .filter((tag) => tag.hasAttribute("selected"))
299
- .map((tag) => this.getTagData(tag).value)
300
- .filter((value) => value !== "");
301
- }
302
- syncSelectedTags(selectedValues) {
303
- const selectedSet = new Set(selectedValues);
304
- const tags = this.getTagElements();
305
- tags.forEach((tag) => {
306
- const tagValue = this.getTagData(tag).value;
307
- const shouldBeSelected = selectedSet.has(tagValue);
308
- const isSelected = tag.hasAttribute("selected");
309
- if (shouldBeSelected && !isSelected) {
310
- this.selectTag(tag);
311
- }
312
- else if (!shouldBeSelected && isSelected) {
313
- this.deselectTag(tag);
314
- }
315
- tag.setAttribute("aria-selected", String(shouldBeSelected));
316
- });
317
- }
318
- ensureOptionRoles() {
319
- this.getTagElements().forEach((tag) => {
320
- if (!tag.hasAttribute("role"))
321
- tag.setAttribute("role", "option");
322
- if (!tag.hasAttribute("aria-selected"))
323
- tag.setAttribute("aria-selected", "false");
324
- });
325
- }
326
- updateComponentValue(newValues, dispatchChange = false, action = "set", item = null) {
327
- const oldValues = this.getSelectedValues();
328
- const valueStr = newValues.join(",");
329
- if (JSON.stringify(newValues.sort()) !== JSON.stringify(oldValues.sort())) {
330
- this.setProperty("value", valueStr);
331
- this.updateValidity();
332
- if (dispatchChange) {
333
- this.dispatchChangeEvent({
334
- value: this._multiple ? [...newValues] : (newValues[0] ?? null),
335
- values: newValues,
336
- items: this.getSelectedItems(newValues),
337
- action,
338
- item,
339
- });
340
- }
341
- }
342
- }
343
- getSelectedItems(values) {
344
- const byValue = new Map(this.getTagElements().map((el) => [this.getTagData(el).value, el]));
345
- return values.map((value) => {
346
- const el = byValue.get(value);
347
- return {
348
- value,
349
- label: el?.getAttribute("label") || el?.textContent?.trim() || value,
350
- flavor: el?.getAttribute("flavor") ?? null,
351
- };
352
- });
353
- }
354
- calculatePosition() {
355
- const shadow = this.shadowRoot;
356
- const stub = shadow.querySelector(".select-stub");
357
- const dialog = shadow.querySelector(".dropdown-dialog");
358
- if (!stub || !dialog)
359
- return;
360
- const stubRect = stub.getBoundingClientRect();
361
- const MAX_POPUP_ESTIMATE = 440;
362
- const dialogRect = dialog.getBoundingClientRect();
363
- const estimatedHeight = Math.min(dialogRect.height || 200, MAX_POPUP_ESTIMATE);
364
- const wrapPadding = 20;
365
- const cssWidth = parseFloat(getComputedStyle(this).getPropertyValue("--ty-select-popup-width"));
366
- const popupWidth = !isNaN(cssWidth)
367
- ? cssWidth
368
- : Math.max(stubRect.width, 320);
369
- const requested = this.getProperty("placement");
370
- const anchored = requested
371
- ? placementToAnchored(requested)
372
- : {
373
- side: "auto",
374
- align: this.getProperty("align"),
375
- };
376
- const pos = computeAnchoredPosition({
377
- anchorRect: stubRect,
378
- popupWidth,
379
- popupHeight: estimatedHeight,
380
- align: anchored.align,
381
- side: anchored.side,
382
- gap: 8,
383
- });
384
- const positionBelow = pos.below;
385
- const x = pos.x - wrapPadding;
386
- const y = (positionBelow ? pos.topY : pos.bottomY) - wrapPadding;
387
- const width = popupWidth + wrapPadding + wrapPadding;
388
- this.style.setProperty("--dropdown-x", `${x}px`);
389
- this.style.setProperty("--dropdown-y", `${y}px`);
390
- this.style.setProperty("--dropdown-width", `${width}px`);
391
- this.style.setProperty("--dropdown-offset-x", "0px");
392
- this.style.setProperty("--dropdown-offset-y", "0px");
393
- this.style.setProperty("--dropdown-padding", `${wrapPadding}px`);
394
- if (positionBelow) {
395
- dialog.classList.add("position-below");
396
- dialog.classList.remove("position-above");
397
- }
398
- else {
399
- dialog.classList.add("position-above");
400
- dialog.classList.remove("position-below");
401
- }
402
- this.style.setProperty("--dropdown-direction", positionBelow ? "below" : "above");
403
- }
404
- _setupOptionsScrollbar() {
405
- if (!isCustomScrollbarEnabled())
406
- return;
407
- const shadow = this.shadowRoot;
408
- const optionsDiv = shadow.querySelector(".dropdown-options");
409
- const optionsWrapper = shadow.querySelector(".dropdown-options-wrapper");
410
- if (!optionsDiv || !optionsWrapper)
411
- return;
412
- this._destroyOptionsScrollbar();
413
- optionsDiv.classList.add("ty-custom-scroll");
414
- this._optionsScrollbar = new CustomScrollbar(optionsDiv, {
415
- vertical: true,
416
- });
417
- if (this._optionsScrollbar.trackY) {
418
- optionsWrapper.appendChild(this._optionsScrollbar.trackY);
419
- }
420
- }
421
- _destroyOptionsScrollbar() {
422
- if (this._optionsScrollbar) {
423
- this._optionsScrollbar.trackY?.remove();
424
- this._optionsScrollbar.destroy();
425
- this._optionsScrollbar = null;
426
- }
427
- }
428
- openDropdown() {
429
- const shadow = this.shadowRoot;
430
- const dialog = shadow.querySelector(".dropdown-dialog");
431
- if (!dialog)
432
- return;
433
- const stubEl = shadow.querySelector(".select-stub");
434
- if (stubEl)
435
- stubEl.setAttribute("aria-expanded", "true");
436
- const lockId = `select-${this.id || "anon"}-${getElementHash(this)}`;
437
- this._scrollLockId = lockId;
438
- lockScroll(lockId);
439
- dialog.showModal();
440
- this.calculatePosition();
441
- dialog.classList.add("open");
442
- this._state.open = true;
443
- const chevron = shadow.querySelector(".dropdown-chevron");
444
- if (chevron)
445
- chevron.classList.add("open");
446
- const searchChevron = shadow.querySelector(".dropdown-search-chevron");
447
- if (searchChevron)
448
- searchChevron.classList.add("open");
449
- const tags = this.getTagElements().map((el) => this.getTagData(el));
450
- this._state.filteredTags = tags;
451
- this._state.highlightedIndex = -1;
452
- this.updateOptionsVisibility(true);
453
- this._setupOptionsScrollbar();
454
- const searchInput = shadow.querySelector(".dropdown-search-input");
455
- if (searchInput && this.shouldShowSearch()) {
456
- setTimeout(() => searchInput.focus(), 100);
457
- }
458
- this.fireOpenEvent();
459
- }
460
- closeDropdown() {
461
- const shadow = this.shadowRoot;
462
- const dialog = shadow.querySelector(".dropdown-dialog");
463
- if (!dialog)
464
- return;
465
- const stubEl = shadow.querySelector(".select-stub");
466
- if (stubEl)
467
- stubEl.setAttribute("aria-expanded", "false");
468
- this._destroyOptionsScrollbar();
469
- dialog.classList.remove("open");
470
- dialog.classList.remove("position-above");
471
- dialog.classList.remove("position-below");
472
- dialog.close();
473
- if (this._scrollLockId) {
474
- unlockScroll(this._scrollLockId);
475
- this._scrollLockId = null;
476
- }
477
- this._state.open = false;
478
- this._state.highlightedIndex = -1;
479
- const chevron = shadow.querySelector(".dropdown-chevron");
480
- if (chevron)
481
- chevron.classList.remove("open");
482
- const searchChevron = shadow.querySelector(".dropdown-search-chevron");
483
- if (searchChevron)
484
- searchChevron.classList.remove("open");
485
- const hadQuery = this._state.search !== "";
486
- this._state.search = "";
487
- const searchInput = shadow.querySelector(".dropdown-search-input");
488
- if (searchInput) {
489
- searchInput.value = "";
490
- }
491
- if (this._externalSearch) {
492
- if (hadQuery) {
493
- if (this._searchDebounceTimer !== null) {
494
- clearTimeout(this._searchDebounceTimer);
495
- this._searchDebounceTimer = null;
496
- }
497
- this.fireSearchEvent("");
498
- }
499
- }
500
- else {
501
- const allTags = this.getTagElements().map((el) => this.getTagData(el));
502
- this._state.filteredTags = allTags;
503
- this.updateTagVisibility(allTags, allTags);
504
- this.clearHighlights(allTags);
505
- }
506
- this.fireCloseEvent();
507
- }
508
- openMobileModal() {
509
- const shadow = this.shadowRoot;
510
- const dialog = shadow.querySelector(".mobile-dialog");
511
- if (!dialog)
512
- return;
513
- const stubEl = shadow.querySelector(".select-stub");
514
- if (stubEl)
515
- stubEl.setAttribute("aria-expanded", "true");
516
- const lockId = `select-${this.id || "anon"}-${getElementHash(this)}`;
517
- this._scrollLockId = lockId;
518
- lockScroll(lockId);
519
- dialog.showModal();
520
- dialog.getBoundingClientRect();
521
- dialog.classList.add("open");
522
- this._state.open = true;
523
- const tags = this.getTagElements().map((el) => this.getTagData(el));
524
- this._state.filteredTags = tags;
525
- this.updateTagVisibility(tags, tags);
526
- requestAnimationFrame(() => {
527
- this.updateMobileSelectedState();
528
- });
529
- this.fireOpenEvent();
530
- }
531
- closeMobileModal() {
532
- const shadow = this.shadowRoot;
533
- const dialog = shadow.querySelector(".mobile-dialog");
534
- if (!dialog)
535
- return;
536
- const stubEl = shadow.querySelector(".select-stub");
537
- if (stubEl)
538
- stubEl.setAttribute("aria-expanded", "false");
539
- dialog.classList.remove("open");
540
- dialog.close();
541
- if (this._scrollLockId) {
542
- unlockScroll(this._scrollLockId);
543
- this._scrollLockId = null;
544
- }
545
- this._state.open = false;
546
- this._state.highlightedIndex = -1;
547
- const hadQuery = this._state.search !== "";
548
- this._state.search = "";
549
- const searchInput = shadow.querySelector(".mobile-search-input");
550
- if (searchInput) {
551
- searchInput.value = "";
552
- }
553
- if (this._externalSearch) {
554
- if (hadQuery) {
555
- if (this._searchDebounceTimer !== null) {
556
- clearTimeout(this._searchDebounceTimer);
557
- this._searchDebounceTimer = null;
558
- }
559
- this.fireSearchEvent("");
560
- }
561
- }
562
- else {
563
- const allTags = this.getTagElements();
564
- allTags.forEach((el) => el.removeAttribute("hidden"));
565
- }
566
- this.fireCloseEvent();
567
- }
568
- handleStubClick(e) {
569
- e.preventDefault();
570
- e.stopPropagation();
571
- if (this._disabled || this._readonly) {
572
- return;
573
- }
574
- this.openDropdown();
575
- }
576
- handleClearClick(e) {
577
- e.preventDefault();
578
- e.stopPropagation();
579
- if (this._disabled || this._readonly) {
580
- return;
581
- }
582
- this.clear();
583
- }
584
- handleStubKeydown(e) {
585
- if (this._disabled || this._readonly || this._state.open)
586
- return;
587
- if (e.key === "Enter" || e.key === " " || e.key === "ArrowDown") {
588
- e.preventDefault();
589
- this.openDropdown();
590
- }
591
- }
592
- handleTagClick(e) {
593
- const target = e.target;
594
- const tag = target.closest("ty-option, ty-tag");
595
- if (!tag || tag.hasAttribute("disabled"))
596
- return;
597
- e.preventDefault();
598
- e.stopPropagation();
599
- this.selectByTag(tag);
600
- }
601
- selectByTag(tag) {
602
- const tagValue = this.getTagData(tag).value;
603
- const currentValues = this.getSelectedValues();
604
- if (!this._multiple) {
605
- this.updateComponentValue([tagValue], true, "set", tagValue);
606
- if (this._state.mode === "mobile") {
607
- this.closeMobileModal();
608
- }
609
- else {
610
- this.closeDropdown();
611
- }
612
- return;
613
- }
614
- if (tag.hasAttribute("selected")) {
615
- const newValues = currentValues.filter((v) => v !== tagValue);
616
- this.updateComponentValue(newValues, true, "remove", tagValue);
617
- }
618
- else {
619
- this.updateComponentValue([...currentValues, tagValue], true, "add", tagValue);
620
- }
621
- }
622
- tryCreateFromSearch() {
623
- if (!this._allowCreate)
624
- return false;
625
- const query = (this._state.search || "").trim();
626
- if (!query)
627
- return false;
628
- const queryLower = query.toLowerCase();
629
- const allTags = this.getTagElements().map((el) => this.getTagData(el));
630
- const existing = allTags.find((t) => t.value.toLowerCase() === queryLower ||
631
- t.text.toLowerCase() === queryLower);
632
- if (existing) {
633
- this.selectByTag(existing.element);
634
- return true;
635
- }
636
- const rawLabel = query;
637
- const defaultValue = this._createTransform === "slug" ? slugify(rawLabel) : rawLabel;
638
- if (!defaultValue)
639
- return true;
640
- const detail = { value: defaultValue, label: rawLabel };
641
- const event = new CustomEvent("create", {
642
- detail,
643
- bubbles: true,
644
- composed: true,
645
- cancelable: true,
646
- });
647
- this.dispatchEvent(event);
648
- if (event.defaultPrevented)
649
- return true;
650
- const finalValue = detail.value;
651
- const option = document.createElement("ty-option");
652
- option.setAttribute("value", finalValue);
653
- option.textContent = rawLabel;
654
- this.appendChild(option);
655
- if (!this._multiple) {
656
- this.updateComponentValue([finalValue], true, "create", finalValue);
657
- if (this._state.mode === "mobile") {
658
- this.closeMobileModal();
659
- }
660
- else {
661
- this.closeDropdown();
662
- }
663
- return true;
664
- }
665
- const currentValues = this.getSelectedValues();
666
- this.updateComponentValue([...currentValues, finalValue], true, "create", finalValue);
667
- this.clearSearchAfterCreate();
668
- return true;
669
- }
670
- clearSearchAfterCreate() {
671
- this._state.search = "";
672
- this._state.highlightedIndex = -1;
673
- const shadow = this.shadowRoot;
674
- if (!shadow)
675
- return;
676
- const desktopInput = shadow.querySelector(".dropdown-search-input");
677
- const mobileInput = shadow.querySelector(".mobile-search-input");
678
- if (desktopInput)
679
- desktopInput.value = "";
680
- if (mobileInput)
681
- mobileInput.value = "";
682
- const allTags = this.getTagElements().map((el) => this.getTagData(el));
683
- this._state.filteredTags = allTags;
684
- this.updateTagVisibility(allTags, allTags);
685
- this.updateOptionsVisibility(true);
686
- this.clearHighlights(allTags);
687
- this.updateMobileSelectedState();
688
- (desktopInput ?? mobileInput)?.focus();
689
- }
690
- blockSearchClick(e) {
691
- e.stopPropagation();
692
- e.preventDefault();
693
- }
694
- handleSearchInput(e) {
695
- const target = e.target;
696
- const query = target.value;
697
- this._state.search = query;
698
- if (this._externalSearch) {
699
- this.dispatchSearchEvent(query);
700
- return;
701
- }
702
- const allTags = this.getTagElements().map((el) => this.getTagData(el));
703
- const filtered = this.filterTags(allTags, query);
704
- this._state.filteredTags = filtered;
705
- this._state.highlightedIndex = -1;
706
- this.updateTagVisibility(filtered, allTags);
707
- this.updateOptionsVisibility(filtered.length > 0);
708
- this.updateMobileSelectedState();
709
- this.clearHighlights(allTags);
710
- }
711
- handleKeyboard(e) {
712
- if (!this._state.open)
713
- return;
714
- const shadow = this.shadowRoot;
715
- const searchInput = shadow.querySelector(".dropdown-search-input");
716
- const target = e.target;
717
- const shouldHandle = target === searchInput || document.activeElement !== searchInput;
718
- if (!shouldHandle)
719
- return;
720
- const filteredTags = this._state.filteredTags;
721
- const tagsCount = filteredTags.length;
722
- const currentHighlightedIndex = this._state.highlightedIndex;
723
- switch (e.key) {
724
- case "Escape":
725
- e.preventDefault();
726
- e.stopPropagation();
727
- this.closeDropdown();
728
- break;
729
- case "Enter":
730
- e.preventDefault();
731
- e.stopPropagation();
732
- if (currentHighlightedIndex >= 0 &&
733
- currentHighlightedIndex < tagsCount) {
734
- this.selectByTag(filteredTags[currentHighlightedIndex].element);
735
- }
736
- else {
737
- this.tryCreateFromSearch();
738
- }
739
- break;
740
- case "ArrowUp":
741
- e.preventDefault();
742
- e.stopPropagation();
743
- let newIndexUp;
744
- if (tagsCount === 0) {
745
- newIndexUp = -1;
746
- }
747
- else if (currentHighlightedIndex === -1) {
748
- newIndexUp = tagsCount - 1;
749
- }
750
- else if (currentHighlightedIndex === 0) {
751
- newIndexUp = tagsCount - 1;
752
- }
753
- else {
754
- newIndexUp = currentHighlightedIndex - 1;
755
- }
756
- this._state.highlightedIndex = newIndexUp;
757
- this.highlightTag(filteredTags, newIndexUp);
758
- break;
759
- case "ArrowDown":
760
- e.preventDefault();
761
- e.stopPropagation();
762
- let newIndexDown;
763
- if (tagsCount === 0) {
764
- newIndexDown = -1;
765
- }
766
- else if (currentHighlightedIndex === -1) {
767
- newIndexDown = 0;
768
- }
769
- else if (currentHighlightedIndex === tagsCount - 1) {
770
- newIndexDown = 0;
771
- }
772
- else {
773
- newIndexDown = currentHighlightedIndex + 1;
774
- }
775
- this._state.highlightedIndex = newIndexDown;
776
- this.highlightTag(filteredTags, newIndexDown);
777
- break;
778
- }
779
- }
780
- filterTags(tags, query) {
781
- if (!query || query.trim() === "") {
782
- return tags;
783
- }
784
- const searchLower = query.toLowerCase();
785
- return tags.filter(({ text }) => text.toLowerCase().includes(searchLower));
786
- }
787
- updateTagVisibility(filteredTags, allTags) {
788
- const visibleValues = new Set(filteredTags.map((tag) => tag.value));
789
- allTags.forEach(({ value, element }) => {
790
- if (visibleValues.has(value)) {
791
- element.removeAttribute("hidden");
792
- }
793
- else {
794
- element.setAttribute("hidden", "");
795
- }
796
- });
797
- }
798
- updateOptionsVisibility(hasOptions) {
799
- const shadow = this.shadowRoot;
800
- const options = shadow.querySelector(".dropdown-options");
801
- if (options) {
802
- options.style.display = hasOptions ? "" : "none";
803
- }
804
- }
805
- clearHighlights(tags) {
806
- tags.forEach(({ element }) => {
807
- element.removeAttribute("highlighted");
808
- });
809
- }
810
- highlightTag(tags, index) {
811
- this.clearHighlights(tags);
812
- if (index >= 0 && index < tags.length) {
813
- const { element } = tags[index];
814
- element.setAttribute("highlighted", "");
815
- element.scrollIntoView({
816
- behavior: "smooth",
817
- block: "nearest",
818
- inline: "nearest",
819
- });
820
- }
821
- }
822
- dispatchSearchEvent(query) {
823
- if (this._searchDebounceTimer !== null) {
824
- clearTimeout(this._searchDebounceTimer);
825
- this._searchDebounceTimer = null;
826
- }
827
- if (this._debounce > 0) {
828
- this._searchDebounceTimer = window.setTimeout(() => {
829
- this.fireSearchEvent(query);
830
- this._searchDebounceTimer = null;
831
- }, this._debounce);
832
- }
833
- else {
834
- this.fireSearchEvent(query);
835
- }
836
- }
837
- fireSearchEvent(query) {
838
- this.dispatchEvent(new CustomEvent("search", {
839
- detail: {
840
- query,
841
- element: this,
842
- },
843
- bubbles: true,
844
- composed: true,
845
- }));
846
- }
847
- fireOpenEvent() {
848
- this.dispatchEvent(new CustomEvent("open", {
849
- detail: { mode: this._state.mode, element: this },
850
- }));
851
- if (this._externalSearch) {
852
- this.fireSearchEvent("");
853
- }
854
- }
855
- fireCloseEvent() {
856
- this.dispatchEvent(new CustomEvent("close", {
857
- detail: { mode: this._state.mode, element: this },
858
- }));
859
- }
860
- dispatchChangeEvent(detail) {
861
- this.dispatchEvent(new CustomEvent("change", {
862
- detail,
863
- bubbles: true,
864
- cancelable: true,
865
- }));
866
- }
867
- render() {
868
- this._state.mode = isMobileTouch() ? "mobile" : "desktop";
869
- if (this._state.mode === "mobile") {
870
- this.renderMobile();
871
- }
872
- else {
873
- this.renderDesktop();
874
- }
875
- this.updateSearchVisibility();
876
- this.applyLoadingState();
877
- this.updateValidity();
878
- }
879
- updateValidity() {
880
- if (this.disabled) {
881
- this._internals.setValidity({});
882
- return;
883
- }
884
- if (this.required && this.getSelectedValues().length === 0) {
885
- const stub = this.shadowRoot?.querySelector(".select-stub");
886
- this._internals.setValidity({ valueMissing: true }, "Please select a value", stub ?? undefined);
887
- }
888
- else {
889
- this._internals.setValidity({});
890
- }
891
- }
892
- setupTriggerSlot() {
893
- const shadow = this.shadowRoot;
894
- const stub = shadow.querySelector(".select-stub");
895
- const slot = shadow.querySelector('slot[name="trigger"]');
896
- if (!stub || !slot)
897
- return;
898
- const sync = () => stub.classList.toggle("custom-trigger", slot.assignedElements().length > 0);
899
- slot.addEventListener("slotchange", sync);
900
- sync();
901
- }
902
- setupEventListeners() {
903
- const shadow = this.shadowRoot;
904
- const stub = shadow.querySelector(".select-stub");
905
- const optionsSlot = shadow.querySelector("#options-slot");
906
- const searchInput = shadow.querySelector(".dropdown-search-input");
907
- if (stub) {
908
- this._stubClickHandler = this.handleStubClick.bind(this);
909
- stub.addEventListener("click", this._stubClickHandler);
910
- stub.addEventListener("keydown", this.handleStubKeydown.bind(this));
911
- }
912
- const clearBtn = shadow.querySelector(".select-clear");
913
- clearBtn?.addEventListener("click", this.handleClearClick.bind(this));
914
- this.setupTriggerSlot();
915
- if (optionsSlot) {
916
- this._tagClickHandler = this.handleTagClick.bind(this);
917
- optionsSlot.addEventListener("click", this._tagClickHandler);
918
- }
919
- if (searchInput) {
920
- this._searchInputHandler = this.handleSearchInput.bind(this);
921
- this._blockSearchClick = this.blockSearchClick.bind(this);
922
- searchInput.addEventListener("input", this._searchInputHandler);
923
- searchInput.addEventListener("click", this._blockSearchClick);
924
- }
925
- const dialog = shadow.querySelector(".dropdown-dialog");
926
- if (dialog) {
927
- dialog.addEventListener("click", (e) => {
928
- if (e.target === dialog) {
929
- this.closeDropdown();
930
- }
931
- });
932
- }
933
- this._keyboardHandler = this.handleKeyboard.bind(this);
934
- document.addEventListener("keydown", this._keyboardHandler);
935
- }
936
- buildStubClasses() {
937
- const classes = [this._size];
938
- if (this._disabled)
939
- classes.push("disabled");
940
- if (this._compact)
941
- classes.push("compact");
942
- return classes.join(" ");
943
- }
944
- renderDesktop() {
945
- const shadow = this.shadowRoot;
946
- if (!shadow.querySelector(".select-container")) {
947
- const stubClasses = this.buildStubClasses();
948
- const listboxId = `select-listbox-${getElementHash(this)}`;
949
- const labelId = `select-label-${getElementHash(this)}`;
950
- const labelHtml = this._label
951
- ? `
952
- <label class="ty-field-label" id="${labelId}">
953
- ${this._label}
954
- ${this._required ? `<span class="required-icon">${REQUIRED_ICON_SVG}</span>` : ""}
955
- </label>
956
- `
957
- : "";
958
- const searchPlaceholder = "Search...";
959
- shadow.innerHTML = `
960
- <div class="select-container dropdown-mode-desktop">
961
- ${labelHtml}
962
- <div class="dropdown-wrapper">
963
- <div class="dropdown-stub select-stub ${stubClasses}"
964
- role="combobox"
965
- aria-haspopup="listbox"
966
- aria-expanded="false"
967
- aria-controls="${listboxId}"
968
- ${this._label ? `aria-labelledby="${labelId}"` : ""}
969
- tabindex="${this._disabled ? "-1" : "0"}"
970
- ${this._disabled ? 'disabled aria-disabled="true"' : ""}>
971
- <slot name="trigger">
972
- <slot name="start"></slot>
973
- <slot name="selected"></slot>
974
- <span class="dropdown-placeholder">${this._placeholder}</span>
975
- <span class="select-count" hidden></span>
976
- <slot name="end"></slot>
977
- <button class="select-clear" type="button" aria-label="Clear selection" hidden>
978
- ${CLEAR_ICON_SVG}
979
- </button>
980
- <div class="dropdown-chevron">
981
- ${CHEVRON_DOWN_SVG}
982
- </div>
983
- </slot>
984
- </div>
985
- <dialog class="dropdown-dialog">
986
- <div class="dropdown-header">
987
- <div class="dropdown-search-icon" aria-hidden="true">
988
- ${SEARCH_ICON_SVG}
989
- </div>
990
- <input
991
- class="dropdown-search-input ${this._size}"
992
- type="text"
993
- placeholder="${searchPlaceholder}"
994
- ${this._disabled ? "disabled" : ""}
995
- />
996
- <div class="dropdown-search-chevron">
997
- ${CHEVRON_DOWN_SVG}
998
- </div>
999
- </div>
1000
- <div class="dropdown-options-wrapper">
1001
- <div class="dropdown-options" id="${listboxId}" role="listbox" ${this._multiple ? 'aria-multiselectable="true"' : ""}>
1002
- <slot id="options-slot"></slot>
1003
- </div>
1004
- <div class="dropdown-loading" aria-hidden="true">
1005
- <slot name="loading">
1006
- <span class="dropdown-loading-spinner"></span>
1007
- <span class="dropdown-loading-text">Searching…</span>
1008
- </slot>
1009
- </div>
1010
- </div>
1011
- </dialog>
1012
- </div>
1013
- </div>
1014
- `;
1015
- this.setupEventListeners();
1016
- }
1017
- this.updateSelectionDisplay();
1018
- }
1019
- renderMobile() {
1020
- const shadow = this.shadowRoot;
1021
- if (!shadow.querySelector(".select-container")) {
1022
- const stubClasses = this.buildStubClasses();
1023
- const listboxId = `select-listbox-${getElementHash(this)}`;
1024
- const labelId = `select-label-${getElementHash(this)}`;
1025
- const labelHtml = this._label
1026
- ? `
1027
- <label class="ty-field-label" id="${labelId}">
1028
- ${this._label}
1029
- ${this._required ? `<span class="required-icon">${REQUIRED_ICON_SVG}</span>` : ""}
1030
- </label>
1031
- `
1032
- : "";
1033
- const closeButtonSvg = `<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
1034
- <line x1="18" y1="6" x2="6" y2="18"></line>
1035
- <line x1="6" y1="6" x2="18" y2="18"></line>
1036
- </svg>`;
1037
- const searchPlaceholder = this._label
1038
- ? `Search ${this._label}...`
1039
- : "Search...";
1040
- const searchHeaderHtml = `
1041
- <div class="mobile-search-header">
1042
- ${this._label ? `<span class="mobile-header-label">${this._label}</span>` : ""}
1043
- <div class="mobile-header-content">
1044
- <div class="dropdown-search-icon" aria-hidden="true">
1045
- ${SEARCH_ICON_SVG}
1046
- </div>
1047
- <input
1048
- class="mobile-search-input ${this._size}"
1049
- type="text"
1050
- placeholder="${searchPlaceholder}"
1051
- autofocus
1052
- ${this._disabled ? "disabled" : ""}
1053
- />
1054
- <button class="mobile-close-button" type="button" aria-label="Close">
1055
- ${closeButtonSvg}
1056
- </button>
1057
- </div>
1058
- </div>
1059
- `;
1060
- shadow.innerHTML = `
1061
- <div class="select-container dropdown-mode-mobile">
1062
- ${labelHtml}
1063
- <div class="dropdown-wrapper">
1064
- <div class="dropdown-stub select-stub ${stubClasses}"
1065
- role="combobox"
1066
- aria-haspopup="listbox"
1067
- aria-expanded="false"
1068
- aria-controls="${listboxId}"
1069
- ${this._label ? `aria-labelledby="${labelId}"` : ""}
1070
- tabindex="${this._disabled ? "-1" : "0"}"
1071
- ${this._disabled ? 'disabled aria-disabled="true"' : ""}>
1072
- <slot name="trigger">
1073
- <slot name="start"></slot>
1074
- <slot name="selected"></slot>
1075
- <span class="dropdown-placeholder">${this._placeholder}</span>
1076
- <span class="select-count" hidden></span>
1077
- <slot name="end"></slot>
1078
- <button class="select-clear" type="button" aria-label="Clear selection" hidden>
1079
- ${CLEAR_ICON_SVG}
1080
- </button>
1081
- <div class="dropdown-chevron">
1082
- ${CHEVRON_DOWN_SVG}
1083
- </div>
1084
- </slot>
1085
- </div>
1086
-
1087
- <dialog class="mobile-dialog">
1088
- <div class="mobile-dialog-content">
1089
-
1090
- <!-- HEADER (matches dropdown.ts) -->
1091
- ${searchHeaderHtml}
1092
-
1093
- <!-- BODY: options list with toggleable selected state -->
1094
- <div class="mobile-body">
1095
-
1096
- <!-- AVAILABLE LIST (always visible, takes remaining space) -->
1097
- <div class="mobile-available-section" data-empty="false">
1098
- <div class="section-header">
1099
- <span class="section-title">${this._availableLabel}</span>
1100
- </div>
1101
- <div class="section-content dropdown-options-wrapper" id="${listboxId}" role="listbox" ${this._multiple ? 'aria-multiselectable="true"' : ""}>
1102
- <slot id="options-slot"></slot>
1103
- <div class="empty-state">${this._noOptionsMessage}</div>
1104
- <div class="dropdown-loading" aria-hidden="true">
1105
- <slot name="loading">
1106
- <span class="dropdown-loading-spinner"></span>
1107
- <span class="dropdown-loading-text">Searching…</span>
1108
- </slot>
1109
- </div>
1110
- </div>
1111
- </div>
1112
-
1113
- </div>
1114
- </div>
1115
- </dialog>
1116
- </div>
1117
- </div>
1118
- `;
1119
- this.setupMobileEventListeners();
1120
- }
1121
- this.updateSelectionDisplay();
1122
- }
1123
- setupMobileEventListeners() {
1124
- const shadow = this.shadowRoot;
1125
- const stub = shadow.querySelector(".select-stub");
1126
- const optionsSlot = shadow.querySelector("#options-slot");
1127
- const searchInput = shadow.querySelector(".mobile-search-input");
1128
- const closeButton = shadow.querySelector(".mobile-close-button");
1129
- const dialog = shadow.querySelector(".mobile-dialog");
1130
- if (stub) {
1131
- stub.addEventListener("click", (e) => this.handleMobileStubClick(e));
1132
- stub.addEventListener("keydown", (e) => this.handleMobileStubKeydown(e));
1133
- }
1134
- const clearBtn = shadow.querySelector(".select-clear");
1135
- clearBtn?.addEventListener("click", this.handleClearClick.bind(this));
1136
- this.setupTriggerSlot();
1137
- if (optionsSlot) {
1138
- optionsSlot.addEventListener("click", (e) => this.handleMobileTagClick(e));
1139
- }
1140
- if (searchInput) {
1141
- searchInput.addEventListener("input", (e) => this.handleSearchInput(e));
1142
- }
1143
- if (closeButton) {
1144
- closeButton.addEventListener("click", () => this.closeMobileModal());
1145
- }
1146
- if (dialog) {
1147
- dialog.addEventListener("click", (e) => {
1148
- if (e.target === dialog) {
1149
- this.closeMobileModal();
1150
- }
1151
- });
1152
- dialog.addEventListener("cancel", (e) => {
1153
- e.preventDefault();
1154
- this.closeMobileModal();
1155
- });
1156
- }
1157
- }
1158
- handleMobileStubClick(e) {
1159
- e.preventDefault();
1160
- e.stopPropagation();
1161
- if (this._disabled || this._readonly) {
1162
- return;
1163
- }
1164
- this.openMobileModal();
1165
- }
1166
- handleMobileStubKeydown(e) {
1167
- if (this._disabled || this._readonly || this._state.open)
1168
- return;
1169
- if (e.key === "Enter" || e.key === " " || e.key === "ArrowDown") {
1170
- e.preventDefault();
1171
- this.openMobileModal();
1172
- }
1173
- }
1174
- handleMobileTagClick(e) {
1175
- this.handleTagClick(e);
1176
- }
1177
- updateMobileSelectedState() {
1178
- if (this._state.mode !== "mobile")
1179
- return;
1180
- const shadow = this.shadowRoot;
1181
- const availableSection = shadow.querySelector(".mobile-available-section");
1182
- if (availableSection) {
1183
- const visibleAvailable = this.getTagElements().filter((tag) => !tag.hasAttribute("hidden")).length;
1184
- availableSection.setAttribute("data-empty", String(visibleAvailable === 0));
1185
- const availableTitleSpan = shadow.querySelector(".mobile-available-section .section-title");
1186
- if (availableTitleSpan) {
1187
- availableTitleSpan.textContent = `${this._availableLabel} (${visibleAvailable})`;
1188
- }
1189
- }
1190
- }
1191
- shouldShowSearch() {
1192
- if (this._externalSearch)
1193
- return true;
1194
- if (this._allowCreate)
1195
- return true;
1196
- if (this._searchable === "always")
1197
- return true;
1198
- if (this._searchable === "never")
1199
- return false;
1200
- return this.getTagElements().length > SEARCH_AUTO_THRESHOLD;
1201
- }
1202
- updateSearchVisibility() {
1203
- const shadow = this.shadowRoot;
1204
- if (!shadow)
1205
- return;
1206
- const show = this.shouldShowSearch();
1207
- const header = shadow.querySelector(".dropdown-header");
1208
- if (header)
1209
- header.hidden = !show;
1210
- const mobileInput = shadow.querySelector(".mobile-search-input");
1211
- if (mobileInput)
1212
- mobileInput.hidden = !show;
1213
- const mobileIcon = shadow.querySelector(".mobile-header-content .dropdown-search-icon");
1214
- if (mobileIcon)
1215
- mobileIcon.hidden = !show;
1216
- }
1217
- updateSelectionDisplay() {
1218
- const shadow = this.shadowRoot;
1219
- const stub = shadow.querySelector(".select-stub");
1220
- if (!stub)
1221
- return;
1222
- const values = this._state.selectedValues;
1223
- const count = values.length;
1224
- stub.classList.toggle("has-selection", count > 0);
1225
- stub.classList.toggle("compact", this._compact);
1226
- const badge = stub.querySelector(".select-count");
1227
- if (badge) {
1228
- badge.textContent = String(count);
1229
- badge.hidden = count === 0 || !this._multiple || !this._compact;
1230
- }
1231
- const clearBtn = stub.querySelector(".select-clear");
1232
- if (clearBtn) {
1233
- clearBtn.hidden = !(count > 0 && this._clearable && !this._disabled && !this._readonly);
1234
- }
1235
- const wantClone = !this._multiple && count > 0 ? values[0] : null;
1236
- this.syncSelectedClone(wantClone);
1237
- stub.classList.toggle("has-clone", wantClone !== null);
1238
- const textEl = stub.querySelector(".dropdown-placeholder");
1239
- if (textEl) {
1240
- textEl.hidden = wantClone !== null;
1241
- const showLabels = count > 0 && this._multiple && !this._compact;
1242
- if (showLabels) {
1243
- textEl.textContent = this.getSelectedItems(values)
1244
- .map((i) => i.label)
1245
- .join(", ");
1246
- textEl.classList.remove("placeholder-shown");
1247
- }
1248
- else {
1249
- textEl.textContent = this._placeholder;
1250
- textEl.classList.add("placeholder-shown");
1251
- }
1252
- }
1253
- }
1254
- syncSelectedClone(value) {
1255
- const existing = this.querySelector(':scope > [cloned][slot="selected"]');
1256
- if (existing?.getAttribute("value") === value) {
1257
- this._selectedClone = existing;
1258
- return;
1259
- }
1260
- existing?.remove();
1261
- if (value === null) {
1262
- this._selectedClone = null;
1263
- return;
1264
- }
1265
- const option = this.getTagElements().find((el) => this.getTagData(el).value === value);
1266
- if (option) {
1267
- const clone = option.cloneNode(true);
1268
- clone.removeAttribute("selected");
1269
- clone.setAttribute("slot", "selected");
1270
- clone.setAttribute("cloned", "true");
1271
- this.appendChild(clone);
1272
- this._selectedClone = clone;
1273
- }
1274
- else if (this._selectedClone?.getAttribute("value") === value) {
1275
- const restored = this._selectedClone.cloneNode(true);
1276
- this.appendChild(restored);
1277
- this._selectedClone = restored;
1278
- }
1279
- }
1280
- deselectValue(value) {
1281
- const current = this.getSelectedValues();
1282
- if (!current.includes(value))
1283
- return;
1284
- this.updateComponentValue(current.filter((v) => v !== value), true, "remove", value);
1285
- }
1286
- clear() {
1287
- this.updateComponentValue([], true, "clear", null);
1288
- }
1289
- get value() {
1290
- return this.getSelectedValues().join(",");
1291
- }
1292
- set value(val) {
1293
- this.setProperty("value", val);
1294
- }
1295
- get name() {
1296
- return this.getProperty("name");
1297
- }
1298
- set name(val) {
1299
- this.setProperty("name", val);
1300
- }
1301
- get multiple() {
1302
- return this.getProperty("multiple");
1303
- }
1304
- set multiple(value) {
1305
- this.setProperty("multiple", value);
1306
- }
1307
- get compact() {
1308
- return this.getProperty("compact");
1309
- }
1310
- set compact(value) {
1311
- this.setProperty("compact", value);
1312
- }
1313
- get placeholder() {
1314
- return this.getProperty("placeholder");
1315
- }
1316
- set placeholder(val) {
1317
- this.setProperty("placeholder", val);
1318
- }
1319
- get label() {
1320
- return this.getProperty("label");
1321
- }
1322
- set label(val) {
1323
- this.setProperty("label", val);
1324
- }
1325
- get disabled() {
1326
- return this.getProperty("disabled");
1327
- }
1328
- set disabled(value) {
1329
- this.setProperty("disabled", value);
1330
- }
1331
- get loading() {
1332
- return this.getProperty("loading");
1333
- }
1334
- set loading(value) {
1335
- this.setProperty("loading", value);
1336
- }
1337
- get readonly() {
1338
- return this.getProperty("readonly");
1339
- }
1340
- set readonly(value) {
1341
- this.setProperty("readonly", value);
1342
- }
1343
- get clearable() {
1344
- return this.getProperty("clearable");
1345
- }
1346
- set clearable(value) {
1347
- this.setProperty("clearable", value);
1348
- }
1349
- get required() {
1350
- return this.getProperty("required");
1351
- }
1352
- set required(value) {
1353
- this.setProperty("required", value);
1354
- }
1355
- get searchable() {
1356
- return this.getProperty("searchable");
1357
- }
1358
- set searchable(value) {
1359
- this.setProperty("searchable", value);
1360
- }
1361
- get externalSearch() {
1362
- return this.getProperty("externalSearch");
1363
- }
1364
- set externalSearch(value) {
1365
- this.setProperty("externalSearch", value);
1366
- }
1367
- get debounce() {
1368
- return this.getProperty("debounce");
1369
- }
1370
- set debounce(value) {
1371
- const numValue = typeof value === "string" ? parseInt(value, 10) : value;
1372
- this.setProperty("debounce", numValue);
1373
- }
1374
- get size() {
1375
- return this.getProperty("size");
1376
- }
1377
- set size(value) {
1378
- this.setProperty("size", value);
1379
- }
1380
- get align() {
1381
- return this.getProperty("align");
1382
- }
1383
- set align(value) {
1384
- this.setProperty("align", value);
1385
- }
1386
- get placement() {
1387
- return this.getProperty("placement");
1388
- }
1389
- set placement(value) {
1390
- this.setProperty("placement", value);
1391
- }
1392
- get flavor() {
1393
- return this.getProperty("flavor");
1394
- }
1395
- set flavor(value) {
1396
- this.setProperty("flavor", value);
1397
- }
1398
- get form() {
1399
- return this._internals.form;
1400
- }
44
+ constructor() {
45
+ super();
46
+ this._name = "";
47
+ this._multiple = false;
48
+ this._compact = false;
49
+ this._placeholder = "Select...";
50
+ this._label = "";
51
+ this._disabled = false;
52
+ this._readonly = false;
53
+ this._required = false;
54
+ this._clearable = true;
55
+ this._externalSearch = false;
56
+ this._allowCreate = false;
57
+ this._createTransform = "none";
58
+ this._notSearchable = false;
59
+ this._loading = false;
60
+ this._scrollLockId = null;
61
+ this._size = DEFAULT_SIZE;
62
+ this._availableLabel = "Available";
63
+ this._selectedLabel = "Selected";
64
+ this._noOptionsMessage = "No options available";
65
+ this._state = {
66
+ open: false,
67
+ search: "",
68
+ highlightedIndex: -1,
69
+ filteredTags: [],
70
+ selectedValues: [],
71
+ mode: "desktop",
72
+ };
73
+ this._stubClickHandler = null;
74
+ this._tagClickHandler = null;
75
+ this._searchInputHandler = null;
76
+ this._blockSearchClick = null;
77
+ this._keyboardHandler = null;
78
+ this._debounce = 0;
79
+ this._searchDebounceTimer = null;
80
+ this._optionsScrollbar = null;
81
+ this._childObserver = null;
82
+ this._selectedClone = null;
83
+ this._customFlavorSheet = null;
84
+ const shadow = this.shadowRoot;
85
+ ensureStyles(shadow, { css: selectBaseStyles, id: "ty-select-base" });
86
+ ensureStyles(shadow, { css: selectStyles, id: "ty-select" });
87
+ }
88
+ onConnect() {
89
+ const shadow = this.shadowRoot;
90
+ const dialogs = shadow.querySelectorAll("dialog");
91
+ dialogs.forEach((dialog) => {
92
+ if (dialog.open) {
93
+ console.warn("⚠️ Found open dialog on connect, closing it");
94
+ dialog.close();
95
+ }
96
+ });
97
+ this.render();
98
+ this.initializeState();
99
+ this._childObserver = new MutationObserver(() => {
100
+ this.ensureOptionRoles();
101
+ if (this._state.selectedValues.length > 0) {
102
+ this.syncSelectedTags(this._state.selectedValues);
103
+ }
104
+ this.updateSearchVisibility();
105
+ this.updateSelectionDisplay();
106
+ this.updateMobileSelectedState();
107
+ });
108
+ this._childObserver.observe(this, { childList: true });
109
+ this.ensureOptionRoles();
110
+ this._syncCustomFlavor();
111
+ }
112
+ _syncCustomFlavor() {
113
+ this._customFlavorSheet = syncCustomFlavorSheet(this.shadowRoot, this._customFlavorSheet, this.getProperty("flavor"), ({ base }) => selectCustomFlavorCss(base));
114
+ }
115
+ onDisconnect() {
116
+ const shadow = this.shadowRoot;
117
+ const dialogs = shadow.querySelectorAll("dialog");
118
+ dialogs.forEach((dialog) => {
119
+ if (dialog.open) {
120
+ dialog.close();
121
+ }
122
+ });
123
+ if (this._keyboardHandler) {
124
+ document.removeEventListener("keydown", this._keyboardHandler);
125
+ this._keyboardHandler = null;
126
+ }
127
+ if (this._searchDebounceTimer !== null) {
128
+ clearTimeout(this._searchDebounceTimer);
129
+ this._searchDebounceTimer = null;
130
+ }
131
+ this._destroyOptionsScrollbar();
132
+ if (this._childObserver) {
133
+ this._childObserver.disconnect();
134
+ this._childObserver = null;
135
+ }
136
+ }
137
+ onPropertiesChanged(changes) {
138
+ for (const { name, newValue } of changes) {
139
+ switch (name) {
140
+ case "value":
141
+ const selectedValues = this.parseValue(newValue);
142
+ this._state.selectedValues = selectedValues;
143
+ if (this.isConnected && this.shadowRoot) {
144
+ this.syncSelectedTags(selectedValues);
145
+ this.updateSelectionDisplay();
146
+ this.updateMobileSelectedState();
147
+ }
148
+ break;
149
+ case "name":
150
+ this._name = newValue || "";
151
+ break;
152
+ case "flavor":
153
+ this._syncCustomFlavor();
154
+ break;
155
+ case "multiple":
156
+ this._multiple = newValue;
157
+ if (!newValue && this._state.selectedValues.length > 1) {
158
+ this.updateComponentValue([this._state.selectedValues[0]], false, "set", null);
159
+ }
160
+ break;
161
+ case "compact":
162
+ this._compact = newValue;
163
+ break;
164
+ case "placeholder":
165
+ this._placeholder = newValue || "Select...";
166
+ if (this.isConnected && this.shadowRoot)
167
+ this.updateSelectionDisplay();
168
+ break;
169
+ case "label":
170
+ this._label = newValue || "";
171
+ break;
172
+ case "disabled":
173
+ this._disabled = newValue;
174
+ break;
175
+ case "readonly":
176
+ this._readonly = newValue;
177
+ break;
178
+ case "required":
179
+ this._required = newValue;
180
+ break;
181
+ case "noClear":
182
+ this._clearable = !newValue;
183
+ if (this.isConnected && this.shadowRoot)
184
+ this.updateSelectionDisplay();
185
+ break;
186
+ case "externalSearch":
187
+ this._externalSearch = newValue;
188
+ break;
189
+ case "notSearchable":
190
+ this._notSearchable = newValue;
191
+ if (this.isConnected && this.shadowRoot)
192
+ this.updateSearchVisibility();
193
+ break;
194
+ case "allowCreate":
195
+ this._allowCreate = newValue;
196
+ if (this.isConnected && this.shadowRoot)
197
+ this.updateSearchVisibility();
198
+ break;
199
+ case "createTransform":
200
+ this._createTransform = newValue;
201
+ break;
202
+ case "size":
203
+ this._size = newValue;
204
+ break;
205
+ case "debounce":
206
+ this._debounce = newValue;
207
+ break;
208
+ case "available-label":
209
+ this._availableLabel = newValue || "Available";
210
+ break;
211
+ case "selected-label":
212
+ this._selectedLabel = newValue || "Selected";
213
+ break;
214
+ case "no-options-message":
215
+ this._noOptionsMessage = newValue || "No options available";
216
+ break;
217
+ case "loading":
218
+ this._loading = newValue;
219
+ this.applyLoadingState();
220
+ break;
221
+ }
222
+ }
223
+ }
224
+ applyLoadingState() {
225
+ const shadow = this.shadowRoot;
226
+ if (!shadow)
227
+ return;
228
+ const svg = this._loading ? getLoaderSvg() : null;
229
+ shadow.querySelectorAll(".dropdown-options-wrapper").forEach((wrapper) => {
230
+ wrapper.classList.toggle("loading", this._loading);
231
+ if (this._loading) {
232
+ wrapper.setAttribute("aria-busy", "true");
233
+ const spinner = wrapper.querySelector(".dropdown-loading-spinner");
234
+ if (spinner && svg)
235
+ spinner.innerHTML = svg;
236
+ }
237
+ else {
238
+ wrapper.removeAttribute("aria-busy");
239
+ }
240
+ });
241
+ }
242
+ getFormValue() {
243
+ const selectedValues = this._state.selectedValues;
244
+ if (this._name && selectedValues.length > 0) {
245
+ if (!this._multiple)
246
+ return selectedValues[0];
247
+ const formData = new FormData();
248
+ selectedValues.forEach((value) => {
249
+ formData.append(this._name, value);
250
+ });
251
+ return formData;
252
+ }
253
+ return null;
254
+ }
255
+ parseValue(value) {
256
+ if (!value || typeof value !== "string" || value.trim() === "")
257
+ return [];
258
+ return value
259
+ .split(",")
260
+ .map((v) => v.trim())
261
+ .filter((v) => v !== "");
262
+ }
263
+ initializeState() {
264
+ const initialValue = this.getProperty("value") || "";
265
+ if (initialValue) {
266
+ const selectedValues = this.parseValue(initialValue);
267
+ this._state.selectedValues = selectedValues;
268
+ this.syncSelectedTags(selectedValues);
269
+ this.updateSelectionDisplay();
270
+ this.updateMobileSelectedState();
271
+ }
272
+ else {
273
+ this.resolveDeclarativeSelection();
274
+ }
275
+ }
276
+ resolveDeclarativeSelection() {
277
+ if (this.getAttribute("value") || this._state.selectedValues.length > 0) {
278
+ return;
279
+ }
280
+ const preSelectedTags = this.getTagElements()
281
+ .filter((tag) => tag.hasAttribute("selected"))
282
+ .map((tag) => this.getTagData(tag).value);
283
+ if (preSelectedTags.length > 0) {
284
+ this.updateComponentValue(preSelectedTags, false);
285
+ return;
286
+ }
287
+ if (document.readyState === "loading") {
288
+ document.addEventListener("DOMContentLoaded", () => { if (this.isConnected)
289
+ this.resolveDeclarativeSelection(); }, { once: true });
290
+ }
291
+ }
292
+ getTagElements() {
293
+ return Array.from(this.querySelectorAll("ty-option:not([cloned]), ty-tag:not([cloned])"));
294
+ }
295
+ getTagData(element) {
296
+ const value = element.value ||
297
+ element.getAttribute("value") ||
298
+ element.textContent ||
299
+ "";
300
+ const text = element.getAttribute("label") || element.textContent || "";
301
+ return { value, text, element };
302
+ }
303
+ selectTag(tag) {
304
+ tag.setAttribute("selected", "");
305
+ }
306
+ deselectTag(tag) {
307
+ tag.removeAttribute("selected");
308
+ }
309
+ getSelectedValues() {
310
+ return [...this._state.selectedValues];
311
+ }
312
+ syncSelectedTags(selectedValues) {
313
+ const selectedSet = new Set(selectedValues);
314
+ const tags = this.getTagElements();
315
+ tags.forEach((tag) => {
316
+ const tagValue = this.getTagData(tag).value;
317
+ const shouldBeSelected = selectedSet.has(tagValue);
318
+ const isSelected = tag.hasAttribute("selected");
319
+ if (shouldBeSelected && !isSelected) {
320
+ this.selectTag(tag);
321
+ }
322
+ else if (!shouldBeSelected && isSelected) {
323
+ this.deselectTag(tag);
324
+ }
325
+ tag.setAttribute("aria-selected", String(shouldBeSelected));
326
+ });
327
+ }
328
+ ensureOptionRoles() {
329
+ this.getTagElements().forEach((tag) => {
330
+ if (!tag.hasAttribute("role"))
331
+ tag.setAttribute("role", "option");
332
+ if (!tag.hasAttribute("aria-selected"))
333
+ tag.setAttribute("aria-selected", "false");
334
+ });
335
+ }
336
+ updateComponentValue(newValues, dispatchChange = false, action = "set", item = null) {
337
+ const oldValues = this.getSelectedValues();
338
+ const valueStr = newValues.join(",");
339
+ if (JSON.stringify(newValues.sort()) !== JSON.stringify(oldValues.sort())) {
340
+ this.setProperty("value", valueStr);
341
+ this.updateValidity();
342
+ if (dispatchChange) {
343
+ this.dispatchChangeEvent({
344
+ value: this._multiple ? [...newValues] : (newValues[0] ?? null),
345
+ values: newValues,
346
+ items: this.getSelectedItems(newValues),
347
+ action,
348
+ item,
349
+ });
350
+ }
351
+ }
352
+ }
353
+ getSelectedItems(values) {
354
+ const byValue = new Map(this.getTagElements().map((el) => [this.getTagData(el).value, el]));
355
+ return values.map((value) => {
356
+ const el = byValue.get(value);
357
+ return {
358
+ value,
359
+ label: el?.getAttribute("label") || el?.textContent?.trim() || value,
360
+ flavor: el?.getAttribute("flavor") ?? null,
361
+ };
362
+ });
363
+ }
364
+ calculatePosition() {
365
+ const shadow = this.shadowRoot;
366
+ const stub = shadow.querySelector(".select-stub");
367
+ const dialog = shadow.querySelector(".dropdown-dialog");
368
+ if (!stub || !dialog)
369
+ return;
370
+ const stubRect = stub.getBoundingClientRect();
371
+ const MAX_POPUP_ESTIMATE = 440;
372
+ const dialogRect = dialog.getBoundingClientRect();
373
+ const estimatedHeight = Math.min(dialogRect.height || 200, MAX_POPUP_ESTIMATE);
374
+ const wrapPadding = 20;
375
+ const cssWidth = parseFloat(getComputedStyle(this).getPropertyValue("--ty-select-popup-width"));
376
+ const popupWidth = !isNaN(cssWidth)
377
+ ? cssWidth
378
+ : Math.max(stubRect.width, 320);
379
+ const requested = this.getProperty("placement");
380
+ const anchored = requested
381
+ ? placementToAnchored(requested)
382
+ : {
383
+ side: "auto",
384
+ align: this.getProperty("align"),
385
+ };
386
+ const pos = computeAnchoredPosition({
387
+ anchorRect: stubRect,
388
+ popupWidth,
389
+ popupHeight: estimatedHeight,
390
+ align: anchored.align,
391
+ side: anchored.side,
392
+ gap: 8,
393
+ });
394
+ const positionBelow = pos.below;
395
+ const x = pos.x - wrapPadding;
396
+ const y = (positionBelow ? pos.topY : pos.bottomY) - wrapPadding;
397
+ const width = popupWidth + wrapPadding + wrapPadding;
398
+ this.style.setProperty("--dropdown-x", `${x}px`);
399
+ this.style.setProperty("--dropdown-y", `${y}px`);
400
+ this.style.setProperty("--dropdown-width", `${width}px`);
401
+ this.style.setProperty("--dropdown-offset-x", "0px");
402
+ this.style.setProperty("--dropdown-offset-y", "0px");
403
+ this.style.setProperty("--dropdown-padding", `${wrapPadding}px`);
404
+ if (positionBelow) {
405
+ dialog.classList.add("position-below");
406
+ dialog.classList.remove("position-above");
407
+ }
408
+ else {
409
+ dialog.classList.add("position-above");
410
+ dialog.classList.remove("position-below");
411
+ }
412
+ this.style.setProperty("--dropdown-direction", positionBelow ? "below" : "above");
413
+ }
414
+ _setupOptionsScrollbar() {
415
+ if (!isCustomScrollbarEnabled())
416
+ return;
417
+ const shadow = this.shadowRoot;
418
+ const optionsDiv = shadow.querySelector(".dropdown-options");
419
+ const optionsWrapper = shadow.querySelector(".dropdown-options-wrapper");
420
+ if (!optionsDiv || !optionsWrapper)
421
+ return;
422
+ this._destroyOptionsScrollbar();
423
+ optionsDiv.classList.add("ty-custom-scroll");
424
+ this._optionsScrollbar = new CustomScrollbar(optionsDiv, {
425
+ vertical: true,
426
+ });
427
+ if (this._optionsScrollbar.trackY) {
428
+ optionsWrapper.appendChild(this._optionsScrollbar.trackY);
429
+ }
430
+ }
431
+ _destroyOptionsScrollbar() {
432
+ if (this._optionsScrollbar) {
433
+ this._optionsScrollbar.trackY?.remove();
434
+ this._optionsScrollbar.destroy();
435
+ this._optionsScrollbar = null;
436
+ }
437
+ }
438
+ openDropdown() {
439
+ const shadow = this.shadowRoot;
440
+ const dialog = shadow.querySelector(".dropdown-dialog");
441
+ if (!dialog)
442
+ return;
443
+ const stubEl = shadow.querySelector(".select-stub");
444
+ if (stubEl)
445
+ stubEl.setAttribute("aria-expanded", "true");
446
+ const lockId = `select-${this.id || "anon"}-${getElementHash(this)}`;
447
+ this._scrollLockId = lockId;
448
+ lockScroll(lockId);
449
+ dialog.showModal();
450
+ this.calculatePosition();
451
+ dialog.classList.add("open");
452
+ this._state.open = true;
453
+ const chevron = shadow.querySelector(".dropdown-chevron");
454
+ if (chevron)
455
+ chevron.classList.add("open");
456
+ const searchChevron = shadow.querySelector(".dropdown-search-chevron");
457
+ if (searchChevron)
458
+ searchChevron.classList.add("open");
459
+ const tags = this.getTagElements().map((el) => this.getTagData(el));
460
+ this._state.filteredTags = tags;
461
+ this._state.highlightedIndex = -1;
462
+ this.updateOptionsVisibility(true);
463
+ this._setupOptionsScrollbar();
464
+ const searchInput = shadow.querySelector(".dropdown-search-input");
465
+ if (searchInput && this.shouldShowSearch()) {
466
+ setTimeout(() => searchInput.focus(), 100);
467
+ }
468
+ this.fireOpenEvent();
469
+ }
470
+ closeDropdown() {
471
+ const shadow = this.shadowRoot;
472
+ const dialog = shadow.querySelector(".dropdown-dialog");
473
+ if (!dialog)
474
+ return;
475
+ const stubEl = shadow.querySelector(".select-stub");
476
+ if (stubEl)
477
+ stubEl.setAttribute("aria-expanded", "false");
478
+ this._destroyOptionsScrollbar();
479
+ dialog.classList.remove("open");
480
+ dialog.classList.remove("position-above");
481
+ dialog.classList.remove("position-below");
482
+ dialog.close();
483
+ if (this._scrollLockId) {
484
+ unlockScroll(this._scrollLockId);
485
+ this._scrollLockId = null;
486
+ }
487
+ this._state.open = false;
488
+ this._state.highlightedIndex = -1;
489
+ const chevron = shadow.querySelector(".dropdown-chevron");
490
+ if (chevron)
491
+ chevron.classList.remove("open");
492
+ const searchChevron = shadow.querySelector(".dropdown-search-chevron");
493
+ if (searchChevron)
494
+ searchChevron.classList.remove("open");
495
+ const hadQuery = this._state.search !== "";
496
+ this._state.search = "";
497
+ const searchInput = shadow.querySelector(".dropdown-search-input");
498
+ if (searchInput) {
499
+ searchInput.value = "";
500
+ }
501
+ if (this._externalSearch) {
502
+ if (hadQuery) {
503
+ if (this._searchDebounceTimer !== null) {
504
+ clearTimeout(this._searchDebounceTimer);
505
+ this._searchDebounceTimer = null;
506
+ }
507
+ this.fireSearchEvent("");
508
+ }
509
+ }
510
+ else {
511
+ const allTags = this.getTagElements().map((el) => this.getTagData(el));
512
+ this._state.filteredTags = allTags;
513
+ this.updateTagVisibility(allTags, allTags);
514
+ this.clearHighlights(allTags);
515
+ }
516
+ this.fireCloseEvent();
517
+ }
518
+ openMobileModal() {
519
+ const shadow = this.shadowRoot;
520
+ const dialog = shadow.querySelector(".mobile-dialog");
521
+ if (!dialog)
522
+ return;
523
+ const stubEl = shadow.querySelector(".select-stub");
524
+ if (stubEl)
525
+ stubEl.setAttribute("aria-expanded", "true");
526
+ const lockId = `select-${this.id || "anon"}-${getElementHash(this)}`;
527
+ this._scrollLockId = lockId;
528
+ lockScroll(lockId);
529
+ dialog.showModal();
530
+ dialog.getBoundingClientRect();
531
+ dialog.classList.add("open");
532
+ this._state.open = true;
533
+ const tags = this.getTagElements().map((el) => this.getTagData(el));
534
+ this._state.filteredTags = tags;
535
+ this.updateTagVisibility(tags, tags);
536
+ requestAnimationFrame(() => {
537
+ this.updateMobileSelectedState();
538
+ });
539
+ this.fireOpenEvent();
540
+ }
541
+ closeMobileModal() {
542
+ const shadow = this.shadowRoot;
543
+ const dialog = shadow.querySelector(".mobile-dialog");
544
+ if (!dialog)
545
+ return;
546
+ const stubEl = shadow.querySelector(".select-stub");
547
+ if (stubEl)
548
+ stubEl.setAttribute("aria-expanded", "false");
549
+ dialog.classList.remove("open");
550
+ dialog.close();
551
+ if (this._scrollLockId) {
552
+ unlockScroll(this._scrollLockId);
553
+ this._scrollLockId = null;
554
+ }
555
+ this._state.open = false;
556
+ this._state.highlightedIndex = -1;
557
+ const hadQuery = this._state.search !== "";
558
+ this._state.search = "";
559
+ const searchInput = shadow.querySelector(".mobile-search-input");
560
+ if (searchInput) {
561
+ searchInput.value = "";
562
+ }
563
+ if (this._externalSearch) {
564
+ if (hadQuery) {
565
+ if (this._searchDebounceTimer !== null) {
566
+ clearTimeout(this._searchDebounceTimer);
567
+ this._searchDebounceTimer = null;
568
+ }
569
+ this.fireSearchEvent("");
570
+ }
571
+ }
572
+ else {
573
+ const allTags = this.getTagElements();
574
+ allTags.forEach((el) => el.removeAttribute("hidden"));
575
+ }
576
+ this.fireCloseEvent();
577
+ }
578
+ handleStubClick(e) {
579
+ e.preventDefault();
580
+ e.stopPropagation();
581
+ if (this._disabled || this._readonly) {
582
+ return;
583
+ }
584
+ this.openDropdown();
585
+ }
586
+ handleClearClick(e) {
587
+ e.preventDefault();
588
+ e.stopPropagation();
589
+ if (this._disabled || this._readonly) {
590
+ return;
591
+ }
592
+ this.clear();
593
+ }
594
+ handleStubKeydown(e) {
595
+ if (this._disabled || this._readonly || this._state.open)
596
+ return;
597
+ if (e.key === "Enter" || e.key === " " || e.key === "ArrowDown") {
598
+ e.preventDefault();
599
+ this.openDropdown();
600
+ }
601
+ }
602
+ handleTagClick(e) {
603
+ const target = e.target;
604
+ const tag = target.closest("ty-option, ty-tag");
605
+ if (!tag || tag.hasAttribute("disabled"))
606
+ return;
607
+ e.preventDefault();
608
+ e.stopPropagation();
609
+ this.selectByTag(tag);
610
+ }
611
+ selectByTag(tag) {
612
+ const tagValue = this.getTagData(tag).value;
613
+ const currentValues = this.getSelectedValues();
614
+ if (!this._multiple) {
615
+ this.updateComponentValue([tagValue], true, "set", tagValue);
616
+ if (this._state.mode === "mobile") {
617
+ this.closeMobileModal();
618
+ }
619
+ else {
620
+ this.closeDropdown();
621
+ }
622
+ return;
623
+ }
624
+ if (currentValues.includes(tagValue)) {
625
+ const newValues = currentValues.filter((v) => v !== tagValue);
626
+ this.updateComponentValue(newValues, true, "remove", tagValue);
627
+ }
628
+ else {
629
+ this.updateComponentValue([...currentValues, tagValue], true, "add", tagValue);
630
+ }
631
+ }
632
+ tryCreateFromSearch() {
633
+ if (!this._allowCreate)
634
+ return false;
635
+ const query = (this._state.search || "").trim();
636
+ if (!query)
637
+ return false;
638
+ const queryLower = query.toLowerCase();
639
+ const allTags = this.getTagElements().map((el) => this.getTagData(el));
640
+ const existing = allTags.find((t) => t.value.toLowerCase() === queryLower ||
641
+ t.text.toLowerCase() === queryLower);
642
+ if (existing) {
643
+ this.selectByTag(existing.element);
644
+ return true;
645
+ }
646
+ const rawLabel = query;
647
+ const defaultValue = this._createTransform === "slug" ? slugify(rawLabel) : rawLabel;
648
+ if (!defaultValue)
649
+ return true;
650
+ const detail = { value: defaultValue, label: rawLabel };
651
+ const event = new CustomEvent("create", {
652
+ detail,
653
+ bubbles: true,
654
+ composed: true,
655
+ cancelable: true,
656
+ });
657
+ this.dispatchEvent(event);
658
+ if (event.defaultPrevented)
659
+ return true;
660
+ const finalValue = detail.value;
661
+ const option = document.createElement("ty-option");
662
+ option.setAttribute("value", finalValue);
663
+ option.textContent = rawLabel;
664
+ this.appendChild(option);
665
+ if (!this._multiple) {
666
+ this.updateComponentValue([finalValue], true, "create", finalValue);
667
+ if (this._state.mode === "mobile") {
668
+ this.closeMobileModal();
669
+ }
670
+ else {
671
+ this.closeDropdown();
672
+ }
673
+ return true;
674
+ }
675
+ const currentValues = this.getSelectedValues();
676
+ this.updateComponentValue([...currentValues, finalValue], true, "create", finalValue);
677
+ this.clearSearchAfterCreate();
678
+ return true;
679
+ }
680
+ clearSearchAfterCreate() {
681
+ this._state.search = "";
682
+ this._state.highlightedIndex = -1;
683
+ const shadow = this.shadowRoot;
684
+ if (!shadow)
685
+ return;
686
+ const desktopInput = shadow.querySelector(".dropdown-search-input");
687
+ const mobileInput = shadow.querySelector(".mobile-search-input");
688
+ if (desktopInput)
689
+ desktopInput.value = "";
690
+ if (mobileInput)
691
+ mobileInput.value = "";
692
+ const allTags = this.getTagElements().map((el) => this.getTagData(el));
693
+ this._state.filteredTags = allTags;
694
+ this.updateTagVisibility(allTags, allTags);
695
+ this.updateOptionsVisibility(true);
696
+ this.clearHighlights(allTags);
697
+ this.updateMobileSelectedState();
698
+ (desktopInput ?? mobileInput)?.focus();
699
+ }
700
+ blockSearchClick(e) {
701
+ e.stopPropagation();
702
+ e.preventDefault();
703
+ }
704
+ handleSearchInput(e) {
705
+ if (this._state.mode === "mobile")
706
+ this.expandMobileSection("available");
707
+ const target = e.target;
708
+ const query = target.value;
709
+ this._state.search = query;
710
+ if (this._externalSearch) {
711
+ this.dispatchSearchEvent(query);
712
+ return;
713
+ }
714
+ const allTags = this.getTagElements().map((el) => this.getTagData(el));
715
+ const filtered = this.filterTags(allTags, query);
716
+ this._state.filteredTags = filtered;
717
+ this._state.highlightedIndex = -1;
718
+ this.updateTagVisibility(filtered, allTags);
719
+ this.updateOptionsVisibility(filtered.length > 0);
720
+ this.updateMobileSelectedState();
721
+ this.clearHighlights(allTags);
722
+ }
723
+ handleKeyboard(e) {
724
+ if (!this._state.open)
725
+ return;
726
+ const shadow = this.shadowRoot;
727
+ const searchInput = shadow.querySelector(".dropdown-search-input");
728
+ const target = e.target;
729
+ const shouldHandle = target === searchInput || document.activeElement !== searchInput;
730
+ if (!shouldHandle)
731
+ return;
732
+ const filteredTags = this._state.filteredTags;
733
+ const tagsCount = filteredTags.length;
734
+ const currentHighlightedIndex = this._state.highlightedIndex;
735
+ switch (e.key) {
736
+ case "Escape":
737
+ e.preventDefault();
738
+ e.stopPropagation();
739
+ if (this._state.mode === "mobile") {
740
+ this.closeMobileModal();
741
+ }
742
+ else {
743
+ this.closeDropdown();
744
+ }
745
+ break;
746
+ case "Enter":
747
+ e.preventDefault();
748
+ e.stopPropagation();
749
+ if (currentHighlightedIndex >= 0 &&
750
+ currentHighlightedIndex < tagsCount) {
751
+ this.selectByTag(filteredTags[currentHighlightedIndex].element);
752
+ }
753
+ else {
754
+ this.tryCreateFromSearch();
755
+ }
756
+ break;
757
+ case "ArrowUp":
758
+ e.preventDefault();
759
+ e.stopPropagation();
760
+ const newIndexUp = this.nextEnabledTagIndex(filteredTags, currentHighlightedIndex, -1);
761
+ this._state.highlightedIndex = newIndexUp;
762
+ this.highlightTag(filteredTags, newIndexUp);
763
+ break;
764
+ case "ArrowDown":
765
+ e.preventDefault();
766
+ e.stopPropagation();
767
+ const newIndexDown = this.nextEnabledTagIndex(filteredTags, currentHighlightedIndex, 1);
768
+ this._state.highlightedIndex = newIndexDown;
769
+ this.highlightTag(filteredTags, newIndexDown);
770
+ break;
771
+ }
772
+ }
773
+ nextEnabledTagIndex(tags, fromIndex, direction) {
774
+ const count = tags.length;
775
+ if (count === 0)
776
+ return -1;
777
+ let index = fromIndex === -1
778
+ ? (direction === 1 ? 0 : count - 1)
779
+ : (fromIndex + direction + count) % count;
780
+ for (let i = 0; i < count; i++) {
781
+ if (!tags[index].element.hasAttribute("disabled"))
782
+ return index;
783
+ index = (index + direction + count) % count;
784
+ }
785
+ return -1;
786
+ }
787
+ filterTags(tags, query) {
788
+ if (!query || query.trim() === "") {
789
+ return tags;
790
+ }
791
+ const searchLower = query.toLowerCase();
792
+ return tags.filter(({ text }) => text.toLowerCase().includes(searchLower));
793
+ }
794
+ updateTagVisibility(filteredTags, allTags) {
795
+ const visibleValues = new Set(filteredTags.map((tag) => tag.value));
796
+ allTags.forEach(({ value, element }) => {
797
+ if (visibleValues.has(value)) {
798
+ element.removeAttribute("hidden");
799
+ }
800
+ else {
801
+ element.setAttribute("hidden", "");
802
+ }
803
+ });
804
+ }
805
+ updateOptionsVisibility(hasOptions) {
806
+ const shadow = this.shadowRoot;
807
+ const options = shadow.querySelector(".dropdown-options");
808
+ if (options) {
809
+ options.style.display = hasOptions ? "" : "none";
810
+ }
811
+ }
812
+ clearHighlights(tags) {
813
+ tags.forEach(({ element }) => {
814
+ element.removeAttribute("highlighted");
815
+ });
816
+ }
817
+ highlightTag(tags, index) {
818
+ this.clearHighlights(tags);
819
+ if (index >= 0 && index < tags.length) {
820
+ const { element } = tags[index];
821
+ element.setAttribute("highlighted", "");
822
+ element.scrollIntoView({
823
+ behavior: "smooth",
824
+ block: "nearest",
825
+ inline: "nearest",
826
+ });
827
+ }
828
+ }
829
+ dispatchSearchEvent(query) {
830
+ if (this._searchDebounceTimer !== null) {
831
+ clearTimeout(this._searchDebounceTimer);
832
+ this._searchDebounceTimer = null;
833
+ }
834
+ if (this._debounce > 0) {
835
+ this._searchDebounceTimer = window.setTimeout(() => {
836
+ this.fireSearchEvent(query);
837
+ this._searchDebounceTimer = null;
838
+ }, this._debounce);
839
+ }
840
+ else {
841
+ this.fireSearchEvent(query);
842
+ }
843
+ }
844
+ fireSearchEvent(query) {
845
+ this.dispatchEvent(new CustomEvent("search", {
846
+ detail: {
847
+ query,
848
+ element: this,
849
+ },
850
+ bubbles: true,
851
+ composed: true,
852
+ }));
853
+ }
854
+ fireOpenEvent() {
855
+ this.dispatchEvent(new CustomEvent("open", {
856
+ detail: { mode: this._state.mode, element: this },
857
+ }));
858
+ if (this._externalSearch) {
859
+ this.fireSearchEvent("");
860
+ }
861
+ }
862
+ fireCloseEvent() {
863
+ this.dispatchEvent(new CustomEvent("close", {
864
+ detail: { mode: this._state.mode, element: this },
865
+ }));
866
+ }
867
+ dispatchChangeEvent(detail) {
868
+ this.dispatchEvent(new CustomEvent("change", {
869
+ detail,
870
+ bubbles: true,
871
+ cancelable: true,
872
+ }));
873
+ }
874
+ render() {
875
+ this._state.mode = isMobileTouch() ? "mobile" : "desktop";
876
+ if (this._state.mode === "mobile") {
877
+ this.renderMobile();
878
+ }
879
+ else {
880
+ this.renderDesktop();
881
+ }
882
+ this.updateSearchVisibility();
883
+ this.applyLoadingState();
884
+ this.syncFieldChrome();
885
+ this.updateValidity();
886
+ }
887
+ syncFieldChrome() {
888
+ const shadow = this.shadowRoot;
889
+ const container = shadow?.querySelector(".select-container");
890
+ if (!shadow || !container)
891
+ return;
892
+ const labelId = `select-label-${getElementHash(this)}`;
893
+ let label = container.querySelector(":scope > .ty-field-label");
894
+ if (this._label) {
895
+ if (!label) {
896
+ label = document.createElement("label");
897
+ label.className = "ty-field-label";
898
+ label.id = labelId;
899
+ container.prepend(label);
900
+ }
901
+ label.innerHTML = `${this._label}${this._required ? `<span class="required-icon">${REQUIRED_ICON_SVG}</span>` : ""}`;
902
+ }
903
+ else {
904
+ label?.remove();
905
+ }
906
+ const stub = shadow.querySelector(".select-stub");
907
+ if (stub) {
908
+ stub.classList.toggle("disabled", this._disabled);
909
+ stub.classList.toggle("compact", this._compact);
910
+ stub.tabIndex = this._disabled ? -1 : 0;
911
+ stub.toggleAttribute("disabled", this._disabled);
912
+ if (this._disabled)
913
+ stub.setAttribute("aria-disabled", "true");
914
+ else
915
+ stub.removeAttribute("aria-disabled");
916
+ if (this._label)
917
+ stub.setAttribute("aria-labelledby", labelId);
918
+ else
919
+ stub.removeAttribute("aria-labelledby");
920
+ }
921
+ shadow
922
+ .querySelectorAll(".dropdown-search-input, .mobile-search-input")
923
+ .forEach((input) => (input.disabled = this._disabled));
924
+ }
925
+ updateValidity() {
926
+ if (this.disabled) {
927
+ this.applyValidity({});
928
+ return;
929
+ }
930
+ if (this.required && this.getSelectedValues().length === 0) {
931
+ const stub = this.shadowRoot?.querySelector(".select-stub");
932
+ this.applyValidity({ valueMissing: true }, "Please select a value", stub ?? undefined);
933
+ }
934
+ else {
935
+ this.applyValidity({});
936
+ }
937
+ }
938
+ setupTriggerSlot() {
939
+ const shadow = this.shadowRoot;
940
+ const stub = shadow.querySelector(".select-stub");
941
+ const slot = shadow.querySelector('slot[name="trigger"]');
942
+ if (!stub || !slot)
943
+ return;
944
+ const sync = () => {
945
+ const hasTrigger = slot.assignedElements().length > 0;
946
+ stub.classList.toggle("custom-trigger", hasTrigger);
947
+ this.toggleAttribute("has-trigger", hasTrigger);
948
+ };
949
+ slot.addEventListener("slotchange", sync);
950
+ sync();
951
+ }
952
+ setupEventListeners() {
953
+ const shadow = this.shadowRoot;
954
+ const stub = shadow.querySelector(".select-stub");
955
+ const optionsSlot = shadow.querySelector("#options-slot");
956
+ const searchInput = shadow.querySelector(".dropdown-search-input");
957
+ if (stub) {
958
+ this._stubClickHandler = this.handleStubClick.bind(this);
959
+ stub.addEventListener("click", this._stubClickHandler);
960
+ stub.addEventListener("keydown", this.handleStubKeydown.bind(this));
961
+ }
962
+ const clearBtn = shadow.querySelector(".select-clear");
963
+ clearBtn?.addEventListener("click", this.handleClearClick.bind(this));
964
+ this.setupTriggerSlot();
965
+ if (optionsSlot) {
966
+ this._tagClickHandler = this.handleTagClick.bind(this);
967
+ optionsSlot.addEventListener("click", this._tagClickHandler);
968
+ }
969
+ if (searchInput) {
970
+ this._searchInputHandler = this.handleSearchInput.bind(this);
971
+ this._blockSearchClick = this.blockSearchClick.bind(this);
972
+ searchInput.addEventListener("input", this._searchInputHandler);
973
+ searchInput.addEventListener("click", this._blockSearchClick);
974
+ }
975
+ const dialog = shadow.querySelector(".dropdown-dialog");
976
+ if (dialog) {
977
+ dialog.addEventListener("click", (e) => {
978
+ if (e.target === dialog) {
979
+ this.closeDropdown();
980
+ }
981
+ });
982
+ }
983
+ this._keyboardHandler = this.handleKeyboard.bind(this);
984
+ document.addEventListener("keydown", this._keyboardHandler);
985
+ }
986
+ buildStubClasses() {
987
+ const classes = [];
988
+ if (this._disabled)
989
+ classes.push("disabled");
990
+ if (this._compact)
991
+ classes.push("compact");
992
+ return classes.join(" ");
993
+ }
994
+ renderDesktop() {
995
+ const shadow = this.shadowRoot;
996
+ if (!shadow.querySelector(".select-container")) {
997
+ const stubClasses = this.buildStubClasses();
998
+ const listboxId = `select-listbox-${getElementHash(this)}`;
999
+ const labelId = `select-label-${getElementHash(this)}`;
1000
+ const labelHtml = this._label
1001
+ ? `
1002
+ <label class="ty-field-label" id="${labelId}">
1003
+ ${this._label}
1004
+ ${this._required ? `<span class="required-icon">${REQUIRED_ICON_SVG}</span>` : ""}
1005
+ </label>
1006
+ `
1007
+ : "";
1008
+ const searchPlaceholder = "Search...";
1009
+ shadow.innerHTML = `
1010
+ <div class="select-container dropdown-mode-desktop">
1011
+ ${labelHtml}
1012
+ <div class="dropdown-wrapper">
1013
+ <div class="dropdown-stub select-stub ${stubClasses}"
1014
+ role="combobox"
1015
+ aria-haspopup="listbox"
1016
+ aria-expanded="false"
1017
+ aria-controls="${listboxId}"
1018
+ ${this._label ? `aria-labelledby="${labelId}"` : ""}
1019
+ tabindex="${this._disabled ? "-1" : "0"}"
1020
+ ${this._disabled ? 'disabled aria-disabled="true"' : ""}>
1021
+ <slot name="trigger">
1022
+ <slot name="start"></slot>
1023
+ <slot name="selected"></slot>
1024
+ <span class="dropdown-placeholder">${this._placeholder}</span>
1025
+ <span class="select-count" hidden></span>
1026
+ <slot name="end"></slot>
1027
+ <button class="select-clear" type="button" aria-label="Clear selection" hidden>
1028
+ ${CLEAR_ICON_SVG}
1029
+ </button>
1030
+ <div class="dropdown-chevron">
1031
+ ${CHEVRON_DOWN_SVG}
1032
+ </div>
1033
+ </slot>
1034
+ </div>
1035
+ <dialog class="dropdown-dialog">
1036
+ <div class="dropdown-header">
1037
+ <div class="dropdown-search-icon" aria-hidden="true">
1038
+ ${SEARCH_ICON_SVG}
1039
+ </div>
1040
+ <input
1041
+ class="dropdown-search-input"
1042
+ type="text"
1043
+ placeholder="${searchPlaceholder}"
1044
+ ${this._disabled ? "disabled" : ""}
1045
+ />
1046
+ <div class="dropdown-search-chevron">
1047
+ ${CHEVRON_DOWN_SVG}
1048
+ </div>
1049
+ </div>
1050
+ <div class="dropdown-options-wrapper">
1051
+ <div class="dropdown-options" id="${listboxId}" role="listbox" ${this._multiple ? 'aria-multiselectable="true"' : ""}>
1052
+ <slot id="options-slot"></slot>
1053
+ </div>
1054
+ <div class="dropdown-loading" aria-hidden="true">
1055
+ <slot name="loading">
1056
+ <span class="dropdown-loading-spinner"></span>
1057
+ <span class="dropdown-loading-text">Searching…</span>
1058
+ </slot>
1059
+ </div>
1060
+ </div>
1061
+ </dialog>
1062
+ </div>
1063
+ </div>
1064
+ `;
1065
+ this.setupEventListeners();
1066
+ }
1067
+ this.updateSelectionDisplay();
1068
+ }
1069
+ renderMobile() {
1070
+ const shadow = this.shadowRoot;
1071
+ if (!shadow.querySelector(".select-container")) {
1072
+ const stubClasses = this.buildStubClasses();
1073
+ const listboxId = `select-listbox-${getElementHash(this)}`;
1074
+ const labelId = `select-label-${getElementHash(this)}`;
1075
+ const labelHtml = this._label
1076
+ ? `
1077
+ <label class="ty-field-label" id="${labelId}">
1078
+ ${this._label}
1079
+ ${this._required ? `<span class="required-icon">${REQUIRED_ICON_SVG}</span>` : ""}
1080
+ </label>
1081
+ `
1082
+ : "";
1083
+ const closeButtonSvg = `<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
1084
+ <line x1="18" y1="6" x2="6" y2="18"></line>
1085
+ <line x1="6" y1="6" x2="18" y2="18"></line>
1086
+ </svg>`;
1087
+ const searchPlaceholder = this._label
1088
+ ? `Search ${this._label}...`
1089
+ : "Search...";
1090
+ const searchHeaderHtml = `
1091
+ <div class="mobile-search-header">
1092
+ ${this._label ? `<span class="mobile-header-label">${this._label}</span>` : ""}
1093
+ <div class="mobile-header-content">
1094
+ <div class="dropdown-search-icon" aria-hidden="true">
1095
+ ${SEARCH_ICON_SVG}
1096
+ </div>
1097
+ <input
1098
+ class="mobile-search-input"
1099
+ type="text"
1100
+ placeholder="${searchPlaceholder}"
1101
+ autofocus
1102
+ ${this._disabled ? "disabled" : ""}
1103
+ />
1104
+ <button class="mobile-close-button" type="button" aria-label="Close">
1105
+ ${closeButtonSvg}
1106
+ </button>
1107
+ </div>
1108
+ </div>
1109
+ `;
1110
+ shadow.innerHTML = `
1111
+ <div class="select-container dropdown-mode-mobile">
1112
+ ${labelHtml}
1113
+ <div class="dropdown-wrapper">
1114
+ <div class="dropdown-stub select-stub ${stubClasses}"
1115
+ role="combobox"
1116
+ aria-haspopup="listbox"
1117
+ aria-expanded="false"
1118
+ aria-controls="${listboxId}"
1119
+ ${this._label ? `aria-labelledby="${labelId}"` : ""}
1120
+ tabindex="${this._disabled ? "-1" : "0"}"
1121
+ ${this._disabled ? 'disabled aria-disabled="true"' : ""}>
1122
+ <slot name="trigger">
1123
+ <slot name="start"></slot>
1124
+ <slot name="selected"></slot>
1125
+ <span class="dropdown-placeholder">${this._placeholder}</span>
1126
+ <span class="select-count" hidden></span>
1127
+ <slot name="end"></slot>
1128
+ <button class="select-clear" type="button" aria-label="Clear selection" hidden>
1129
+ ${CLEAR_ICON_SVG}
1130
+ </button>
1131
+ <div class="dropdown-chevron">
1132
+ ${CHEVRON_DOWN_SVG}
1133
+ </div>
1134
+ </slot>
1135
+ </div>
1136
+ <dialog class="mobile-dialog">
1137
+ <div class="mobile-dialog-content">
1138
+ <!-- HEADER (matches dropdown.ts) -->
1139
+ ${searchHeaderHtml}
1140
+ <!-- BODY: options list with toggleable selected state -->
1141
+ <div class="mobile-body" data-expanded="available">
1142
+ <!-- SELECTED STRIP (pinned above the list, collapses when empty) -->
1143
+ <div class="mobile-selected-section" data-empty="true">
1144
+ <button class="section-header" type="button" data-section="selected" aria-expanded="false">
1145
+ <span class="section-title">${this._selectedLabel} <span class="section-count">(0)</span></span>
1146
+ ${CHEVRON_DOWN_SVG}
1147
+ </button>
1148
+ <div class="section-content mobile-selected-chips"></div>
1149
+ </div>
1150
+ <!-- AVAILABLE LIST (always visible, takes remaining space) -->
1151
+ <div class="mobile-available-section" data-empty="false">
1152
+ <button class="section-header" type="button" data-section="available" aria-expanded="true">
1153
+ <span class="section-title">${this._availableLabel} <span class="section-count"></span></span>
1154
+ ${CHEVRON_DOWN_SVG}
1155
+ </button>
1156
+ <div class="section-content dropdown-options-wrapper" id="${listboxId}" role="listbox" ${this._multiple ? 'aria-multiselectable="true"' : ""}>
1157
+ <slot id="options-slot"></slot>
1158
+ <div class="empty-state">${this._noOptionsMessage}</div>
1159
+ <div class="dropdown-loading" aria-hidden="true">
1160
+ <slot name="loading">
1161
+ <span class="dropdown-loading-spinner"></span>
1162
+ <span class="dropdown-loading-text">Searching…</span>
1163
+ </slot>
1164
+ </div>
1165
+ </div>
1166
+ </div>
1167
+ </div>
1168
+ </div>
1169
+ </dialog>
1170
+ </div>
1171
+ </div>
1172
+ `;
1173
+ this.setupMobileEventListeners();
1174
+ }
1175
+ this.updateSelectionDisplay();
1176
+ }
1177
+ setupMobileEventListeners() {
1178
+ if (!this._keyboardHandler) {
1179
+ this._keyboardHandler = this.handleKeyboard.bind(this);
1180
+ document.addEventListener("keydown", this._keyboardHandler);
1181
+ }
1182
+ const shadow = this.shadowRoot;
1183
+ const stub = shadow.querySelector(".select-stub");
1184
+ const optionsSlot = shadow.querySelector("#options-slot");
1185
+ const searchInput = shadow.querySelector(".mobile-search-input");
1186
+ const closeButton = shadow.querySelector(".mobile-close-button");
1187
+ const dialog = shadow.querySelector(".mobile-dialog");
1188
+ if (stub) {
1189
+ stub.addEventListener("click", (e) => this.handleMobileStubClick(e));
1190
+ stub.addEventListener("keydown", (e) => this.handleMobileStubKeydown(e));
1191
+ }
1192
+ const clearBtn = shadow.querySelector(".select-clear");
1193
+ clearBtn?.addEventListener("click", this.handleClearClick.bind(this));
1194
+ this.setupTriggerSlot();
1195
+ if (optionsSlot) {
1196
+ optionsSlot.addEventListener("click", (e) => this.handleMobileTagClick(e));
1197
+ }
1198
+ if (searchInput) {
1199
+ searchInput.addEventListener("input", (e) => this.handleSearchInput(e));
1200
+ }
1201
+ shadow.querySelectorAll(".mobile-body .section-header").forEach((header) => {
1202
+ header.addEventListener("click", () => this.expandMobileSection(header.dataset.section === "selected" ? "selected" : "available"));
1203
+ });
1204
+ if (closeButton) {
1205
+ closeButton.addEventListener("click", () => this.closeMobileModal());
1206
+ }
1207
+ if (dialog) {
1208
+ dialog.addEventListener("click", (e) => {
1209
+ if (e.target === dialog) {
1210
+ this.closeMobileModal();
1211
+ }
1212
+ });
1213
+ dialog.addEventListener("cancel", (e) => {
1214
+ e.preventDefault();
1215
+ this.closeMobileModal();
1216
+ });
1217
+ }
1218
+ }
1219
+ handleMobileStubClick(e) {
1220
+ e.preventDefault();
1221
+ e.stopPropagation();
1222
+ if (this._disabled || this._readonly) {
1223
+ return;
1224
+ }
1225
+ this.openMobileModal();
1226
+ }
1227
+ handleMobileStubKeydown(e) {
1228
+ if (this._disabled || this._readonly || this._state.open)
1229
+ return;
1230
+ if (e.key === "Enter" || e.key === " " || e.key === "ArrowDown") {
1231
+ e.preventDefault();
1232
+ this.openMobileModal();
1233
+ }
1234
+ }
1235
+ handleMobileTagClick(e) {
1236
+ this.handleTagClick(e);
1237
+ }
1238
+ updateMobileSelectedState() {
1239
+ if (this._state.mode !== "mobile")
1240
+ return;
1241
+ const shadow = this.shadowRoot;
1242
+ this.renderMobileSelectedChips();
1243
+ const availableSection = shadow.querySelector(".mobile-available-section");
1244
+ if (availableSection) {
1245
+ const visibleAvailable = this.getTagElements().filter((tag) => !tag.hasAttribute("hidden") && !tag.hasAttribute("selected")).length;
1246
+ availableSection.setAttribute("data-empty", String(visibleAvailable === 0));
1247
+ this.setMobileSectionTitle(availableSection, this._availableLabel, visibleAvailable);
1248
+ }
1249
+ }
1250
+ setMobileSectionTitle(section, label, n) {
1251
+ const title = section.querySelector(".section-title");
1252
+ if (!title)
1253
+ return;
1254
+ const count = document.createElement("span");
1255
+ count.className = "section-count";
1256
+ count.textContent = `(${n})`;
1257
+ title.replaceChildren(`${label} `, count);
1258
+ }
1259
+ expandMobileSection(section) {
1260
+ const shadow = this.shadowRoot;
1261
+ const body = shadow?.querySelector(".mobile-body");
1262
+ if (!body)
1263
+ return;
1264
+ if (section === "selected" && this._state.selectedValues.length === 0)
1265
+ return;
1266
+ body.dataset.expanded = section;
1267
+ shadow.querySelectorAll(".mobile-body .section-header").forEach((header) => {
1268
+ const owns = header.dataset.section === section;
1269
+ header.setAttribute("aria-expanded", String(owns));
1270
+ });
1271
+ }
1272
+ renderMobileSelectedChips() {
1273
+ const shadow = this.shadowRoot;
1274
+ const section = shadow.querySelector(".mobile-selected-section");
1275
+ const host = shadow.querySelector(".mobile-selected-chips");
1276
+ if (!section || !host)
1277
+ return;
1278
+ const selected = this._state.selectedValues;
1279
+ section.setAttribute("data-empty", String(selected.length === 0));
1280
+ this.setMobileSectionTitle(section, this._selectedLabel, selected.length);
1281
+ if (selected.length === 0)
1282
+ this.expandMobileSection("available");
1283
+ const labelFor = (value) => {
1284
+ const option = this.getTagElements().find((tag) => this.getTagData(tag).value === value);
1285
+ return option ? this.getTagData(option).text : value;
1286
+ };
1287
+ host.textContent = "";
1288
+ for (const value of selected) {
1289
+ const chip = document.createElement("button");
1290
+ chip.type = "button";
1291
+ chip.className = "mobile-selected-chip";
1292
+ chip.dataset.value = value;
1293
+ chip.setAttribute("aria-label", `Remove ${labelFor(value)}`);
1294
+ chip.innerHTML = `<span class="chip-label"></span>${CLEAR_ICON_SVG}`;
1295
+ chip.querySelector(".chip-label").textContent = labelFor(value);
1296
+ chip.addEventListener("click", (e) => {
1297
+ e.preventDefault();
1298
+ e.stopPropagation();
1299
+ this.deselectValue(value);
1300
+ });
1301
+ host.appendChild(chip);
1302
+ }
1303
+ }
1304
+ shouldShowSearch() {
1305
+ if (this._externalSearch)
1306
+ return true;
1307
+ if (this._allowCreate)
1308
+ return true;
1309
+ if (this._notSearchable)
1310
+ return false;
1311
+ return this.getTagElements().length > SEARCH_AUTO_THRESHOLD;
1312
+ }
1313
+ updateSearchVisibility() {
1314
+ const shadow = this.shadowRoot;
1315
+ if (!shadow)
1316
+ return;
1317
+ const show = this.shouldShowSearch();
1318
+ const header = shadow.querySelector(".dropdown-header");
1319
+ if (header)
1320
+ header.hidden = !show;
1321
+ const mobileInput = shadow.querySelector(".mobile-search-input");
1322
+ if (mobileInput)
1323
+ mobileInput.hidden = !show;
1324
+ const mobileIcon = shadow.querySelector(".mobile-header-content .dropdown-search-icon");
1325
+ if (mobileIcon)
1326
+ mobileIcon.hidden = !show;
1327
+ }
1328
+ updateSelectionDisplay() {
1329
+ const shadow = this.shadowRoot;
1330
+ const stub = shadow.querySelector(".select-stub");
1331
+ if (!stub)
1332
+ return;
1333
+ const values = this._state.selectedValues;
1334
+ const count = values.length;
1335
+ stub.classList.toggle("has-selection", count > 0);
1336
+ stub.classList.toggle("compact", this._compact);
1337
+ const badge = stub.querySelector(".select-count");
1338
+ if (badge) {
1339
+ badge.textContent = String(count);
1340
+ badge.hidden = count === 0 || !this._multiple || !this._compact;
1341
+ }
1342
+ const clearBtn = stub.querySelector(".select-clear");
1343
+ if (clearBtn) {
1344
+ clearBtn.hidden = !(count > 0 && this._clearable && !this._disabled && !this._readonly);
1345
+ }
1346
+ const wantClone = !this._multiple && count > 0 ? values[0] : null;
1347
+ this.syncSelectedClone(wantClone);
1348
+ const hasClone = this._selectedClone !== null;
1349
+ stub.classList.toggle("has-clone", hasClone);
1350
+ const textEl = stub.querySelector(".dropdown-placeholder");
1351
+ if (textEl) {
1352
+ textEl.hidden = hasClone;
1353
+ const showLabels = count > 0 && this._multiple && !this._compact;
1354
+ if (showLabels) {
1355
+ textEl.textContent = this.getSelectedItems(values)
1356
+ .map((i) => i.label)
1357
+ .join(", ");
1358
+ textEl.classList.remove("placeholder-shown");
1359
+ }
1360
+ else if (wantClone !== null) {
1361
+ textEl.textContent = values.join(", ");
1362
+ textEl.classList.remove("placeholder-shown");
1363
+ }
1364
+ else {
1365
+ textEl.textContent = this._placeholder;
1366
+ textEl.classList.add("placeholder-shown");
1367
+ }
1368
+ }
1369
+ }
1370
+ syncSelectedClone(value) {
1371
+ const existing = this.querySelector(':scope > [cloned][slot="selected"]');
1372
+ if (existing?.getAttribute("value") === value) {
1373
+ const stale = value !== null &&
1374
+ existing.childNodes.length === 0 &&
1375
+ (this.getTagElements().find((el) => this.getTagData(el).value === value)?.childNodes.length ?? 0) > 0;
1376
+ if (!stale) {
1377
+ this._selectedClone = existing;
1378
+ return;
1379
+ }
1380
+ }
1381
+ existing?.remove();
1382
+ if (value === null) {
1383
+ this._selectedClone = null;
1384
+ return;
1385
+ }
1386
+ const option = this.getTagElements().find((el) => this.getTagData(el).value === value);
1387
+ if (option) {
1388
+ const clone = option.cloneNode(true);
1389
+ clone.removeAttribute("selected");
1390
+ clone.setAttribute("slot", "selected");
1391
+ clone.setAttribute("cloned", "true");
1392
+ this.appendChild(clone);
1393
+ this._selectedClone = clone;
1394
+ if (option.childNodes.length === 0 && document.readyState === "loading") {
1395
+ document.addEventListener("DOMContentLoaded", () => { if (this.isConnected)
1396
+ this.updateSelectionDisplay(); }, { once: true });
1397
+ }
1398
+ }
1399
+ else if (this._selectedClone?.getAttribute("value") === value) {
1400
+ const restored = this._selectedClone.cloneNode(true);
1401
+ this.appendChild(restored);
1402
+ this._selectedClone = restored;
1403
+ }
1404
+ }
1405
+ deselectValue(value) {
1406
+ const current = this.getSelectedValues();
1407
+ if (!current.includes(value))
1408
+ return;
1409
+ this.updateComponentValue(current.filter((v) => v !== value), true, "remove", value);
1410
+ }
1411
+ clear() {
1412
+ this.updateComponentValue([], true, "clear", null);
1413
+ }
1414
+ get value() {
1415
+ return this.getSelectedValues().join(",");
1416
+ }
1417
+ set value(val) {
1418
+ this.setProperty("value", val);
1419
+ }
1420
+ get name() {
1421
+ return this.getProperty("name");
1422
+ }
1423
+ set name(val) {
1424
+ this.setProperty("name", val);
1425
+ }
1426
+ get multiple() {
1427
+ return this.getProperty("multiple");
1428
+ }
1429
+ set multiple(value) {
1430
+ this.setProperty("multiple", value);
1431
+ }
1432
+ get compact() {
1433
+ return this.getProperty("compact");
1434
+ }
1435
+ set compact(value) {
1436
+ this.setProperty("compact", value);
1437
+ }
1438
+ get placeholder() {
1439
+ return this.getProperty("placeholder");
1440
+ }
1441
+ set placeholder(val) {
1442
+ this.setProperty("placeholder", val);
1443
+ }
1444
+ get label() {
1445
+ return this.getProperty("label");
1446
+ }
1447
+ set label(val) {
1448
+ this.setProperty("label", val);
1449
+ }
1450
+ get disabled() {
1451
+ return this.getProperty("disabled");
1452
+ }
1453
+ set disabled(value) {
1454
+ this.setProperty("disabled", value);
1455
+ }
1456
+ get loading() {
1457
+ return this.getProperty("loading");
1458
+ }
1459
+ set loading(value) {
1460
+ this.setProperty("loading", value);
1461
+ }
1462
+ get readonly() {
1463
+ return this.getProperty("readonly");
1464
+ }
1465
+ set readonly(value) {
1466
+ this.setProperty("readonly", value);
1467
+ }
1468
+ get noClear() {
1469
+ return this.getProperty("noClear");
1470
+ }
1471
+ set noClear(value) {
1472
+ this.setProperty("noClear", value);
1473
+ }
1474
+ get required() {
1475
+ return this.getProperty("required");
1476
+ }
1477
+ set required(value) {
1478
+ this.setProperty("required", value);
1479
+ }
1480
+ get notSearchable() {
1481
+ return this.getProperty("notSearchable");
1482
+ }
1483
+ set notSearchable(value) {
1484
+ this.setProperty("notSearchable", value);
1485
+ }
1486
+ get externalSearch() {
1487
+ return this.getProperty("externalSearch");
1488
+ }
1489
+ set externalSearch(value) {
1490
+ this.setProperty("externalSearch", value);
1491
+ }
1492
+ get debounce() {
1493
+ return this.getProperty("debounce");
1494
+ }
1495
+ set debounce(value) {
1496
+ const numValue = typeof value === "string" ? parseInt(value, 10) : value;
1497
+ this.setProperty("debounce", numValue);
1498
+ }
1499
+ get size() {
1500
+ return this.getProperty("size");
1501
+ }
1502
+ set size(value) {
1503
+ this.setProperty("size", value);
1504
+ }
1505
+ get align() {
1506
+ return this.getProperty("align");
1507
+ }
1508
+ set align(value) {
1509
+ this.setProperty("align", value);
1510
+ }
1511
+ get placement() {
1512
+ return this.getProperty("placement");
1513
+ }
1514
+ set placement(value) {
1515
+ this.setProperty("placement", value);
1516
+ }
1517
+ get flavor() {
1518
+ return this.getProperty("flavor");
1519
+ }
1520
+ set flavor(value) {
1521
+ this.setProperty("flavor", value);
1522
+ }
1523
+ get form() {
1524
+ return this._internals.form;
1525
+ }
1401
1526
  }
1402
1527
  TySelect.properties = {
1403
- value: {
1404
- type: "string",
1405
- visual: true,
1406
- formValue: true,
1407
- emitChange: false,
1408
- default: "",
1409
- coerce: (v) => {
1410
- if (Array.isArray(v)) {
1411
- return v.join(",");
1412
- }
1413
- if (v === null || v === undefined) {
1414
- return "";
1415
- }
1416
- return String(v);
1417
- },
1418
- },
1419
- name: {
1420
- type: "string",
1421
- default: "",
1422
- },
1423
- multiple: {
1424
- type: "boolean",
1425
- visual: true,
1426
- default: false,
1427
- },
1428
- compact: {
1429
- type: "boolean",
1430
- visual: true,
1431
- default: false,
1432
- },
1433
- placeholder: {
1434
- type: "string",
1435
- visual: true,
1436
- default: "Select...",
1437
- },
1438
- label: {
1439
- type: "string",
1440
- visual: true,
1441
- default: "",
1442
- },
1443
- disabled: {
1444
- type: "boolean",
1445
- visual: true,
1446
- default: false,
1447
- },
1448
- readonly: {
1449
- type: "boolean",
1450
- visual: true,
1451
- default: false,
1452
- },
1453
- required: {
1454
- type: "boolean",
1455
- visual: true,
1456
- default: false,
1457
- },
1458
- clearable: {
1459
- type: "boolean",
1460
- visual: true,
1461
- default: true,
1462
- aliases: { "not-clearable": false },
1463
- },
1464
- externalSearch: {
1465
- type: "boolean",
1466
- visual: true,
1467
- default: false,
1468
- aliases: { "external-search": true },
1469
- },
1470
- allowCreate: {
1471
- type: "boolean",
1472
- visual: true,
1473
- default: false,
1474
- aliases: { "allow-create": true },
1475
- },
1476
- createTransform: {
1477
- type: "string",
1478
- visual: false,
1479
- default: "none",
1480
- aliases: { "create-transform": true },
1481
- coerce: (v) => (v === "slug" ? "slug" : "none"),
1482
- },
1483
- searchable: {
1484
- type: "string",
1485
- visual: true,
1486
- default: "auto",
1487
- coerce: (v) => {
1488
- if (v === true || v === "" || v === "true")
1489
- return "always";
1490
- if (v === false || v === "false")
1491
- return "never";
1492
- return "auto";
1493
- },
1494
- },
1495
- align: {
1496
- type: "string",
1497
- visual: true,
1498
- default: "start",
1499
- validate: (v) => ["start", "center", "end"].includes(v),
1500
- coerce: (v) => v === "end" ? "end" : v === "center" ? "center" : "start",
1501
- },
1502
- placement: {
1503
- type: "string",
1504
- visual: true,
1505
- default: "",
1506
- },
1507
- size: {
1508
- type: "string",
1509
- visual: true,
1510
- default: "md",
1511
- validate: (v) => ["sm", "md", "lg"].includes(v),
1512
- coerce: (v) => {
1513
- if (v === "xs")
1514
- return "sm";
1515
- if (v === "xl")
1516
- return "lg";
1517
- if (!["sm", "md", "lg"].includes(v)) {
1518
- console.warn(`[ty-select] Invalid size. Using md.`);
1519
- return "md";
1520
- }
1521
- return v;
1522
- },
1523
- },
1524
- debounce: {
1525
- type: "number",
1526
- default: 0,
1527
- validate: (v) => v >= 0 && v <= 5000,
1528
- coerce: (v) => {
1529
- const num = Number(v);
1530
- if (isNaN(num))
1531
- return 0;
1532
- return Math.max(0, Math.min(5000, num));
1533
- },
1534
- },
1535
- "available-label": {
1536
- type: "string",
1537
- visual: true,
1538
- default: "Available",
1539
- },
1540
- "no-options-message": {
1541
- type: "string",
1542
- visual: true,
1543
- default: "No options available",
1544
- },
1545
- loading: {
1546
- type: "boolean",
1547
- visual: true,
1548
- default: false,
1549
- },
1550
- flavor: {
1551
- type: "string",
1552
- visual: true,
1553
- default: "neutral",
1554
- },
1528
+ value: {
1529
+ type: "string",
1530
+ visual: true,
1531
+ formValue: true,
1532
+ emitChange: false,
1533
+ default: "",
1534
+ coerce: (v) => {
1535
+ if (Array.isArray(v)) {
1536
+ return v.join(",");
1537
+ }
1538
+ if (v === null || v === undefined) {
1539
+ return "";
1540
+ }
1541
+ return String(v);
1542
+ },
1543
+ },
1544
+ name: {
1545
+ type: "string",
1546
+ default: "",
1547
+ },
1548
+ multiple: {
1549
+ type: "boolean",
1550
+ visual: true,
1551
+ default: false,
1552
+ },
1553
+ compact: {
1554
+ type: "boolean",
1555
+ visual: true,
1556
+ default: false,
1557
+ },
1558
+ placeholder: {
1559
+ type: "string",
1560
+ visual: true,
1561
+ default: "Select...",
1562
+ },
1563
+ label: {
1564
+ type: "string",
1565
+ visual: true,
1566
+ default: "",
1567
+ },
1568
+ disabled: {
1569
+ type: "boolean",
1570
+ visual: true,
1571
+ default: false,
1572
+ },
1573
+ readonly: {
1574
+ type: "boolean",
1575
+ visual: true,
1576
+ default: false,
1577
+ },
1578
+ required: {
1579
+ type: "boolean",
1580
+ visual: true,
1581
+ default: false,
1582
+ },
1583
+ noClear: {
1584
+ type: "boolean",
1585
+ visual: true,
1586
+ default: false,
1587
+ },
1588
+ externalSearch: {
1589
+ type: "boolean",
1590
+ visual: true,
1591
+ default: false,
1592
+ aliases: { "external-search": true },
1593
+ },
1594
+ allowCreate: {
1595
+ type: "boolean",
1596
+ visual: true,
1597
+ default: false,
1598
+ aliases: { "allow-create": true },
1599
+ },
1600
+ createTransform: {
1601
+ type: "string",
1602
+ visual: false,
1603
+ default: "none",
1604
+ aliases: { "create-transform": true },
1605
+ coerce: (v) => (v === "slug" ? "slug" : "none"),
1606
+ },
1607
+ notSearchable: {
1608
+ type: "boolean",
1609
+ visual: true,
1610
+ default: false,
1611
+ },
1612
+ align: {
1613
+ type: "string",
1614
+ visual: true,
1615
+ default: "start",
1616
+ validate: (v) => ["start", "center", "end"].includes(v),
1617
+ coerce: (v) => v === "end" ? "end" : v === "center" ? "center" : "start",
1618
+ },
1619
+ placement: {
1620
+ type: "string",
1621
+ visual: true,
1622
+ default: "",
1623
+ },
1624
+ size: {
1625
+ type: "string",
1626
+ visual: true,
1627
+ default: DEFAULT_SIZE,
1628
+ validate: (v) => ["xs", "sm", "md", "lg", "xl"].includes(v),
1629
+ coerce: (v) => {
1630
+ if (!["xs", "sm", "md", "lg", "xl"].includes(v)) {
1631
+ console.warn(`[ty-select] Invalid size. Using ${DEFAULT_SIZE}.`);
1632
+ return DEFAULT_SIZE;
1633
+ }
1634
+ return v;
1635
+ },
1636
+ },
1637
+ debounce: {
1638
+ type: "number",
1639
+ default: 0,
1640
+ validate: (v) => v >= 0 && v <= 5000,
1641
+ coerce: (v) => {
1642
+ const num = Number(v);
1643
+ if (isNaN(num))
1644
+ return 0;
1645
+ return Math.max(0, Math.min(5000, num));
1646
+ },
1647
+ },
1648
+ "available-label": {
1649
+ type: "string",
1650
+ visual: true,
1651
+ default: "Available",
1652
+ },
1653
+ "selected-label": {
1654
+ type: "string",
1655
+ visual: true,
1656
+ default: "Selected",
1657
+ },
1658
+ "no-options-message": {
1659
+ type: "string",
1660
+ visual: true,
1661
+ default: "No options available",
1662
+ },
1663
+ loading: {
1664
+ type: "boolean",
1665
+ visual: true,
1666
+ default: false,
1667
+ },
1668
+ flavor: {
1669
+ type: "string",
1670
+ visual: true,
1671
+ default: "neutral",
1672
+ },
1555
1673
  };
1556
1674
  if (!customElements.get("ty-select")) {
1557
- customElements.define("ty-select", TySelect);
1675
+ customElements.define("ty-select", TySelect);
1558
1676
  }