tyrell-components 1.0.0-TC52 → 1.0.0-TC53
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/css/tyrell-theme.css +25 -2
- package/css/tyrell.css +4 -0
- package/dist/tyrell-colors-static.css +1 -316
- package/dist/tyrell-theme.css +1 -1443
- package/dist/tyrell.css +1 -1390
- package/dist/tyrell.js +1 -1
- package/lib/base/ty-component.js +196 -196
- package/lib/components/button.d.ts +1 -1
- package/lib/components/button.js +260 -259
- package/lib/components/calendar-month.d.ts +2 -2
- package/lib/components/calendar-month.js +426 -416
- package/lib/components/calendar-navigation.js +238 -238
- package/lib/components/calendar.js +605 -602
- package/lib/components/checkbox.d.ts +1 -1
- package/lib/components/checkbox.js +273 -272
- package/lib/components/copy.d.ts +1 -1
- package/lib/components/copy.js +279 -278
- package/lib/components/date-picker.d.ts +1 -1
- package/lib/components/date-picker.js +1103 -1108
- package/lib/components/file-upload.js +404 -404
- package/lib/components/icon.js +141 -141
- package/lib/components/input.d.ts +1 -1
- package/lib/components/input.js +593 -596
- package/lib/components/modal.js +319 -319
- package/lib/components/option.js +167 -167
- package/lib/components/popup.js +233 -233
- package/lib/components/radio.d.ts +2 -2
- package/lib/components/radio.js +407 -406
- package/lib/components/resize-observer.js +67 -67
- package/lib/components/scroll-container.js +294 -294
- package/lib/components/select.d.ts +1 -1
- package/lib/components/select.js +1524 -1532
- package/lib/components/selected-tags.js +184 -184
- package/lib/components/step.js +62 -62
- package/lib/components/switch.d.ts +1 -1
- package/lib/components/switch.js +214 -213
- package/lib/components/tab.js +62 -62
- package/lib/components/tabs.js +507 -507
- package/lib/components/tag.d.ts +1 -1
- package/lib/components/tag.js +236 -235
- package/lib/components/textarea.d.ts +1 -1
- package/lib/components/textarea.js +457 -456
- package/lib/components/tooltip.js +283 -283
- package/lib/components/wizard.js +384 -384
- package/lib/icons/heroicons/micro.js +392 -392
- package/lib/icons/heroicons/mini.js +382 -382
- package/lib/icons/heroicons/outline.js +337 -337
- package/lib/icons/heroicons/solid.js +407 -407
- package/lib/icons/lucide.js +22843 -22843
- package/lib/icons/material/filled.js +9653 -9653
- package/lib/icons/material/outlined.js +10271 -10271
- package/lib/icons/material/round.js +10495 -10495
- package/lib/icons/material/sharp.js +9905 -9905
- package/lib/icons/material/two-tone.js +12792 -12792
- package/lib/index.js +21 -21
- package/lib/styles/button.js +176 -288
- package/lib/styles/calendar-month.js +123 -183
- package/lib/styles/calendar-navigation.js +60 -79
- package/lib/styles/calendar.js +8 -9
- package/lib/styles/checkbox.js +45 -72
- package/lib/styles/copy.js +67 -91
- package/lib/styles/custom-scrollbar.js +80 -100
- package/lib/styles/date-picker.js +261 -331
- package/lib/styles/field-size.d.ts +6 -0
- package/lib/styles/field-size.js +91 -0
- package/lib/styles/file-upload.js +139 -169
- package/lib/styles/icon.js +92 -156
- package/lib/styles/input.js +125 -241
- package/lib/styles/modal.js +67 -102
- package/lib/styles/option.d.ts +1 -1
- package/lib/styles/option.js +47 -67
- package/lib/styles/popup.js +34 -65
- package/lib/styles/radio.js +69 -96
- package/lib/styles/resize-observer.js +2 -5
- package/lib/styles/scroll-container.js +100 -130
- package/lib/styles/select-base.js +525 -778
- package/lib/styles/select.js +140 -228
- package/lib/styles/step.js +22 -29
- package/lib/styles/switch.js +34 -55
- package/lib/styles/tab.js +22 -29
- package/lib/styles/tabs.js +171 -252
- package/lib/styles/tag.js +135 -188
- package/lib/styles/textarea.d.ts +1 -1
- package/lib/styles/textarea.js +205 -271
- package/lib/styles/tooltip.js +3 -3
- package/lib/styles/wizard.js +170 -226
- package/lib/types/common.d.ts +2 -0
- package/lib/types/common.js +6 -5
- package/lib/utils/calendar-utils.js +100 -100
- package/lib/utils/custom-scrollbar.js +272 -272
- package/lib/utils/flavor-sheet.js +14 -14
- package/lib/utils/icon-registry.js +127 -127
- package/lib/utils/loader-registry.js +3 -3
- package/lib/utils/locale.js +34 -34
- package/lib/utils/mobile.js +1 -1
- package/lib/utils/number-format.js +54 -54
- package/lib/utils/positioning.js +212 -212
- package/lib/utils/property-capture.js +36 -36
- package/lib/utils/property-manager.js +125 -125
- package/lib/utils/resize-observer.js +23 -23
- package/lib/utils/scroll-lock.js +57 -57
- package/lib/utils/styles.js +17 -17
- package/lib/version.d.ts +1 -1
- package/lib/version.js +1 -1
- package/package.json +4 -4
|
@@ -6,1149 +6,1144 @@ import { computeAnchoredPosition, placementToAnchored } from '../utils/positioni
|
|
|
6
6
|
import { getEffectiveLocale, observeLocaleChanges } from '../utils/locale.js';
|
|
7
7
|
import { isMobileTouch } from '../utils/mobile.js';
|
|
8
8
|
import { TyComponent } from '../base/ty-component.js';
|
|
9
|
+
import { DEFAULT_SIZE } from '../types/common.js';
|
|
9
10
|
const CALENDAR_ICON_SVG = `<svg stroke='currentColor' fill='none' stroke-width='2' viewBox='0 0 24 24' width='16' height='16' xmlns='http://www.w3.org/2000/svg'><rect x='3' y='4' width='18' height='18' rx='2' ry='2'></rect><line x1='16' y1='2' x2='16' y2='6'></line><line x1='8' y1='2' x2='8' y2='6'></line><line x1='3' y1='10' x2='21' y2='10'></line></svg>`;
|
|
10
11
|
const CLEAR_ICON_SVG = `<svg stroke='currentColor' fill='none' stroke-width='2' viewBox='0 0 24 24' width='14' height='14' xmlns='http://www.w3.org/2000/svg'><line x1='18' y1='6' x2='6' y2='18'></line><line x1='6' y1='6' x2='18' y2='18'></line></svg>`;
|
|
11
12
|
const SCHEDULE_ICON_SVG = `<svg stroke='currentColor' fill='none' stroke-width='2' viewBox='0 0 24 24' width='16' height='16' xmlns='http://www.w3.org/2000/svg'><circle cx='12' cy='12' r='10'></circle><polyline points='12,6 12,12 16,14'></polyline></svg>`;
|
|
12
13
|
const elementIds = new WeakMap();
|
|
13
14
|
let elementIdCounter = 0;
|
|
14
15
|
function getElementHash(element) {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
16
|
+
let id = elementIds.get(element);
|
|
17
|
+
if (id === undefined) {
|
|
18
|
+
id = ++elementIdCounter;
|
|
19
|
+
elementIds.set(element, id);
|
|
20
|
+
}
|
|
21
|
+
return id;
|
|
21
22
|
}
|
|
22
23
|
function parseValue(value, withTime) {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
24
|
+
if (!value)
|
|
25
|
+
return null;
|
|
26
|
+
let dateObj = null;
|
|
27
|
+
if (typeof value === 'string') {
|
|
28
|
+
if (/^\d{4}-\d{2}-\d{2}$/.test(value)) {
|
|
29
|
+
const [yearStr, monthStr, dayStr] = value.split('-');
|
|
30
|
+
const year = parseInt(yearStr, 10);
|
|
31
|
+
const month = parseInt(monthStr, 10) - 1;
|
|
32
|
+
const day = parseInt(dayStr, 10);
|
|
33
|
+
dateObj = new Date(year, month, day, 0, 0, 0, 0);
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
dateObj = new Date(value);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
else if (typeof value === 'number') {
|
|
40
|
+
dateObj = new Date(value);
|
|
41
|
+
}
|
|
42
|
+
if (!dateObj || isNaN(dateObj.getTime())) {
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
const components = {
|
|
46
|
+
year: dateObj.getFullYear(),
|
|
47
|
+
month: dateObj.getMonth() + 1,
|
|
48
|
+
day: dateObj.getDate(),
|
|
49
|
+
hour: withTime ? dateObj.getHours() : 0,
|
|
50
|
+
minute: withTime ? dateObj.getMinutes() : 0,
|
|
51
|
+
};
|
|
52
|
+
return components;
|
|
52
53
|
}
|
|
53
54
|
function componentsToDateObject(components) {
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
55
|
+
const { year, month, day, hour, minute } = components;
|
|
56
|
+
if (!year || !month || !day)
|
|
57
|
+
return null;
|
|
58
|
+
return new Date(year, month - 1, day, hour || 0, minute || 0, 0, 0);
|
|
58
59
|
}
|
|
59
60
|
function componentsToOutputValue(components) {
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
61
|
+
if (!components.year || !components.month || !components.day) {
|
|
62
|
+
return null;
|
|
63
|
+
}
|
|
64
|
+
const dateObj = componentsToDateObject(components);
|
|
65
|
+
return dateObj ? dateObj.toISOString() : null;
|
|
65
66
|
}
|
|
66
67
|
function componentsToLocalValue(components, withTime) {
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
68
|
+
if (!components.year || !components.month || !components.day) {
|
|
69
|
+
return null;
|
|
70
|
+
}
|
|
71
|
+
const year = components.year.toString().padStart(4, '0');
|
|
72
|
+
const month = components.month.toString().padStart(2, '0');
|
|
73
|
+
const day = components.day.toString().padStart(2, '0');
|
|
74
|
+
if (withTime) {
|
|
75
|
+
const hour = (components.hour || 0).toString().padStart(2, '0');
|
|
76
|
+
const minute = (components.minute || 0).toString().padStart(2, '0');
|
|
77
|
+
return `${year}-${month}-${day}T${hour}:${minute}`;
|
|
78
|
+
}
|
|
79
|
+
else {
|
|
80
|
+
return `${year}-${month}-${day}`;
|
|
81
|
+
}
|
|
81
82
|
}
|
|
82
83
|
function formatDisplayValue(components, formatType, locale, withTime) {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
84
|
+
if (!components.year || !components.month || !components.day) {
|
|
85
|
+
return null;
|
|
86
|
+
}
|
|
87
|
+
const dateObj = componentsToDateObject(components);
|
|
88
|
+
if (!dateObj)
|
|
89
|
+
return null;
|
|
90
|
+
const options = {
|
|
91
|
+
dateStyle: formatType || 'long',
|
|
92
|
+
};
|
|
93
|
+
if (withTime) {
|
|
94
|
+
options.timeStyle = 'short';
|
|
95
|
+
}
|
|
96
|
+
const formatter = new Intl.DateTimeFormat(locale, options);
|
|
97
|
+
return formatter.format(dateObj);
|
|
97
98
|
}
|
|
98
99
|
function parseTimeComponents(rawDigits) {
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
100
|
+
if (!rawDigits || rawDigits.length !== 4)
|
|
101
|
+
return null;
|
|
102
|
+
const hourStr = rawDigits.substring(0, 2);
|
|
103
|
+
const minuteStr = rawDigits.substring(2, 4);
|
|
104
|
+
const hour = parseInt(hourStr, 10);
|
|
105
|
+
const minute = parseInt(minuteStr, 10);
|
|
106
|
+
if (hour < 0 || hour > 23 || minute < 0 || minute > 59) {
|
|
107
|
+
return null;
|
|
108
|
+
}
|
|
109
|
+
return { hour, minute };
|
|
109
110
|
}
|
|
110
111
|
function validateTimeDigit(digit, position, currentDigits) {
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
112
|
+
switch (position) {
|
|
113
|
+
case 0:
|
|
114
|
+
return digit <= 2;
|
|
115
|
+
case 1: {
|
|
116
|
+
const firstHour = parseInt(currentDigits[0], 10);
|
|
117
|
+
return firstHour === 2 ? digit <= 3 : true;
|
|
118
|
+
}
|
|
119
|
+
case 3:
|
|
120
|
+
return digit <= 5;
|
|
121
|
+
case 4:
|
|
122
|
+
return true;
|
|
123
|
+
default:
|
|
124
|
+
return false;
|
|
125
|
+
}
|
|
125
126
|
}
|
|
126
127
|
function formatTimeDisplay(hour, minute) {
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
128
|
+
const hourStr = hour.toString().padStart(2, '0');
|
|
129
|
+
const minuteStr = minute.toString().padStart(2, '0');
|
|
130
|
+
return `${hourStr}:${minuteStr}`;
|
|
130
131
|
}
|
|
131
132
|
function findNextEditablePosition(currentPos) {
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
133
|
+
switch (currentPos) {
|
|
134
|
+
case 0: return 1;
|
|
135
|
+
case 1: return 3;
|
|
136
|
+
case 3: return 4;
|
|
137
|
+
case 4: return 5;
|
|
138
|
+
case 5: return 5;
|
|
139
|
+
default: return currentPos;
|
|
140
|
+
}
|
|
140
141
|
}
|
|
141
142
|
function findPrevEditablePosition(currentPos) {
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
143
|
+
switch (currentPos) {
|
|
144
|
+
case 5: return 4;
|
|
145
|
+
case 4: return 3;
|
|
146
|
+
case 3: return 1;
|
|
147
|
+
case 1: return 0;
|
|
148
|
+
case 0: return 0;
|
|
149
|
+
default: return currentPos;
|
|
150
|
+
}
|
|
150
151
|
}
|
|
151
152
|
function positionToRawDigitIndex(internalPos) {
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
153
|
+
switch (internalPos) {
|
|
154
|
+
case 0: return 0;
|
|
155
|
+
case 1: return 1;
|
|
156
|
+
case 3: return 2;
|
|
157
|
+
case 4: return 3;
|
|
158
|
+
default: return 0;
|
|
159
|
+
}
|
|
159
160
|
}
|
|
160
161
|
class TimeInput {
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
162
|
+
constructor(element, datePickerElement, hour = 0, minute = 0) {
|
|
163
|
+
this.element = element;
|
|
164
|
+
this.datePickerElement = datePickerElement;
|
|
165
|
+
const display = formatTimeDisplay(hour, minute);
|
|
166
|
+
const rawDigits = hour.toString().padStart(2, '0') + minute.toString().padStart(2, '0');
|
|
167
|
+
this.state = {
|
|
168
|
+
hour,
|
|
169
|
+
minute,
|
|
170
|
+
caretPosition: 0,
|
|
171
|
+
displayValue: display,
|
|
172
|
+
rawDigits,
|
|
173
|
+
};
|
|
174
|
+
this.element.value = display;
|
|
175
|
+
this.setupEventListeners();
|
|
176
|
+
}
|
|
177
|
+
setupEventListeners() {
|
|
178
|
+
this.element.addEventListener('keydown', (e) => this.handleKeyDown(e));
|
|
179
|
+
this.element.addEventListener('input', (e) => this.handleInput(e));
|
|
180
|
+
this.element.addEventListener('click', () => this.handleClick());
|
|
181
|
+
this.element.addEventListener('focus', () => this.handleFocus());
|
|
182
|
+
}
|
|
183
|
+
handleKeyDown(event) {
|
|
184
|
+
const key = event.key;
|
|
185
|
+
switch (key) {
|
|
186
|
+
case 'ArrowRight':
|
|
187
|
+
this.handleArrowRight(event);
|
|
188
|
+
break;
|
|
189
|
+
case 'ArrowLeft':
|
|
190
|
+
this.handleArrowLeft(event);
|
|
191
|
+
break;
|
|
192
|
+
case 'Backspace':
|
|
193
|
+
this.handleBackspace(event);
|
|
194
|
+
break;
|
|
195
|
+
case 'Delete':
|
|
196
|
+
this.handleDelete(event);
|
|
197
|
+
break;
|
|
198
|
+
case 'Home':
|
|
199
|
+
event.preventDefault();
|
|
200
|
+
this.updateState({ caretPosition: 0 });
|
|
201
|
+
break;
|
|
202
|
+
case 'End':
|
|
203
|
+
event.preventDefault();
|
|
204
|
+
this.updateState({ caretPosition: 5 });
|
|
205
|
+
break;
|
|
206
|
+
case 'Tab':
|
|
207
|
+
break;
|
|
208
|
+
default:
|
|
209
|
+
if (/^\d$/.test(key)) {
|
|
210
|
+
this.handleDigitInput(event, parseInt(key, 10));
|
|
211
|
+
}
|
|
212
|
+
break;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
handleInput(event) {
|
|
216
|
+
event.preventDefault();
|
|
217
|
+
}
|
|
218
|
+
handleClick() {
|
|
219
|
+
this.updateState({ caretPosition: 0 });
|
|
220
|
+
}
|
|
221
|
+
handleFocus() {
|
|
222
|
+
this.updateState({ caretPosition: 0 });
|
|
223
|
+
}
|
|
224
|
+
handleArrowRight(event) {
|
|
225
|
+
event.preventDefault();
|
|
226
|
+
const nextPos = findNextEditablePosition(this.state.caretPosition);
|
|
227
|
+
this.updateState({ caretPosition: nextPos });
|
|
228
|
+
}
|
|
229
|
+
handleArrowLeft(event) {
|
|
230
|
+
event.preventDefault();
|
|
231
|
+
const prevPos = findPrevEditablePosition(this.state.caretPosition);
|
|
232
|
+
this.updateState({ caretPosition: prevPos });
|
|
233
|
+
}
|
|
234
|
+
handleDigitInput(event, digit) {
|
|
235
|
+
event.preventDefault();
|
|
236
|
+
const currentPos = this.state.caretPosition;
|
|
237
|
+
if (![0, 1, 3, 4].includes(currentPos))
|
|
238
|
+
return;
|
|
239
|
+
if (!validateTimeDigit(digit, currentPos, this.state.rawDigits))
|
|
240
|
+
return;
|
|
241
|
+
const newState = this.replaceDigitAtPosition(currentPos, digit);
|
|
242
|
+
if (!newState)
|
|
243
|
+
return;
|
|
244
|
+
const nextPos = findNextEditablePosition(currentPos);
|
|
245
|
+
this.updateState({ ...newState, caretPosition: nextPos });
|
|
246
|
+
this.notifyTimeChange();
|
|
247
|
+
}
|
|
248
|
+
handleBackspace(event) {
|
|
249
|
+
event.preventDefault();
|
|
250
|
+
const currentPos = this.state.caretPosition;
|
|
251
|
+
if (currentPos === 0)
|
|
252
|
+
return;
|
|
253
|
+
const targetPos = currentPos === 1 ? 0 :
|
|
254
|
+
currentPos === 3 ? 1 :
|
|
255
|
+
currentPos === 4 ? 3 :
|
|
256
|
+
currentPos === 5 ? 4 : 0;
|
|
257
|
+
const newState = this.zeroDigitAtPosition(targetPos);
|
|
258
|
+
if (!newState)
|
|
259
|
+
return;
|
|
260
|
+
this.updateState({ ...newState, caretPosition: targetPos });
|
|
261
|
+
this.notifyTimeChange();
|
|
262
|
+
}
|
|
263
|
+
handleDelete(event) {
|
|
264
|
+
event.preventDefault();
|
|
265
|
+
const currentPos = this.state.caretPosition;
|
|
266
|
+
if (![0, 1, 3, 4].includes(currentPos))
|
|
267
|
+
return;
|
|
268
|
+
const newState = this.zeroDigitAtPosition(currentPos);
|
|
269
|
+
if (!newState)
|
|
270
|
+
return;
|
|
271
|
+
this.updateState({ ...newState, caretPosition: currentPos });
|
|
272
|
+
this.notifyTimeChange();
|
|
273
|
+
}
|
|
274
|
+
replaceDigitAtPosition(position, newDigit) {
|
|
275
|
+
const rawIndex = positionToRawDigitIndex(position);
|
|
276
|
+
const digitsArray = this.state.rawDigits.split('');
|
|
277
|
+
digitsArray[rawIndex] = newDigit.toString();
|
|
278
|
+
const newDigits = digitsArray.join('');
|
|
279
|
+
const parsed = parseTimeComponents(newDigits);
|
|
280
|
+
if (!parsed)
|
|
281
|
+
return null;
|
|
282
|
+
return {
|
|
283
|
+
rawDigits: newDigits,
|
|
284
|
+
hour: parsed.hour,
|
|
285
|
+
minute: parsed.minute,
|
|
286
|
+
displayValue: formatTimeDisplay(parsed.hour, parsed.minute),
|
|
287
|
+
};
|
|
288
|
+
}
|
|
289
|
+
zeroDigitAtPosition(position) {
|
|
290
|
+
return this.replaceDigitAtPosition(position, 0);
|
|
291
|
+
}
|
|
292
|
+
updateState(updates) {
|
|
293
|
+
this.state = { ...this.state, ...updates };
|
|
294
|
+
this.element.value = this.state.displayValue;
|
|
295
|
+
const caretPos = this.state.caretPosition;
|
|
296
|
+
const actualPos = caretPos === 0 ? 0 :
|
|
297
|
+
caretPos === 1 ? 1 :
|
|
298
|
+
caretPos === 2 ? 3 :
|
|
299
|
+
caretPos === 3 ? 3 :
|
|
300
|
+
caretPos === 4 ? 4 :
|
|
301
|
+
caretPos === 5 ? 5 : caretPos;
|
|
302
|
+
this.element.setSelectionRange(actualPos, actualPos);
|
|
303
|
+
}
|
|
304
|
+
notifyTimeChange() {
|
|
305
|
+
this.datePickerElement.handleTimeInputChange(this.state.hour, this.state.minute);
|
|
306
|
+
}
|
|
307
|
+
getTime() {
|
|
308
|
+
return {
|
|
309
|
+
hour: this.state.hour,
|
|
310
|
+
minute: this.state.minute,
|
|
311
|
+
};
|
|
312
|
+
}
|
|
313
|
+
setTime(hour, minute) {
|
|
314
|
+
const display = formatTimeDisplay(hour, minute);
|
|
315
|
+
const rawDigits = hour.toString().padStart(2, '0') + minute.toString().padStart(2, '0');
|
|
316
|
+
this.state = {
|
|
317
|
+
...this.state,
|
|
318
|
+
hour,
|
|
319
|
+
minute,
|
|
320
|
+
displayValue: display,
|
|
321
|
+
rawDigits,
|
|
322
|
+
};
|
|
323
|
+
this.element.value = display;
|
|
324
|
+
}
|
|
324
325
|
}
|
|
325
326
|
export class TyDatePicker extends TyComponent {
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
this._timeInput = undefined;
|
|
1046
|
-
}
|
|
327
|
+
constructor() {
|
|
328
|
+
super();
|
|
329
|
+
this._state = {
|
|
330
|
+
withTime: false,
|
|
331
|
+
open: false,
|
|
332
|
+
};
|
|
333
|
+
this._closeTimestamp = 0;
|
|
334
|
+
this._customFlavorSheet = null;
|
|
335
|
+
const shadow = this.shadowRoot;
|
|
336
|
+
ensureStyles(shadow, { css: datePickerStyles, id: 'ty-date-picker' });
|
|
337
|
+
}
|
|
338
|
+
onConnect() {
|
|
339
|
+
this.initializeState();
|
|
340
|
+
this.render();
|
|
341
|
+
this._localeObserver = observeLocaleChanges(this, () => {
|
|
342
|
+
this.render();
|
|
343
|
+
});
|
|
344
|
+
this._syncCustomFlavor();
|
|
345
|
+
}
|
|
346
|
+
_syncCustomFlavor() {
|
|
347
|
+
const flavor = this.flavor === 'default' ? 'neutral' : this.flavor;
|
|
348
|
+
this._customFlavorSheet = syncCustomFlavorSheet(this.shadowRoot, this._customFlavorSheet, flavor, ({ base }) => datePickerCustomFlavorCss(base));
|
|
349
|
+
}
|
|
350
|
+
onDisconnect() {
|
|
351
|
+
if (this._localeObserver) {
|
|
352
|
+
this._localeObserver();
|
|
353
|
+
this._localeObserver = undefined;
|
|
354
|
+
}
|
|
355
|
+
this.cleanup();
|
|
356
|
+
}
|
|
357
|
+
onPropertiesChanged(changes) {
|
|
358
|
+
for (const { name, newValue } of changes) {
|
|
359
|
+
switch (name) {
|
|
360
|
+
case 'flavor':
|
|
361
|
+
this._syncCustomFlavor();
|
|
362
|
+
break;
|
|
363
|
+
case 'size':
|
|
364
|
+
case 'label':
|
|
365
|
+
case 'placeholder':
|
|
366
|
+
case 'required':
|
|
367
|
+
case 'disabled':
|
|
368
|
+
case 'clearable':
|
|
369
|
+
case 'format':
|
|
370
|
+
case 'locale':
|
|
371
|
+
break;
|
|
372
|
+
case 'value': {
|
|
373
|
+
const newComponents = parseValue(newValue, this._state.withTime);
|
|
374
|
+
if (newComponents === null) {
|
|
375
|
+
const hasDate = this._state.year !== undefined ||
|
|
376
|
+
this._state.month !== undefined ||
|
|
377
|
+
this._state.day !== undefined;
|
|
378
|
+
if (hasDate) {
|
|
379
|
+
this._state = {
|
|
380
|
+
withTime: this._state.withTime,
|
|
381
|
+
open: this._state.open,
|
|
382
|
+
};
|
|
383
|
+
}
|
|
384
|
+
break;
|
|
385
|
+
}
|
|
386
|
+
const currentComponents = {
|
|
387
|
+
year: this._state.year,
|
|
388
|
+
month: this._state.month,
|
|
389
|
+
day: this._state.day,
|
|
390
|
+
hour: this._state.hour,
|
|
391
|
+
minute: this._state.minute,
|
|
392
|
+
};
|
|
393
|
+
const changed = newComponents?.year !== currentComponents.year ||
|
|
394
|
+
newComponents?.month !== currentComponents.month ||
|
|
395
|
+
newComponents?.day !== currentComponents.day ||
|
|
396
|
+
newComponents?.hour !== currentComponents.hour ||
|
|
397
|
+
newComponents?.minute !== currentComponents.minute;
|
|
398
|
+
if (changed) {
|
|
399
|
+
this._state = {
|
|
400
|
+
...this._state,
|
|
401
|
+
...newComponents,
|
|
402
|
+
};
|
|
403
|
+
}
|
|
404
|
+
break;
|
|
405
|
+
}
|
|
406
|
+
case 'with-time': {
|
|
407
|
+
const newWithTime = newValue;
|
|
408
|
+
const oldWithTime = this._state.withTime;
|
|
409
|
+
if (newWithTime !== oldWithTime) {
|
|
410
|
+
this._state.withTime = newWithTime;
|
|
411
|
+
const hasDate = this._state.year !== undefined &&
|
|
412
|
+
this._state.month !== undefined &&
|
|
413
|
+
this._state.day !== undefined;
|
|
414
|
+
if (hasDate) {
|
|
415
|
+
this.syncFormValue();
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
break;
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
getFormValue() {
|
|
424
|
+
return componentsToOutputValue(this.getComponents());
|
|
425
|
+
}
|
|
426
|
+
getComponents() {
|
|
427
|
+
return {
|
|
428
|
+
year: this._state.year,
|
|
429
|
+
month: this._state.month,
|
|
430
|
+
day: this._state.day,
|
|
431
|
+
hour: this._state.hour,
|
|
432
|
+
minute: this._state.minute,
|
|
433
|
+
};
|
|
434
|
+
}
|
|
435
|
+
hasDate() {
|
|
436
|
+
return !!(this._state.year && this._state.month && this._state.day);
|
|
437
|
+
}
|
|
438
|
+
getFormattedValue(components) {
|
|
439
|
+
const c = components || this.getComponents();
|
|
440
|
+
if (!c.year || !c.month || !c.day)
|
|
441
|
+
return null;
|
|
442
|
+
return formatDisplayValue(c, this.getProperty('format') || 'long', getEffectiveLocale(this, this.getProperty('locale')), this._state.withTime);
|
|
443
|
+
}
|
|
444
|
+
initializeState() {
|
|
445
|
+
const valueAttr = this.getProperty('value');
|
|
446
|
+
const withTime = this.getProperty('with-time');
|
|
447
|
+
const components = parseValue(valueAttr, withTime);
|
|
448
|
+
if (components === null) {
|
|
449
|
+
this._state = {
|
|
450
|
+
withTime,
|
|
451
|
+
open: false,
|
|
452
|
+
};
|
|
453
|
+
}
|
|
454
|
+
else {
|
|
455
|
+
this._state = {
|
|
456
|
+
...components,
|
|
457
|
+
withTime,
|
|
458
|
+
open: false,
|
|
459
|
+
};
|
|
460
|
+
}
|
|
461
|
+
this.syncFormValue();
|
|
462
|
+
}
|
|
463
|
+
updateState(updates, forceSync = false) {
|
|
464
|
+
this._state = { ...this._state, ...updates };
|
|
465
|
+
const shouldSync = forceSync || !this._state.open || !this._state.withTime;
|
|
466
|
+
if (shouldSync) {
|
|
467
|
+
this.syncFormValue();
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
syncFormValue() {
|
|
471
|
+
const utcValue = this.getFormValue();
|
|
472
|
+
const currentValue = this.getProperty('value');
|
|
473
|
+
if (utcValue !== currentValue) {
|
|
474
|
+
if (utcValue) {
|
|
475
|
+
this.setProperty('value', utcValue);
|
|
476
|
+
}
|
|
477
|
+
else {
|
|
478
|
+
this.setProperty('value', null);
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
handleTimeInputChange(hour, minute) {
|
|
483
|
+
if (!this.hasDate())
|
|
484
|
+
return;
|
|
485
|
+
const components = { ...this.getComponents(), hour, minute };
|
|
486
|
+
this.updateState(components, true);
|
|
487
|
+
this.emitChangeEvent(components, 'time-change');
|
|
488
|
+
}
|
|
489
|
+
emitChangeEvent(components, source) {
|
|
490
|
+
const utcValue = components ? componentsToOutputValue(components) : null;
|
|
491
|
+
const localValue = components ? componentsToLocalValue(components, this._state.withTime) : null;
|
|
492
|
+
const milliseconds = components ? componentsToDateObject(components)?.getTime() ?? null : null;
|
|
493
|
+
const formatted = components ? this.getFormattedValue(components) : null;
|
|
494
|
+
const detail = {
|
|
495
|
+
value: utcValue,
|
|
496
|
+
localValue,
|
|
497
|
+
milliseconds,
|
|
498
|
+
formatted,
|
|
499
|
+
source,
|
|
500
|
+
};
|
|
501
|
+
const event = new CustomEvent('change', {
|
|
502
|
+
detail,
|
|
503
|
+
bubbles: true,
|
|
504
|
+
cancelable: true,
|
|
505
|
+
});
|
|
506
|
+
this.dispatchEvent(event);
|
|
507
|
+
}
|
|
508
|
+
get size() { return this.getProperty('size'); }
|
|
509
|
+
set size(v) { this.setProperty('size', v); }
|
|
510
|
+
get placement() { return this.getProperty('placement'); }
|
|
511
|
+
set placement(v) { this.setProperty('placement', v); }
|
|
512
|
+
get flavor() { return this.getProperty('flavor'); }
|
|
513
|
+
set flavor(v) { this.setProperty('flavor', v); }
|
|
514
|
+
get label() { return this.getProperty('label'); }
|
|
515
|
+
set label(v) { this.setProperty('label', v); }
|
|
516
|
+
get placeholder() { return this.getProperty('placeholder'); }
|
|
517
|
+
set placeholder(v) { this.setProperty('placeholder', v); }
|
|
518
|
+
get name() { return this.getProperty('name'); }
|
|
519
|
+
set name(v) { this.setProperty('name', v); }
|
|
520
|
+
get format() { return this.getProperty('format'); }
|
|
521
|
+
set format(v) { this.setProperty('format', v); }
|
|
522
|
+
get locale() { return this.getProperty('locale'); }
|
|
523
|
+
set locale(v) { this.setProperty('locale', v); }
|
|
524
|
+
get required() { return this.getProperty('required'); }
|
|
525
|
+
set required(v) { this.setProperty('required', v); }
|
|
526
|
+
get disabled() { return this.getProperty('disabled'); }
|
|
527
|
+
set disabled(v) { this.setProperty('disabled', v); }
|
|
528
|
+
get clearable() { return this.getProperty('clearable'); }
|
|
529
|
+
set clearable(v) { this.setProperty('clearable', v); }
|
|
530
|
+
get min() { return this.getProperty('min'); }
|
|
531
|
+
set min(v) { this.setProperty('min', v ?? ''); }
|
|
532
|
+
get max() { return this.getProperty('max'); }
|
|
533
|
+
set max(v) { this.setProperty('max', v ?? ''); }
|
|
534
|
+
get withTime() { return this.getProperty('with-time'); }
|
|
535
|
+
set withTime(v) { this.setProperty('with-time', v); }
|
|
536
|
+
get value() {
|
|
537
|
+
return this.getProperty('value') || null;
|
|
538
|
+
}
|
|
539
|
+
set value(val) {
|
|
540
|
+
if (val === null || val === undefined || val === '') {
|
|
541
|
+
this.setProperty('value', null);
|
|
542
|
+
}
|
|
543
|
+
else {
|
|
544
|
+
const strValue = val instanceof Date ? val.toISOString() : val;
|
|
545
|
+
this.setProperty('value', strValue);
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
clear() {
|
|
549
|
+
this.clearValue();
|
|
550
|
+
}
|
|
551
|
+
buildStubClasses() {
|
|
552
|
+
const classes = ['date-picker-stub'];
|
|
553
|
+
if (this.getProperty('disabled'))
|
|
554
|
+
classes.push('disabled');
|
|
555
|
+
if (this.getProperty('required'))
|
|
556
|
+
classes.push('required');
|
|
557
|
+
if (this._state.open)
|
|
558
|
+
classes.push('open');
|
|
559
|
+
return classes.join(' ');
|
|
560
|
+
}
|
|
561
|
+
renderStub() {
|
|
562
|
+
const stub = document.createElement('div');
|
|
563
|
+
stub.className = this.buildStubClasses();
|
|
564
|
+
const isDisabled = this.getProperty('disabled');
|
|
565
|
+
if (isDisabled) {
|
|
566
|
+
stub.setAttribute('disabled', 'true');
|
|
567
|
+
}
|
|
568
|
+
stub.setAttribute('role', 'button');
|
|
569
|
+
stub.setAttribute('aria-haspopup', 'dialog');
|
|
570
|
+
stub.setAttribute('aria-expanded', String(this._state.open));
|
|
571
|
+
stub.setAttribute('tabindex', isDisabled ? '-1' : '0');
|
|
572
|
+
if (isDisabled)
|
|
573
|
+
stub.setAttribute('aria-disabled', 'true');
|
|
574
|
+
if (this.getProperty('label')) {
|
|
575
|
+
stub.setAttribute('aria-labelledby', `date-picker-label-${getElementHash(this)}`);
|
|
576
|
+
}
|
|
577
|
+
stub.addEventListener('keydown', (e) => {
|
|
578
|
+
const ke = e;
|
|
579
|
+
if (this._state.open)
|
|
580
|
+
return;
|
|
581
|
+
if (ke.key === 'Enter' || ke.key === ' ') {
|
|
582
|
+
ke.preventDefault();
|
|
583
|
+
this.handleStubClick(e);
|
|
584
|
+
}
|
|
585
|
+
});
|
|
586
|
+
const startSlot = document.createElement('slot');
|
|
587
|
+
startSlot.name = 'start';
|
|
588
|
+
const displayText = document.createElement('span');
|
|
589
|
+
displayText.className = 'stub-text';
|
|
590
|
+
const formattedValue = this.getFormattedValue();
|
|
591
|
+
const placeholder = this.getProperty('placeholder') || 'Select date...';
|
|
592
|
+
displayText.textContent = formattedValue || placeholder;
|
|
593
|
+
if (!formattedValue) {
|
|
594
|
+
displayText.classList.add('placeholder');
|
|
595
|
+
}
|
|
596
|
+
const iconContainer = document.createElement('div');
|
|
597
|
+
iconContainer.className = 'stub-icons';
|
|
598
|
+
const clearable = this.getProperty('clearable');
|
|
599
|
+
if (clearable && formattedValue && !isDisabled) {
|
|
600
|
+
const clearButton = document.createElement('button');
|
|
601
|
+
clearButton.className = 'stub-clear';
|
|
602
|
+
clearButton.type = 'button';
|
|
603
|
+
clearButton.innerHTML = CLEAR_ICON_SVG;
|
|
604
|
+
clearButton.addEventListener('click', (e) => this.handleClearClick(e));
|
|
605
|
+
iconContainer.appendChild(clearButton);
|
|
606
|
+
}
|
|
607
|
+
const calendarIcon = document.createElement('span');
|
|
608
|
+
calendarIcon.className = 'stub-arrow';
|
|
609
|
+
calendarIcon.innerHTML = CALENDAR_ICON_SVG;
|
|
610
|
+
iconContainer.appendChild(calendarIcon);
|
|
611
|
+
stub.addEventListener('click', (e) => this.handleStubClick(e));
|
|
612
|
+
stub.appendChild(startSlot);
|
|
613
|
+
stub.appendChild(displayText);
|
|
614
|
+
stub.appendChild(iconContainer);
|
|
615
|
+
return stub;
|
|
616
|
+
}
|
|
617
|
+
createTimeInputElement() {
|
|
618
|
+
const input = document.createElement('input');
|
|
619
|
+
input.type = 'text';
|
|
620
|
+
input.className = 'time-input';
|
|
621
|
+
input.placeholder = 'HH:mm';
|
|
622
|
+
input.autocomplete = 'off';
|
|
623
|
+
input.maxLength = 5;
|
|
624
|
+
return input;
|
|
625
|
+
}
|
|
626
|
+
renderTimeSection() {
|
|
627
|
+
const timeSection = document.createElement('div');
|
|
628
|
+
timeSection.className = 'time-section';
|
|
629
|
+
const timeLabel = document.createElement('label');
|
|
630
|
+
timeLabel.className = 'time-label';
|
|
631
|
+
timeLabel.textContent = 'Time:';
|
|
632
|
+
const timeInputElement = this.createTimeInputElement();
|
|
633
|
+
const hour = this._state.hour || 0;
|
|
634
|
+
const minute = this._state.minute || 0;
|
|
635
|
+
this._timeInput = new TimeInput(timeInputElement, this, hour, minute);
|
|
636
|
+
const timeIcon = document.createElement('span');
|
|
637
|
+
timeIcon.className = 'time-icon';
|
|
638
|
+
timeIcon.innerHTML = SCHEDULE_ICON_SVG;
|
|
639
|
+
timeSection.appendChild(timeLabel);
|
|
640
|
+
timeSection.appendChild(timeInputElement);
|
|
641
|
+
timeSection.appendChild(timeIcon);
|
|
642
|
+
return timeSection;
|
|
643
|
+
}
|
|
644
|
+
renderCalendarDropdown() {
|
|
645
|
+
const dialog = document.createElement('dialog');
|
|
646
|
+
dialog.className = 'calendar-dialog';
|
|
647
|
+
const contentWrapper = document.createElement('div');
|
|
648
|
+
contentWrapper.className = 'calendar-content';
|
|
649
|
+
const calendar = document.createElement('ty-calendar');
|
|
650
|
+
if (this._state.year && this._state.month && this._state.day) {
|
|
651
|
+
calendar.setAttribute('year', this._state.year.toString());
|
|
652
|
+
calendar.setAttribute('month', this._state.month.toString());
|
|
653
|
+
calendar.setAttribute('day', this._state.day.toString());
|
|
654
|
+
}
|
|
655
|
+
const locale = getEffectiveLocale(this, this.getProperty('locale'));
|
|
656
|
+
if (locale) {
|
|
657
|
+
calendar.setAttribute('locale', locale);
|
|
658
|
+
}
|
|
659
|
+
const min = this.getProperty('min');
|
|
660
|
+
const max = this.getProperty('max');
|
|
661
|
+
if (min)
|
|
662
|
+
calendar.setAttribute('min', min);
|
|
663
|
+
if (max)
|
|
664
|
+
calendar.setAttribute('max', max);
|
|
665
|
+
const flavor = this.getProperty('flavor');
|
|
666
|
+
if (flavor && flavor !== 'default')
|
|
667
|
+
calendar.setAttribute('flavor', flavor);
|
|
668
|
+
calendar.addEventListener('change', (e) => this.handleCalendarChange(e));
|
|
669
|
+
contentWrapper.appendChild(calendar);
|
|
670
|
+
if (this._state.withTime) {
|
|
671
|
+
contentWrapper.appendChild(this.renderTimeSection());
|
|
672
|
+
}
|
|
673
|
+
dialog.appendChild(contentWrapper);
|
|
674
|
+
dialog.addEventListener('close', () => {
|
|
675
|
+
this.updateState({ open: false });
|
|
676
|
+
});
|
|
677
|
+
return dialog;
|
|
678
|
+
}
|
|
679
|
+
renderNativeInput() {
|
|
680
|
+
const wrapper = document.createElement('div');
|
|
681
|
+
wrapper.className = 'dropdown-wrapper';
|
|
682
|
+
const stub = document.createElement('div');
|
|
683
|
+
stub.className = this.buildStubClasses();
|
|
684
|
+
const isDisabled = this.getProperty('disabled');
|
|
685
|
+
if (isDisabled) {
|
|
686
|
+
stub.setAttribute('disabled', 'true');
|
|
687
|
+
}
|
|
688
|
+
const input = document.createElement('input');
|
|
689
|
+
input.className = 'native-date-input';
|
|
690
|
+
input.type = this._state.withTime ? 'datetime-local' : 'date';
|
|
691
|
+
if (isDisabled)
|
|
692
|
+
input.disabled = true;
|
|
693
|
+
if (this.getProperty('required'))
|
|
694
|
+
input.required = true;
|
|
695
|
+
const minBound = this.getProperty('min');
|
|
696
|
+
const maxBound = this.getProperty('max');
|
|
697
|
+
if (minBound)
|
|
698
|
+
input.min = this._state.withTime ? `${minBound}T00:00` : minBound;
|
|
699
|
+
if (maxBound)
|
|
700
|
+
input.max = this._state.withTime ? `${maxBound}T23:59` : maxBound;
|
|
701
|
+
const localValue = componentsToLocalValue(this.getComponents(), this._state.withTime);
|
|
702
|
+
if (localValue) {
|
|
703
|
+
input.value = localValue;
|
|
704
|
+
}
|
|
705
|
+
const placeholder = this.getProperty('placeholder') || 'Select date...';
|
|
706
|
+
const formattedValue = this.getFormattedValue();
|
|
707
|
+
const placeholderEl = document.createElement('span');
|
|
708
|
+
placeholderEl.className = 'stub-text' + (formattedValue ? '' : ' placeholder');
|
|
709
|
+
placeholderEl.textContent = formattedValue || placeholder;
|
|
710
|
+
input.addEventListener('change', () => {
|
|
711
|
+
if (!input.value) {
|
|
712
|
+
this.clearValue();
|
|
713
|
+
return;
|
|
714
|
+
}
|
|
715
|
+
const parsed = parseValue(input.value, this._state.withTime);
|
|
716
|
+
if (parsed) {
|
|
717
|
+
this.updateState(parsed, true);
|
|
718
|
+
this.emitChangeEvent(parsed, 'selection');
|
|
719
|
+
}
|
|
720
|
+
});
|
|
721
|
+
const startSlot = document.createElement('slot');
|
|
722
|
+
startSlot.name = 'start';
|
|
723
|
+
stub.appendChild(startSlot);
|
|
724
|
+
stub.appendChild(input);
|
|
725
|
+
stub.appendChild(placeholderEl);
|
|
726
|
+
const iconContainer = document.createElement('div');
|
|
727
|
+
iconContainer.className = 'stub-icons';
|
|
728
|
+
const clearable = this.getProperty('clearable');
|
|
729
|
+
if (clearable && localValue && !isDisabled) {
|
|
730
|
+
const clearButton = document.createElement('button');
|
|
731
|
+
clearButton.className = 'stub-clear';
|
|
732
|
+
clearButton.type = 'button';
|
|
733
|
+
clearButton.innerHTML = CLEAR_ICON_SVG;
|
|
734
|
+
clearButton.addEventListener('click', (e) => this.handleClearClick(e));
|
|
735
|
+
iconContainer.appendChild(clearButton);
|
|
736
|
+
}
|
|
737
|
+
const calendarIcon = document.createElement('span');
|
|
738
|
+
calendarIcon.className = 'stub-arrow';
|
|
739
|
+
calendarIcon.innerHTML = CALENDAR_ICON_SVG;
|
|
740
|
+
iconContainer.appendChild(calendarIcon);
|
|
741
|
+
stub.appendChild(iconContainer);
|
|
742
|
+
wrapper.appendChild(stub);
|
|
743
|
+
return wrapper;
|
|
744
|
+
}
|
|
745
|
+
calculateCalendarPosition() {
|
|
746
|
+
if (!this.shadowRoot)
|
|
747
|
+
return;
|
|
748
|
+
if (isMobileTouch())
|
|
749
|
+
return;
|
|
750
|
+
const stub = this.shadowRoot.querySelector('.date-picker-stub');
|
|
751
|
+
const dialog = this.shadowRoot.querySelector('.calendar-dialog');
|
|
752
|
+
if (!stub || !dialog)
|
|
753
|
+
return;
|
|
754
|
+
const stubRect = stub.getBoundingClientRect();
|
|
755
|
+
const dialogRect = dialog.getBoundingClientRect();
|
|
756
|
+
const estimatedHeight = dialogRect.height || 400;
|
|
757
|
+
const popupWidth = dialogRect.width || 320;
|
|
758
|
+
const wrapPadding = 8;
|
|
759
|
+
const anchored = placementToAnchored(this.getProperty('placement'));
|
|
760
|
+
const pos = computeAnchoredPosition({
|
|
761
|
+
anchorRect: stubRect,
|
|
762
|
+
popupWidth,
|
|
763
|
+
popupHeight: estimatedHeight,
|
|
764
|
+
gap: 0,
|
|
765
|
+
align: anchored.align,
|
|
766
|
+
side: anchored.side,
|
|
767
|
+
});
|
|
768
|
+
const positionBelow = pos.below;
|
|
769
|
+
const x = pos.x - wrapPadding;
|
|
770
|
+
const y = positionBelow ? pos.topY : pos.bottomY;
|
|
771
|
+
this.style.setProperty('--calendar-x', `${x}px`);
|
|
772
|
+
this.style.setProperty('--calendar-y', `${y}px`);
|
|
773
|
+
this.style.setProperty('--calendar-offset-x', '0px');
|
|
774
|
+
this.style.setProperty('--calendar-offset-y', '0px');
|
|
775
|
+
if (positionBelow) {
|
|
776
|
+
dialog.classList.add('position-below');
|
|
777
|
+
dialog.classList.remove('position-above');
|
|
778
|
+
}
|
|
779
|
+
else {
|
|
780
|
+
dialog.classList.add('position-above');
|
|
781
|
+
dialog.classList.remove('position-below');
|
|
782
|
+
}
|
|
783
|
+
}
|
|
784
|
+
renderContainer() {
|
|
785
|
+
const container = document.createElement('div');
|
|
786
|
+
container.className = 'dropdown-container';
|
|
787
|
+
const label = this.getProperty('label');
|
|
788
|
+
if (label) {
|
|
789
|
+
const labelEl = document.createElement('label');
|
|
790
|
+
labelEl.className = 'ty-field-label';
|
|
791
|
+
labelEl.id = `date-picker-label-${getElementHash(this)}`;
|
|
792
|
+
labelEl.innerHTML = label + (this.getProperty('required') ? '<span class="required-icon">*</span>' : '');
|
|
793
|
+
container.appendChild(labelEl);
|
|
794
|
+
}
|
|
795
|
+
return container;
|
|
796
|
+
}
|
|
797
|
+
render() {
|
|
798
|
+
if (!this.shadowRoot)
|
|
799
|
+
return;
|
|
800
|
+
ensureStyles(this.shadowRoot, { css: datePickerStyles, id: 'ty-date-picker' });
|
|
801
|
+
if (isMobileTouch()) {
|
|
802
|
+
const existingNativeInput = this.shadowRoot.querySelector('.native-date-input');
|
|
803
|
+
if (existingNativeInput) {
|
|
804
|
+
this.updateDisplay();
|
|
805
|
+
return;
|
|
806
|
+
}
|
|
807
|
+
this.shadowRoot.innerHTML = '';
|
|
808
|
+
const container = this.renderContainer();
|
|
809
|
+
container.appendChild(this.renderNativeInput());
|
|
810
|
+
this.shadowRoot.appendChild(container);
|
|
811
|
+
return;
|
|
812
|
+
}
|
|
813
|
+
const existingDialog = this.shadowRoot.querySelector('.calendar-dialog');
|
|
814
|
+
const isDialogOpen = existingDialog && existingDialog.open;
|
|
815
|
+
if (isDialogOpen && this._state.open) {
|
|
816
|
+
this.updateDisplay();
|
|
817
|
+
this.calculateCalendarPosition();
|
|
818
|
+
return;
|
|
819
|
+
}
|
|
820
|
+
this.shadowRoot.innerHTML = '';
|
|
821
|
+
const container = this.renderContainer();
|
|
822
|
+
const wrapper = document.createElement('div');
|
|
823
|
+
wrapper.className = 'dropdown-wrapper';
|
|
824
|
+
wrapper.appendChild(this.renderStub());
|
|
825
|
+
wrapper.appendChild(this.renderCalendarDropdown());
|
|
826
|
+
container.appendChild(wrapper);
|
|
827
|
+
this.shadowRoot.appendChild(container);
|
|
828
|
+
this.setupEventListeners();
|
|
829
|
+
}
|
|
830
|
+
updateDisplay() {
|
|
831
|
+
if (!this.shadowRoot)
|
|
832
|
+
return;
|
|
833
|
+
const formattedValue = this.getFormattedValue();
|
|
834
|
+
const placeholder = this.getProperty('placeholder') || 'Select date...';
|
|
835
|
+
const hasValue = this.hasDate();
|
|
836
|
+
const clearable = this.getProperty('clearable');
|
|
837
|
+
const isDisabled = this.getProperty('disabled');
|
|
838
|
+
const stubText = this.shadowRoot.querySelector('.stub-text');
|
|
839
|
+
if (stubText) {
|
|
840
|
+
stubText.textContent = formattedValue || placeholder;
|
|
841
|
+
stubText.classList.toggle('placeholder', !formattedValue);
|
|
842
|
+
}
|
|
843
|
+
const clearButton = this.shadowRoot.querySelector('.stub-clear');
|
|
844
|
+
if (clearButton) {
|
|
845
|
+
clearButton.style.display = (clearable && hasValue && !isDisabled) ? '' : 'none';
|
|
846
|
+
}
|
|
847
|
+
if (isMobileTouch()) {
|
|
848
|
+
const nativeInput = this.shadowRoot.querySelector('.native-date-input');
|
|
849
|
+
if (nativeInput) {
|
|
850
|
+
const localValue = componentsToLocalValue(this.getComponents(), this._state.withTime);
|
|
851
|
+
nativeInput.value = localValue || '';
|
|
852
|
+
}
|
|
853
|
+
return;
|
|
854
|
+
}
|
|
855
|
+
const calendar = this.shadowRoot.querySelector('ty-calendar');
|
|
856
|
+
if (calendar && hasValue) {
|
|
857
|
+
calendar.setAttribute('year', this._state.year.toString());
|
|
858
|
+
calendar.setAttribute('month', this._state.month.toString());
|
|
859
|
+
calendar.setAttribute('day', this._state.day.toString());
|
|
860
|
+
}
|
|
861
|
+
if (calendar) {
|
|
862
|
+
const flavor = this.getProperty('flavor');
|
|
863
|
+
if (flavor && flavor !== 'default')
|
|
864
|
+
calendar.setAttribute('flavor', flavor);
|
|
865
|
+
else
|
|
866
|
+
calendar.removeAttribute('flavor');
|
|
867
|
+
}
|
|
868
|
+
if (this._timeInput && this._state.hour !== undefined && this._state.minute !== undefined) {
|
|
869
|
+
this._timeInput.setTime(this._state.hour, this._state.minute);
|
|
870
|
+
}
|
|
871
|
+
}
|
|
872
|
+
setupEventListeners() {
|
|
873
|
+
if (this._clickListener) {
|
|
874
|
+
document.removeEventListener('click', this._clickListener);
|
|
875
|
+
}
|
|
876
|
+
if (this._keydownListener) {
|
|
877
|
+
document.removeEventListener('keydown', this._keydownListener);
|
|
878
|
+
}
|
|
879
|
+
this._clickListener = (e) => this.handleOutsideClick(e);
|
|
880
|
+
this._keydownListener = (e) => this.handleEscapeKey(e);
|
|
881
|
+
document.addEventListener('click', this._clickListener);
|
|
882
|
+
document.addEventListener('keydown', this._keydownListener);
|
|
883
|
+
const dialog = this.shadowRoot?.querySelector('.calendar-dialog');
|
|
884
|
+
if (dialog) {
|
|
885
|
+
this._dialogClickListener = (e) => this.handleDialogClick(e);
|
|
886
|
+
dialog.addEventListener('click', this._dialogClickListener);
|
|
887
|
+
}
|
|
888
|
+
}
|
|
889
|
+
handleStubClick(event) {
|
|
890
|
+
event.preventDefault();
|
|
891
|
+
const dialog = this.shadowRoot?.querySelector('.calendar-dialog');
|
|
892
|
+
if (this._state.open && (!dialog || !dialog.open)) {
|
|
893
|
+
this.updateState({ open: false }, true);
|
|
894
|
+
unlockScroll(`date-picker-${this.id || this.toString()}`);
|
|
895
|
+
}
|
|
896
|
+
const disabled = this.getProperty('disabled');
|
|
897
|
+
if (!disabled && Date.now() - this._closeTimestamp > 300) {
|
|
898
|
+
this.openDropdown();
|
|
899
|
+
}
|
|
900
|
+
}
|
|
901
|
+
clearValue() {
|
|
902
|
+
this.updateState({
|
|
903
|
+
year: undefined,
|
|
904
|
+
month: undefined,
|
|
905
|
+
day: undefined,
|
|
906
|
+
hour: undefined,
|
|
907
|
+
minute: undefined,
|
|
908
|
+
}, true);
|
|
909
|
+
this.value = null;
|
|
910
|
+
this.emitChangeEvent(null, 'clear');
|
|
911
|
+
this.render();
|
|
912
|
+
}
|
|
913
|
+
handleClearClick(event) {
|
|
914
|
+
event.preventDefault();
|
|
915
|
+
event.stopPropagation();
|
|
916
|
+
this.clearValue();
|
|
917
|
+
}
|
|
918
|
+
handleCalendarChange(event) {
|
|
919
|
+
event.preventDefault();
|
|
920
|
+
event.stopPropagation();
|
|
921
|
+
const customEvent = event;
|
|
922
|
+
const detail = customEvent.detail;
|
|
923
|
+
const dayContext = detail.dayContext;
|
|
924
|
+
if (!dayContext)
|
|
925
|
+
return;
|
|
926
|
+
const newComponents = {
|
|
927
|
+
year: dayContext.year,
|
|
928
|
+
month: dayContext.month,
|
|
929
|
+
day: dayContext.dayInMonth,
|
|
930
|
+
hour: this._state.hour,
|
|
931
|
+
minute: this._state.minute,
|
|
932
|
+
};
|
|
933
|
+
this.updateState(newComponents, true);
|
|
934
|
+
this.emitChangeEvent(newComponents, 'selection');
|
|
935
|
+
if (!this._state.withTime) {
|
|
936
|
+
this.closeDropdown();
|
|
937
|
+
}
|
|
938
|
+
else {
|
|
939
|
+
requestAnimationFrame(() => {
|
|
940
|
+
if (!this.shadowRoot)
|
|
941
|
+
return;
|
|
942
|
+
const timeInput = this.shadowRoot.querySelector('.time-input');
|
|
943
|
+
if (timeInput) {
|
|
944
|
+
timeInput.focus();
|
|
945
|
+
}
|
|
946
|
+
});
|
|
947
|
+
}
|
|
948
|
+
}
|
|
949
|
+
handleDialogClick(event) {
|
|
950
|
+
if (!this.shadowRoot)
|
|
951
|
+
return;
|
|
952
|
+
const dialog = this.shadowRoot.querySelector('.calendar-dialog');
|
|
953
|
+
const content = this.shadowRoot.querySelector('.calendar-content');
|
|
954
|
+
if (event.target === dialog && this._state.open && content && !content.contains(event.target)) {
|
|
955
|
+
event.preventDefault();
|
|
956
|
+
event.stopPropagation();
|
|
957
|
+
this.closeDropdown();
|
|
958
|
+
}
|
|
959
|
+
}
|
|
960
|
+
handleOutsideClick(event) {
|
|
961
|
+
if (!this.shadowRoot)
|
|
962
|
+
return;
|
|
963
|
+
const target = event.target;
|
|
964
|
+
const dialog = this.shadowRoot.querySelector('.calendar-dialog');
|
|
965
|
+
if (this._state.open && dialog && !this.contains(target) && !dialog.contains(target)) {
|
|
966
|
+
this.closeDropdown();
|
|
967
|
+
}
|
|
968
|
+
}
|
|
969
|
+
handleEscapeKey(event) {
|
|
970
|
+
const keyboardEvent = event;
|
|
971
|
+
if (keyboardEvent.key === 'Escape' && this._state.open) {
|
|
972
|
+
keyboardEvent.preventDefault();
|
|
973
|
+
this.closeDropdown();
|
|
974
|
+
}
|
|
975
|
+
}
|
|
976
|
+
openDropdown() {
|
|
977
|
+
if (this._state.open)
|
|
978
|
+
return;
|
|
979
|
+
if (isMobileTouch())
|
|
980
|
+
return;
|
|
981
|
+
this.updateState({ open: true });
|
|
982
|
+
this.dispatchEvent(new CustomEvent('open'));
|
|
983
|
+
this.render();
|
|
984
|
+
requestAnimationFrame(() => {
|
|
985
|
+
if (!this.shadowRoot)
|
|
986
|
+
return;
|
|
987
|
+
const dialog = this.shadowRoot.querySelector('.calendar-dialog');
|
|
988
|
+
if (dialog) {
|
|
989
|
+
const pickerId = `date-picker-${this.id || this.toString()}`;
|
|
990
|
+
lockScroll(pickerId);
|
|
991
|
+
dialog.showModal();
|
|
992
|
+
this.calculateCalendarPosition();
|
|
993
|
+
dialog.classList.add('open');
|
|
994
|
+
const stubEl = this.shadowRoot.querySelector('.date-picker-stub');
|
|
995
|
+
if (stubEl)
|
|
996
|
+
stubEl.setAttribute('aria-expanded', 'true');
|
|
997
|
+
const focusedElement = this.shadowRoot.activeElement;
|
|
998
|
+
if (focusedElement) {
|
|
999
|
+
focusedElement.blur();
|
|
1000
|
+
}
|
|
1001
|
+
}
|
|
1002
|
+
});
|
|
1003
|
+
}
|
|
1004
|
+
closeDropdown() {
|
|
1005
|
+
if (!this._state.open)
|
|
1006
|
+
return;
|
|
1007
|
+
this._closeTimestamp = Date.now();
|
|
1008
|
+
const pickerId = `date-picker-${this.id || this.toString()}`;
|
|
1009
|
+
this.updateState({ open: false }, true);
|
|
1010
|
+
unlockScroll(pickerId);
|
|
1011
|
+
if (this.shadowRoot) {
|
|
1012
|
+
const dialog = this.shadowRoot.querySelector('.calendar-dialog');
|
|
1013
|
+
if (dialog) {
|
|
1014
|
+
dialog.classList.remove('position-above', 'position-below', 'open');
|
|
1015
|
+
dialog.close();
|
|
1016
|
+
}
|
|
1017
|
+
const stubEl = this.shadowRoot.querySelector('.date-picker-stub');
|
|
1018
|
+
if (stubEl)
|
|
1019
|
+
stubEl.setAttribute('aria-expanded', 'false');
|
|
1020
|
+
}
|
|
1021
|
+
this.dispatchEvent(new CustomEvent('close'));
|
|
1022
|
+
this.render();
|
|
1023
|
+
}
|
|
1024
|
+
cleanup() {
|
|
1025
|
+
if (this._clickListener) {
|
|
1026
|
+
document.removeEventListener('click', this._clickListener);
|
|
1027
|
+
}
|
|
1028
|
+
if (this._keydownListener) {
|
|
1029
|
+
document.removeEventListener('keydown', this._keydownListener);
|
|
1030
|
+
}
|
|
1031
|
+
if (this._dialogClickListener && this.shadowRoot) {
|
|
1032
|
+
const dialog = this.shadowRoot.querySelector('.calendar-dialog');
|
|
1033
|
+
if (dialog) {
|
|
1034
|
+
dialog.removeEventListener('click', this._dialogClickListener);
|
|
1035
|
+
}
|
|
1036
|
+
}
|
|
1037
|
+
if (this._state.open) {
|
|
1038
|
+
const pickerId = `date-picker-${this.id || this.toString()}`;
|
|
1039
|
+
unlockScroll(pickerId);
|
|
1040
|
+
}
|
|
1041
|
+
this._clickListener = undefined;
|
|
1042
|
+
this._keydownListener = undefined;
|
|
1043
|
+
this._dialogClickListener = undefined;
|
|
1044
|
+
this._timeInput = undefined;
|
|
1045
|
+
}
|
|
1047
1046
|
}
|
|
1048
1047
|
TyDatePicker.properties = {
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
type: 'boolean',
|
|
1147
|
-
visual: true,
|
|
1148
|
-
default: false
|
|
1149
|
-
}
|
|
1048
|
+
placement: {
|
|
1049
|
+
type: 'string',
|
|
1050
|
+
visual: true,
|
|
1051
|
+
default: '',
|
|
1052
|
+
},
|
|
1053
|
+
size: {
|
|
1054
|
+
type: 'string',
|
|
1055
|
+
visual: true,
|
|
1056
|
+
default: DEFAULT_SIZE,
|
|
1057
|
+
validate: (v) => ["xs", "sm", "md", "lg", "xl"].includes(v),
|
|
1058
|
+
coerce: (v) => {
|
|
1059
|
+
if (!["xs", "sm", "md", "lg", "xl"].includes(v)) {
|
|
1060
|
+
console.warn(`[ty-date-picker] Invalid size '${v}'. Using '${DEFAULT_SIZE}'.`);
|
|
1061
|
+
return DEFAULT_SIZE;
|
|
1062
|
+
}
|
|
1063
|
+
return v;
|
|
1064
|
+
}
|
|
1065
|
+
},
|
|
1066
|
+
flavor: {
|
|
1067
|
+
type: 'string',
|
|
1068
|
+
visual: true,
|
|
1069
|
+
default: 'default'
|
|
1070
|
+
},
|
|
1071
|
+
label: {
|
|
1072
|
+
type: 'string',
|
|
1073
|
+
visual: true,
|
|
1074
|
+
default: ''
|
|
1075
|
+
},
|
|
1076
|
+
placeholder: {
|
|
1077
|
+
type: 'string',
|
|
1078
|
+
visual: true,
|
|
1079
|
+
default: 'Select date...'
|
|
1080
|
+
},
|
|
1081
|
+
name: {
|
|
1082
|
+
type: 'string',
|
|
1083
|
+
visual: false,
|
|
1084
|
+
default: ''
|
|
1085
|
+
},
|
|
1086
|
+
min: {
|
|
1087
|
+
type: 'string',
|
|
1088
|
+
visual: true,
|
|
1089
|
+
default: ''
|
|
1090
|
+
},
|
|
1091
|
+
max: {
|
|
1092
|
+
type: 'string',
|
|
1093
|
+
visual: true,
|
|
1094
|
+
default: ''
|
|
1095
|
+
},
|
|
1096
|
+
required: {
|
|
1097
|
+
type: 'boolean',
|
|
1098
|
+
visual: true,
|
|
1099
|
+
default: false
|
|
1100
|
+
},
|
|
1101
|
+
disabled: {
|
|
1102
|
+
type: 'boolean',
|
|
1103
|
+
visual: true,
|
|
1104
|
+
default: false
|
|
1105
|
+
},
|
|
1106
|
+
clearable: {
|
|
1107
|
+
type: 'boolean',
|
|
1108
|
+
visual: true,
|
|
1109
|
+
default: true,
|
|
1110
|
+
aliases: {
|
|
1111
|
+
'not-clearable': false
|
|
1112
|
+
}
|
|
1113
|
+
},
|
|
1114
|
+
format: {
|
|
1115
|
+
type: 'string',
|
|
1116
|
+
visual: true,
|
|
1117
|
+
default: 'long',
|
|
1118
|
+
validate: (v) => ['short', 'medium', 'long', 'full'].includes(v),
|
|
1119
|
+
coerce: (v) => {
|
|
1120
|
+
const validFormats = ['short', 'medium', 'long', 'full'];
|
|
1121
|
+
if (!validFormats.includes(v)) {
|
|
1122
|
+
console.warn(`[ty-date-picker] Invalid format '${v}'. Using 'long'.`);
|
|
1123
|
+
return 'long';
|
|
1124
|
+
}
|
|
1125
|
+
return v;
|
|
1126
|
+
}
|
|
1127
|
+
},
|
|
1128
|
+
locale: {
|
|
1129
|
+
type: 'string',
|
|
1130
|
+
visual: true,
|
|
1131
|
+
default: ''
|
|
1132
|
+
},
|
|
1133
|
+
value: {
|
|
1134
|
+
type: 'string',
|
|
1135
|
+
visual: true,
|
|
1136
|
+
formValue: true,
|
|
1137
|
+
emitChange: false,
|
|
1138
|
+
default: null
|
|
1139
|
+
},
|
|
1140
|
+
'with-time': {
|
|
1141
|
+
type: 'boolean',
|
|
1142
|
+
visual: true,
|
|
1143
|
+
default: false
|
|
1144
|
+
}
|
|
1150
1145
|
};
|
|
1151
1146
|
TyDatePicker.formAssociated = true;
|
|
1152
1147
|
if (!customElements.get('ty-date-picker')) {
|
|
1153
|
-
|
|
1148
|
+
customElements.define('ty-date-picker', TyDatePicker);
|
|
1154
1149
|
}
|