@statistikzh/leu 0.0.2

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 (141) hide show
  1. package/.editorconfig +29 -0
  2. package/.eslintrc.json +27 -0
  3. package/.github/workflows/publish.yml +19 -0
  4. package/.github/workflows/release-please.yml +19 -0
  5. package/.github/workflows/test.yml +38 -0
  6. package/.husky/commit-msg +4 -0
  7. package/.husky/pre-commit +4 -0
  8. package/.prettierignore +1 -0
  9. package/.storybook/main.js +11 -0
  10. package/.storybook/preview-head.html +5 -0
  11. package/.storybook/preview.js +23 -0
  12. package/CHANGELOG.md +63 -0
  13. package/CODE_OF_CONDUCT.md +128 -0
  14. package/CONTRIBUTING.md +31 -0
  15. package/LICENSE +21 -0
  16. package/README.md +170 -0
  17. package/commitlint.config.cjs +1 -0
  18. package/dist/Button-83c6df93.js +403 -0
  19. package/dist/Checkbox.js +144 -0
  20. package/dist/CheckboxGroup.js +82 -0
  21. package/dist/Chip-60af1402.js +162 -0
  22. package/dist/ChipGroup.js +79 -0
  23. package/dist/ChipLink.js +46 -0
  24. package/dist/ChipRemovable.js +43 -0
  25. package/dist/ChipSelectable.js +92 -0
  26. package/dist/Input.js +686 -0
  27. package/dist/Radio.js +156 -0
  28. package/dist/RadioGroup.js +194 -0
  29. package/dist/Select.js +859 -0
  30. package/dist/Table-72d305d7.js +506 -0
  31. package/dist/Table.js +8 -0
  32. package/dist/defineElement-47d4f665.js +15 -0
  33. package/dist/icon-b68c7e1e.js +202 -0
  34. package/dist/index.js +21 -0
  35. package/dist/leu-checkbox-group.js +6 -0
  36. package/dist/leu-checkbox.js +6 -0
  37. package/dist/leu-chip-group.js +5 -0
  38. package/dist/leu-chip-link.js +6 -0
  39. package/dist/leu-chip-removable.js +7 -0
  40. package/dist/leu-chip-selectable.js +6 -0
  41. package/dist/leu-input.js +9 -0
  42. package/dist/leu-radio-group.js +6 -0
  43. package/dist/leu-radio.js +5 -0
  44. package/dist/leu-select.js +12 -0
  45. package/dist/leu-table.js +10 -0
  46. package/dist/theme.css +51 -0
  47. package/index.js +10 -0
  48. package/package.json +85 -0
  49. package/postcss.config.cjs +14 -0
  50. package/rollup.config.js +54 -0
  51. package/scripts/generate-component/generate.js +167 -0
  52. package/scripts/generate-component/templates/[Name].js +33 -0
  53. package/scripts/generate-component/templates/[name].css +11 -0
  54. package/scripts/generate-component/templates/[namespace]-[name].js +3 -0
  55. package/scripts/generate-component/templates/stories/[name].stories.js +13 -0
  56. package/scripts/generate-component/templates/test/[name].test.js +22 -0
  57. package/src/components/button/Button.js +150 -0
  58. package/src/components/button/button.css +232 -0
  59. package/src/components/button/leu-button.js +3 -0
  60. package/src/components/button/stories/button.stories.js +333 -0
  61. package/src/components/button/test/button.test.js +22 -0
  62. package/src/components/button-group/ButtonGroup.js +63 -0
  63. package/src/components/button-group/button-group.css +10 -0
  64. package/src/components/button-group/leu-button-group.js +3 -0
  65. package/src/components/button-group/stories/button-group.stories.js +41 -0
  66. package/src/components/button-group/test/button-group.test.js +22 -0
  67. package/src/components/checkbox/Checkbox.js +142 -0
  68. package/src/components/checkbox/CheckboxGroup.js +80 -0
  69. package/src/components/checkbox/leu-checkbox-group.js +3 -0
  70. package/src/components/checkbox/leu-checkbox.js +3 -0
  71. package/src/components/checkbox/stories/checkbox-group.stories.js +52 -0
  72. package/src/components/checkbox/stories/checkbox.stories.js +43 -0
  73. package/src/components/checkbox/test/checkbox.test.js +101 -0
  74. package/src/components/chip/Chip.js +24 -0
  75. package/src/components/chip/ChipGroup.js +71 -0
  76. package/src/components/chip/ChipLink.js +45 -0
  77. package/src/components/chip/ChipRemovable.js +42 -0
  78. package/src/components/chip/ChipSelectable.js +91 -0
  79. package/src/components/chip/chip-group.css +5 -0
  80. package/src/components/chip/chip.css +130 -0
  81. package/src/components/chip/exports.js +10 -0
  82. package/src/components/chip/leu-chip-group.js +3 -0
  83. package/src/components/chip/leu-chip-link.js +3 -0
  84. package/src/components/chip/leu-chip-removable.js +3 -0
  85. package/src/components/chip/leu-chip-selectable.js +3 -0
  86. package/src/components/chip/stories/chip-group.stories.js +99 -0
  87. package/src/components/chip/stories/chip-link.stories.js +37 -0
  88. package/src/components/chip/stories/chip-removable.stories.js +28 -0
  89. package/src/components/chip/stories/chip-selectable.stories.js +46 -0
  90. package/src/components/chip/test/chip.test.js +22 -0
  91. package/src/components/dropdown/Dropdown.js +55 -0
  92. package/src/components/dropdown/dropdown.css +17 -0
  93. package/src/components/dropdown/leu-dropdown.js +3 -0
  94. package/src/components/dropdown/stories/dropdown.stories.js +25 -0
  95. package/src/components/dropdown/test/dropdown.test.js +31 -0
  96. package/src/components/icon/icon.js +201 -0
  97. package/src/components/input/Input.js +421 -0
  98. package/src/components/input/input.css +231 -0
  99. package/src/components/input/leu-input.js +3 -0
  100. package/src/components/input/stories/input.stories.js +185 -0
  101. package/src/components/input/test/input.test.js +22 -0
  102. package/src/components/menu/Menu.js +18 -0
  103. package/src/components/menu/MenuItem.js +95 -0
  104. package/src/components/menu/leu-menu-item.js +3 -0
  105. package/src/components/menu/leu-menu.js +3 -0
  106. package/src/components/menu/menu-item.css +72 -0
  107. package/src/components/menu/menu.css +14 -0
  108. package/src/components/menu/stories/menu-item.stories.js +51 -0
  109. package/src/components/menu/stories/menu.stories.js +21 -0
  110. package/src/components/menu/test/menu.test.js +22 -0
  111. package/src/components/pagination/Pagination.js +152 -0
  112. package/src/components/pagination/leu-pagination.js +3 -0
  113. package/src/components/pagination/pagination.css +49 -0
  114. package/src/components/pagination/stories/pagination.stories.js +82 -0
  115. package/src/components/pagination/test/pagination.test.js +22 -0
  116. package/src/components/radio/Radio.js +62 -0
  117. package/src/components/radio/RadioGroup.js +193 -0
  118. package/src/components/radio/leu-radio-group.js +3 -0
  119. package/src/components/radio/leu-radio.js +3 -0
  120. package/src/components/radio/radio.css +76 -0
  121. package/src/components/radio/stories/radio-group.stories.js +49 -0
  122. package/src/components/radio/stories/radio.stories.js +48 -0
  123. package/src/components/radio/test/radio.test.js +38 -0
  124. package/src/components/select/Select.js +350 -0
  125. package/src/components/select/leu-select.js +3 -0
  126. package/src/components/select/select.css +215 -0
  127. package/src/components/select/stories/select.stories.js +302 -0
  128. package/src/components/select/test/select.test.js +29 -0
  129. package/src/components/table/Table.js +301 -0
  130. package/src/components/table/leu-table.js +3 -0
  131. package/src/components/table/stories/table.stories.js +116 -0
  132. package/src/components/table/test/table.test.js +36 -0
  133. package/src/lib/defineElement.js +13 -0
  134. package/src/lib/hasSlotController.js +85 -0
  135. package/src/styles/custom-media.css +5 -0
  136. package/src/styles/custom-properties.css +51 -0
  137. package/src/styles/theme.css +1 -0
  138. package/stat_zh.png +0 -0
  139. package/stylelint.config.mjs +21 -0
  140. package/web-dev-server-storybook.config.mjs +19 -0
  141. package/web-test-runner.config.mjs +49 -0
package/dist/Input.js ADDED
@@ -0,0 +1,686 @@
1
+ import { css, LitElement, nothing, html } from 'lit';
2
+ import { classMap } from 'lit/directives/class-map.js';
3
+ import { ifDefined } from 'lit/directives/if-defined.js';
4
+ import { createRef, ref } from 'lit/directives/ref.js';
5
+ import { I as Icon } from './icon-b68c7e1e.js';
6
+ import { d as defineElement } from './defineElement-47d4f665.js';
7
+
8
+ var css_248z = css`:host,
9
+ :host * {
10
+ box-sizing: border-box;
11
+ }
12
+
13
+ :host {
14
+ --input-color: var(--leu-color-black-100);
15
+ --input-color-disabled: var(--leu-color-black-20);
16
+ --input-color-invalid: var(--leu-color-func-red);
17
+ --input-color-focus: var(--leu-color-func-cyan);
18
+ --input-border-width: 2px;
19
+
20
+ --input-label-color: var(--leu-color-black-100);
21
+ --input-label-color-disabled: var(--input-color-disabled);
22
+ --input-label-color-empty: var(--leu-color-black-60);
23
+
24
+ --input-affix-color: var(--leu-color-black-60);
25
+ --input-affix-color-disabled: var(--input-color-disabled);
26
+
27
+ --input-border-color: var(--leu-color-black-40);
28
+ --input-border-color-focus: var(--input-color-focus);
29
+ --input-border-color-disabled: var(--leu-color-black-20);
30
+ --input-border-color-invalid: var(--input-color-invalid);
31
+
32
+ --input-error-color: var(--leu-color-black-0);
33
+
34
+ --input-clear-color: var(--leu-color-black-60);
35
+
36
+ --input-font-regular: var(--leu-font-regular);
37
+ --input-font-black: var(--leu-font-black);
38
+
39
+ display: block;
40
+ font-family: var(--leu-font-regular);
41
+ font-family: var(--input-font-regular);
42
+ }
43
+
44
+ :host([disabled]) {
45
+ --input-color: var(--input-color-disabled);
46
+ --input-border-color: var(--input-border-color-disabled);
47
+ --input-label-color: var(--input-color-disabled);
48
+ --input-color-focus: var(--input-color-disabled);
49
+ --input-affix-color: var(--input-color-disabled);
50
+ }
51
+
52
+ :host(:focus-within) {
53
+ outline: 2px solid var(--input-color-focus);
54
+ outline-offset: 2px;
55
+ }
56
+
57
+ .input-wrapper {
58
+ position: relative;
59
+ display: flex;
60
+ gap: 0.5rem;
61
+ padding-left: 0.875rem;
62
+ padding-right: 0.875rem;
63
+
64
+ border: var(--input-border-width) solid var(--input-border-color);
65
+ border-radius: 2px;
66
+
67
+ line-height: 1;
68
+ }
69
+
70
+ .input-wrapper:focus-within,
71
+ .input-wrapper:hover {
72
+ --input-border-color: var(--input-border-color-focus);
73
+ }
74
+
75
+ .input-wrapper--invalid:focus-within {
76
+ --input-border-color: var(--input-border-color-invalid);
77
+ border-radius: 2px 2px 0 0;
78
+ }
79
+
80
+ .input-wrapper--invalid,
81
+ .input-wrapper--invalid:hover {
82
+ --input-border-color: var(--input-border-color-invalid);
83
+ border-radius: 2px 2px 0 0;
84
+ }
85
+
86
+ .input {
87
+ -webkit-appearance: none;
88
+ -moz-appearance: none;
89
+ appearance: none;
90
+ display: block;
91
+ width: 100%;
92
+
93
+ font-size: 1rem;
94
+ line-height: 1;
95
+ color: var(--input-color);
96
+
97
+ border: 0;
98
+
99
+ padding-top: 2rem;
100
+
101
+ padding-bottom: 1rem;
102
+ }
103
+
104
+ .input:focus-visible {
105
+ outline: none;
106
+ }
107
+
108
+ :host([size="small"]) .input {
109
+ padding-top: 0.75rem;
110
+ padding-bottom: 0.75rem;
111
+ }
112
+
113
+ .prefix,
114
+ .suffix {
115
+ padding-top: 2rem;
116
+ padding-bottom: 1rem;
117
+
118
+ font-size: 1rem;
119
+ line-height: 1.5;
120
+ color: var(--input-affix-color);
121
+ pointer-events: none;
122
+ }
123
+
124
+ .prefix {
125
+ order: -1;
126
+ }
127
+
128
+ /* Label styles for when the input has focus OR contains a value */
129
+
130
+ .label {
131
+ position: absolute;
132
+ left: 1rem;
133
+
134
+ line-height: 1.5;
135
+ color: var(--input-label-color);
136
+ }
137
+
138
+ /* is size regular AND (has focus OR contains a value) */
139
+
140
+ :host(:not([size])) .label,
141
+ :host([size="regular"]) .label {
142
+ top: calc(0.75rem - var(--input-border-width));
143
+
144
+ font-size: 0.75rem;
145
+ font-family: var(--input-font-black);
146
+
147
+ transition: 0.15s ease-out;
148
+ transition-property: font-size, top;
149
+ }
150
+
151
+ /* is size small AND (has focus OR contains a value) */
152
+
153
+ :host([size="small"]) .label {
154
+ opacity: 0;
155
+ visibility: hidden;
156
+
157
+ font-size: 1rem;
158
+ }
159
+
160
+ :host([required]) .label::after {
161
+ content: "*";
162
+ }
163
+
164
+ /* is empty AND has no focus */
165
+
166
+ :host(:not(:focus-within)) .input-wrapper--empty .label {
167
+ font-family: var(--input-font-regular);
168
+ font-size: 1rem;
169
+ top: calc(1.5rem - var(--input-border-width));
170
+ }
171
+
172
+ /* is not disabled AND has focus AND is empty */
173
+
174
+ :host(:not([disabled]):not(:focus-within)) .input-wrapper--empty .label {
175
+ --input-label-color: var(--input-label-color-empty);
176
+ }
177
+
178
+ /* is size small AND has no focus AND is empty */
179
+
180
+ :host(:not(:focus-within)[size="small"]) .input-wrapper--empty .label {
181
+ top: calc(0.75rem - var(--input-border-width));
182
+ opacity: 1;
183
+ visibility: visible;
184
+ }
185
+
186
+ /* The label has to behave a bit different when the input has a suffix or a prefix */
187
+
188
+ :host([suffix])
189
+ .input-wrapper--empty
190
+ .input:not(:focus)
191
+ + .label {
192
+ top: calc(0.75rem - var(--input-border-width));
193
+
194
+ font-family: var(--input-font-black);
195
+ font-size: 0.75rem;
196
+ }
197
+
198
+ :host([prefix])
199
+ .input-wrapper--empty
200
+ .input:not(:focus)
201
+ + .label {
202
+ top: calc(0.75rem - var(--input-border-width));
203
+
204
+ font-family: var(--input-font-black);
205
+ font-size: 0.75rem;
206
+ }
207
+
208
+ .error {
209
+ list-style: none;
210
+ padding: 0.0625rem 1rem 0.1875rem;
211
+ margin: 0;
212
+
213
+ color: var(--input-error-color);
214
+ font-size: 0.75rem;
215
+ line-height: 1.5;
216
+
217
+ border: 2px solid var(--input-color-invalid);
218
+ border-radius: 0 0 2px 2px;
219
+
220
+ background-color: var(--input-color-invalid);
221
+ }
222
+
223
+ .clear-button {
224
+ --_length: 1.5rem;
225
+
226
+ align-self: center;
227
+
228
+ width: 1.5rem;
229
+
230
+ width: var(--_length);
231
+ height: 1.5rem;
232
+ height: var(--_length);
233
+ padding: 0;
234
+
235
+ cursor: pointer;
236
+
237
+ background: none;
238
+ color: var(--input-clear-color);
239
+ border: none;
240
+
241
+ /* border-radius is only defined for a nice focus outline */
242
+ border-radius: 2px;
243
+ }
244
+
245
+ .clear-button:focus-visible {
246
+ outline: 2px solid var(--input-color-focus);
247
+ outline-offset: 2px;
248
+ }
249
+
250
+ .clear-button:disabled {
251
+ cursor: default;
252
+ color: var(--input-color-disabled);
253
+ }
254
+
255
+ .icon {
256
+ align-self: center;
257
+ color: var(--input-color);
258
+ }
259
+
260
+ :host(:not([disabled]):not(:focus-within)) .input-wrapper--empty .icon {
261
+ color: var(--input-label-color-empty);
262
+ }
263
+
264
+ .error-icon {
265
+ align-self: center;
266
+ color: var(--input-color-invalid);
267
+ }
268
+
269
+ .icon svg, .error-icon svg {
270
+ display: block;
271
+ }
272
+ `;
273
+
274
+ const SIZE_TYPES = {
275
+ SMALL: "small",
276
+ REGULAR: "regular",
277
+ };
278
+
279
+ /**
280
+ * TODO:
281
+ * - Add section to docs about how to mark up suffix and prefix for screenreaders
282
+ * - Handle validation
283
+ * - Infotext attribute or slot?
284
+ */
285
+
286
+ const VALIDATION_MESSAGES = {
287
+ badInput: "Bitte überprüfen Sie das Format.",
288
+ patternMismatch: "Bitte überprüfen Sie das Format.",
289
+ rangeOverflow: (max) => `Der Wert darf nicht grösser als ${max} sein.`,
290
+ rangeUnderflow: (min) => `Der Wert darf nicht kleiner als ${min} sein.`,
291
+ stepMismatch: "Bitte überprüfen Sie das Format.",
292
+ tooLong: (maxlength) =>
293
+ `Die Eingabe muss kürzer als ${maxlength} Zeichen sein.`,
294
+ tooShort: (minlength) =>
295
+ `Die Eingabe muss länger als ${minlength} Zeichen sein.`,
296
+ typeMismatch: "Bitte überprüfen Sie das Format.",
297
+ valueMissing: "Bitte füllen Sie das Feld aus.",
298
+ };
299
+
300
+ /**
301
+ * A text input element.
302
+ *
303
+ * @prop {boolean} disabled - Disables the input element.
304
+ * @prop {boolean} required - Marks the input element as required.
305
+ * @prop {boolean} clearable - Adds a button to clear the input element.
306
+ * @prop {string} value - The value of the input element.
307
+ * @prop {string} name - The name of the input element.
308
+ * @prop {string} label - The label of the input element.
309
+ * @prop {string} error - A custom error that is completely independent of the validity state. Useful for displaying server side errors.
310
+ * @prop {string} size - The size of the input element.
311
+ * @prop {string} icon - The icon that is displayed at the end of the input element.
312
+ * @prop {string} prefix - A prefix that relates to the value of the input (e.g. CHF).
313
+ * @prop {string} suffix - A suffix that relates to the value of the input (e.g. mm).
314
+ * @prop {string} pattern - A regular expression that the value is checked against.
315
+ * @prop {string} type - The type of the input element.
316
+ * @prop {string} min - The minimum value of the input element.
317
+ * @prop {string} max - The maximum value of the input element.
318
+ * @prop {string} minlength - The minimum length of the input element.
319
+ * @prop {string} maxlength - The maximum length of the input element.
320
+ * @prop {object} validationMessages - Custom validation messages. The key is the name of the validity state and the value is the message.
321
+ * @prop {boolean} novalidate - Disables the browser's validation.
322
+ *
323
+ * @fires {CustomEvent} input - Dispatched when the value of the input element changes.
324
+ * @fires {CustomEvent} change - Dispatched when the value of the input element changes and the input element loses focus.
325
+ *
326
+ * @tagname leu-input
327
+ */
328
+ class LeuInput extends LitElement {
329
+ static styles = css_248z
330
+
331
+ static properties = {
332
+ disabled: { type: Boolean, reflect: true },
333
+ required: { type: Boolean, reflect: true },
334
+ clearable: { type: Boolean, reflect: true },
335
+
336
+ value: { type: String },
337
+ name: { type: String },
338
+ error: { type: String },
339
+
340
+ label: { type: String },
341
+ prefix: { type: String },
342
+ suffix: { type: String },
343
+ size: { type: String },
344
+ icon: { type: String },
345
+
346
+ /* Validation attributes */
347
+ pattern: { type: String },
348
+ type: { type: String },
349
+ min: { type: Number },
350
+ max: { type: Number },
351
+ maxlength: { type: Number },
352
+ minlength: { type: Number },
353
+ validationMessages: { type: Object },
354
+ novalidate: { type: Boolean },
355
+
356
+ /** @type {ValidityState} */
357
+ _validity: { state: true },
358
+ }
359
+
360
+ static resolveErrorMessage(message, refernceValue) {
361
+ if (typeof message === "function") {
362
+ return message(refernceValue)
363
+ }
364
+
365
+ return message
366
+ }
367
+
368
+ constructor() {
369
+ super();
370
+
371
+ this.disabled = false;
372
+ this.required = false;
373
+ this.clearable = false;
374
+
375
+ this.value = "";
376
+ this.name = "";
377
+ this.error = "";
378
+
379
+ this.label = "";
380
+ this.prefix = "";
381
+ this.suffix = "";
382
+
383
+ /** @type {keyof typeof SIZE_TYPES} */
384
+ this.size = SIZE_TYPES.REGULAR;
385
+
386
+ this.icon = "";
387
+
388
+ this.type = "text";
389
+ this._validity = null;
390
+ this.validationMessages = {};
391
+ this.novalidate = false;
392
+
393
+ /** @internal */
394
+ this._identifier = "";
395
+
396
+ /** @internal */
397
+ this._clearIcon = Icon("clear");
398
+
399
+ /**
400
+ * @internal
401
+ * @type {import("lit/directives/ref.js").Ref<HTMLInputElement>}
402
+ */
403
+ this._inputRef = createRef();
404
+ }
405
+
406
+ /**
407
+ * Method for handling the click event of the wrapper element.
408
+ * Redirect every click on the wrapper to the input element.
409
+ * This is only necessary for click events because the wrapper element
410
+ * looks like the input element. But the actual input field does not
411
+ * completely fill the wrapper element. Keyboard events don't need to be
412
+ * handled because the actual input element is focusable.
413
+ * @private
414
+ * @param {MouseEvent|PointerEvent} event
415
+ * @returns {void}
416
+ */
417
+ handleWrapperClick(event) {
418
+ if (event.target === event.currentTarget) {
419
+ this._inputRef.value.focus();
420
+ }
421
+ }
422
+
423
+ /**
424
+ * Method for handling the blur event of the input element.
425
+ * Checks validity of the input element and sets the validity state.
426
+ * @private
427
+ * @param {FocusEvent} event
428
+ * @returns {void}
429
+ */
430
+ handleBlur(event) {
431
+ this._validity = null;
432
+
433
+ if (!this.novalidate) {
434
+ event.target.checkValidity();
435
+ }
436
+ }
437
+
438
+ /**
439
+ * Method for handling the invalid event of the input element.
440
+ * Sets the validity state.
441
+ * @private
442
+ * @param {Event} event
443
+ * @returns {void}
444
+ */
445
+ handleInvalid(event) {
446
+ this._validity = event.target.validity;
447
+ }
448
+
449
+ /**
450
+ * Method for handling the change event of the input element.
451
+ * Sets the value property and dispatches a change event so that
452
+ * the event can be handled outside the shadow DOM.
453
+ * @private
454
+ * @param {Event} event
455
+ * @fires {CustomEvent} change
456
+ * @returns {void}
457
+ */
458
+ handleChange(event) {
459
+ this.value = event.target.value;
460
+
461
+ const customEvent = new CustomEvent(event.type, event);
462
+ this.dispatchEvent(customEvent);
463
+ }
464
+
465
+ /**
466
+ * Method for handling the input event of the input element.
467
+ * Sets the value property and dispatches an input event so that
468
+ * the event can be handled outside the shadow DOM.
469
+ * @private
470
+ * @param {Event} event
471
+ * @returns {void}
472
+ */
473
+ handleInput(event) {
474
+ this.value = event.target.value;
475
+ }
476
+
477
+ /**
478
+ * Method for clearing the input element.
479
+ * Sets the value property to an empty string and dispatches
480
+ * an input and a change event.
481
+ * @private
482
+ * @returns {void}
483
+ * @fires {CustomEvent} input
484
+ * @fires {CustomEvent} change
485
+ */
486
+ clear() {
487
+ this.value = "";
488
+
489
+ this.dispatchEvent(
490
+ new CustomEvent("input", { bubbles: true, composed: true })
491
+ );
492
+ this.dispatchEvent(
493
+ new CustomEvent("change", { bubbles: true, composed: true })
494
+ );
495
+ }
496
+
497
+ /**
498
+ * Method for getting the id of the input element.
499
+ * If the id attribute is set, the value of the id attribute is returned.
500
+ * Otherwise a random id is generated and returned.
501
+ *
502
+ * @private
503
+ * @returns {string} id
504
+ */
505
+ getId() {
506
+ const id = this.getAttribute("id");
507
+
508
+ if (id !== null && id !== "") {
509
+ return id
510
+ }
511
+
512
+ if (this._identifier !== "") {
513
+ return this._identifier
514
+ }
515
+
516
+ this._identifier = crypto.randomUUID();
517
+ return this._identifier
518
+ }
519
+
520
+ /**
521
+ * Merge custom and default validation messages.
522
+ * A validation message can be a function or a string.
523
+ * If it s a function, the function is called with the corresponding
524
+ * attribute value as argument.
525
+ * e.g.
526
+ * `tooLong(this.maxlength)`
527
+ * This way the framework user can create reasonable validation messages
528
+ *
529
+ * @returns {Object} validationMessages
530
+ */
531
+ getValidationMessages() {
532
+ const validationMessages = {
533
+ ...VALIDATION_MESSAGES,
534
+ ...this.validationMessages,
535
+ };
536
+
537
+ const { tooLong, tooShort, rangeOverflow, rangeUnderflow } =
538
+ validationMessages;
539
+
540
+ validationMessages.tooLong = LeuInput.resolveErrorMessage(
541
+ tooLong,
542
+ this.maxlength
543
+ );
544
+ validationMessages.tooShort = LeuInput.resolveErrorMessage(
545
+ tooShort,
546
+ this.minlength
547
+ );
548
+ validationMessages.rangeOverflow = LeuInput.resolveErrorMessage(
549
+ rangeOverflow,
550
+ this.max
551
+ );
552
+ validationMessages.rangeUnderflow = LeuInput.resolveErrorMessage(
553
+ rangeUnderflow,
554
+ this.min
555
+ );
556
+
557
+ return validationMessages
558
+ }
559
+
560
+ /**
561
+ * Creates an error list with an item for the given validity state.
562
+ * @param {ValidityState} validityState
563
+ * @param {Object} validationMessages
564
+ * @param {String} idRef
565
+ * @returns
566
+ */
567
+ renderErrorMessages() {
568
+ if (!this.isInvalid()) {
569
+ return nothing
570
+ }
571
+
572
+ const validationMessages = this.getValidationMessages();
573
+ let errorMessages = this._validity
574
+ ? Object.entries(validationMessages)
575
+ .filter(([property]) => this._validity[property])
576
+ .map(([_, message]) => message)
577
+ : [];
578
+
579
+ if (this.error !== "") {
580
+ errorMessages = [this.error, errorMessages];
581
+ }
582
+
583
+ return html`
584
+ <ul class="error" aria-errormessage=${`input-${this.getId()}`}>
585
+ ${errorMessages.map(
586
+ (message) => html`<li class="error-message">${message}</li>`
587
+ )}
588
+ </ul>
589
+ `
590
+ }
591
+
592
+ /**
593
+ * Determines the content that is displayed after the input element.
594
+ * This can be either an icon, a clear button or an error indicator icon.
595
+ *
596
+ * @private
597
+ * @returns {TemplateResult}
598
+ */
599
+ renderAfterContent() {
600
+ if (this.isInvalid()) {
601
+ return html`<div class="error-icon">${Icon("caution")}</div>`
602
+ }
603
+
604
+ if (this.clearable && this.value !== "") {
605
+ return html`<button
606
+ class="clear-button"
607
+ @click=${this.clear}
608
+ aria-label="Eingabefeld zurücksetzen"
609
+ ?disabled=${this.disabled}
610
+ >
611
+ ${this._clearIcon}
612
+ </button>`
613
+ }
614
+
615
+ if (this.icon !== "") {
616
+ return html`<div class="icon">${Icon(this.icon)}</div>`
617
+ }
618
+
619
+ return nothing
620
+ }
621
+
622
+ isInvalid() {
623
+ if (this.error !== "") {
624
+ return true
625
+ }
626
+
627
+ return this._validity === null || this.novalidate
628
+ ? false
629
+ : !this._validity.valid
630
+ }
631
+
632
+ render() {
633
+ const isInvalid = this.isInvalid();
634
+
635
+ const inputWrapperClasses = {
636
+ "input-wrapper": true,
637
+ "input-wrapper--empty": this.value === "",
638
+ "input-wrapper--invalid": isInvalid,
639
+ };
640
+
641
+ /* See the description of the handleWrapperClick method on why this rule is disabled */
642
+ /* eslint-disable lit-a11y/click-events-have-key-events */
643
+ return html`
644
+ <div
645
+ @click=${this.handleWrapperClick}
646
+ class=${classMap(inputWrapperClasses)}
647
+ >
648
+ <input
649
+ class="input"
650
+ id="input-${this.getId()}"
651
+ type=${this.type}
652
+ name=${this.name}
653
+ @change=${this.handleChange}
654
+ @blur=${this.handleBlur}
655
+ @input=${this.handleInput}
656
+ @invalid=${this.handleInvalid}
657
+ ?disabled=${this.disabled}
658
+ ?required=${this.required}
659
+ pattern=${ifDefined(this.pattern)}
660
+ min=${ifDefined(this.min)}
661
+ max=${ifDefined(this.max)}
662
+ maxlength=${ifDefined(this.maxlength)}
663
+ minlength=${ifDefined(this.minlength)}
664
+ .value=${this.value}
665
+ ref=${ref(this._inputRef)}
666
+ aria-invalid=${isInvalid}
667
+ />
668
+ <label for="input-${this.getId()}" class="label"><slot></slot></label>
669
+ ${this.prefix !== ""
670
+ ? html`<div class="prefix" .aria-hidden=${true}>${this.prefix}</div>`
671
+ : nothing}
672
+ ${this.suffix !== ""
673
+ ? html`<div class="suffix" .aria-hidden=${true}>${this.suffix}</div>`
674
+ : nothing}
675
+ ${this.renderAfterContent()}
676
+ </div>
677
+ ${this.renderErrorMessages()}
678
+ `
679
+ }
680
+ }
681
+
682
+ function defineInputElements() {
683
+ defineElement("input", LeuInput);
684
+ }
685
+
686
+ export { LeuInput, SIZE_TYPES, defineInputElements };