tyrell-components 1.0.0-RC15 → 1.0.0-RC17
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/README.md +44 -52
- package/css/tyrell-colors-static.css +14 -0
- package/css/tyrell-theme.css +13 -7
- package/css/tyrell.css +15 -22
- package/dist/tyrell-colors-static.css +1 -1
- package/dist/tyrell-theme.css +1 -1
- package/dist/tyrell.css +1 -1
- package/dist/tyrell.js +1 -1
- package/lib/base/ty-component.d.ts +37 -15
- package/lib/base/ty-component.js +90 -26
- package/lib/components/button.d.ts +14 -0
- package/lib/components/button.js +61 -7
- package/lib/components/calendar-month.d.ts +7 -1
- package/lib/components/calendar-month.js +38 -30
- package/lib/components/calendar-navigation.d.ts +11 -0
- package/lib/components/calendar-navigation.js +68 -44
- package/lib/components/calendar.d.ts +52 -3
- package/lib/components/calendar.js +274 -61
- package/lib/components/checkbox.d.ts +8 -1
- package/lib/components/checkbox.js +29 -8
- package/lib/components/copy.d.ts +2 -0
- package/lib/components/copy.js +24 -33
- package/lib/components/date-picker.d.ts +68 -34
- package/lib/components/date-picker.js +386 -389
- package/lib/components/file-upload.d.ts +7 -0
- package/lib/components/file-upload.js +36 -4
- package/lib/components/input.d.ts +73 -3
- package/lib/components/input.js +159 -24
- package/lib/components/modal.d.ts +4 -8
- package/lib/components/modal.js +103 -33
- package/lib/components/option.js +7 -5
- package/lib/components/popup.js +117 -14
- package/lib/components/radio.d.ts +24 -0
- package/lib/components/radio.js +55 -32
- package/lib/components/resize-observer.js +2 -0
- package/lib/components/scroll-container.d.ts +3 -2
- package/lib/components/scroll-container.js +7 -8
- package/lib/components/select.d.ts +68 -11
- package/lib/components/select.js +196 -44
- package/lib/components/selected-tags.d.ts +7 -1
- package/lib/components/selected-tags.js +10 -1
- package/lib/components/switch.d.ts +9 -1
- package/lib/components/switch.js +29 -8
- package/lib/components/tab.d.ts +49 -1
- package/lib/components/tab.js +151 -10
- package/lib/components/tabs.d.ts +14 -2
- package/lib/components/tabs.js +185 -136
- package/lib/components/tag.d.ts +4 -7
- package/lib/components/tag.js +6 -7
- package/lib/components/textarea.d.ts +51 -5
- package/lib/components/textarea.js +95 -30
- package/lib/components/tooltip.d.ts +4 -3
- package/lib/components/tooltip.js +34 -24
- package/lib/components/wizard.d.ts +5 -4
- package/lib/components/wizard.js +120 -156
- package/lib/index.d.ts +2 -1
- package/lib/index.js +1 -0
- package/lib/styles/button.js +21 -19
- package/lib/styles/calendar-month.d.ts +3 -0
- package/lib/styles/calendar-month.js +43 -27
- package/lib/styles/calendar-navigation.d.ts +1 -1
- package/lib/styles/calendar-navigation.js +55 -26
- package/lib/styles/calendar.d.ts +3 -2
- package/lib/styles/calendar.js +77 -0
- package/lib/styles/checkbox.js +31 -13
- package/lib/styles/copy.js +5 -1
- package/lib/styles/date-picker.js +68 -26
- package/lib/styles/file-upload.d.ts +1 -1
- package/lib/styles/file-upload.js +3 -3
- package/lib/styles/icon.d.ts +1 -1
- package/lib/styles/icon.js +2 -2
- package/lib/styles/input.js +6 -10
- package/lib/styles/option.d.ts +1 -1
- package/lib/styles/option.js +7 -3
- package/lib/styles/popup.d.ts +1 -1
- package/lib/styles/popup.js +10 -5
- package/lib/styles/radio.js +18 -6
- package/lib/styles/scroll-container.d.ts +1 -1
- package/lib/styles/scroll-container.js +1 -1
- package/lib/styles/select-base.js +24 -3
- package/lib/styles/select.d.ts +1 -1
- package/lib/styles/select.js +10 -0
- package/lib/styles/step.d.ts +1 -1
- package/lib/styles/step.js +1 -9
- package/lib/styles/switch.js +14 -6
- package/lib/styles/tab.d.ts +1 -1
- package/lib/styles/tab.js +67 -3
- package/lib/styles/tabs.d.ts +1 -1
- package/lib/styles/tabs.js +7 -3
- package/lib/styles/tag.js +3 -3
- package/lib/styles/textarea.d.ts +1 -1
- package/lib/styles/textarea.js +7 -0
- package/lib/styles/wizard.d.ts +9 -5
- package/lib/styles/wizard.js +211 -99
- package/lib/types/common.d.ts +2 -2
- package/lib/utils/calendar-utils.d.ts +49 -0
- package/lib/utils/calendar-utils.js +63 -6
- package/lib/utils/property-manager.d.ts +7 -1
- package/lib/utils/property-manager.js +16 -4
- package/lib/utils/scroll-lock.d.ts +10 -4
- package/lib/utils/scroll-lock.js +102 -2
- package/lib/utils/upgrade-properties.d.ts +21 -0
- package/lib/utils/upgrade-properties.js +47 -0
- package/lib/utils/validity.d.ts +122 -0
- package/lib/utils/validity.js +289 -0
- package/lib/version.d.ts +1 -1
- package/lib/version.js +1 -1
- package/package.json +27 -5
|
@@ -7,6 +7,8 @@ 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
9
|
import { DEFAULT_SIZE } from '../types/common.js';
|
|
10
|
+
import { nativeMessage } from '../utils/validity.js';
|
|
11
|
+
import { parseDateTime, toISODay, wallClockDate } from '../utils/calendar-utils.js';
|
|
10
12
|
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>`;
|
|
11
13
|
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>`;
|
|
12
14
|
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>`;
|
|
@@ -20,48 +22,28 @@ elementIds.set(element, id);
|
|
|
20
22
|
}
|
|
21
23
|
return id;
|
|
22
24
|
}
|
|
23
|
-
function parseValue(value, withTime) {
|
|
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;
|
|
53
|
-
}
|
|
54
25
|
function componentsToDateObject(components) {
|
|
55
|
-
const { year, month, day, hour, minute } = components;
|
|
26
|
+
const { year, month, day, hour, minute, second } = components;
|
|
56
27
|
if (!year || !month || !day)
|
|
57
28
|
return null;
|
|
58
|
-
|
|
29
|
+
const date = new Date(2000, 0, 1, 12);
|
|
30
|
+
date.setFullYear(year, month - 1, day);
|
|
31
|
+
date.setHours(hour || 0, minute || 0, second || 0, 0);
|
|
32
|
+
return date;
|
|
33
|
+
}
|
|
34
|
+
function effectiveComponents(components, withTime) {
|
|
35
|
+
return withTime ? components : { ...components, hour: 0, minute: 0, second: 0, epoch: undefined };
|
|
59
36
|
}
|
|
60
|
-
function componentsToOutputValue(components) {
|
|
37
|
+
function componentsToOutputValue(components, withTime, native) {
|
|
61
38
|
if (!components.year || !components.month || !components.day) {
|
|
62
39
|
return null;
|
|
63
40
|
}
|
|
64
|
-
const
|
|
41
|
+
const c = effectiveComponents(components, withTime);
|
|
42
|
+
if (native)
|
|
43
|
+
return componentsToLocalValue(c, withTime);
|
|
44
|
+
if (c.epoch !== undefined)
|
|
45
|
+
return new Date(c.epoch).toISOString();
|
|
46
|
+
const dateObj = componentsToDateObject(c);
|
|
65
47
|
return dateObj ? dateObj.toISOString() : null;
|
|
66
48
|
}
|
|
67
49
|
function componentsToLocalValue(components, withTime) {
|
|
@@ -74,253 +56,142 @@ const day = components.day.toString().padStart(2, '0');
|
|
|
74
56
|
if (withTime) {
|
|
75
57
|
const hour = (components.hour || 0).toString().padStart(2, '0');
|
|
76
58
|
const minute = (components.minute || 0).toString().padStart(2, '0');
|
|
77
|
-
|
|
59
|
+
const second = components.second ? `:${components.second.toString().padStart(2, '0')}` : '';
|
|
60
|
+
return `${year}-${month}-${day}T${hour}:${minute}${second}`;
|
|
78
61
|
}
|
|
79
62
|
else {
|
|
80
63
|
return `${year}-${month}-${day}`;
|
|
81
64
|
}
|
|
82
65
|
}
|
|
83
|
-
function
|
|
66
|
+
function displayDate(components, native) {
|
|
67
|
+
return native
|
|
68
|
+
? { date: wallClockDate(components), timeZone: 'UTC' }
|
|
69
|
+
: { date: componentsToDateObject(components) };
|
|
70
|
+
}
|
|
71
|
+
function formatDisplayValue(components, formatType, locale, withTime, native) {
|
|
84
72
|
if (!components.year || !components.month || !components.day) {
|
|
85
73
|
return null;
|
|
86
74
|
}
|
|
87
|
-
const
|
|
88
|
-
if (!
|
|
75
|
+
const { date, timeZone } = displayDate(components, native);
|
|
76
|
+
if (!date)
|
|
89
77
|
return null;
|
|
90
78
|
const options = {
|
|
91
79
|
dateStyle: formatType || 'long',
|
|
80
|
+
timeZone,
|
|
92
81
|
};
|
|
93
82
|
if (withTime) {
|
|
94
83
|
options.timeStyle = 'short';
|
|
95
84
|
}
|
|
96
85
|
const formatter = new Intl.DateTimeFormat(locale, options);
|
|
97
|
-
return formatter.format(
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
86
|
+
return formatter.format(date);
|
|
87
|
+
}
|
|
88
|
+
class TimeFields {
|
|
89
|
+
constructor(hourEl, minuteEl, onChange, hour = 0, minute = 0) {
|
|
90
|
+
this.hourEl = hourEl;
|
|
91
|
+
this.minuteEl = minuteEl;
|
|
92
|
+
this.onChange = onChange;
|
|
93
|
+
this.typed = '';
|
|
94
|
+
this.hour = hour;
|
|
95
|
+
this.minute = minute;
|
|
96
|
+
this.paint();
|
|
97
|
+
this.wire(hourEl, 'hour');
|
|
98
|
+
this.wire(minuteEl, 'minute');
|
|
99
|
+
}
|
|
100
|
+
static pad(n) {
|
|
101
|
+
return n.toString().padStart(2, '0');
|
|
102
|
+
}
|
|
103
|
+
paint() {
|
|
104
|
+
this.hourEl.value = TimeFields.pad(this.hour);
|
|
105
|
+
this.minuteEl.value = TimeFields.pad(this.minute);
|
|
106
|
+
}
|
|
107
|
+
wire(el, part) {
|
|
108
|
+
const max = part === 'hour' ? 23 : 59;
|
|
109
|
+
const maxLead = part === 'hour' ? 2 : 5;
|
|
110
|
+
el.addEventListener('focus', () => {
|
|
111
|
+
this.typed = '';
|
|
112
|
+
el.select();
|
|
113
|
+
});
|
|
114
|
+
el.addEventListener('click', () => el.select());
|
|
115
|
+
el.addEventListener('blur', () => { this.typed = ''; });
|
|
116
|
+
el.addEventListener('input', () => this.paint());
|
|
117
|
+
el.addEventListener('keydown', (e) => {
|
|
118
|
+
const set = (v) => {
|
|
119
|
+
if (part === 'hour')
|
|
120
|
+
this.hour = v;
|
|
121
|
+
else
|
|
122
|
+
this.minute = v;
|
|
123
|
+
this.paint();
|
|
124
|
+
this.onChange(this.hour, this.minute);
|
|
125
|
+
};
|
|
126
|
+
const current = part === 'hour' ? this.hour : this.minute;
|
|
127
|
+
if (/^\d$/.test(e.key)) {
|
|
128
|
+
e.preventDefault();
|
|
129
|
+
const digit = Number(e.key);
|
|
130
|
+
const next = this.typed + e.key;
|
|
131
|
+
const complete = next.length === 2 || (next.length === 1 && digit > maxLead);
|
|
132
|
+
set(Math.min(Number(next), max));
|
|
133
|
+
if (complete) {
|
|
134
|
+
this.typed = '';
|
|
135
|
+
if (part === 'hour')
|
|
136
|
+
this.minuteEl.focus();
|
|
137
|
+
else
|
|
138
|
+
el.select();
|
|
108
139
|
}
|
|
109
|
-
|
|
140
|
+
else {
|
|
141
|
+
this.typed = next;
|
|
110
142
|
}
|
|
111
|
-
|
|
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;
|
|
143
|
+
return;
|
|
118
144
|
}
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
case
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
const hourStr = hour.toString().padStart(2, '0');
|
|
129
|
-
const minuteStr = minute.toString().padStart(2, '0');
|
|
130
|
-
return `${hourStr}:${minuteStr}`;
|
|
131
|
-
}
|
|
132
|
-
function findNextEditablePosition(currentPos) {
|
|
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
|
-
}
|
|
141
|
-
}
|
|
142
|
-
function findPrevEditablePosition(currentPos) {
|
|
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
|
-
}
|
|
151
|
-
}
|
|
152
|
-
function positionToRawDigitIndex(internalPos) {
|
|
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
|
-
}
|
|
160
|
-
}
|
|
161
|
-
class TimeInput {
|
|
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();
|
|
145
|
+
switch (e.key) {
|
|
146
|
+
case 'ArrowUp':
|
|
147
|
+
case 'ArrowDown': {
|
|
148
|
+
e.preventDefault();
|
|
149
|
+
const delta = e.key === 'ArrowUp' ? 1 : -1;
|
|
150
|
+
this.typed = '';
|
|
151
|
+
set((current + delta + max + 1) % (max + 1));
|
|
152
|
+
el.select();
|
|
153
|
+
return;
|
|
176
154
|
}
|
|
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
155
|
case 'Backspace':
|
|
193
|
-
this.handleBackspace(event);
|
|
194
|
-
break;
|
|
195
156
|
case 'Delete':
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
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)
|
|
157
|
+
e.preventDefault();
|
|
158
|
+
if (part === 'minute' && this.typed === '' && e.key === 'Backspace' && current === 0) {
|
|
159
|
+
this.hourEl.focus();
|
|
243
160
|
return;
|
|
244
|
-
const nextPos = findNextEditablePosition(currentPos);
|
|
245
|
-
this.updateState({ ...newState, caretPosition: nextPos });
|
|
246
|
-
this.notifyTimeChange();
|
|
247
161
|
}
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
if (currentPos === 0)
|
|
162
|
+
this.typed = '';
|
|
163
|
+
set(0);
|
|
164
|
+
el.select();
|
|
252
165
|
return;
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
if (!newState)
|
|
166
|
+
case 'ArrowLeft':
|
|
167
|
+
if (part === 'minute') {
|
|
168
|
+
e.preventDefault();
|
|
169
|
+
this.hourEl.focus();
|
|
170
|
+
}
|
|
259
171
|
return;
|
|
260
|
-
|
|
261
|
-
|
|
172
|
+
case 'ArrowRight':
|
|
173
|
+
if (part === 'hour') {
|
|
174
|
+
e.preventDefault();
|
|
175
|
+
this.minuteEl.focus();
|
|
262
176
|
}
|
|
263
|
-
handleDelete(event) {
|
|
264
|
-
event.preventDefault();
|
|
265
|
-
const currentPos = this.state.caretPosition;
|
|
266
|
-
if (![0, 1, 3, 4].includes(currentPos))
|
|
267
177
|
return;
|
|
268
|
-
|
|
269
|
-
|
|
178
|
+
case 'Tab':
|
|
179
|
+
case 'Escape':
|
|
180
|
+
case 'Enter':
|
|
270
181
|
return;
|
|
271
|
-
|
|
272
|
-
|
|
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);
|
|
182
|
+
default:
|
|
183
|
+
if (e.key.length === 1 && !e.metaKey && !e.ctrlKey)
|
|
184
|
+
e.preventDefault();
|
|
303
185
|
}
|
|
304
|
-
|
|
305
|
-
this.datePickerElement.handleTimeInputChange(this.state.hour, this.state.minute);
|
|
186
|
+
});
|
|
306
187
|
}
|
|
307
188
|
getTime() {
|
|
308
|
-
return {
|
|
309
|
-
hour: this.state.hour,
|
|
310
|
-
minute: this.state.minute,
|
|
311
|
-
};
|
|
189
|
+
return { hour: this.hour, minute: this.minute };
|
|
312
190
|
}
|
|
313
191
|
setTime(hour, minute) {
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
this.
|
|
317
|
-
...this.state,
|
|
318
|
-
hour,
|
|
319
|
-
minute,
|
|
320
|
-
displayValue: display,
|
|
321
|
-
rawDigits,
|
|
322
|
-
};
|
|
323
|
-
this.element.value = display;
|
|
192
|
+
this.hour = hour;
|
|
193
|
+
this.minute = minute;
|
|
194
|
+
this.paint();
|
|
324
195
|
}
|
|
325
196
|
}
|
|
326
197
|
export class TyDatePicker extends TyComponent {
|
|
@@ -331,13 +202,19 @@ withTime: false,
|
|
|
331
202
|
open: false,
|
|
332
203
|
};
|
|
333
204
|
this._closeTimestamp = 0;
|
|
205
|
+
this._initialized = false;
|
|
206
|
+
this._syncing = false;
|
|
334
207
|
this._customFlavorSheet = null;
|
|
335
208
|
const shadow = this.shadowRoot;
|
|
336
209
|
ensureStyles(shadow, { css: datePickerStyles, id: 'ty-date-picker' });
|
|
337
210
|
}
|
|
338
211
|
onConnect() {
|
|
212
|
+
if (this._initialized)
|
|
213
|
+
this._state.open = false;
|
|
214
|
+
else
|
|
339
215
|
this.initializeState();
|
|
340
216
|
this.render();
|
|
217
|
+
this.updateFormValue();
|
|
341
218
|
this._localeObserver = observeLocaleChanges(this, () => {
|
|
342
219
|
this.render();
|
|
343
220
|
});
|
|
@@ -365,65 +242,73 @@ case 'label':
|
|
|
365
242
|
case 'placeholder':
|
|
366
243
|
case 'required':
|
|
367
244
|
case 'disabled':
|
|
368
|
-
|
|
245
|
+
this.updateValidity();
|
|
246
|
+
break;
|
|
247
|
+
case 'noClear':
|
|
369
248
|
case 'format':
|
|
370
249
|
case 'locale':
|
|
250
|
+
case 'error':
|
|
371
251
|
break;
|
|
372
252
|
case 'value': {
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
const hasDate = this._state.year !== undefined ||
|
|
376
|
-
this._state.month !== undefined ||
|
|
377
|
-
this._state.day !== undefined;
|
|
378
|
-
if (hasDate) {
|
|
253
|
+
if (!this._initialized || this._syncing)
|
|
254
|
+
break;
|
|
379
255
|
this._state = {
|
|
380
256
|
withTime: this._state.withTime,
|
|
381
257
|
open: this._state.open,
|
|
258
|
+
...parseDateTime(newValue),
|
|
382
259
|
};
|
|
383
|
-
}
|
|
384
260
|
this.syncFormValue();
|
|
385
261
|
break;
|
|
386
262
|
}
|
|
387
|
-
const currentComponents = {
|
|
388
|
-
year: this._state.year,
|
|
389
|
-
month: this._state.month,
|
|
390
|
-
day: this._state.day,
|
|
391
|
-
hour: this._state.hour,
|
|
392
|
-
minute: this._state.minute,
|
|
393
|
-
};
|
|
394
|
-
const changed = newComponents?.year !== currentComponents.year ||
|
|
395
|
-
newComponents?.month !== currentComponents.month ||
|
|
396
|
-
newComponents?.day !== currentComponents.day ||
|
|
397
|
-
newComponents?.hour !== currentComponents.hour ||
|
|
398
|
-
newComponents?.minute !== currentComponents.minute;
|
|
399
|
-
if (changed) {
|
|
400
|
-
this._state = {
|
|
401
|
-
...this._state,
|
|
402
|
-
...newComponents,
|
|
403
|
-
};
|
|
404
|
-
this.syncFormValue();
|
|
405
|
-
}
|
|
406
|
-
break;
|
|
407
|
-
}
|
|
408
263
|
case 'with-time': {
|
|
264
|
+
if (!this._initialized)
|
|
265
|
+
break;
|
|
409
266
|
const newWithTime = newValue;
|
|
410
|
-
|
|
411
|
-
if (newWithTime !== oldWithTime) {
|
|
267
|
+
if (newWithTime !== this._state.withTime) {
|
|
412
268
|
this._state.withTime = newWithTime;
|
|
413
|
-
const hasDate = this._state.year !== undefined &&
|
|
414
|
-
this._state.month !== undefined &&
|
|
415
|
-
this._state.day !== undefined;
|
|
416
|
-
if (hasDate) {
|
|
417
269
|
this.syncFormValue();
|
|
418
|
-
|
|
270
|
+
this.updateValidity();
|
|
419
271
|
}
|
|
420
272
|
break;
|
|
421
273
|
}
|
|
274
|
+
case 'native':
|
|
275
|
+
if (this._initialized)
|
|
276
|
+
this.syncFormValue();
|
|
277
|
+
break;
|
|
422
278
|
}
|
|
423
279
|
}
|
|
424
280
|
}
|
|
425
281
|
getFormValue() {
|
|
426
|
-
return
|
|
282
|
+
return this.outputValue(this.getComponents());
|
|
283
|
+
}
|
|
284
|
+
outputValue(components) {
|
|
285
|
+
return componentsToOutputValue(components, this._state.withTime, !!this.getProperty('native'));
|
|
286
|
+
}
|
|
287
|
+
updateFormValue() {
|
|
288
|
+
this._internals.setFormValue(this.getFormValue() ?? '');
|
|
289
|
+
this.updateValidity();
|
|
290
|
+
}
|
|
291
|
+
focusTarget() {
|
|
292
|
+
return this.shadowRoot?.querySelector('.native-date-input')
|
|
293
|
+
?? this.shadowRoot?.querySelector('.date-picker-stub')
|
|
294
|
+
?? null;
|
|
295
|
+
}
|
|
296
|
+
focus(options) {
|
|
297
|
+
if (this.getProperty('disabled'))
|
|
298
|
+
return;
|
|
299
|
+
const target = this.focusTarget();
|
|
300
|
+
if (target)
|
|
301
|
+
target.focus(options);
|
|
302
|
+
else
|
|
303
|
+
super.focus(options);
|
|
304
|
+
}
|
|
305
|
+
updateValidity() {
|
|
306
|
+
if (this.getProperty('disabled') || !this.getProperty('required') || this.getFormValue()) {
|
|
307
|
+
this.applyValidity({});
|
|
308
|
+
return;
|
|
309
|
+
}
|
|
310
|
+
const type = this._state.withTime ? 'datetime-local' : 'date';
|
|
311
|
+
this.applyValidity({ valueMissing: true }, nativeMessage('input', { type, required: true }), this.focusTarget() ?? undefined);
|
|
427
312
|
}
|
|
428
313
|
getComponents() {
|
|
429
314
|
return {
|
|
@@ -432,6 +317,8 @@ month: this._state.month,
|
|
|
432
317
|
day: this._state.day,
|
|
433
318
|
hour: this._state.hour,
|
|
434
319
|
minute: this._state.minute,
|
|
320
|
+
second: this._state.second,
|
|
321
|
+
epoch: this._state.epoch,
|
|
435
322
|
};
|
|
436
323
|
}
|
|
437
324
|
hasDate() {
|
|
@@ -441,25 +328,15 @@ getFormattedValue(components) {
|
|
|
441
328
|
const c = components || this.getComponents();
|
|
442
329
|
if (!c.year || !c.month || !c.day)
|
|
443
330
|
return null;
|
|
444
|
-
return formatDisplayValue(c, this.getProperty('format') || 'long', getEffectiveLocale(this, this.getProperty('locale')), this._state.withTime);
|
|
331
|
+
return formatDisplayValue(c, this.getProperty('format') || 'long', getEffectiveLocale(this, this.getProperty('locale')), this._state.withTime, !!this.getProperty('native'));
|
|
445
332
|
}
|
|
446
333
|
initializeState() {
|
|
447
|
-
const valueAttr = this.getProperty('value');
|
|
448
|
-
const withTime = this.getProperty('with-time');
|
|
449
|
-
const components = parseValue(valueAttr, withTime);
|
|
450
|
-
if (components === null) {
|
|
451
|
-
this._state = {
|
|
452
|
-
withTime,
|
|
453
|
-
open: false,
|
|
454
|
-
};
|
|
455
|
-
}
|
|
456
|
-
else {
|
|
457
334
|
this._state = {
|
|
458
|
-
...
|
|
459
|
-
withTime,
|
|
335
|
+
...parseDateTime(this.getProperty('value')),
|
|
336
|
+
withTime: this.getProperty('with-time'),
|
|
460
337
|
open: false,
|
|
461
338
|
};
|
|
462
|
-
|
|
339
|
+
this._initialized = true;
|
|
463
340
|
this.syncFormValue();
|
|
464
341
|
}
|
|
465
342
|
updateState(updates, forceSync = false) {
|
|
@@ -470,43 +347,55 @@ this.syncFormValue();
|
|
|
470
347
|
}
|
|
471
348
|
}
|
|
472
349
|
syncFormValue() {
|
|
473
|
-
const
|
|
350
|
+
const formValue = this.getFormValue();
|
|
474
351
|
const currentValue = this.getProperty('value');
|
|
475
|
-
if (
|
|
476
|
-
|
|
477
|
-
|
|
352
|
+
if (formValue !== currentValue) {
|
|
353
|
+
this._syncing = true;
|
|
354
|
+
try {
|
|
355
|
+
this.setProperty('value', formValue || null);
|
|
478
356
|
}
|
|
479
|
-
|
|
480
|
-
this.
|
|
357
|
+
finally {
|
|
358
|
+
this._syncing = false;
|
|
481
359
|
}
|
|
482
360
|
}
|
|
483
361
|
}
|
|
484
|
-
updateValidity() {
|
|
485
|
-
if (this.disabled) {
|
|
486
|
-
this._internals.setValidity({});
|
|
487
|
-
return;
|
|
488
|
-
}
|
|
489
|
-
if (this.required && !this.value) {
|
|
490
|
-
this._internals.setValidity({ valueMissing: true }, 'Please fill out this field');
|
|
491
|
-
}
|
|
492
|
-
else {
|
|
493
|
-
this._internals.setValidity({});
|
|
494
|
-
}
|
|
495
|
-
}
|
|
496
362
|
handleTimeInputChange(hour, minute) {
|
|
497
363
|
if (!this.hasDate())
|
|
498
364
|
return;
|
|
499
|
-
const components = { ...this.getComponents(), hour, minute };
|
|
365
|
+
const components = { ...this.getComponents(), hour, minute, second: 0, epoch: undefined };
|
|
500
366
|
this.updateState(components, true);
|
|
501
367
|
this.emitChangeEvent(components, 'time-change');
|
|
368
|
+
this.updateTimeSummary();
|
|
369
|
+
this.revealValidity();
|
|
370
|
+
}
|
|
371
|
+
updateTimeSummary(summary = this.shadowRoot?.querySelector('.time-summary')) {
|
|
372
|
+
if (!summary)
|
|
373
|
+
return;
|
|
374
|
+
const c = this.getComponents();
|
|
375
|
+
const text = summary.querySelector('.time-summary-date');
|
|
376
|
+
const iso = summary.querySelector('.time-summary-iso');
|
|
377
|
+
if (!c.year || !c.month || !c.day) {
|
|
378
|
+
text.textContent = 'Pick a day';
|
|
379
|
+
iso.textContent = '';
|
|
380
|
+
return;
|
|
381
|
+
}
|
|
382
|
+
const locale = getEffectiveLocale(this, this.getProperty('locale')) || undefined;
|
|
383
|
+
const { date, timeZone } = displayDate(c, !!this.getProperty('native'));
|
|
384
|
+
const day = new Intl.DateTimeFormat(locale, { weekday: 'short', month: 'short', day: 'numeric', timeZone }).format(date);
|
|
385
|
+
const time = new Intl.DateTimeFormat(locale, { hour: '2-digit', minute: '2-digit', hourCycle: 'h23', timeZone }).format(date);
|
|
386
|
+
text.textContent = `${day} · ${time}`;
|
|
387
|
+
iso.textContent = componentsToLocalValue(c, true) ?? '';
|
|
502
388
|
}
|
|
503
389
|
emitChangeEvent(components, source) {
|
|
504
|
-
const
|
|
390
|
+
const value = components ? this.outputValue(components) : null;
|
|
505
391
|
const localValue = components ? componentsToLocalValue(components, this._state.withTime) : null;
|
|
506
|
-
const
|
|
392
|
+
const effective = components && effectiveComponents(components, this._state.withTime);
|
|
393
|
+
const milliseconds = effective
|
|
394
|
+
? effective.epoch ?? componentsToDateObject(effective)?.getTime() ?? null
|
|
395
|
+
: null;
|
|
507
396
|
const formatted = components ? this.getFormattedValue(components) : null;
|
|
508
397
|
const detail = {
|
|
509
|
-
value
|
|
398
|
+
value,
|
|
510
399
|
localValue,
|
|
511
400
|
milliseconds,
|
|
512
401
|
formatted,
|
|
@@ -539,24 +428,34 @@ get required() { return this.getProperty('required'); }
|
|
|
539
428
|
set required(v) { this.setProperty('required', v); }
|
|
540
429
|
get disabled() { return this.getProperty('disabled'); }
|
|
541
430
|
set disabled(v) { this.setProperty('disabled', v); }
|
|
542
|
-
get
|
|
543
|
-
set
|
|
431
|
+
get noClear() { return this.getProperty('noClear'); }
|
|
432
|
+
set noClear(v) { this.setProperty('noClear', v); }
|
|
544
433
|
get min() { return this.getProperty('min'); }
|
|
545
434
|
set min(v) { this.setProperty('min', v ?? ''); }
|
|
546
435
|
get max() { return this.getProperty('max'); }
|
|
547
436
|
set max(v) { this.setProperty('max', v ?? ''); }
|
|
548
437
|
get withTime() { return this.getProperty('with-time'); }
|
|
549
438
|
set withTime(v) { this.setProperty('with-time', v); }
|
|
439
|
+
get native() { return this.getProperty('native'); }
|
|
440
|
+
set native(v) { this.setProperty('native', v); }
|
|
441
|
+
get error() { return this.getProperty('error'); }
|
|
442
|
+
set error(v) { this.setProperty('error', v); }
|
|
550
443
|
get value() {
|
|
444
|
+
if (this._initialized)
|
|
551
445
|
return this.getProperty('value') || null;
|
|
446
|
+
const parsed = parseDateTime(this.getProperty('value'));
|
|
447
|
+
return parsed && componentsToOutputValue(parsed, !!this.getProperty('with-time'), !!this.getProperty('native'));
|
|
552
448
|
}
|
|
553
449
|
set value(val) {
|
|
554
450
|
if (val === null || val === undefined || val === '') {
|
|
555
451
|
this.setProperty('value', null);
|
|
556
452
|
}
|
|
453
|
+
else if (typeof val === 'string') {
|
|
454
|
+
this.setProperty('value', val);
|
|
455
|
+
}
|
|
557
456
|
else {
|
|
558
|
-
const
|
|
559
|
-
this.setProperty('value',
|
|
457
|
+
const date = new Date(val);
|
|
458
|
+
this.setProperty('value', isNaN(date.getTime()) ? null : date.toISOString());
|
|
560
459
|
}
|
|
561
460
|
}
|
|
562
461
|
clear() {
|
|
@@ -568,6 +467,8 @@ if (this.getProperty('disabled'))
|
|
|
568
467
|
classes.push('disabled');
|
|
569
468
|
if (this.getProperty('required'))
|
|
570
469
|
classes.push('required');
|
|
470
|
+
if (this.getProperty('error'))
|
|
471
|
+
classes.push('error');
|
|
571
472
|
if (this._state.open)
|
|
572
473
|
classes.push('open');
|
|
573
474
|
return classes.join(' ');
|
|
@@ -590,7 +491,7 @@ stub.setAttribute('aria-labelledby', `date-picker-label-${getElementHash(this)}`
|
|
|
590
491
|
}
|
|
591
492
|
stub.addEventListener('keydown', (e) => {
|
|
592
493
|
const ke = e;
|
|
593
|
-
if (this._state.open)
|
|
494
|
+
if (this._state.open || e.target !== stub)
|
|
594
495
|
return;
|
|
595
496
|
if (ke.key === 'Enter' || ke.key === ' ') {
|
|
596
497
|
ke.preventDefault();
|
|
@@ -609,7 +510,7 @@ displayText.classList.add('placeholder');
|
|
|
609
510
|
}
|
|
610
511
|
const iconContainer = document.createElement('div');
|
|
611
512
|
iconContainer.className = 'stub-icons';
|
|
612
|
-
const clearable = this.getProperty('
|
|
513
|
+
const clearable = !this.getProperty('noClear');
|
|
613
514
|
if (clearable && formattedValue && !isDisabled) {
|
|
614
515
|
const clearButton = document.createElement('button');
|
|
615
516
|
clearButton.className = 'stub-clear';
|
|
@@ -628,31 +529,55 @@ stub.appendChild(displayText);
|
|
|
628
529
|
stub.appendChild(iconContainer);
|
|
629
530
|
return stub;
|
|
630
531
|
}
|
|
631
|
-
|
|
532
|
+
createTimeField(part) {
|
|
632
533
|
const input = document.createElement('input');
|
|
633
534
|
input.type = 'text';
|
|
634
|
-
input.className =
|
|
635
|
-
input.
|
|
535
|
+
input.className = `time-input time-${part}`;
|
|
536
|
+
input.inputMode = 'numeric';
|
|
636
537
|
input.autocomplete = 'off';
|
|
637
|
-
input.maxLength =
|
|
538
|
+
input.maxLength = 2;
|
|
539
|
+
input.setAttribute('aria-label', part === 'hour' ? 'Hours' : 'Minutes');
|
|
638
540
|
return input;
|
|
639
541
|
}
|
|
640
542
|
renderTimeSection() {
|
|
641
543
|
const timeSection = document.createElement('div');
|
|
642
544
|
timeSection.className = 'time-section';
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
const
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
545
|
+
timeSection.setAttribute('role', 'group');
|
|
546
|
+
timeSection.setAttribute('aria-label', 'Time');
|
|
547
|
+
const row = document.createElement('div');
|
|
548
|
+
row.className = 'time-row';
|
|
549
|
+
const label = document.createElement('span');
|
|
550
|
+
label.className = 'time-label';
|
|
551
|
+
const icon = document.createElement('span');
|
|
552
|
+
icon.className = 'time-icon';
|
|
553
|
+
icon.innerHTML = SCHEDULE_ICON_SVG;
|
|
554
|
+
label.appendChild(icon);
|
|
555
|
+
label.appendChild(document.createTextNode('Time'));
|
|
556
|
+
const fields = document.createElement('div');
|
|
557
|
+
fields.className = 'time-fields';
|
|
558
|
+
const hourEl = this.createTimeField('hour');
|
|
559
|
+
const minuteEl = this.createTimeField('minute');
|
|
560
|
+
const colon = document.createElement('span');
|
|
561
|
+
colon.className = 'time-colon';
|
|
562
|
+
colon.textContent = ':';
|
|
563
|
+
fields.appendChild(hourEl);
|
|
564
|
+
fields.appendChild(colon);
|
|
565
|
+
fields.appendChild(minuteEl);
|
|
566
|
+
this._timeInput = new TimeFields(hourEl, minuteEl, (hour, minute) => this.handleTimeInputChange(hour, minute), this._state.hour || 0, this._state.minute || 0);
|
|
567
|
+
row.appendChild(label);
|
|
568
|
+
row.appendChild(fields);
|
|
569
|
+
const summary = document.createElement('div');
|
|
570
|
+
summary.className = 'time-summary';
|
|
571
|
+
summary.setAttribute('aria-live', 'polite');
|
|
572
|
+
const summaryDate = document.createElement('span');
|
|
573
|
+
summaryDate.className = 'time-summary-date';
|
|
574
|
+
const summaryIso = document.createElement('span');
|
|
575
|
+
summaryIso.className = 'time-summary-iso';
|
|
576
|
+
summary.appendChild(summaryDate);
|
|
577
|
+
summary.appendChild(summaryIso);
|
|
578
|
+
this.updateTimeSummary(summary);
|
|
579
|
+
timeSection.appendChild(row);
|
|
580
|
+
timeSection.appendChild(summary);
|
|
656
581
|
return timeSection;
|
|
657
582
|
}
|
|
658
583
|
renderCalendarDropdown() {
|
|
@@ -662,7 +587,7 @@ const contentWrapper = document.createElement('div');
|
|
|
662
587
|
contentWrapper.className = 'calendar-content';
|
|
663
588
|
const calendar = document.createElement('ty-calendar');
|
|
664
589
|
if (this._state.year && this._state.month && this._state.day) {
|
|
665
|
-
calendar.setAttribute('year', this._state.year.
|
|
590
|
+
calendar.setAttribute('year', String(this._state.year).padStart(4, '0'));
|
|
666
591
|
calendar.setAttribute('month', this._state.month.toString());
|
|
667
592
|
calendar.setAttribute('day', this._state.day.toString());
|
|
668
593
|
}
|
|
@@ -676,8 +601,8 @@ if (min)
|
|
|
676
601
|
calendar.setAttribute('min', min);
|
|
677
602
|
if (max)
|
|
678
603
|
calendar.setAttribute('max', max);
|
|
679
|
-
const flavor = this.
|
|
680
|
-
if (flavor
|
|
604
|
+
const flavor = this.calendarFlavor();
|
|
605
|
+
if (flavor)
|
|
681
606
|
calendar.setAttribute('flavor', flavor);
|
|
682
607
|
calendar.addEventListener('change', (e) => this.handleCalendarChange(e));
|
|
683
608
|
contentWrapper.appendChild(calendar);
|
|
@@ -685,11 +610,18 @@ if (this._state.withTime) {
|
|
|
685
610
|
contentWrapper.appendChild(this.renderTimeSection());
|
|
686
611
|
}
|
|
687
612
|
dialog.appendChild(contentWrapper);
|
|
688
|
-
|
|
689
|
-
this.
|
|
690
|
-
|
|
613
|
+
new MutationObserver(() => {
|
|
614
|
+
if (this._state.open && !dialog.open && dialog.isConnected && this.isConnected)
|
|
615
|
+
this.closeDropdown();
|
|
616
|
+
}).observe(dialog, { attributes: true, attributeFilter: ['open'] });
|
|
691
617
|
return dialog;
|
|
692
618
|
}
|
|
619
|
+
calendarFlavor() {
|
|
620
|
+
if (this.getProperty('error'))
|
|
621
|
+
return 'danger';
|
|
622
|
+
const flavor = this.getProperty('flavor');
|
|
623
|
+
return flavor && flavor !== 'default' ? flavor : null;
|
|
624
|
+
}
|
|
693
625
|
renderNativeInput() {
|
|
694
626
|
const wrapper = document.createElement('div');
|
|
695
627
|
wrapper.className = 'dropdown-wrapper';
|
|
@@ -701,21 +633,8 @@ stub.setAttribute('disabled', 'true');
|
|
|
701
633
|
}
|
|
702
634
|
const input = document.createElement('input');
|
|
703
635
|
input.className = 'native-date-input';
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
input.disabled = true;
|
|
707
|
-
if (this.getProperty('required'))
|
|
708
|
-
input.required = true;
|
|
709
|
-
const minBound = this.getProperty('min');
|
|
710
|
-
const maxBound = this.getProperty('max');
|
|
711
|
-
if (minBound)
|
|
712
|
-
input.min = this._state.withTime ? `${minBound}T00:00` : minBound;
|
|
713
|
-
if (maxBound)
|
|
714
|
-
input.max = this._state.withTime ? `${maxBound}T23:59` : maxBound;
|
|
715
|
-
const localValue = componentsToLocalValue(this.getComponents(), this._state.withTime);
|
|
716
|
-
if (localValue) {
|
|
717
|
-
input.value = localValue;
|
|
718
|
-
}
|
|
636
|
+
this.syncNativeInput(input);
|
|
637
|
+
const localValue = input.value;
|
|
719
638
|
const placeholder = this.getProperty('placeholder') || 'Select date...';
|
|
720
639
|
const formattedValue = this.getFormattedValue();
|
|
721
640
|
const placeholderEl = document.createElement('span');
|
|
@@ -724,13 +643,17 @@ placeholderEl.textContent = formattedValue || placeholder;
|
|
|
724
643
|
input.addEventListener('change', () => {
|
|
725
644
|
if (!input.value) {
|
|
726
645
|
this.clearValue();
|
|
727
|
-
return;
|
|
728
646
|
}
|
|
729
|
-
|
|
647
|
+
else {
|
|
648
|
+
const parsed = parseDateTime(input.value);
|
|
730
649
|
if (parsed) {
|
|
731
|
-
this.
|
|
732
|
-
|
|
650
|
+
const { hour, minute, second } = this._state.withTime ? parsed : this._state;
|
|
651
|
+
const components = { ...parsed, hour, minute, second, epoch: undefined };
|
|
652
|
+
this.updateState(components, true);
|
|
653
|
+
this.emitChangeEvent(components, 'selection');
|
|
733
654
|
}
|
|
655
|
+
}
|
|
656
|
+
this.revealValidity();
|
|
734
657
|
});
|
|
735
658
|
const startSlot = document.createElement('slot');
|
|
736
659
|
startSlot.name = 'start';
|
|
@@ -739,7 +662,7 @@ stub.appendChild(input);
|
|
|
739
662
|
stub.appendChild(placeholderEl);
|
|
740
663
|
const iconContainer = document.createElement('div');
|
|
741
664
|
iconContainer.className = 'stub-icons';
|
|
742
|
-
const clearable = this.getProperty('
|
|
665
|
+
const clearable = !this.getProperty('noClear');
|
|
743
666
|
if (clearable && localValue && !isDisabled) {
|
|
744
667
|
const clearButton = document.createElement('button');
|
|
745
668
|
clearButton.className = 'stub-clear';
|
|
@@ -756,6 +679,23 @@ stub.appendChild(iconContainer);
|
|
|
756
679
|
wrapper.appendChild(stub);
|
|
757
680
|
return wrapper;
|
|
758
681
|
}
|
|
682
|
+
syncNativeInput(input) {
|
|
683
|
+
const withTime = this._state.withTime;
|
|
684
|
+
input.type = withTime ? 'datetime-local' : 'date';
|
|
685
|
+
input.disabled = !!this.getProperty('disabled');
|
|
686
|
+
input.required = !!this.getProperty('required');
|
|
687
|
+
const min = toISODay(this.getProperty('min'));
|
|
688
|
+
const max = toISODay(this.getProperty('max'));
|
|
689
|
+
if (min)
|
|
690
|
+
input.min = withTime ? `${min}T00:00` : min;
|
|
691
|
+
else
|
|
692
|
+
input.removeAttribute('min');
|
|
693
|
+
if (max)
|
|
694
|
+
input.max = withTime ? `${max}T23:59` : max;
|
|
695
|
+
else
|
|
696
|
+
input.removeAttribute('max');
|
|
697
|
+
input.value = componentsToLocalValue(this.getComponents(), withTime) ?? '';
|
|
698
|
+
}
|
|
759
699
|
calculateCalendarPosition() {
|
|
760
700
|
if (!this.shadowRoot)
|
|
761
701
|
return;
|
|
@@ -811,7 +751,6 @@ return container;
|
|
|
811
751
|
render() {
|
|
812
752
|
if (!this.shadowRoot)
|
|
813
753
|
return;
|
|
814
|
-
this.updateValidity();
|
|
815
754
|
ensureStyles(this.shadowRoot, { css: datePickerStyles, id: 'ty-date-picker' });
|
|
816
755
|
if (isMobileTouch()) {
|
|
817
756
|
const existingNativeInput = this.shadowRoot.querySelector('.native-date-input');
|
|
@@ -823,6 +762,8 @@ this.shadowRoot.innerHTML = '';
|
|
|
823
762
|
const container = this.renderContainer();
|
|
824
763
|
container.appendChild(this.renderNativeInput());
|
|
825
764
|
this.shadowRoot.appendChild(container);
|
|
765
|
+
this.syncErrorLine();
|
|
766
|
+
this.updateValidity();
|
|
826
767
|
return;
|
|
827
768
|
}
|
|
828
769
|
const existingDialog = this.shadowRoot.querySelector('.calendar-dialog');
|
|
@@ -832,6 +773,7 @@ this.updateDisplay();
|
|
|
832
773
|
this.calculateCalendarPosition();
|
|
833
774
|
return;
|
|
834
775
|
}
|
|
776
|
+
const hadFocus = !!this.shadowRoot.activeElement;
|
|
835
777
|
this.shadowRoot.innerHTML = '';
|
|
836
778
|
const container = this.renderContainer();
|
|
837
779
|
const wrapper = document.createElement('div');
|
|
@@ -840,7 +782,33 @@ wrapper.appendChild(this.renderStub());
|
|
|
840
782
|
wrapper.appendChild(this.renderCalendarDropdown());
|
|
841
783
|
container.appendChild(wrapper);
|
|
842
784
|
this.shadowRoot.appendChild(container);
|
|
785
|
+
this.syncErrorLine();
|
|
843
786
|
this.setupEventListeners();
|
|
787
|
+
this.updateValidity();
|
|
788
|
+
if (hadFocus)
|
|
789
|
+
this.focus({ preventScroll: true });
|
|
790
|
+
}
|
|
791
|
+
syncErrorLine() {
|
|
792
|
+
const container = this.shadowRoot?.querySelector('.dropdown-container');
|
|
793
|
+
if (!container)
|
|
794
|
+
return;
|
|
795
|
+
const error = this.getProperty('error');
|
|
796
|
+
const errorId = `date-picker-error-${getElementHash(this)}`;
|
|
797
|
+
let line = container.querySelector(':scope > .error-message');
|
|
798
|
+
if (error) {
|
|
799
|
+
if (!line) {
|
|
800
|
+
line = document.createElement('div');
|
|
801
|
+
line.className = 'error-message';
|
|
802
|
+
line.id = errorId;
|
|
803
|
+
container.appendChild(line);
|
|
804
|
+
}
|
|
805
|
+
line.textContent = error;
|
|
806
|
+
this.focusTarget()?.setAttribute('aria-describedby', errorId);
|
|
807
|
+
}
|
|
808
|
+
else {
|
|
809
|
+
line?.remove();
|
|
810
|
+
this.focusTarget()?.removeAttribute('aria-describedby');
|
|
811
|
+
}
|
|
844
812
|
}
|
|
845
813
|
updateDisplay() {
|
|
846
814
|
if (!this.shadowRoot)
|
|
@@ -848,7 +816,7 @@ return;
|
|
|
848
816
|
const formattedValue = this.getFormattedValue();
|
|
849
817
|
const placeholder = this.getProperty('placeholder') || 'Select date...';
|
|
850
818
|
const hasValue = this.hasDate();
|
|
851
|
-
const clearable = this.getProperty('
|
|
819
|
+
const clearable = !this.getProperty('noClear');
|
|
852
820
|
const isDisabled = this.getProperty('disabled');
|
|
853
821
|
const stubText = this.shadowRoot.querySelector('.stub-text');
|
|
854
822
|
if (stubText) {
|
|
@@ -859,23 +827,26 @@ const clearButton = this.shadowRoot.querySelector('.stub-clear');
|
|
|
859
827
|
if (clearButton) {
|
|
860
828
|
clearButton.style.display = (clearable && hasValue && !isDisabled) ? '' : 'none';
|
|
861
829
|
}
|
|
830
|
+
const stub = this.shadowRoot.querySelector('.date-picker-stub');
|
|
831
|
+
if (stub)
|
|
832
|
+
stub.className = this.buildStubClasses();
|
|
833
|
+
this.syncErrorLine();
|
|
862
834
|
if (isMobileTouch()) {
|
|
863
835
|
const nativeInput = this.shadowRoot.querySelector('.native-date-input');
|
|
864
|
-
if (nativeInput)
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
}
|
|
836
|
+
if (nativeInput)
|
|
837
|
+
this.syncNativeInput(nativeInput);
|
|
838
|
+
stub?.toggleAttribute('disabled', !!isDisabled);
|
|
868
839
|
return;
|
|
869
840
|
}
|
|
870
841
|
const calendar = this.shadowRoot.querySelector('ty-calendar');
|
|
871
842
|
if (calendar && hasValue) {
|
|
872
|
-
calendar.setAttribute('year', this._state.year.
|
|
843
|
+
calendar.setAttribute('year', String(this._state.year).padStart(4, '0'));
|
|
873
844
|
calendar.setAttribute('month', this._state.month.toString());
|
|
874
845
|
calendar.setAttribute('day', this._state.day.toString());
|
|
875
846
|
}
|
|
876
847
|
if (calendar) {
|
|
877
|
-
const flavor = this.
|
|
878
|
-
if (flavor
|
|
848
|
+
const flavor = this.calendarFlavor();
|
|
849
|
+
if (flavor)
|
|
879
850
|
calendar.setAttribute('flavor', flavor);
|
|
880
851
|
else
|
|
881
852
|
calendar.removeAttribute('flavor');
|
|
@@ -893,6 +864,7 @@ calendar.removeAttribute('max');
|
|
|
893
864
|
if (this._timeInput && this._state.hour !== undefined && this._state.minute !== undefined) {
|
|
894
865
|
this._timeInput.setTime(this._state.hour, this._state.minute);
|
|
895
866
|
}
|
|
867
|
+
this.updateTimeSummary();
|
|
896
868
|
}
|
|
897
869
|
setupEventListeners() {
|
|
898
870
|
if (this._clickListener) {
|
|
@@ -916,7 +888,7 @@ event.preventDefault();
|
|
|
916
888
|
const dialog = this.shadowRoot?.querySelector('.calendar-dialog');
|
|
917
889
|
if (this._state.open && (!dialog || !dialog.open)) {
|
|
918
890
|
this.updateState({ open: false }, true);
|
|
919
|
-
unlockScroll(`date-picker-${this
|
|
891
|
+
unlockScroll(`date-picker-${getElementHash(this)}`);
|
|
920
892
|
}
|
|
921
893
|
const disabled = this.getProperty('disabled');
|
|
922
894
|
if (!disabled && Date.now() - this._closeTimestamp > 300) {
|
|
@@ -930,6 +902,8 @@ month: undefined,
|
|
|
930
902
|
day: undefined,
|
|
931
903
|
hour: undefined,
|
|
932
904
|
minute: undefined,
|
|
905
|
+
second: undefined,
|
|
906
|
+
epoch: undefined,
|
|
933
907
|
}, true);
|
|
934
908
|
this.value = null;
|
|
935
909
|
this.emitChangeEvent(null, 'clear');
|
|
@@ -939,6 +913,11 @@ handleClearClick(event) {
|
|
|
939
913
|
event.preventDefault();
|
|
940
914
|
event.stopPropagation();
|
|
941
915
|
this.clearValue();
|
|
916
|
+
const active = document.activeElement;
|
|
917
|
+
if (this.matches(':focus-within') || !active || active === document.body) {
|
|
918
|
+
this.shadowRoot?.querySelector('.native-date-input')?.focus({ preventScroll: true });
|
|
919
|
+
}
|
|
920
|
+
this.revealValidity();
|
|
942
921
|
}
|
|
943
922
|
handleCalendarChange(event) {
|
|
944
923
|
event.preventDefault();
|
|
@@ -954,9 +933,12 @@ month: dayContext.month,
|
|
|
954
933
|
day: dayContext.dayInMonth,
|
|
955
934
|
hour: this._state.hour,
|
|
956
935
|
minute: this._state.minute,
|
|
936
|
+
second: this._state.second,
|
|
937
|
+
epoch: undefined,
|
|
957
938
|
};
|
|
958
939
|
this.updateState(newComponents, true);
|
|
959
940
|
this.emitChangeEvent(newComponents, 'selection');
|
|
941
|
+
this.revealValidity();
|
|
960
942
|
if (!this._state.withTime) {
|
|
961
943
|
this.closeDropdown();
|
|
962
944
|
}
|
|
@@ -964,7 +946,8 @@ else {
|
|
|
964
946
|
requestAnimationFrame(() => {
|
|
965
947
|
if (!this.shadowRoot)
|
|
966
948
|
return;
|
|
967
|
-
|
|
949
|
+
this.updateTimeSummary();
|
|
950
|
+
const timeInput = this.shadowRoot.querySelector('.time-hour');
|
|
968
951
|
if (timeInput) {
|
|
969
952
|
timeInput.focus();
|
|
970
953
|
}
|
|
@@ -1009,9 +992,15 @@ this.render();
|
|
|
1009
992
|
requestAnimationFrame(() => {
|
|
1010
993
|
if (!this.shadowRoot)
|
|
1011
994
|
return;
|
|
995
|
+
if (!this._state.open || !this.isConnected)
|
|
996
|
+
return;
|
|
997
|
+
if (this.shadowRoot.querySelector('.date-picker-stub')?.checkVisibility() === false) {
|
|
998
|
+
this.closeDropdown();
|
|
999
|
+
return;
|
|
1000
|
+
}
|
|
1012
1001
|
const dialog = this.shadowRoot.querySelector('.calendar-dialog');
|
|
1013
1002
|
if (dialog) {
|
|
1014
|
-
const pickerId = `date-picker-${this
|
|
1003
|
+
const pickerId = `date-picker-${getElementHash(this)}`;
|
|
1015
1004
|
lockScroll(pickerId);
|
|
1016
1005
|
dialog.showModal();
|
|
1017
1006
|
this.calculateCalendarPosition();
|
|
@@ -1030,7 +1019,7 @@ closeDropdown() {
|
|
|
1030
1019
|
if (!this._state.open)
|
|
1031
1020
|
return;
|
|
1032
1021
|
this._closeTimestamp = Date.now();
|
|
1033
|
-
const pickerId = `date-picker-${this
|
|
1022
|
+
const pickerId = `date-picker-${getElementHash(this)}`;
|
|
1034
1023
|
this.updateState({ open: false }, true);
|
|
1035
1024
|
unlockScroll(pickerId);
|
|
1036
1025
|
if (this.shadowRoot) {
|
|
@@ -1045,6 +1034,7 @@ stubEl.setAttribute('aria-expanded', 'false');
|
|
|
1045
1034
|
}
|
|
1046
1035
|
this.dispatchEvent(new CustomEvent('close'));
|
|
1047
1036
|
this.render();
|
|
1037
|
+
this.focus();
|
|
1048
1038
|
}
|
|
1049
1039
|
cleanup() {
|
|
1050
1040
|
if (this._clickListener) {
|
|
@@ -1060,7 +1050,7 @@ dialog.removeEventListener('click', this._dialogClickListener);
|
|
|
1060
1050
|
}
|
|
1061
1051
|
}
|
|
1062
1052
|
if (this._state.open) {
|
|
1063
|
-
const pickerId = `date-picker-${this
|
|
1053
|
+
const pickerId = `date-picker-${getElementHash(this)}`;
|
|
1064
1054
|
unlockScroll(pickerId);
|
|
1065
1055
|
}
|
|
1066
1056
|
this._clickListener = undefined;
|
|
@@ -1128,13 +1118,15 @@ type: 'boolean',
|
|
|
1128
1118
|
visual: true,
|
|
1129
1119
|
default: false
|
|
1130
1120
|
},
|
|
1131
|
-
|
|
1121
|
+
noClear: {
|
|
1132
1122
|
type: 'boolean',
|
|
1133
1123
|
visual: true,
|
|
1134
|
-
default:
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1124
|
+
default: false
|
|
1125
|
+
},
|
|
1126
|
+
error: {
|
|
1127
|
+
type: 'string',
|
|
1128
|
+
visual: true,
|
|
1129
|
+
default: ''
|
|
1138
1130
|
},
|
|
1139
1131
|
format: {
|
|
1140
1132
|
type: 'string',
|
|
@@ -1166,6 +1158,11 @@ default: null
|
|
|
1166
1158
|
type: 'boolean',
|
|
1167
1159
|
visual: true,
|
|
1168
1160
|
default: false
|
|
1161
|
+
},
|
|
1162
|
+
native: {
|
|
1163
|
+
type: 'boolean',
|
|
1164
|
+
visual: true,
|
|
1165
|
+
default: false
|
|
1169
1166
|
}
|
|
1170
1167
|
};
|
|
1171
1168
|
TyDatePicker.formAssociated = true;
|