@commmerce/ui 1.14.0 → 1.16.0
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/dist/calendar.d.ts +2 -0
- package/dist/calendar.js +5 -3
- package/dist/cdn.js +646 -578
- package/dist/components/date-range-picker.d.ts +59 -4
- package/dist/components/date-range-picker.js +191 -29
- package/dist/components/text-field.d.ts +9 -0
- package/dist/components/text-field.js +9 -1
- package/dist/components/time-picker.d.ts +162 -0
- package/dist/components/time-picker.js +375 -0
- package/dist/custom-elements.json +1029 -5
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/package.json +3 -3
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import { type PropertyValues } from 'lit';
|
|
2
|
+
import { CmLocalizedElement } from '../localize.js';
|
|
3
|
+
import './icon.js';
|
|
4
|
+
declare const strings: {
|
|
5
|
+
/** Accessible name of the clock button that shows the list of times. */
|
|
6
|
+
showTimes: string;
|
|
7
|
+
/** Accessible name of the list of times. `{label}` is the field's label. */
|
|
8
|
+
times: string;
|
|
9
|
+
timesFor: string;
|
|
10
|
+
/** Letters for hours and minutes in the placeholder, e.g. hh:mm am. */
|
|
11
|
+
patternHour: string;
|
|
12
|
+
patternMinute: string;
|
|
13
|
+
/** Validation. `{pattern}` is e.g. hh:mm am; `{time}` a time in the field's clock. */
|
|
14
|
+
badInput: string;
|
|
15
|
+
required: string;
|
|
16
|
+
rangeUnderflow: string;
|
|
17
|
+
rangeOverflow: string;
|
|
18
|
+
stepMismatch: {
|
|
19
|
+
one: string;
|
|
20
|
+
other: string;
|
|
21
|
+
};
|
|
22
|
+
};
|
|
23
|
+
declare module '../localize.js' {
|
|
24
|
+
interface CmStrings {
|
|
25
|
+
timePicker: typeof strings;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Time field: type a time or choose one from a list. Shows the 12-hour clock with am/pm or the 24-hour clock,
|
|
30
|
+
* following the locale or `hour-cycle`; the value is always 24-hour ISO `hh:mm`. Follows the WAI-ARIA APG
|
|
31
|
+
* "combobox" pattern (a text box with a list of suggested times; any time can be typed). Participates in native forms.
|
|
32
|
+
* @element cm-time-picker
|
|
33
|
+
* @fires change - When the time changes: a time chosen from the list, or typed text read on Enter or on leaving the box.
|
|
34
|
+
* @fires input - Same moment as `change`.
|
|
35
|
+
* @csspart label - The visible label.
|
|
36
|
+
* @csspart box - The bordered box around the input and the clock button.
|
|
37
|
+
* @csspart input - The text input.
|
|
38
|
+
* @csspart trigger - The clock button.
|
|
39
|
+
* @csspart popover - The dropdown that holds the list.
|
|
40
|
+
* @csspart listbox - The list of times.
|
|
41
|
+
*/
|
|
42
|
+
export declare class CmTimePicker extends CmLocalizedElement {
|
|
43
|
+
#private;
|
|
44
|
+
static formAssociated: boolean;
|
|
45
|
+
static shadowRootOptions: {
|
|
46
|
+
delegatesFocus: boolean;
|
|
47
|
+
mode: ShadowRootMode;
|
|
48
|
+
serializable?: boolean;
|
|
49
|
+
slotAssignment?: SlotAssignmentMode;
|
|
50
|
+
};
|
|
51
|
+
static properties: {
|
|
52
|
+
label: {};
|
|
53
|
+
helpText: {
|
|
54
|
+
attribute: string;
|
|
55
|
+
};
|
|
56
|
+
error: {
|
|
57
|
+
reflect: boolean;
|
|
58
|
+
};
|
|
59
|
+
name: {
|
|
60
|
+
reflect: boolean;
|
|
61
|
+
};
|
|
62
|
+
value: {};
|
|
63
|
+
placeholder: {};
|
|
64
|
+
min: {};
|
|
65
|
+
max: {};
|
|
66
|
+
step: {
|
|
67
|
+
type: NumberConstructor;
|
|
68
|
+
};
|
|
69
|
+
interval: {
|
|
70
|
+
type: NumberConstructor;
|
|
71
|
+
};
|
|
72
|
+
hourCycle: {
|
|
73
|
+
attribute: string;
|
|
74
|
+
};
|
|
75
|
+
locale: {
|
|
76
|
+
noAccessor: boolean;
|
|
77
|
+
};
|
|
78
|
+
required: {
|
|
79
|
+
type: BooleanConstructor;
|
|
80
|
+
reflect: boolean;
|
|
81
|
+
};
|
|
82
|
+
disabled: {
|
|
83
|
+
type: BooleanConstructor;
|
|
84
|
+
reflect: boolean;
|
|
85
|
+
};
|
|
86
|
+
open: {
|
|
87
|
+
type: BooleanConstructor;
|
|
88
|
+
reflect: boolean;
|
|
89
|
+
};
|
|
90
|
+
size: {
|
|
91
|
+
reflect: boolean;
|
|
92
|
+
};
|
|
93
|
+
_text: {
|
|
94
|
+
state: boolean;
|
|
95
|
+
};
|
|
96
|
+
_active: {
|
|
97
|
+
state: boolean;
|
|
98
|
+
};
|
|
99
|
+
};
|
|
100
|
+
/** Visible label above the box, e.g. "Pickup time". Required for accessibility unless the element has an aria-label. */
|
|
101
|
+
label: string;
|
|
102
|
+
/** One sentence under the box in text-tertiary. */
|
|
103
|
+
helpText: string;
|
|
104
|
+
/** Error message; turns the border critical and sets aria-invalid. */
|
|
105
|
+
error: string;
|
|
106
|
+
/** Form field name. */
|
|
107
|
+
name: string;
|
|
108
|
+
/** The time as 24-hour ISO `hh:mm` (`15:30`), whichever clock is shown; empty when not set. */
|
|
109
|
+
value: string;
|
|
110
|
+
/** Placeholder. Default: the format, e.g. "hh:mm am" or "hh:mm". */
|
|
111
|
+
placeholder: string;
|
|
112
|
+
/** Earliest time that can be chosen, `hh:mm`. The list starts here. */
|
|
113
|
+
min: string;
|
|
114
|
+
/** Latest time that can be chosen, `hh:mm`. The list ends here. */
|
|
115
|
+
max: string;
|
|
116
|
+
/** Minutes between allowed times, counted from midnight (15 allows :00, :15, :30, :45). A typed time off the step is invalid. Default 1. */
|
|
117
|
+
step: number;
|
|
118
|
+
/** Minutes between the times in the list. Default: `step` when it is 5 or more, else 30. Any allowed time can still be typed. */
|
|
119
|
+
interval?: number;
|
|
120
|
+
/** `h12` (am/pm) or `h23` (24-hour) to force a clock. Empty: the locale's clock (12-hour for `en-IN` and `en-US`, 24-hour for `en-GB` and most of Europe). */
|
|
121
|
+
hourCycle: '' | 'h12' | 'h23';
|
|
122
|
+
/** BCP 47 language for the clock and am/pm words. Empty: the same language as every other component. */
|
|
123
|
+
get locale(): string;
|
|
124
|
+
set locale(v: string);
|
|
125
|
+
/** The form will not submit until a time is set. */
|
|
126
|
+
required: boolean;
|
|
127
|
+
/** Blocks interaction. Also set by a disabled `<fieldset>`. */
|
|
128
|
+
disabled: boolean;
|
|
129
|
+
/** Whether the list of times is showing. */
|
|
130
|
+
open: boolean;
|
|
131
|
+
/** `small`: a 32px box (filter rows); `medium`: 36px. Default 40px. On touch screens all are 44px. */
|
|
132
|
+
size: 'default' | 'medium' | 'small';
|
|
133
|
+
_text: string;
|
|
134
|
+
/** Index of the keyboard-active time in the list, or -1. */
|
|
135
|
+
_active: number;
|
|
136
|
+
static styles: import("lit").CSSResult[];
|
|
137
|
+
/** The value as minutes since midnight, or null. */
|
|
138
|
+
get valueAsNumber(): number | null;
|
|
139
|
+
connectedCallback(): void;
|
|
140
|
+
disconnectedCallback(): void;
|
|
141
|
+
get form(): HTMLFormElement | null;
|
|
142
|
+
get validity(): ValidityState;
|
|
143
|
+
get validationMessage(): string;
|
|
144
|
+
checkValidity(): boolean;
|
|
145
|
+
reportValidity(): boolean;
|
|
146
|
+
formResetCallback(): void;
|
|
147
|
+
formDisabledCallback(d: boolean): void;
|
|
148
|
+
formStateRestoreCallback(state: string): void;
|
|
149
|
+
/** Shows the list of times, with the chosen time (or the one nearest to now) active and scrolled into view. */
|
|
150
|
+
show(): void;
|
|
151
|
+
/** Hides the list without changing the time. */
|
|
152
|
+
close(): void;
|
|
153
|
+
willUpdate(changed: PropertyValues): void;
|
|
154
|
+
updated(changed: PropertyValues): void;
|
|
155
|
+
render(): import("lit-html").TemplateResult<1>;
|
|
156
|
+
}
|
|
157
|
+
declare global {
|
|
158
|
+
interface HTMLElementTagNameMap {
|
|
159
|
+
'cm-time-picker': CmTimePicker;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
export {};
|
|
@@ -0,0 +1,375 @@
|
|
|
1
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
2
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
3
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
4
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
5
|
+
};
|
|
6
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
7
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
8
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
9
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
10
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
11
|
+
};
|
|
12
|
+
var _CmTimePicker_instances, _CmTimePicker_localeProp, _CmTimePicker_internals, _CmTimePicker_v, _CmTimePicker_id, _CmTimePicker_default, _CmTimePicker_keepText, _CmTimePicker_layer, _CmTimePicker_locale_get, _CmTimePicker_tf_get, _CmTimePicker_stepMinutes_get, _CmTimePicker_listEvery_get, _CmTimePicker_pattern_get, _CmTimePicker_format, _CmTimePicker_times_get, _CmTimePicker_now, _CmTimePicker_nearest, _CmTimePicker_scrollToActive, _CmTimePicker_setValue, _CmTimePicker_commitText, _CmTimePicker_pick, _CmTimePicker_onKey, _CmTimePicker_onInput, _CmTimePicker_toggle, _CmTimePicker_onOutside;
|
|
13
|
+
import { LitElement, html, css, nothing } from 'lit';
|
|
14
|
+
import { live } from 'lit/directives/live.js';
|
|
15
|
+
import { base, define, nextId } from '../base.js';
|
|
16
|
+
import { CmLocalizedElement, defineStrings } from '../localize.js';
|
|
17
|
+
import { FieldValidity } from '../validation.js';
|
|
18
|
+
import { TopLayer, topLayerStyles } from '../top-layer.js';
|
|
19
|
+
import { optionStyles } from '../field.js';
|
|
20
|
+
import { timeToMinutes, minutesToTime, timeFormat, timePattern, parseTime, resolveLocale, dateFieldStyles } from '../calendar.js';
|
|
21
|
+
import './icon.js';
|
|
22
|
+
const strings = defineStrings('timePicker', {
|
|
23
|
+
/** Accessible name of the clock button that shows the list of times. */
|
|
24
|
+
showTimes: 'Show times',
|
|
25
|
+
/** Accessible name of the list of times. `{label}` is the field's label. */
|
|
26
|
+
times: 'Times',
|
|
27
|
+
timesFor: 'Times for {label}',
|
|
28
|
+
/** Letters for hours and minutes in the placeholder, e.g. hh:mm am. */
|
|
29
|
+
patternHour: 'hh',
|
|
30
|
+
patternMinute: 'mm',
|
|
31
|
+
/** Validation. `{pattern}` is e.g. hh:mm am; `{time}` a time in the field's clock. */
|
|
32
|
+
badInput: 'Enter a time like {pattern}.',
|
|
33
|
+
required: 'Enter or choose a time.',
|
|
34
|
+
rangeUnderflow: 'Choose {time} or later.',
|
|
35
|
+
rangeOverflow: 'Choose {time} or earlier.',
|
|
36
|
+
stepMismatch: { one: 'Choose a time on the minute.', other: 'Choose a time in steps of {count} minutes.' },
|
|
37
|
+
});
|
|
38
|
+
/**
|
|
39
|
+
* Time field: type a time or choose one from a list. Shows the 12-hour clock with am/pm or the 24-hour clock,
|
|
40
|
+
* following the locale or `hour-cycle`; the value is always 24-hour ISO `hh:mm`. Follows the WAI-ARIA APG
|
|
41
|
+
* "combobox" pattern (a text box with a list of suggested times; any time can be typed). Participates in native forms.
|
|
42
|
+
* @element cm-time-picker
|
|
43
|
+
* @fires change - When the time changes: a time chosen from the list, or typed text read on Enter or on leaving the box.
|
|
44
|
+
* @fires input - Same moment as `change`.
|
|
45
|
+
* @csspart label - The visible label.
|
|
46
|
+
* @csspart box - The bordered box around the input and the clock button.
|
|
47
|
+
* @csspart input - The text input.
|
|
48
|
+
* @csspart trigger - The clock button.
|
|
49
|
+
* @csspart popover - The dropdown that holds the list.
|
|
50
|
+
* @csspart listbox - The list of times.
|
|
51
|
+
*/
|
|
52
|
+
export class CmTimePicker extends CmLocalizedElement {
|
|
53
|
+
constructor() {
|
|
54
|
+
super(...arguments);
|
|
55
|
+
_CmTimePicker_instances.add(this);
|
|
56
|
+
/** Visible label above the box, e.g. "Pickup time". Required for accessibility unless the element has an aria-label. */
|
|
57
|
+
this.label = '';
|
|
58
|
+
/** One sentence under the box in text-tertiary. */
|
|
59
|
+
this.helpText = '';
|
|
60
|
+
/** Error message; turns the border critical and sets aria-invalid. */
|
|
61
|
+
this.error = '';
|
|
62
|
+
/** Form field name. */
|
|
63
|
+
this.name = '';
|
|
64
|
+
/** The time as 24-hour ISO `hh:mm` (`15:30`), whichever clock is shown; empty when not set. */
|
|
65
|
+
this.value = '';
|
|
66
|
+
/** Placeholder. Default: the format, e.g. "hh:mm am" or "hh:mm". */
|
|
67
|
+
this.placeholder = '';
|
|
68
|
+
/** Earliest time that can be chosen, `hh:mm`. The list starts here. */
|
|
69
|
+
this.min = '';
|
|
70
|
+
/** Latest time that can be chosen, `hh:mm`. The list ends here. */
|
|
71
|
+
this.max = '';
|
|
72
|
+
/** Minutes between allowed times, counted from midnight (15 allows :00, :15, :30, :45). A typed time off the step is invalid. Default 1. */
|
|
73
|
+
this.step = 1;
|
|
74
|
+
/** `h12` (am/pm) or `h23` (24-hour) to force a clock. Empty: the locale's clock (12-hour for `en-IN` and `en-US`, 24-hour for `en-GB` and most of Europe). */
|
|
75
|
+
this.hourCycle = '';
|
|
76
|
+
_CmTimePicker_localeProp.set(this, '');
|
|
77
|
+
/** The form will not submit until a time is set. */
|
|
78
|
+
this.required = false;
|
|
79
|
+
/** Blocks interaction. Also set by a disabled `<fieldset>`. */
|
|
80
|
+
this.disabled = false;
|
|
81
|
+
/** Whether the list of times is showing. */
|
|
82
|
+
this.open = false;
|
|
83
|
+
/** `small`: a 32px box (filter rows); `medium`: 36px. Default 40px. On touch screens all are 44px. */
|
|
84
|
+
this.size = 'default';
|
|
85
|
+
this._text = '';
|
|
86
|
+
/** Index of the keyboard-active time in the list, or -1. */
|
|
87
|
+
this._active = -1;
|
|
88
|
+
_CmTimePicker_internals.set(this, this.attachInternals());
|
|
89
|
+
_CmTimePicker_v.set(this, new FieldValidity(this, __classPrivateFieldGet(this, _CmTimePicker_internals, "f")));
|
|
90
|
+
_CmTimePicker_id.set(this, nextId('cm-tp'));
|
|
91
|
+
_CmTimePicker_default.set(this, null);
|
|
92
|
+
_CmTimePicker_keepText.set(this, false);
|
|
93
|
+
_CmTimePicker_layer.set(this, new TopLayer());
|
|
94
|
+
_CmTimePicker_onKey.set(this, (e) => {
|
|
95
|
+
const times = __classPrivateFieldGet(this, _CmTimePicker_instances, "a", _CmTimePicker_times_get);
|
|
96
|
+
switch (e.key) {
|
|
97
|
+
case 'ArrowDown':
|
|
98
|
+
case 'ArrowUp': {
|
|
99
|
+
e.preventDefault();
|
|
100
|
+
if (!this.open) {
|
|
101
|
+
this.show();
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
if (!times.length)
|
|
105
|
+
return;
|
|
106
|
+
const d = e.key === 'ArrowDown' ? 1 : -1;
|
|
107
|
+
this._active = this._active < 0 ? 0 : Math.min(times.length - 1, Math.max(0, this._active + d));
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
case 'PageDown':
|
|
111
|
+
case 'PageUp':
|
|
112
|
+
if (!this.open || !times.length)
|
|
113
|
+
return;
|
|
114
|
+
e.preventDefault();
|
|
115
|
+
this._active = Math.min(times.length - 1, Math.max(0, this._active + (e.key === 'PageDown' ? 6 : -6)));
|
|
116
|
+
return;
|
|
117
|
+
case 'Enter':
|
|
118
|
+
if (this.open && this._active >= 0 && times[this._active] !== undefined) {
|
|
119
|
+
e.preventDefault();
|
|
120
|
+
__classPrivateFieldGet(this, _CmTimePicker_instances, "m", _CmTimePicker_pick).call(this, times[this._active]);
|
|
121
|
+
}
|
|
122
|
+
else {
|
|
123
|
+
this.open = false;
|
|
124
|
+
__classPrivateFieldGet(this, _CmTimePicker_instances, "m", _CmTimePicker_commitText).call(this);
|
|
125
|
+
}
|
|
126
|
+
return;
|
|
127
|
+
case 'Escape':
|
|
128
|
+
if (this.open) {
|
|
129
|
+
e.preventDefault();
|
|
130
|
+
e.stopPropagation();
|
|
131
|
+
this.open = false;
|
|
132
|
+
}
|
|
133
|
+
return;
|
|
134
|
+
case 'Tab':
|
|
135
|
+
this.open = false;
|
|
136
|
+
return;
|
|
137
|
+
default:
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
_CmTimePicker_onInput.set(this, (e) => {
|
|
141
|
+
e.stopPropagation();
|
|
142
|
+
this._text = e.target.value;
|
|
143
|
+
// While the list shows, the first time at or after what is typed becomes active, so Enter picks it.
|
|
144
|
+
if (this.open) {
|
|
145
|
+
const t = parseTime(this._text, __classPrivateFieldGet(this, _CmTimePicker_instances, "a", _CmTimePicker_tf_get));
|
|
146
|
+
if (t)
|
|
147
|
+
this._active = __classPrivateFieldGet(this, _CmTimePicker_instances, "m", _CmTimePicker_nearest).call(this, t);
|
|
148
|
+
}
|
|
149
|
+
});
|
|
150
|
+
_CmTimePicker_toggle.set(this, () => {
|
|
151
|
+
if (this.disabled)
|
|
152
|
+
return;
|
|
153
|
+
if (this.open)
|
|
154
|
+
this.open = false;
|
|
155
|
+
else {
|
|
156
|
+
__classPrivateFieldGet(this, _CmTimePicker_instances, "m", _CmTimePicker_commitText).call(this);
|
|
157
|
+
this.show();
|
|
158
|
+
}
|
|
159
|
+
this.renderRoot.querySelector('input')?.focus();
|
|
160
|
+
});
|
|
161
|
+
_CmTimePicker_onOutside.set(this, (e) => {
|
|
162
|
+
if (this.open && !e.composedPath().includes(this))
|
|
163
|
+
this.open = false;
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
/** BCP 47 language for the clock and am/pm words. Empty: the same language as every other component. */
|
|
167
|
+
get locale() { return __classPrivateFieldGet(this, _CmTimePicker_localeProp, "f"); }
|
|
168
|
+
set locale(v) { const old = __classPrivateFieldGet(this, _CmTimePicker_localeProp, "f"); __classPrivateFieldSet(this, _CmTimePicker_localeProp, v ?? '', "f"); this.requestUpdate('locale', old); }
|
|
169
|
+
/** The value as minutes since midnight, or null. */
|
|
170
|
+
get valueAsNumber() { return timeToMinutes(this.value); }
|
|
171
|
+
connectedCallback() {
|
|
172
|
+
super.connectedCallback();
|
|
173
|
+
if (__classPrivateFieldGet(this, _CmTimePicker_default, "f") === null)
|
|
174
|
+
__classPrivateFieldSet(this, _CmTimePicker_default, this.value, "f");
|
|
175
|
+
document.addEventListener('pointerdown', __classPrivateFieldGet(this, _CmTimePicker_onOutside, "f"), true);
|
|
176
|
+
}
|
|
177
|
+
disconnectedCallback() {
|
|
178
|
+
super.disconnectedCallback();
|
|
179
|
+
__classPrivateFieldGet(this, _CmTimePicker_layer, "f").close();
|
|
180
|
+
document.removeEventListener('pointerdown', __classPrivateFieldGet(this, _CmTimePicker_onOutside, "f"), true);
|
|
181
|
+
}
|
|
182
|
+
get form() { return __classPrivateFieldGet(this, _CmTimePicker_internals, "f").form; }
|
|
183
|
+
get validity() { return __classPrivateFieldGet(this, _CmTimePicker_internals, "f").validity; }
|
|
184
|
+
get validationMessage() { return __classPrivateFieldGet(this, _CmTimePicker_internals, "f").validationMessage; }
|
|
185
|
+
checkValidity() { return __classPrivateFieldGet(this, _CmTimePicker_internals, "f").checkValidity(); }
|
|
186
|
+
reportValidity() { return __classPrivateFieldGet(this, _CmTimePicker_v, "f").report(() => __classPrivateFieldGet(this, _CmTimePicker_internals, "f").reportValidity()); }
|
|
187
|
+
formResetCallback() { this.value = __classPrivateFieldGet(this, _CmTimePicker_default, "f") ?? ''; this._text = __classPrivateFieldGet(this, _CmTimePicker_instances, "m", _CmTimePicker_format).call(this, this.value); this.open = false; __classPrivateFieldGet(this, _CmTimePicker_v, "f").reset(); }
|
|
188
|
+
formDisabledCallback(d) { this.disabled = d; if (d)
|
|
189
|
+
this.open = false; }
|
|
190
|
+
formStateRestoreCallback(state) { this.value = state ?? ''; }
|
|
191
|
+
/** Shows the list of times, with the chosen time (or the one nearest to now) active and scrolled into view. */
|
|
192
|
+
show() {
|
|
193
|
+
if (this.disabled)
|
|
194
|
+
return;
|
|
195
|
+
this._active = __classPrivateFieldGet(this, _CmTimePicker_instances, "m", _CmTimePicker_nearest).call(this, timeToMinutes(this.value) !== null ? this.value : parseTime(this._text, __classPrivateFieldGet(this, _CmTimePicker_instances, "a", _CmTimePicker_tf_get)) ?? __classPrivateFieldGet(this, _CmTimePicker_instances, "m", _CmTimePicker_now).call(this));
|
|
196
|
+
this.open = true;
|
|
197
|
+
}
|
|
198
|
+
/** Hides the list without changing the time. */
|
|
199
|
+
close() { this.open = false; }
|
|
200
|
+
willUpdate(changed) {
|
|
201
|
+
if (changed.has('value') && !__classPrivateFieldGet(this, _CmTimePicker_keepText, "f"))
|
|
202
|
+
this._text = __classPrivateFieldGet(this, _CmTimePicker_instances, "m", _CmTimePicker_format).call(this, this.value);
|
|
203
|
+
else if ((changed.has('locale') || changed.has('hourCycle')) && timeToMinutes(this.value) !== null)
|
|
204
|
+
this._text = __classPrivateFieldGet(this, _CmTimePicker_instances, "m", _CmTimePicker_format).call(this, this.value);
|
|
205
|
+
__classPrivateFieldSet(this, _CmTimePicker_keepText, false, "f");
|
|
206
|
+
}
|
|
207
|
+
updated(changed) {
|
|
208
|
+
if (this.open && !__classPrivateFieldGet(this, _CmTimePicker_layer, "f").active)
|
|
209
|
+
__classPrivateFieldGet(this, _CmTimePicker_layer, "f").open(this.renderRoot.querySelector('.pop'), this.renderRoot.querySelector('.box'), { align: 'stretch', gap: 4 });
|
|
210
|
+
else if (!this.open && __classPrivateFieldGet(this, _CmTimePicker_layer, "f").active)
|
|
211
|
+
__classPrivateFieldGet(this, _CmTimePicker_layer, "f").close();
|
|
212
|
+
else if (this.open)
|
|
213
|
+
__classPrivateFieldGet(this, _CmTimePicker_layer, "f").place();
|
|
214
|
+
if (this.open)
|
|
215
|
+
__classPrivateFieldGet(this, _CmTimePicker_instances, "m", _CmTimePicker_scrollToActive).call(this, changed.has('open'));
|
|
216
|
+
const m = timeToMinutes(this.value);
|
|
217
|
+
__classPrivateFieldGet(this, _CmTimePicker_internals, "f").setFormValue(m === null ? null : this.value, this.value);
|
|
218
|
+
const input = this.renderRoot.querySelector('input') ?? undefined;
|
|
219
|
+
const lo = timeToMinutes(this.min), hi = timeToMinutes(this.max);
|
|
220
|
+
const tf = __classPrivateFieldGet(this, _CmTimePicker_instances, "a", _CmTimePicker_tf_get);
|
|
221
|
+
if (this.error)
|
|
222
|
+
__classPrivateFieldGet(this, _CmTimePicker_internals, "f").setValidity({ customError: true }, this.error, input);
|
|
223
|
+
else if (this._text.trim() && m === null)
|
|
224
|
+
__classPrivateFieldGet(this, _CmTimePicker_internals, "f").setValidity({ badInput: true }, this.t('badInput', { pattern: __classPrivateFieldGet(this, _CmTimePicker_instances, "a", _CmTimePicker_pattern_get) }), input);
|
|
225
|
+
else if (this.required && m === null)
|
|
226
|
+
__classPrivateFieldGet(this, _CmTimePicker_internals, "f").setValidity({ valueMissing: true }, this.t('required'), input);
|
|
227
|
+
else if (m !== null && lo !== null && m < lo)
|
|
228
|
+
__classPrivateFieldGet(this, _CmTimePicker_internals, "f").setValidity({ rangeUnderflow: true }, this.t('rangeUnderflow', { time: tf.format(lo) }), input);
|
|
229
|
+
else if (m !== null && hi !== null && m > hi)
|
|
230
|
+
__classPrivateFieldGet(this, _CmTimePicker_internals, "f").setValidity({ rangeOverflow: true }, this.t('rangeOverflow', { time: tf.format(hi) }), input);
|
|
231
|
+
else if (m !== null && m % __classPrivateFieldGet(this, _CmTimePicker_instances, "a", _CmTimePicker_stepMinutes_get))
|
|
232
|
+
__classPrivateFieldGet(this, _CmTimePicker_internals, "f").setValidity({ stepMismatch: true }, this.t('stepMismatch', { count: __classPrivateFieldGet(this, _CmTimePicker_instances, "a", _CmTimePicker_stepMinutes_get) }), input);
|
|
233
|
+
else
|
|
234
|
+
__classPrivateFieldGet(this, _CmTimePicker_internals, "f").setValidity({});
|
|
235
|
+
__classPrivateFieldGet(this, _CmTimePicker_v, "f").sync();
|
|
236
|
+
}
|
|
237
|
+
render() {
|
|
238
|
+
const tf = __classPrivateFieldGet(this, _CmTimePicker_instances, "a", _CmTimePicker_tf_get);
|
|
239
|
+
const message = __classPrivateFieldGet(this, _CmTimePicker_v, "f").message;
|
|
240
|
+
const described = [message && `${__classPrivateFieldGet(this, _CmTimePicker_id, "f")}-err`, this.helpText && `${__classPrivateFieldGet(this, _CmTimePicker_id, "f")}-help`].filter(Boolean).join(' ');
|
|
241
|
+
const hostLabel = this.getAttribute('aria-label');
|
|
242
|
+
const name = this.label || hostLabel || '';
|
|
243
|
+
const times = this.open ? __classPrivateFieldGet(this, _CmTimePicker_instances, "a", _CmTimePicker_times_get) : [];
|
|
244
|
+
const chosen = timeToMinutes(this.value);
|
|
245
|
+
const listId = `${__classPrivateFieldGet(this, _CmTimePicker_id, "f")}-list`;
|
|
246
|
+
return html `<div class="field">
|
|
247
|
+
${this.label
|
|
248
|
+
? html `<label class="label" for="${__classPrivateFieldGet(this, _CmTimePicker_id, "f")}-in" part="label">${this.label}${this.required ? html `<span class="req" aria-hidden="true">*</span>` : nothing}</label>`
|
|
249
|
+
: nothing}
|
|
250
|
+
<div class="wrap">
|
|
251
|
+
<div class="box" part="box">
|
|
252
|
+
<input id="${__classPrivateFieldGet(this, _CmTimePicker_id, "f")}-in" part="input" type="text" autocomplete="off" spellcheck="false" inputmode="text"
|
|
253
|
+
role="combobox" aria-autocomplete="none" aria-expanded=${this.open ? 'true' : 'false'} aria-controls=${listId}
|
|
254
|
+
aria-activedescendant=${this.open && this._active >= 0 && times[this._active] !== undefined ? `${__classPrivateFieldGet(this, _CmTimePicker_id, "f")}-o${times[this._active]}` : nothing}
|
|
255
|
+
.value=${live(this._text)} placeholder=${this.placeholder || __classPrivateFieldGet(this, _CmTimePicker_instances, "a", _CmTimePicker_pattern_get)}
|
|
256
|
+
aria-label=${!this.label && hostLabel ? hostLabel : nothing}
|
|
257
|
+
?required=${this.required} ?disabled=${this.disabled}
|
|
258
|
+
aria-invalid=${message ? 'true' : nothing} aria-describedby=${described || nothing}
|
|
259
|
+
@input=${__classPrivateFieldGet(this, _CmTimePicker_onInput, "f")} @change=${() => __classPrivateFieldGet(this, _CmTimePicker_instances, "m", _CmTimePicker_commitText).call(this)} @keydown=${__classPrivateFieldGet(this, _CmTimePicker_onKey, "f")} />
|
|
260
|
+
<button type="button" class="trigger" part="trigger" tabindex="-1" ?disabled=${this.disabled}
|
|
261
|
+
aria-label=${this.t('showTimes')} aria-expanded=${this.open ? 'true' : 'false'} aria-controls=${listId}
|
|
262
|
+
@pointerdown=${(e) => e.preventDefault()} @click=${__classPrivateFieldGet(this, _CmTimePicker_toggle, "f")}><cm-icon name="clock" size="16"></cm-icon></button>
|
|
263
|
+
</div>
|
|
264
|
+
<div class="pop" part="popover" ?hidden=${!this.open} @pointerdown=${(e) => e.preventDefault()}>
|
|
265
|
+
<ul role="listbox" id=${listId} part="listbox" aria-label=${this.t(name ? 'timesFor' : 'times', { label: name })}>
|
|
266
|
+
${times.map((m, i) => html `<li role="option" id="${__classPrivateFieldGet(this, _CmTimePicker_id, "f")}-o${m}" data-value=${minutesToTime(m)}
|
|
267
|
+
class=${i === this._active ? 'active' : ''} aria-selected=${m === chosen ? 'true' : 'false'}
|
|
268
|
+
@pointerenter=${() => { this._active = i; }} @click=${() => __classPrivateFieldGet(this, _CmTimePicker_instances, "m", _CmTimePicker_pick).call(this, m)}>${tf.format(m)}</li>`)}
|
|
269
|
+
</ul>
|
|
270
|
+
</div>
|
|
271
|
+
</div>
|
|
272
|
+
${message ? html `<p class="error" id="${__classPrivateFieldGet(this, _CmTimePicker_id, "f")}-err" role="alert">${message}</p>` : nothing}
|
|
273
|
+
${this.helpText ? html `<p class="help" id="${__classPrivateFieldGet(this, _CmTimePicker_id, "f")}-help">${this.helpText}</p>` : nothing}
|
|
274
|
+
</div>`;
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
_CmTimePicker_localeProp = new WeakMap(), _CmTimePicker_internals = new WeakMap(), _CmTimePicker_v = new WeakMap(), _CmTimePicker_id = new WeakMap(), _CmTimePicker_default = new WeakMap(), _CmTimePicker_keepText = new WeakMap(), _CmTimePicker_layer = new WeakMap(), _CmTimePicker_onKey = new WeakMap(), _CmTimePicker_onInput = new WeakMap(), _CmTimePicker_toggle = new WeakMap(), _CmTimePicker_onOutside = new WeakMap(), _CmTimePicker_instances = new WeakSet(), _CmTimePicker_locale_get = function _CmTimePicker_locale_get() { return resolveLocale(this, this.locale); }, _CmTimePicker_tf_get = function _CmTimePicker_tf_get() { return timeFormat(__classPrivateFieldGet(this, _CmTimePicker_instances, "a", _CmTimePicker_locale_get), this.hourCycle); }, _CmTimePicker_stepMinutes_get = function _CmTimePicker_stepMinutes_get() { const n = Math.trunc(Number(this.step)); return n >= 1 && n <= 720 ? n : 1; }, _CmTimePicker_listEvery_get = function _CmTimePicker_listEvery_get() {
|
|
278
|
+
const n = Math.trunc(Number(this.interval));
|
|
279
|
+
if (n >= 1 && n <= 720)
|
|
280
|
+
return n;
|
|
281
|
+
return __classPrivateFieldGet(this, _CmTimePicker_instances, "a", _CmTimePicker_stepMinutes_get) >= 5 ? __classPrivateFieldGet(this, _CmTimePicker_instances, "a", _CmTimePicker_stepMinutes_get) : 30;
|
|
282
|
+
}, _CmTimePicker_pattern_get = function _CmTimePicker_pattern_get() { return timePattern(__classPrivateFieldGet(this, _CmTimePicker_instances, "a", _CmTimePicker_tf_get), { h: this.t('patternHour'), m: this.t('patternMinute') }); }, _CmTimePicker_format = function _CmTimePicker_format(v) { const m = timeToMinutes(v); return m === null ? '' : __classPrivateFieldGet(this, _CmTimePicker_instances, "a", _CmTimePicker_tf_get).format(m); }, _CmTimePicker_times_get = function _CmTimePicker_times_get() {
|
|
283
|
+
const lo = timeToMinutes(this.min) ?? 0, hi = timeToMinutes(this.max) ?? 1439;
|
|
284
|
+
const every = __classPrivateFieldGet(this, _CmTimePicker_instances, "a", _CmTimePicker_listEvery_get), step = __classPrivateFieldGet(this, _CmTimePicker_instances, "a", _CmTimePicker_stepMinutes_get);
|
|
285
|
+
const out = [];
|
|
286
|
+
for (let m = Math.ceil(lo / every) * every; m <= hi && out.length < 1440; m += every)
|
|
287
|
+
if (m % step === 0)
|
|
288
|
+
out.push(m);
|
|
289
|
+
return out;
|
|
290
|
+
}, _CmTimePicker_now = function _CmTimePicker_now() { const n = new Date(); return minutesToTime(n.getHours() * 60 + n.getMinutes()); }, _CmTimePicker_nearest = function _CmTimePicker_nearest(t) {
|
|
291
|
+
const times = __classPrivateFieldGet(this, _CmTimePicker_instances, "a", _CmTimePicker_times_get);
|
|
292
|
+
if (!times.length)
|
|
293
|
+
return -1;
|
|
294
|
+
const m = timeToMinutes(t);
|
|
295
|
+
if (m === null)
|
|
296
|
+
return 0;
|
|
297
|
+
const i = times.findIndex((x) => x >= m);
|
|
298
|
+
return i === -1 ? times.length - 1 : i;
|
|
299
|
+
}, _CmTimePicker_scrollToActive = function _CmTimePicker_scrollToActive(center) {
|
|
300
|
+
const list = this.renderRoot.querySelector('[role=listbox]');
|
|
301
|
+
const opt = this.renderRoot.querySelector('[role=option].active');
|
|
302
|
+
if (!list || !opt)
|
|
303
|
+
return;
|
|
304
|
+
const top = opt.offsetTop - list.offsetTop, bottom = top + opt.offsetHeight;
|
|
305
|
+
if (center)
|
|
306
|
+
list.scrollTop = top - (list.clientHeight - opt.offsetHeight) / 2;
|
|
307
|
+
else if (top < list.scrollTop)
|
|
308
|
+
list.scrollTop = top;
|
|
309
|
+
else if (bottom > list.scrollTop + list.clientHeight)
|
|
310
|
+
list.scrollTop = bottom - list.clientHeight;
|
|
311
|
+
}, _CmTimePicker_setValue = function _CmTimePicker_setValue(v) {
|
|
312
|
+
if (v === this.value)
|
|
313
|
+
return;
|
|
314
|
+
this.value = v;
|
|
315
|
+
this.dispatchEvent(new Event('input', { bubbles: true, composed: true }));
|
|
316
|
+
this.dispatchEvent(new Event('change', { bubbles: true, composed: true }));
|
|
317
|
+
}, _CmTimePicker_commitText = function _CmTimePicker_commitText() {
|
|
318
|
+
const text = this._text.trim();
|
|
319
|
+
const t = text ? parseTime(text, __classPrivateFieldGet(this, _CmTimePicker_instances, "a", _CmTimePicker_tf_get)) : '';
|
|
320
|
+
if (t === null) {
|
|
321
|
+
__classPrivateFieldSet(this, _CmTimePicker_keepText, true, "f");
|
|
322
|
+
__classPrivateFieldGet(this, _CmTimePicker_instances, "m", _CmTimePicker_setValue).call(this, '');
|
|
323
|
+
this.requestUpdate();
|
|
324
|
+
return;
|
|
325
|
+
}
|
|
326
|
+
__classPrivateFieldGet(this, _CmTimePicker_instances, "m", _CmTimePicker_setValue).call(this, t);
|
|
327
|
+
this._text = __classPrivateFieldGet(this, _CmTimePicker_instances, "m", _CmTimePicker_format).call(this, t);
|
|
328
|
+
}, _CmTimePicker_pick = function _CmTimePicker_pick(minutes) {
|
|
329
|
+
const t = minutesToTime(minutes);
|
|
330
|
+
this.open = false;
|
|
331
|
+
__classPrivateFieldGet(this, _CmTimePicker_instances, "m", _CmTimePicker_setValue).call(this, t);
|
|
332
|
+
this._text = __classPrivateFieldGet(this, _CmTimePicker_instances, "m", _CmTimePicker_format).call(this, t);
|
|
333
|
+
this.renderRoot.querySelector('input')?.focus();
|
|
334
|
+
};
|
|
335
|
+
CmTimePicker.formAssociated = true;
|
|
336
|
+
CmTimePicker.shadowRootOptions = { ...LitElement.shadowRootOptions, delegatesFocus: true };
|
|
337
|
+
CmTimePicker.properties = {
|
|
338
|
+
label: {},
|
|
339
|
+
helpText: { attribute: 'help-text' },
|
|
340
|
+
error: { reflect: true },
|
|
341
|
+
name: { reflect: true },
|
|
342
|
+
value: {},
|
|
343
|
+
placeholder: {},
|
|
344
|
+
min: {},
|
|
345
|
+
max: {},
|
|
346
|
+
step: { type: Number },
|
|
347
|
+
interval: { type: Number },
|
|
348
|
+
hourCycle: { attribute: 'hour-cycle' },
|
|
349
|
+
locale: { noAccessor: true },
|
|
350
|
+
required: { type: Boolean, reflect: true },
|
|
351
|
+
disabled: { type: Boolean, reflect: true },
|
|
352
|
+
open: { type: Boolean, reflect: true },
|
|
353
|
+
size: { reflect: true },
|
|
354
|
+
_text: { state: true },
|
|
355
|
+
_active: { state: true },
|
|
356
|
+
};
|
|
357
|
+
CmTimePicker.styles = [
|
|
358
|
+
base, topLayerStyles, dateFieldStyles, optionStyles,
|
|
359
|
+
css `
|
|
360
|
+
:host([data-cm-invalid]) .box { border-color: var(--cm-border-critical); }
|
|
361
|
+
:host { max-width: 100%; }
|
|
362
|
+
.pop {
|
|
363
|
+
position: absolute; z-index: 20; inset-block-start: calc(100% + var(--cm-space-4)); inset-inline-start: 0; min-width: 100%;
|
|
364
|
+
background: var(--cm-bg-surface); color: var(--cm-text-default);
|
|
365
|
+
border: var(--cm-border-1) solid var(--cm-border-subtle); border-radius: var(--cm-radius-md); box-shadow: var(--cm-shadow-md);
|
|
366
|
+
}
|
|
367
|
+
.pop[hidden] { display: none; }
|
|
368
|
+
[role='option'] { font-variant-numeric: tabular-nums; white-space: nowrap; }
|
|
369
|
+
[role='option'].active { background: var(--cm-bg-hover); }
|
|
370
|
+
[role='option'][aria-selected='true'] { background: var(--cm-action-primary); color: var(--cm-action-primary-text); }
|
|
371
|
+
[role='option'][aria-selected='true'].active { background: var(--cm-action-primary-hover); }
|
|
372
|
+
input { font-variant-numeric: tabular-nums; }
|
|
373
|
+
`,
|
|
374
|
+
];
|
|
375
|
+
define('cm-time-picker', CmTimePicker);
|