@api-client/ui 0.5.6 → 0.5.8

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 (82) hide show
  1. package/.cursor/rules/html-and-css-best-practices.mdc +63 -0
  2. package/.cursor/rules/lit-best-practices.mdc +78 -0
  3. package/.github/instructions/html-and-css-best-practices.instructions.md +70 -0
  4. package/.github/instructions/lit-best-practices.instructions.md +86 -0
  5. package/build/src/elements/currency/currency-picker.d.ts +10 -0
  6. package/build/src/elements/currency/currency-picker.d.ts.map +1 -0
  7. package/build/src/elements/currency/currency-picker.js +27 -0
  8. package/build/src/elements/currency/currency-picker.js.map +1 -0
  9. package/build/src/elements/currency/internals/Picker.d.ts +311 -0
  10. package/build/src/elements/currency/internals/Picker.d.ts.map +1 -0
  11. package/build/src/elements/currency/internals/Picker.js +857 -0
  12. package/build/src/elements/currency/internals/Picker.js.map +1 -0
  13. package/build/src/elements/currency/internals/Picker.styles.d.ts +3 -0
  14. package/build/src/elements/currency/internals/Picker.styles.d.ts.map +1 -0
  15. package/build/src/elements/currency/internals/Picker.styles.js +58 -0
  16. package/build/src/elements/currency/internals/Picker.styles.js.map +1 -0
  17. package/build/src/elements/mention-textarea/internals/MentionTextArea.d.ts +216 -0
  18. package/build/src/elements/mention-textarea/internals/MentionTextArea.d.ts.map +1 -0
  19. package/build/src/elements/mention-textarea/internals/MentionTextArea.js +1037 -0
  20. package/build/src/elements/mention-textarea/internals/MentionTextArea.js.map +1 -0
  21. package/build/src/elements/mention-textarea/internals/MentionTextArea.styles.d.ts +3 -0
  22. package/build/src/elements/mention-textarea/internals/MentionTextArea.styles.d.ts.map +1 -0
  23. package/build/src/elements/mention-textarea/internals/MentionTextArea.styles.js +274 -0
  24. package/build/src/elements/mention-textarea/internals/MentionTextArea.styles.js.map +1 -0
  25. package/build/src/elements/mention-textarea/ui-mention-textarea.d.ts +13 -0
  26. package/build/src/elements/mention-textarea/ui-mention-textarea.d.ts.map +1 -0
  27. package/build/src/elements/mention-textarea/ui-mention-textarea.js +28 -0
  28. package/build/src/elements/mention-textarea/ui-mention-textarea.js.map +1 -0
  29. package/build/src/md/button/internals/base.d.ts +1 -0
  30. package/build/src/md/button/internals/base.d.ts.map +1 -1
  31. package/build/src/md/button/internals/base.js +7 -0
  32. package/build/src/md/button/internals/base.js.map +1 -1
  33. package/build/src/md/chip/internals/Chip.styles.d.ts.map +1 -1
  34. package/build/src/md/chip/internals/Chip.styles.js +2 -0
  35. package/build/src/md/chip/internals/Chip.styles.js.map +1 -1
  36. package/build/src/md/date-picker/internals/DatePicker.styles.d.ts.map +1 -1
  37. package/build/src/md/date-picker/internals/DatePicker.styles.js +73 -0
  38. package/build/src/md/date-picker/internals/DatePicker.styles.js.map +1 -1
  39. package/build/src/md/date-picker/internals/DatePickerCalendar.d.ts +164 -51
  40. package/build/src/md/date-picker/internals/DatePickerCalendar.d.ts.map +1 -1
  41. package/build/src/md/date-picker/internals/DatePickerCalendar.js +660 -368
  42. package/build/src/md/date-picker/internals/DatePickerCalendar.js.map +1 -1
  43. package/build/src/md/date-picker/ui-date-picker-input.d.ts +65 -13
  44. package/build/src/md/date-picker/ui-date-picker-input.d.ts.map +1 -1
  45. package/build/src/md/date-picker/ui-date-picker-input.js +143 -76
  46. package/build/src/md/date-picker/ui-date-picker-input.js.map +1 -1
  47. package/build/src/md/date-picker/ui-date-picker-modal-input.d.ts +76 -17
  48. package/build/src/md/date-picker/ui-date-picker-modal-input.d.ts.map +1 -1
  49. package/build/src/md/date-picker/ui-date-picker-modal-input.js +192 -127
  50. package/build/src/md/date-picker/ui-date-picker-modal-input.js.map +1 -1
  51. package/build/src/md/date-picker/ui-date-picker-modal.d.ts +63 -15
  52. package/build/src/md/date-picker/ui-date-picker-modal.d.ts.map +1 -1
  53. package/build/src/md/date-picker/ui-date-picker-modal.js +143 -64
  54. package/build/src/md/date-picker/ui-date-picker-modal.js.map +1 -1
  55. package/demo/elements/currency/index.html +91 -0
  56. package/demo/elements/currency/index.ts +272 -0
  57. package/demo/elements/index.html +6 -0
  58. package/demo/elements/mention-textarea/index.html +19 -0
  59. package/demo/elements/mention-textarea/index.ts +205 -0
  60. package/demo/md/date-picker/date-picker.ts +138 -103
  61. package/package.json +2 -2
  62. package/src/elements/currency/currency-picker.ts +14 -0
  63. package/src/elements/currency/internals/Picker.styles.ts +58 -0
  64. package/src/elements/currency/internals/Picker.ts +846 -0
  65. package/src/elements/mention-textarea/internals/MentionTextArea.styles.ts +274 -0
  66. package/src/elements/mention-textarea/internals/MentionTextArea.ts +1036 -0
  67. package/src/elements/mention-textarea/ui-mention-textarea.ts +18 -0
  68. package/src/md/button/internals/base.ts +7 -0
  69. package/src/md/chip/internals/Chip.styles.ts +2 -0
  70. package/src/md/date-picker/internals/DatePicker.styles.ts +73 -0
  71. package/src/md/date-picker/internals/DatePickerCalendar.ts +643 -309
  72. package/src/md/date-picker/ui-date-picker-input.ts +110 -49
  73. package/src/md/date-picker/ui-date-picker-modal-input.ts +168 -99
  74. package/src/md/date-picker/ui-date-picker-modal.ts +136 -53
  75. package/test/README.md +3 -2
  76. package/test/elements/currency/CurrencyPicker.accessibility.test.ts +328 -0
  77. package/test/elements/currency/CurrencyPicker.core.test.ts +318 -0
  78. package/test/elements/currency/CurrencyPicker.integration.test.ts +482 -0
  79. package/test/elements/currency/CurrencyPicker.test.ts +486 -0
  80. package/test/elements/mention-textarea/MentionTextArea.basic.test.ts +63 -0
  81. package/test/elements/mention-textarea/MentionTextArea.test.ts +321 -0
  82. package/tsconfig.json +1 -1
@@ -0,0 +1,846 @@
1
+ import { LitElement, html, type TemplateResult, type PropertyValues, nothing } from 'lit'
2
+ import { property, state, query } from 'lit/decorators.js'
3
+ import { repeat } from 'lit/directives/repeat.js'
4
+
5
+ import '@material/web/select/outlined-select.js'
6
+ import '@material/web/select/select-option.js'
7
+ import '../../../md/chip/ui-chip-set.js'
8
+ import '../../../md/chip/ui-chip.js'
9
+
10
+ /**
11
+ * Represents a currency with all its display information.
12
+ */
13
+ export interface Currency {
14
+ /** ISO 4217 currency code (e.g., 'USD', 'EUR') */
15
+ code: string
16
+ /** Full name of the currency (e.g., 'US Dollar', 'Euro') */
17
+ name: string
18
+ /** Currency symbol (e.g., '$', '€') */
19
+ symbol: string
20
+ /** Country or region name (e.g., 'United States', 'European Union') */
21
+ country: string
22
+ /** Flag emoji representing the currency's origin */
23
+ flag: string
24
+ }
25
+
26
+ /**
27
+ * Represents an error that can occur in the CurrencyPicker component.
28
+ */
29
+ export interface CurrencyPickerError {
30
+ /** Type of error: validation, selection constraint, or internal error */
31
+ type: 'validation' | 'selection' | 'internal'
32
+ /** Human-readable error message */
33
+ message: string
34
+ /** Optional additional details about the error for debugging */
35
+ details?: Record<string, unknown>
36
+ }
37
+
38
+ /**
39
+ * Event detail interface for currency picker error events.
40
+ */
41
+ export interface CurrencyPickerErrorEvent {
42
+ /** The error information */
43
+ error: CurrencyPickerError
44
+ }
45
+
46
+ /**
47
+ * A web component for selecting currencies with chips display.
48
+ * Provides a searchable interface with country flags and currency information.
49
+ *
50
+ * This component uses ElementInternals for native form integration and
51
+ * follows web standards for error handling and validation.
52
+ *
53
+ * ## Features
54
+ * - Single or multi-select currency selection
55
+ * - Visual chips display for selected currencies
56
+ * - Native form integration with ElementInternals
57
+ * - Comprehensive validation and error handling
58
+ * - Accessibility support with ARIA attributes
59
+ * - Keyboard navigation support
60
+ * - Currency filtering based on allowed currencies
61
+ *
62
+ * ## Usage
63
+ * ```html
64
+ * <!-- Basic usage -->
65
+ * <currency-picker></currency-picker>
66
+ *
67
+ * <!-- Multi-select with allowed currencies -->
68
+ * <currency-picker multi .allowedCurrencies="${['USD', 'EUR', 'GBP']}"></currency-picker>
69
+ *
70
+ * <!-- Form integration -->
71
+ * <form>
72
+ * <currency-picker name="currencies" required></currency-picker>
73
+ * </form>
74
+ * ```
75
+ *
76
+ * @fires change - Dispatched when selected currencies change due to user interaction.
77
+ * Contains {currencies: Currency[], codes: string[]} in event.detail
78
+ * @fires error - Dispatched when validation or other errors occur.
79
+ * Contains {error: CurrencyPickerError} in event.detail
80
+ *
81
+ * @example
82
+ * ```typescript
83
+ * const picker = document.querySelector('currency-picker');
84
+ * picker.addEventListener('change', (e) => {
85
+ * console.log('Selected currencies:', e.detail.currencies);
86
+ * });
87
+ * picker.addEventListener('error', (e) => {
88
+ * console.error('Picker error:', e.detail.error.message);
89
+ * });
90
+ * ```
91
+ */
92
+ export default class CurrencyPicker extends LitElement {
93
+ /**
94
+ * Indicates that this custom element is form-associated and can participate in form submission.
95
+ * This enables the component to work with native HTML forms and use ElementInternals.
96
+ */
97
+ static formAssociated = true
98
+
99
+ /**
100
+ * Private ElementInternals instance for form integration and validation.
101
+ * Provides access to form APIs like setFormValue, setValidity, etc.
102
+ */
103
+ private internals: ElementInternals
104
+
105
+ /**
106
+ * Shadow root configuration for the component.
107
+ * Uses 'open' mode for accessibility and delegates focus to enable proper focus management.
108
+ */
109
+ static override shadowRootOptions: ShadowRootInit = {
110
+ mode: 'open',
111
+ delegatesFocus: true,
112
+ }
113
+
114
+ /**
115
+ * The currently selected currency codes.
116
+ * This is an array of ISO 4217 currency codes (e.g., 'USD', 'EUR').
117
+ */
118
+ @property({ type: Array }) accessor selected: string[] = []
119
+
120
+ /**
121
+ * The currencies that should be available for selection.
122
+ * If not specified, all supported currencies will be available.
123
+ * This is an array of ISO 4217 currency codes (e.g., 'USD', 'EUR').
124
+ */
125
+ @property({ type: Array }) accessor allowedCurrencies: string[] = []
126
+
127
+ /**
128
+ * The label for the currency selection dropdown.
129
+ * This is displayed as the label for the select input.
130
+ * @attribute
131
+ */
132
+ @property({ type: String }) accessor label = 'Add Currency'
133
+
134
+ /**
135
+ * The name attribute for the currency selection dropdown.
136
+ * This is used when submitting forms that include this component.
137
+ * @attribute
138
+ */
139
+ @property({ type: String }) accessor name: string | undefined
140
+
141
+ /**
142
+ * Supporting text for the currency selection dropdown.
143
+ * This is displayed below the select input.
144
+ * @attribute
145
+ */
146
+ @property({ type: String }) accessor supportingText: string | undefined
147
+
148
+ /**
149
+ * Whether the currency selection is required.
150
+ * If true, the component will enforce at least one currency to be selected.
151
+ * @attribute
152
+ */
153
+ @property({ type: Boolean }) accessor required = false
154
+
155
+ /**
156
+ * Whether the currency selection is disabled.
157
+ * If true, the component will not allow any changes to the selected currencies.
158
+ * @attribute
159
+ */
160
+ @property({ type: Boolean }) accessor disabled = false
161
+ /**
162
+ * Whether multiple currencies can be selected.
163
+ * If true, the component allows selecting multiple currencies.
164
+ * If false, only one currency can be selected at a time.
165
+ * @attribute
166
+ */
167
+ @property({ type: Boolean }) accessor multi = false
168
+
169
+ /**
170
+ * Whether to show errors inline within the component.
171
+ * If true, errors will be displayed below the component.
172
+ * If false, errors will only be dispatched as events.
173
+ * @attribute
174
+ */
175
+ @property({ type: Boolean }) accessor showErrors = true
176
+
177
+ /**
178
+ * The currently selectable currencies (filtered based on selection and allowed currencies).
179
+ * This is automatically updated based on the selected currencies and allowedCurrencies property.
180
+ */
181
+ @state() private accessor selectableCurrencies: Currency[] = []
182
+
183
+ /**
184
+ * Reference to the Material Design outlined select element.
185
+ * Used to manage the select's internal state and keep it synchronized with the component's selected property.
186
+ */
187
+ @query('md-outlined-select')
188
+ private accessor selectElement!: HTMLElement & { value: string }
189
+
190
+ /**
191
+ * Returns the form element that contains this component, if any.
192
+ * Part of the ElementInternals API for form-associated custom elements.
193
+ */
194
+ get form() {
195
+ return this.internals.form
196
+ }
197
+
198
+ /**
199
+ * Returns the validity state of the component.
200
+ * Part of the ElementInternals API for form-associated custom elements.
201
+ */
202
+ get validity() {
203
+ return this.internals.validity
204
+ }
205
+
206
+ /**
207
+ * Returns the validation message for the component.
208
+ * Part of the ElementInternals API for form-associated custom elements.
209
+ */
210
+ get validationMessage() {
211
+ return this.internals.validationMessage
212
+ }
213
+
214
+ /**
215
+ * Returns whether the component will be validated when the form is submitted.
216
+ * Part of the ElementInternals API for form-associated custom elements.
217
+ */
218
+ get willValidate() {
219
+ return this.internals.willValidate
220
+ }
221
+
222
+ /**
223
+ * Checks the validity of the component without displaying validation UI.
224
+ * Part of the ElementInternals API for form-associated custom elements.
225
+ * @returns True if the component is valid, false otherwise
226
+ */
227
+ checkValidity() {
228
+ return this.internals.checkValidity()
229
+ }
230
+
231
+ /**
232
+ * Checks the validity of the component and displays validation UI if invalid.
233
+ * Part of the ElementInternals API for form-associated custom elements.
234
+ * @returns True if the component is valid, false otherwise
235
+ */
236
+ reportValidity() {
237
+ return this.internals.reportValidity()
238
+ }
239
+
240
+ /**
241
+ * Master list of supported currencies with their display information.
242
+ * This includes popular currencies with their ISO codes, names, symbols, countries, and flag emojis.
243
+ * Private and readonly to ensure data integrity.
244
+ */
245
+ private readonly currencies: Currency[] = [
246
+ { code: 'USD', name: 'US Dollar', symbol: '$', country: 'United States', flag: '🇺🇸' },
247
+ { code: 'EUR', name: 'Euro', symbol: '€', country: 'European Union', flag: '🇪🇺' },
248
+ { code: 'GBP', name: 'British Pound', symbol: '£', country: 'United Kingdom', flag: '🇬🇧' },
249
+ { code: 'JPY', name: 'Japanese Yen', symbol: '¥', country: 'Japan', flag: '🇯🇵' },
250
+ { code: 'CAD', name: 'Canadian Dollar', symbol: 'C$', country: 'Canada', flag: '🇨🇦' },
251
+ { code: 'AUD', name: 'Australian Dollar', symbol: 'A$', country: 'Australia', flag: '🇦🇺' },
252
+ { code: 'CHF', name: 'Swiss Franc', symbol: 'Fr', country: 'Switzerland', flag: '🇨🇭' },
253
+ { code: 'CNY', name: 'Chinese Yuan', symbol: '¥', country: 'China', flag: '🇨🇳' },
254
+ { code: 'INR', name: 'Indian Rupee', symbol: '₹', country: 'India', flag: '🇮🇳' },
255
+ { code: 'KRW', name: 'South Korean Won', symbol: '₩', country: 'South Korea', flag: '🇰🇷' },
256
+ { code: 'BRL', name: 'Brazilian Real', symbol: 'R$', country: 'Brazil', flag: '🇧🇷' },
257
+ { code: 'MXN', name: 'Mexican Peso', symbol: '$', country: 'Mexico', flag: '🇲🇽' },
258
+ { code: 'SGD', name: 'Singapore Dollar', symbol: 'S$', country: 'Singapore', flag: '🇸🇬' },
259
+ { code: 'HKD', name: 'Hong Kong Dollar', symbol: 'HK$', country: 'Hong Kong', flag: '🇭🇰' },
260
+ { code: 'NOK', name: 'Norwegian Krone', symbol: 'kr', country: 'Norway', flag: '🇳🇴' },
261
+ { code: 'SEK', name: 'Swedish Krona', symbol: 'kr', country: 'Sweden', flag: '🇸🇪' },
262
+ { code: 'DKK', name: 'Danish Krone', symbol: 'kr', country: 'Denmark', flag: '🇩🇰' },
263
+ { code: 'PLN', name: 'Polish Zloty', symbol: 'zł', country: 'Poland', flag: '🇵🇱' },
264
+ { code: 'RUB', name: 'Russian Ruble', symbol: '₽', country: 'Russia', flag: '🇷🇺' },
265
+ { code: 'ZAR', name: 'South African Rand', symbol: 'R', country: 'South Africa', flag: '🇿🇦' },
266
+ { code: 'TRY', name: 'Turkish Lira', symbol: '₺', country: 'Turkey', flag: '🇹🇷' },
267
+ { code: 'NZD', name: 'New Zealand Dollar', symbol: 'NZ$', country: 'New Zealand', flag: '🇳🇿' },
268
+ { code: 'THB', name: 'Thai Baht', symbol: '฿', country: 'Thailand', flag: '🇹🇭' },
269
+ { code: 'ILS', name: 'Israeli Shekel', symbol: '₪', country: 'Israel', flag: '🇮🇱' },
270
+ { code: 'AED', name: 'UAE Dirham', symbol: 'د.إ', country: 'United Arab Emirates', flag: '🇦🇪' },
271
+ ]
272
+
273
+ constructor() {
274
+ super()
275
+ this.internals = this.attachInternals()
276
+ }
277
+
278
+ override connectedCallback() {
279
+ super.connectedCallback()
280
+ this.updateSelectableCurrencies()
281
+ this.updateFormValue()
282
+ }
283
+
284
+ /**
285
+ * Updates the form value using ElementInternals.
286
+ */
287
+ private updateFormValue(): void {
288
+ const value = this.selected.length > 0 ? this.selected.join(',') : null
289
+ this.internals.setFormValue(value)
290
+ }
291
+
292
+ protected override willUpdate(changed: PropertyValues<this>): void {
293
+ super.willUpdate(changed)
294
+
295
+ // Validate selected currencies with error handling
296
+ if (changed.has('selected')) {
297
+ this.selected = this.safeCurrencyValidation(this.selected, 'selected')
298
+
299
+ // Also validate against allowedCurrencies if specified
300
+ if (this.allowedCurrencies.length > 0) {
301
+ const allowedSet = new Set(this.allowedCurrencies)
302
+ const invalidSelectedCodes = this.selected.filter((code) => !allowedSet.has(code))
303
+
304
+ if (invalidSelectedCodes.length > 0) {
305
+ this.setError({
306
+ type: 'validation',
307
+ message: `Selected currencies not in allowed list: ${invalidSelectedCodes.join(', ')}`,
308
+ details: {
309
+ property: 'selected',
310
+ invalidCodes: invalidSelectedCodes,
311
+ allowedCurrencies: this.allowedCurrencies,
312
+ },
313
+ })
314
+
315
+ // Filter out invalid codes
316
+ this.selected = this.selected.filter((code) => allowedSet.has(code))
317
+ }
318
+ }
319
+
320
+ // Also validate selection constraints when property changes
321
+ if (!this.multi && this.selected.length > 1) {
322
+ // In single-select mode, keep only the first valid selection
323
+ this.selected = this.selected.slice(0, 1)
324
+ this.setError({
325
+ type: 'selection',
326
+ message: 'Multiple currency selection is not allowed when multi=false. Only first selection kept.',
327
+ details: { multi: this.multi, originalSelection: changed.get('selected') },
328
+ })
329
+ }
330
+
331
+ // Validate required constraint
332
+ if (this.required && this.selected.length === 0) {
333
+ this.internals.setValidity({ valueMissing: true }, 'At least one currency must be selected')
334
+ this.setAttribute('aria-invalid', 'true')
335
+ this.dispatchErrorEvent({
336
+ type: 'selection',
337
+ message: 'At least one currency must be selected when required=true',
338
+ details: { required: this.required, currentSelection: this.selected },
339
+ })
340
+ } else if (!this.required || this.selected.length > 0) {
341
+ // Clear validation if not required or has selection
342
+ this.internals.setValidity({})
343
+ this.setAttribute('aria-invalid', 'false')
344
+ }
345
+ }
346
+ // Validate allowed currencies with error handling
347
+ if (changed.has('allowedCurrencies')) {
348
+ this.allowedCurrencies = this.safeCurrencyValidation(this.allowedCurrencies, 'allowedCurrencies')
349
+
350
+ // Filter selected currencies to only include allowed ones
351
+ if (this.allowedCurrencies.length > 0) {
352
+ const allowedSet = new Set(this.allowedCurrencies)
353
+ const filteredSelected = this.selected.filter((code) => allowedSet.has(code))
354
+ if (filteredSelected.length !== this.selected.length) {
355
+ this.selected = filteredSelected
356
+ }
357
+ }
358
+ }
359
+
360
+ if (changed.has('selected') || changed.has('allowedCurrencies') || changed.has('multi')) {
361
+ this.updateSelectableCurrencies()
362
+ }
363
+
364
+ // Update form value when selected changes
365
+ if (changed.has('selected')) {
366
+ this.updateFormValue()
367
+ }
368
+
369
+ // Synchronize the select element's value with the component's selected state
370
+ if (changed.has('selected') && this.selectElement) {
371
+ // In single-select mode, set the select value to the first selected currency or empty
372
+ // In multi-select mode, always clear the select after selection to allow adding more
373
+ if (!this.multi) {
374
+ this.selectElement.value = this.selected.length > 0 ? this.selected[0] : ''
375
+ } else {
376
+ // For multi-select, always keep the select cleared to allow adding more currencies
377
+ this.selectElement.value = ''
378
+ }
379
+ }
380
+ }
381
+
382
+ /**
383
+ * Called after the component has been updated and rendered.
384
+ * Ensures the select element's value stays synchronized with the component's selected state.
385
+ */
386
+ protected override updated(changed: PropertyValues<this>): void {
387
+ super.updated(changed)
388
+
389
+ // Ensure select element is synchronized after rendering
390
+ if (this.selectElement && (changed.has('selected') || changed.has('multi'))) {
391
+ if (!this.multi) {
392
+ // In single-select mode, show the selected currency or clear the select
393
+ this.selectElement.value = this.selected.length > 0 ? this.selected[0] : ''
394
+ } else {
395
+ // In multi-select mode, always keep the select cleared to allow adding more currencies
396
+ this.selectElement.value = ''
397
+ }
398
+ }
399
+ }
400
+
401
+ /**
402
+ * Sets an error state using ElementInternals and optionally dispatches an error event.
403
+ * @param error The error to set
404
+ * @param dispatch Whether to dispatch an error event (default: true)
405
+ */
406
+ private setError(error: CurrencyPickerError, dispatch = true): void {
407
+ // Use ElementInternals for native form validation
408
+ this.internals.setValidity({ customError: true }, error.message)
409
+ this.setAttribute('aria-invalid', 'true')
410
+ if (dispatch) {
411
+ this.dispatchErrorEvent(error)
412
+ }
413
+ }
414
+
415
+ /**
416
+ * Dispatches an error event.
417
+ * @param error The error to dispatch
418
+ */
419
+ private dispatchErrorEvent(error: CurrencyPickerError): void {
420
+ this.dispatchEvent(
421
+ new CustomEvent('error', {
422
+ detail: { error },
423
+ bubbles: false,
424
+ })
425
+ )
426
+ }
427
+
428
+ /**
429
+ * Clears the current error state using ElementInternals.
430
+ */
431
+ private clearError(): void {
432
+ this.internals.setValidity({})
433
+ this.setAttribute('aria-invalid', 'false')
434
+ }
435
+
436
+ /**
437
+ * Validates currency codes against the supported currencies.
438
+ * @param codes Array of currency codes to validate
439
+ * @returns Array of invalid currency codes
440
+ */
441
+ private validateCurrencyCodes(codes: string[]): string[] {
442
+ return codes.filter((code) => !this.currencies.some((c) => c.code === code))
443
+ }
444
+
445
+ /**
446
+ * Safely validates and filters currency codes, setting errors for invalid codes.
447
+ * @param codes Array of currency codes to validate
448
+ * @param property Name of the property being validated
449
+ * @returns Array of valid currency codes
450
+ */
451
+ private safeCurrencyValidation(codes: string[], property: string): string[] {
452
+ if (!Array.isArray(codes)) {
453
+ this.setError({
454
+ type: 'validation',
455
+ message: `Invalid ${property}: expected array of currency codes`,
456
+ details: { property, value: codes },
457
+ })
458
+ return []
459
+ }
460
+
461
+ const invalidCodes = this.validateCurrencyCodes(codes)
462
+ if (invalidCodes.length > 0) {
463
+ this.setError({
464
+ type: 'validation',
465
+ message: `Invalid currency codes in ${property}: ${invalidCodes.join(', ')}`,
466
+ details: { property, invalidCodes, validCodes: this.currencies.map((c) => c.code) },
467
+ }) // Dispatch error event for validation errors
468
+
469
+ // Return only valid codes
470
+ return codes.filter((code) => !invalidCodes.includes(code))
471
+ }
472
+
473
+ // Clear any previous validation errors for this property
474
+ if (!this.internals.validity.valid) {
475
+ this.clearError()
476
+ }
477
+
478
+ return codes
479
+ }
480
+
481
+ /**
482
+ * Validates selection constraints using ElementInternals.
483
+ * @param newSelection The new selection to validate
484
+ * @returns Whether the selection is valid
485
+ */
486
+ private validateSelectionConstraints(newSelection: string[]): boolean {
487
+ if (!this.multi && newSelection.length > 1) {
488
+ this.setError({
489
+ type: 'selection',
490
+ message: 'Multiple currency selection is not allowed when multi=false',
491
+ details: { multi: this.multi, attemptedSelection: newSelection },
492
+ })
493
+ return false
494
+ }
495
+
496
+ if (this.required && newSelection.length === 0) {
497
+ this.internals.setValidity({ valueMissing: true }, 'At least one currency must be selected')
498
+ this.setAttribute('aria-invalid', 'true')
499
+ this.dispatchErrorEvent({
500
+ type: 'selection',
501
+ message: 'At least one currency must be selected when required=true',
502
+ details: { required: this.required, currentSelection: newSelection },
503
+ })
504
+ return false
505
+ }
506
+
507
+ return true
508
+ }
509
+
510
+ private updateSelectableCurrencies() {
511
+ const selectedCodes = new Set(this.selected)
512
+
513
+ // If allowedCurrencies is specified, filter the master list by it
514
+ let availableCurrencies = this.currencies
515
+ if (this.allowedCurrencies.length > 0) {
516
+ const allowedSet = new Set(this.allowedCurrencies)
517
+ availableCurrencies = this.currencies.filter((c) => allowedSet.has(c.code))
518
+ }
519
+ if (this.multi) {
520
+ // Then filter out already selected currencies
521
+ this.selectableCurrencies = availableCurrencies.filter((c) => !selectedCodes.has(c.code))
522
+ } else {
523
+ // If single-select, just use the available currencies
524
+ this.selectableCurrencies = availableCurrencies
525
+ }
526
+ }
527
+
528
+ private handleCurrencySelect(event: Event) {
529
+ try {
530
+ const select = event.target as HTMLSelectElement
531
+ const selectedCode = select.value
532
+
533
+ if (!selectedCode) return
534
+
535
+ const currency = this.currencies.find((c) => c.code === selectedCode)
536
+ if (!currency) {
537
+ this.setError({
538
+ type: 'selection',
539
+ message: `Currency not found: ${selectedCode}`,
540
+ details: { attemptedCode: selectedCode },
541
+ })
542
+ return
543
+ }
544
+
545
+ let newSelection: string[]
546
+
547
+ if (this.multi) {
548
+ // If multi-select, add the currency to the selection
549
+ if (!this.selected.includes(currency.code)) {
550
+ newSelection = [...this.selected, currency.code]
551
+ } else {
552
+ // Currency already selected, clear any selection errors but don't add duplicate
553
+ if (!this.internals.validity.valid) {
554
+ this.clearError()
555
+ }
556
+ return
557
+ }
558
+ } else {
559
+ // If single-select, replace the current selection
560
+ newSelection = [currency.code]
561
+ }
562
+
563
+ // Validate the new selection
564
+ if (!this.validateSelectionConstraints(newSelection)) {
565
+ return // Error was set in validateSelectionConstraints
566
+ }
567
+
568
+ this.selected = newSelection
569
+ this.updateSelectableCurrencies()
570
+
571
+ // Reset the select for multi-select mode
572
+ if (this.multi) {
573
+ select.value = ''
574
+ }
575
+
576
+ // Clear any previous errors on successful selection
577
+ this.clearError()
578
+ this.dispatchChangeEvent()
579
+ } catch (error) {
580
+ this.setError({
581
+ type: 'internal',
582
+ message: `Internal error during currency selection: ${error instanceof Error ? error.message : String(error)}`,
583
+ details: { originalError: error },
584
+ })
585
+ }
586
+ }
587
+
588
+ private handleRemoveCurrency(event: Event) {
589
+ try {
590
+ const chip = event.target as HTMLElement
591
+ const currencyCode = chip.dataset.code
592
+
593
+ if (!currencyCode) {
594
+ this.setError({
595
+ type: 'internal',
596
+ message: 'Unable to determine currency code from chip element',
597
+ details: { chipElement: chip },
598
+ })
599
+ return
600
+ }
601
+
602
+ const newSelection = this.selected.filter((code) => code !== currencyCode)
603
+
604
+ // Validate the new selection (e.g., required constraint)
605
+ if (!this.validateSelectionConstraints(newSelection)) {
606
+ return // Error was set in validateSelectionConstraints
607
+ }
608
+
609
+ this.selected = newSelection
610
+ this.updateSelectableCurrencies()
611
+
612
+ // Clear any previous errors on successful removal
613
+ this.clearError()
614
+ this.dispatchChangeEvent()
615
+ } catch (error) {
616
+ this.setError({
617
+ type: 'internal',
618
+ message: `Internal error during currency removal: ${error instanceof Error ? error.message : String(error)}`,
619
+ details: { originalError: error },
620
+ })
621
+ }
622
+ }
623
+
624
+ private dispatchChangeEvent() {
625
+ const selectedCurrencies = this.getSelectedCurrencies()
626
+
627
+ // Update form value using ElementInternals
628
+ this.updateFormValue()
629
+
630
+ this.dispatchEvent(
631
+ new CustomEvent('change', {
632
+ detail: {
633
+ currencies: selectedCurrencies,
634
+ codes: this.selected,
635
+ },
636
+ bubbles: false,
637
+ })
638
+ )
639
+ }
640
+
641
+ /**
642
+ * Get the currently selected currency codes as a copy of the array.
643
+ * This is a read-only getter that returns a shallow copy to prevent external modification.
644
+ * @returns Array of selected ISO 4217 currency codes
645
+ */
646
+ get selectedCurrencyCodes(): string[] {
647
+ return [...this.selected]
648
+ }
649
+
650
+ /**
651
+ * Get the full currency objects for currently selected currencies.
652
+ * This method looks up each selected currency code in the master currencies list
653
+ * and returns the complete currency information including name, symbol, country, and flag.
654
+ * @returns Array of complete Currency objects for selected currencies
655
+ */
656
+ getSelectedCurrencies(): Currency[] {
657
+ const result: Currency[] = []
658
+ for (const code of this.selected) {
659
+ const currency = this.currencies.find((c) => c.code === code)
660
+ if (currency) {
661
+ result.push(currency)
662
+ }
663
+ }
664
+ return result
665
+ }
666
+
667
+ /**
668
+ * Clear all selected currencies.
669
+ * This method removes all selections, validates constraints (such as required),
670
+ * updates the UI, and dispatches a change event. If validation fails (e.g.,
671
+ * component is required), the operation will be cancelled and an error will be set.
672
+ * @throws Will dispatch an error event if validation fails or an internal error occurs
673
+ */
674
+ clearSelection() {
675
+ try {
676
+ const newSelection: string[] = []
677
+
678
+ // Validate clearing selection (e.g., required constraint)
679
+ if (!this.validateSelectionConstraints(newSelection)) {
680
+ return // Error was set in validateSelectionConstraints
681
+ }
682
+
683
+ this.selected = newSelection
684
+ this.updateSelectableCurrencies()
685
+
686
+ // Clear any previous errors on successful clear
687
+ this.clearError()
688
+ this.dispatchChangeEvent()
689
+
690
+ // Synchronize the select element to show no selection
691
+ if (this.selectElement) {
692
+ this.selectElement.value = ''
693
+ }
694
+ } catch (error) {
695
+ this.setError({
696
+ type: 'internal',
697
+ message: `Internal error during selection clear: ${error instanceof Error ? error.message : String(error)}`,
698
+ details: { originalError: error },
699
+ })
700
+ } finally {
701
+ this.requestUpdate()
702
+ }
703
+ }
704
+
705
+ /**
706
+ * Form state restore callback for ElementInternals.
707
+ * This method is called by the browser when form state is being restored
708
+ * (e.g., browser back/forward navigation, form autofill).
709
+ * It parses a comma-separated string of currency codes and restores the selection.
710
+ * @param state The state to restore - typically a comma-separated string of currency codes
711
+ * @param _mode The restore mode (unused in this implementation)
712
+ */
713
+ formStateRestoreCallback(state: string | FormData | null): void {
714
+ if (typeof state === 'string' && state) {
715
+ this.selected = state.split(',').filter(Boolean)
716
+ this.updateSelectableCurrencies()
717
+
718
+ // Synchronize the select element with restored state
719
+ if (this.selectElement) {
720
+ if (!this.multi && this.selected.length > 0) {
721
+ this.selectElement.value = this.selected[0]
722
+ } else {
723
+ this.selectElement.value = ''
724
+ }
725
+ }
726
+ }
727
+ }
728
+
729
+ /**
730
+ * Form reset callback for ElementInternals.
731
+ * This method is called by the browser when the containing form is reset.
732
+ * It clears all selections, updates the UI, clears any errors, and updates the form value.
733
+ */
734
+ formResetCallback(): void {
735
+ this.selected = []
736
+ this.updateSelectableCurrencies()
737
+ this.clearError()
738
+ this.updateFormValue()
739
+
740
+ // Synchronize the select element to show no selection
741
+ if (this.selectElement) {
742
+ this.selectElement.value = ''
743
+ }
744
+
745
+ this.requestUpdate()
746
+ }
747
+
748
+ /**
749
+ * Main render method for the component.
750
+ * Renders the currency selector with dropdown, selected chips (if multi-select), and error display.
751
+ * Updates ARIA attributes and error states based on current component state.
752
+ * @returns TemplateResult containing the complete component HTML
753
+ */
754
+ override render(): TemplateResult {
755
+ const ariaLabel = this.multi
756
+ ? `Currency selector. ${this.selected.length} currencies selected.`
757
+ : `Currency selector. ${this.selected.length > 0 ? this.selected[0] + ' selected' : 'No currency selected'}.`
758
+
759
+ const hasError = !this.internals.validity.valid
760
+
761
+ return html`
762
+ <div class="currency-picker" role="group" aria-label="${ariaLabel}">
763
+ <md-outlined-select
764
+ label="${this.label}"
765
+ @change="${this.handleCurrencySelect}"
766
+ menuPositioning="popover"
767
+ ?disabled="${this.disabled}"
768
+ ?required="${this.required}"
769
+ .supportingText="${this.supportingText || ''}"
770
+ .name="${this.name || ''}"
771
+ aria-describedby="${this.supportingText ? 'supporting-text' : ''}"
772
+ aria-invalid="${hasError ? 'true' : 'false'}"
773
+ >
774
+ <md-select-option value="">
775
+ <div slot="headline">Select a currency...</div>
776
+ </md-select-option>
777
+ ${repeat(
778
+ this.selectableCurrencies,
779
+ (currency) => currency.code,
780
+ (currency) => html`
781
+ <md-select-option value="${currency.code}">
782
+ <div slot="overline">${currency.country}</div>
783
+ <div slot="supporting-text">${currency.name}</div>
784
+ <div slot="trailing-supporting-text" class="currency-symbol">${currency.symbol}</div>
785
+ <div slot="start" class="flag">${currency.flag}</div>
786
+ <div slot="headline">${currency.code}</div>
787
+ </md-select-option>
788
+ `
789
+ )}
790
+ </md-outlined-select>
791
+ ${this.renderSelected()}${this.renderError()}
792
+ </div>
793
+ `
794
+ }
795
+
796
+ /**
797
+ * Renders error messages when showErrors is true and the component is invalid.
798
+ * The error display uses ARIA live regions for accessibility.
799
+ * @returns TemplateResult with error display or nothing if no errors should be shown
800
+ */
801
+ protected renderError(): TemplateResult | typeof nothing {
802
+ if (!this.showErrors || this.internals.validity.valid) {
803
+ return nothing
804
+ }
805
+
806
+ return html`
807
+ <div class="error" role="alert" aria-live="polite">
808
+ <span class="error-message">${this.internals.validationMessage}</span>
809
+ </div>
810
+ `
811
+ }
812
+
813
+ /**
814
+ * Renders the selected currencies as removable chips in multi-select mode.
815
+ * Only renders when multi=true and there are selected currencies.
816
+ * Each chip displays the currency flag and code, and can be removed by the user.
817
+ * @returns TemplateResult with chip display or nothing if not applicable
818
+ */
819
+ protected renderSelected(): TemplateResult | typeof nothing {
820
+ if (!this.multi) {
821
+ return nothing
822
+ }
823
+ const selected = this.getSelectedCurrencies()
824
+ if (selected.length === 0) return nothing
825
+
826
+ return html`
827
+ <ui-chip-set>
828
+ ${repeat(
829
+ selected,
830
+ (currency) => currency.code,
831
+ (currency) => html`
832
+ <ui-chip
833
+ data-code="${currency.code}"
834
+ type="input"
835
+ @remove="${this.handleRemoveCurrency}"
836
+ ?removable="${true}"
837
+ >
838
+ <span slot="icon" class="chip-flag">${currency.flag}</span>
839
+ <span class="chip-code">${currency.code}</span>
840
+ </ui-chip>
841
+ `
842
+ )}
843
+ </ui-chip-set>
844
+ `
845
+ }
846
+ }