@dile/utils 3.4.0 → 3.4.1

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.
@@ -200,7 +200,7 @@ export class DileDatepicker extends DileCloseOnEscPressed(DileInputIcon) {
200
200
  }
201
201
 
202
202
  renderIconButton() {
203
- if (this.disabled) {
203
+ if (this.isDisabled) {
204
204
  return super.renderIconButton();
205
205
  }
206
206
  return html`
@@ -1,17 +1,26 @@
1
- import { html, css } from 'lit';
1
+ import { html, css, svg, nothing } from 'lit';
2
2
  import { DileDatepicker } from './DileDatepicker.js';
3
3
  import '@dile/iconlib/lucide-icons/calendar-clock.js';
4
- import '@dile/ui/components/time-picker/time-picker.js';
4
+ import '@dile/ui/components/wheel-picker/wheel-picker.js';
5
5
  import '@dile/ui/components/icon/icon.js';
6
6
  import { doneIcon } from '@dile/icons';
7
7
 
8
+ const checkIcon = svg`<path d="M9 16.17 4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z"/>`;
9
+ const pad = n => String(n).padStart(2, '0');
10
+
8
11
  function formatToIsoOnlyDate(date) {
9
- const pad = n => String(n).padStart(2, '0');
10
- return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}`;
12
+ return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}`;
11
13
  }
12
14
 
15
+ /**
16
+ * Converts ISO 8601 date-times ("2024-03-15T10:30:00Z", "2024-03-15T10:30:00+02:00",
17
+ * "2024-03-15T10:30") to the "YYYY-MM-DD HH:mm:ss" format, in local time.
18
+ * Other values are returned without changes.
19
+ * @param {string} dateStr
20
+ * @returns {string}
21
+ */
13
22
  function formatIfIso8601(dateStr) {
14
- const iso8601Regex = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?Z$/;
23
+ const iso8601Regex = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}(?::\d{2}(?:\.\d+)?)?(?:Z|[+-]\d{2}:?\d{2})?$/;
15
24
 
16
25
  if (!iso8601Regex.test(dateStr)) {
17
26
  return dateStr;
@@ -22,11 +31,52 @@ function formatIfIso8601(dateStr) {
22
31
  return dateStr;
23
32
  }
24
33
 
25
- const pad = n => n.toString().padStart(2, '0');
26
- return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())} ` +
34
+ return `${formatToIsoOnlyDate(date)} ` +
27
35
  `${pad(date.getHours())}:${pad(date.getMinutes())}:${pad(date.getSeconds())}`;
28
36
  }
29
37
 
38
+ /**
39
+ * Reads the "HH:mm" or "HH:mm:ss" time written after the date in the value.
40
+ * @param {string} value
41
+ * @returns {string|undefined} the time as "HH:mm:ss", or undefined when there is no valid time
42
+ */
43
+ export function valueToTime(value) {
44
+ const match = /(?:^|[ T])(\d{1,2}):(\d{1,2})(?::(\d{1,2}))?/.exec(String(value ?? '').trim());
45
+ if (!match) {
46
+ return undefined;
47
+ }
48
+ const [hours, minutes, seconds = 0] = match.slice(1).map((part) => Number(part ?? 0));
49
+ if (hours > 23 || minutes > 59 || seconds > 59) {
50
+ return undefined;
51
+ }
52
+ return `${pad(hours)}:${pad(minutes)}:${pad(seconds)}`;
53
+ }
54
+
55
+ function timeToParts(time) {
56
+ const [hours, minutes, seconds] = time.split(':').map(Number);
57
+ return { hours, minutes, seconds };
58
+ }
59
+
60
+ function validStep(step) {
61
+ const value = Math.round(Number(step));
62
+ return value >= 1 && value <= 59 ? value : 1;
63
+ }
64
+
65
+ /**
66
+ * Wheel items from 0 to max with the given step. A current value out of the step is also included.
67
+ */
68
+ function stepItems(max, step, current) {
69
+ const values = [];
70
+ for (let value = 0; value <= max; value += step) {
71
+ values.push(value);
72
+ }
73
+ if (Number.isInteger(current) && !values.includes(current)) {
74
+ values.push(current);
75
+ values.sort((a, b) => a - b);
76
+ }
77
+ return values.map((value) => ({ value, label: pad(value) }));
78
+ }
79
+
30
80
  export class DileDatetimepicker extends DileDatepicker {
31
81
 
32
82
  static get styles() {
@@ -39,6 +89,58 @@ export class DileDatetimepicker extends DileDatepicker {
39
89
  gap: 0.5rem;
40
90
  align-items: center;
41
91
  }
92
+ .date-time-body {
93
+ position: relative;
94
+ }
95
+ .date-time-body.time-open > dile-calendar {
96
+ visibility: hidden;
97
+ }
98
+ .time-layer {
99
+ position: absolute;
100
+ inset: 0;
101
+ display: flex;
102
+ flex-direction: column;
103
+ align-items: center;
104
+ justify-content: center;
105
+ gap: 0.5rem;
106
+ box-sizing: border-box;
107
+ --dile-wheel-picker-width: var(--dile-datetimepicker-wheel-width, 4.5rem);
108
+ --dile-wheel-picker-item-height: var(--dile-calendar-wheel-item-height, 36px);
109
+ --dile-wheel-picker-font-size: var(--dile-calendar-wheel-font-size, 1.1rem);
110
+ --dile-wheel-picker-text-color: var(--dile-calendar-wheel-text-color, color-mix(in srgb, var(--dile-calendar-day-text-color, #303030) 60%, transparent));
111
+ --dile-wheel-picker-selected-text-color: var(--dile-calendar-wheel-selected-text-color, var(--dile-calendar-day-text-color, #303030));
112
+ --dile-wheel-picker-band-background-color: var(--dile-calendar-wheel-band-background-color, var(--dile-calendar-day-background-color, rgba(90, 90, 90, 0.08)));
113
+ }
114
+ .time-columns {
115
+ display: flex;
116
+ align-items: center;
117
+ gap: 0.25rem;
118
+ }
119
+ .time-separator {
120
+ font-size: var(--dile-calendar-wheel-font-size, 1.1rem);
121
+ color: var(--dile-calendar-day-text-color, #303030);
122
+ }
123
+ .time-layer-accept {
124
+ display: inline-flex;
125
+ align-items: center;
126
+ justify-content: center;
127
+ width: 28px;
128
+ height: 28px;
129
+ padding: 0;
130
+ border: 1px solid var(--dile-calendar-month-year-accept-border-color, color-mix(in srgb, currentColor 35%, transparent));
131
+ border-radius: 50%;
132
+ background: transparent;
133
+ color: var(--dile-calendar-month-year-accept-color, var(--dile-calendar-nav-buttons-color, #303030));
134
+ cursor: pointer;
135
+ }
136
+ .time-layer-accept:hover {
137
+ background-color: var(--dile-calendar-day-background-color, rgba(90, 90, 90, 0.08));
138
+ }
139
+ .time-layer-accept svg {
140
+ width: 18px;
141
+ height: 18px;
142
+ fill: currentColor;
143
+ }
42
144
  .time-area {
43
145
  display: flex;
44
146
  flex-direction: row;
@@ -47,9 +149,26 @@ export class DileDatetimepicker extends DileDatepicker {
47
149
  align-items: center;
48
150
  margin-bottom: 0.5rem;
49
151
  }
50
- .time-area span {
51
- display: flex;
52
- align-items: center;
152
+ .time-toggle {
153
+ min-height: 30px;
154
+ padding: 0 4px;
155
+ border: 0;
156
+ border-bottom: 1px dotted currentColor;
157
+ border-radius: 0;
158
+ background: transparent;
159
+ color: var(--dile-datetimepicker-time-color, var(--dile-calendar-day-text-color, #303030));
160
+ font: inherit;
161
+ font-size: var(--dile-datetimepicker-time-font-size, 1.1rem);
162
+ font-variant-numeric: tabular-nums;
163
+ cursor: pointer;
164
+ }
165
+ .time-toggle[aria-expanded="true"] {
166
+ border-bottom-style: solid;
167
+ }
168
+ .time-toggle:focus-visible,
169
+ .time-layer-accept:focus-visible {
170
+ outline: 2px solid var(--dile-input-focus-border-color, var(--dile-link-color, #6af));
171
+ outline-offset: 2px;
53
172
  }
54
173
  .accept-button {
55
174
  display: flex;
@@ -73,9 +192,6 @@ export class DileDatetimepicker extends DileDatepicker {
73
192
  --dile-icon-rounded-background-color: var(--dile-datetimepicker-accept-icon-background-color, var(--dile-primary-color, #f4f4f4));
74
193
  cursor: pointer;
75
194
  }
76
- dile-time-picker {
77
- margin-bottom: 0;
78
- }
79
195
  `];
80
196
  }
81
197
 
@@ -83,6 +199,13 @@ export class DileDatetimepicker extends DileDatepicker {
83
199
  return {
84
200
  ...super.properties,
85
201
  acceptLabel: { type: String },
202
+ timeLabel: { type: String },
203
+ timeAcceptLabel: { type: String },
204
+ showSeconds: { type: Boolean },
205
+ minuteStep: { type: Number },
206
+ secondStep: { type: Number },
207
+ _time: { state: true },
208
+ _timeOpen: { state: true },
86
209
  };
87
210
  }
88
211
 
@@ -90,31 +213,48 @@ export class DileDatetimepicker extends DileDatepicker {
90
213
  super();
91
214
  this.icon = 'lucide.calendar-clock';
92
215
  this.acceptLabel = 'Accept date and time';
216
+ this.timeLabel = 'Time';
217
+ this.timeAcceptLabel = 'OK';
218
+ this.showSeconds = false;
219
+ this.minuteStep = 1;
220
+ this.secondStep = 1;
221
+ this._time = undefined;
222
+ this._timeOpen = false;
93
223
  }
94
224
 
95
225
  get contentTemplate() {
226
+ const time = this.selectedTime;
227
+ const shownTime = this.showSeconds ? time : time.slice(0, 5);
96
228
  return html`
97
229
  <div class="overlay-view">
98
- <dile-calendar
99
- class="demo-calendar"
100
- .firstDayOfWeek="${this.firstDayOfWeek}"
101
- .weekdayHeaderNotation="${this.weekdayHeaderNotation}"
102
- .locale="${this.locale}"
103
- .minYear="${this.minYear}"
104
- .maxYear="${this.maxYear}"
105
- .minDate="${this._calendarMinDate}"
106
- .maxDate="${this._calendarMaxDate}"
107
- .disableDates="${this.disableDates}"
108
- @user-selected-date-changed=${this.showDateHandler}
109
- ></dile-calendar>
230
+ <div class="date-time-body ${this._timeOpen ? 'time-open' : ''}">
231
+ <dile-calendar
232
+ class="demo-calendar"
233
+ .firstDayOfWeek="${this.firstDayOfWeek}"
234
+ .weekdayHeaderNotation="${this.weekdayHeaderNotation}"
235
+ .locale="${this.locale}"
236
+ .minYear="${this.minYear}"
237
+ .maxYear="${this.maxYear}"
238
+ .minDate="${this._calendarMinDate}"
239
+ .maxDate="${this._calendarMaxDate}"
240
+ .disableDates="${this.disableDates}"
241
+ ></dile-calendar>
242
+ ${this._timeOpen ? this.timeLayerTemplate : nothing}
243
+ </div>
110
244
  <div class="time-area">
111
- <span>
112
- <dile-time-picker></dile-time-picker>
113
- </span>
114
- <button
115
- type="button"
116
- class="accept-button"
117
- aria-label="${this.acceptLabel}"
245
+ <button
246
+ type="button"
247
+ class="time-toggle"
248
+ aria-expanded="${this._timeOpen ? 'true' : 'false'}"
249
+ aria-controls="time-layer"
250
+ aria-label="${this.timeLabel}: ${shownTime}"
251
+ title="${this.timeLabel}"
252
+ @click=${this.toggleTimePicker}
253
+ >${shownTime}</button>
254
+ <button
255
+ type="button"
256
+ class="accept-button"
257
+ aria-label="${this.acceptLabel}"
118
258
  title="${this.acceptLabel}"
119
259
  @click=${this.acceptTime}
120
260
  >
@@ -125,26 +265,172 @@ export class DileDatetimepicker extends DileDatepicker {
125
265
  `;
126
266
  }
127
267
 
128
- get eltime() {
129
- return this.shadowRoot.querySelector('dile-time-picker');
268
+ get timeLayerTemplate() {
269
+ const { hours, minutes, seconds } = this._timeParts;
270
+ return html`
271
+ <div
272
+ id="time-layer"
273
+ class="time-layer"
274
+ role="group"
275
+ aria-label="${this.timeLabel}"
276
+ @keydown=${this.timeLayerKeydown}
277
+ @dile-wheel-picker-changed=${this.stopInnerEvent}
278
+ @element-changed=${this.stopInnerEvent}
279
+ >
280
+ <div class="time-columns">
281
+ <dile-wheel-picker
282
+ class="hours-wheel"
283
+ label="${this.fieldLabel('hour')}"
284
+ .items="${stepItems(23, 1, hours)}"
285
+ .value="${hours}"
286
+ @dile-wheel-picker-changed=${(e) => this.setTimePart('hours', e.detail.value)}
287
+ ></dile-wheel-picker>
288
+ <span class="time-separator" aria-hidden="true">:</span>
289
+ <dile-wheel-picker
290
+ class="minutes-wheel"
291
+ label="${this.fieldLabel('minute')}"
292
+ .items="${stepItems(59, validStep(this.minuteStep), minutes)}"
293
+ .value="${minutes}"
294
+ @dile-wheel-picker-changed=${(e) => this.setTimePart('minutes', e.detail.value)}
295
+ ></dile-wheel-picker>
296
+ ${this.showSeconds ? html`
297
+ <span class="time-separator" aria-hidden="true">:</span>
298
+ <dile-wheel-picker
299
+ class="seconds-wheel"
300
+ label="${this.fieldLabel('second')}"
301
+ .items="${stepItems(59, validStep(this.secondStep), seconds)}"
302
+ .value="${seconds}"
303
+ @dile-wheel-picker-changed=${(e) => this.setTimePart('seconds', e.detail.value)}
304
+ ></dile-wheel-picker>
305
+ ` : nothing}
306
+ </div>
307
+ <button
308
+ type="button"
309
+ class="time-layer-accept"
310
+ aria-label="${this.timeAcceptLabel}"
311
+ title="${this.timeAcceptLabel}"
312
+ @click=${this.closeTimePickerAndFocusToggle}
313
+ ><svg viewBox="0 0 24 24" aria-hidden="true">${checkIcon}</svg></button>
314
+ </div>
315
+ `;
130
316
  }
131
317
 
132
318
  get elmenu() {
133
319
  return this.shadowRoot.getElementById('menu');
134
320
  }
135
321
 
136
- showDateHandler(e) {
137
- this.showDate(e.detail.selectedDate);
322
+ /**
323
+ * Time selected in the picker, as "HH:mm:ss".
324
+ * Before the picker is opened, it is the current time.
325
+ * @returns {string}
326
+ */
327
+ get selectedTime() {
328
+ return this._time ?? this.currentTime();
329
+ }
330
+
331
+ get timePickerOpened() {
332
+ return this._timeOpen;
333
+ }
334
+
335
+ get _timeParts() {
336
+ return timeToParts(this.selectedTime);
337
+ }
338
+
339
+ /**
340
+ * Current time (seconds at 00), with the minutes rounded down to the minuteStep.
341
+ * @returns {string}
342
+ */
343
+ currentTime() {
344
+ const now = new Date();
345
+ const step = validStep(this.minuteStep);
346
+ return `${pad(now.getHours())}:${pad(Math.floor(now.getMinutes() / step) * step)}:00`;
347
+ }
348
+
349
+ /**
350
+ * Besides the calendar, sets the time of the picker to the time of the current value.
351
+ * Without a valid time in the value, the picker shows the current time.
352
+ */
353
+ syncCalendarWithValue() {
354
+ super.syncCalendarWithValue();
355
+ this._time = valueToTime(this.value) ?? this.currentTime();
356
+ this._timeOpen = false;
357
+ }
358
+
359
+ overlayClosedHandler() {
360
+ this._timeOpen = false;
361
+ super.overlayClosedHandler();
362
+ }
363
+
364
+ /**
365
+ * Shows the hours and minutes wheels over the calendar.
366
+ */
367
+ async openTimePicker() {
368
+ this._timeOpen = true;
369
+ await this.updateComplete;
370
+ const wheel = this.shadowRoot.querySelector('.hours-wheel');
371
+ await wheel?.updateComplete;
372
+ wheel?.focus();
138
373
  }
139
374
 
375
+ /**
376
+ * Hides the time wheels and shows the calendar again.
377
+ */
378
+ closeTimePicker() {
379
+ this._timeOpen = false;
380
+ }
381
+
382
+ toggleTimePicker() {
383
+ if (this._timeOpen) {
384
+ this.closeTimePicker();
385
+ } else {
386
+ this.openTimePicker();
387
+ }
388
+ }
389
+
390
+ async closeTimePickerAndFocusToggle() {
391
+ this.closeTimePicker();
392
+ await this.updateComplete;
393
+ this.shadowRoot.querySelector('.time-toggle')?.focus();
394
+ }
395
+
396
+ timeLayerKeydown(e) {
397
+ if (e.key === 'Escape' || e.key === 'Enter') {
398
+ e.preventDefault();
399
+ e.stopPropagation();
400
+ this.closeTimePickerAndFocusToggle();
401
+ }
402
+ }
403
+
404
+ // The inner wheels are not fields of the form: their changes must not leave the component.
405
+ stopInnerEvent(e) {
406
+ e.stopPropagation();
407
+ }
408
+
409
+ setTimePart(part, value) {
410
+ const parts = { ...this._timeParts, [part]: Number(value) };
411
+ this._time = `${pad(parts.hours)}:${pad(parts.minutes)}:${pad(parts.seconds)}`;
412
+ }
413
+
414
+ fieldLabel(field) {
415
+ const fallback = { hour: 'Hour', minute: 'Minute', second: 'Second' }[field];
416
+ try {
417
+ const locale = this.locale || document.documentElement.lang || navigator.language || 'en';
418
+ return new Intl.DisplayNames([locale], { type: 'dateTimeField' }).of(field) || fallback;
419
+ } catch (e) {
420
+ return fallback;
421
+ }
422
+ }
423
+
424
+ /**
425
+ * Writes the given date with the selected time in the value and closes the picker.
426
+ * @param {Date} selectedDate
427
+ */
140
428
  showDate(selectedDate) {
141
- let date = formatToIsoOnlyDate(selectedDate);
142
- let time = this.eltime.value;
143
- this.value = `${date} ${time}`;
429
+ this.value = `${formatToIsoOnlyDate(selectedDate)} ${this.selectedTime}`;
144
430
  if (this.validateDate) {
145
431
  this.validate();
146
432
  }
147
- this.elmenu.close();
433
+ this.elmenu?.close();
148
434
  }
149
435
 
150
436
  dateToValue(date) {
@@ -158,13 +444,16 @@ export class DileDatetimepicker extends DileDatepicker {
158
444
  }
159
445
  }
160
446
 
447
+ /**
448
+ * Accepts the day selected in the calendar and the selected time.
449
+ * Without a selected day, uses the day focused in the calendar,
450
+ * which is always an enabled date (today or the nearest enabled date by default).
451
+ */
161
452
  acceptTime() {
162
- let date = this.shadowRoot.querySelector('dile-calendar').selectedDate;
163
- if(!date) {
164
- date = new Date();
165
- }
453
+ const calendar = this.calendar;
454
+ const date = calendar?.selectedDate || calendar?.centralDate || new Date();
166
455
  this.showDate(date);
167
- }
456
+ }
168
457
 
169
458
  computeValue(value){
170
459
  return formatIfIso8601(value);
@@ -1,5 +1,4 @@
1
1
  import { html, css } from 'lit';
2
- import { ifDefined } from 'lit/directives/if-defined.js';
3
2
  import { DileInput } from '@dile/ui/components/input/index.js';
4
3
  import '../../phone-prefix-select/phone-prefix-select.js';
5
4
  import {
@@ -117,39 +116,21 @@ export class DileInputPhone extends DileInput {
117
116
  }
118
117
  }
119
118
 
120
- render() {
121
- return html`
122
- <main>
123
- ${this.label
124
- ? html`<label for="textField">${this.label}</label>`
125
- : ""}
126
- <section class="for-input">
127
- ${this.noPrefix ? '' : this.prefixTemplate}
128
- <input
129
- type="tel"
130
- inputmode="tel"
131
- id="textField"
132
- name="${this.name}"
133
- aria-label="${ifDefined(this.inputAriaLabel)}"
134
- placeholder="${this.placeholder}"
135
- ?disabled="${this.disabled}"
136
- ?readonly="${this.readonly}"
137
- autocomplete="${this.computeAutocomplete()}"
138
- .value="${this._typedNumber}"
139
- class="${this.errored ? 'errored' : ''}"
140
- @keypress="${this._lookForEnter}"
141
- @input="${this._input}"
142
- @blur="${this.doBlur}"
143
- @focus="${this.doFocus}"
144
- />
145
- ${this.labelRight
146
- ? html`<span class="labelright">${this.labelRight}</span>`
147
- : ''
148
- }
149
- </section>
150
- ${this.messageTemplate}
151
- </main>
152
- `;
119
+ get fieldStartTemplate() {
120
+ return this.noPrefix ? '' : this.prefixTemplate;
121
+ }
122
+
123
+ get inputType() {
124
+ return 'tel';
125
+ }
126
+
127
+ get inputModeValue() {
128
+ return 'tel';
129
+ }
130
+
131
+ /** The input field shows the number as typed by the user, not the E.164 value */
132
+ computeValue() {
133
+ return this._typedNumber;
153
134
  }
154
135
 
155
136
  get prefixTemplate() {
@@ -161,7 +142,7 @@ export class DileInputPhone extends DileInput {
161
142
  language="${this.language}"
162
143
  priority-countries="${this.priorityCountries}"
163
144
  placeholder="${this.prefixPlaceholder || PREFIX_PLACEHOLDERS[this.language] || PREFIX_PLACEHOLDERS.en}"
164
- ?disabled=${this.disabled}
145
+ ?disabled=${this.isDisabled}
165
146
  ?errored=${this.errored}
166
147
  @dile-phone-prefix-changed=${this.onPrefixChanged}
167
148
  @element-changed=${this.stopEvent}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dile/utils",
3
- "version": "3.4.0",
3
+ "version": "3.4.1",
4
4
  "description": "Utility Components of Diverse Uses Based on the Lit Library and Web Components Standard.",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -21,7 +21,7 @@
21
21
  "homepage": "https://dile-components.com/",
22
22
  "dependencies": {
23
23
  "@dile/iconlib": "^2.0.2",
24
- "@dile/ui": "^3.22.1",
24
+ "@dile/ui": "^3.23.0",
25
25
  "@lion/ui": "^0.27.0",
26
26
  "linkify-string": "^4.1.3",
27
27
  "linkifyjs": "^4.1.3",
@@ -31,5 +31,5 @@
31
31
  "publishConfig": {
32
32
  "access": "public"
33
33
  },
34
- "gitHead": "cef9349e18ff89a24a206a541f8129fd30ccb553"
34
+ "gitHead": "828a98ebe286539d42d66f8acf6d5efcdb463136"
35
35
  }