@3mo/date-time-fields 0.15.1 → 0.16.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.
Files changed (35) hide show
  1. package/dist/FieldDateTimeBase.d.ts +1 -0
  2. package/dist/FieldDateTimeBase.d.ts.map +1 -1
  3. package/dist/FieldDateTimeBase.js +1 -1
  4. package/dist/FieldTime.d.ts +43 -4
  5. package/dist/FieldTime.d.ts.map +1 -1
  6. package/dist/FieldTime.js +157 -9
  7. package/dist/FieldTimeController.d.ts +72 -0
  8. package/dist/FieldTimeController.d.ts.map +1 -0
  9. package/dist/FieldTimeController.js +110 -0
  10. package/dist/custom-elements.json +140 -103
  11. package/dist/index.d.ts +3 -0
  12. package/dist/index.d.ts.map +1 -1
  13. package/dist/index.js +3 -0
  14. package/dist/segments/DateTimeSegment.d.ts +20 -0
  15. package/dist/segments/DateTimeSegment.d.ts.map +1 -0
  16. package/dist/segments/DateTimeSegment.js +4 -0
  17. package/dist/segments/DateTimeSegmenter.d.ts +68 -0
  18. package/dist/segments/DateTimeSegmenter.d.ts.map +1 -0
  19. package/dist/segments/DateTimeSegmenter.js +247 -0
  20. package/dist/segments/DateTimeSegmentsController.d.ts +90 -0
  21. package/dist/segments/DateTimeSegmentsController.d.ts.map +1 -0
  22. package/dist/segments/DateTimeSegmentsController.js +235 -0
  23. package/dist/segments/index.d.ts +5 -0
  24. package/dist/segments/index.d.ts.map +1 -0
  25. package/dist/segments/index.js +4 -0
  26. package/dist/segments/segmentsStyles.d.ts +3 -0
  27. package/dist/segments/segmentsStyles.d.ts.map +1 -0
  28. package/dist/segments/segmentsStyles.js +46 -0
  29. package/dist/selection/Calendar.d.ts.map +1 -1
  30. package/dist/selection/Calendar.js +3 -0
  31. package/dist/selection/HourList.d.ts +4 -0
  32. package/dist/selection/HourList.d.ts.map +1 -1
  33. package/dist/selection/HourList.js +24 -2
  34. package/dist/tsconfig.tsbuildinfo +1 -1
  35. package/package.json +4 -2
@@ -3,6 +3,7 @@ import { InputFieldComponent } from '@3mo/field';
3
3
  import { type MaterialIcon } from '@3mo/icon';
4
4
  import { FieldDateTimePrecision } from './FieldDateTimePrecision.js';
5
5
  import type { Calendar } from './selection/index.js';
6
+ export declare const dateTimeConverter: (value: unknown) => import("@3mo/date-time/DateTime.js").DateTime | undefined;
6
7
  /**
7
8
  * @attr open - Whether the date picker is open
8
9
  * @attr pickerHidden - Hide the date picker
@@ -1 +1 @@
1
- {"version":3,"file":"FieldDateTimeBase.d.ts","sourceRoot":"","sources":["../FieldDateTimeBase.ts"],"names":[],"mappings":"AACA,OAAO,EAA0E,KAAK,kBAAkB,EAAE,MAAM,WAAW,CAAA;AAE3H,OAAO,EAAE,mBAAmB,EAAE,MAAM,YAAY,CAAA;AAChD,OAAO,EAAE,KAAK,YAAY,EAAE,MAAM,WAAW,CAAA;AAC7C,OAAO,EAAE,sBAAsB,EAAE,MAAM,6BAA6B,CAAA;AACpE,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAA;AAepD;;;;;;;;GAQG;AACH,8BAAsB,iBAAiB,CAAC,CAAC,CAAE,SAAQ,mBAAmB,CAAC,CAAC,CAAC;IAC5B,IAAI,UAAQ;IAC3B,YAAY,UAAQ;IACS,qBAAqB,gDAAiB;IACE,SAAS,yBAAgC;IACjF,GAAG,CAAC,EAAE,QAAQ,CAAA;IACd,GAAG,CAAC,EAAE,QAAQ,CAAA;IAChC,YAAY,CAAC,EAAE,CAAC,IAAI,EAAE,QAAQ,KAAK,OAAO,CAAA;IAEzE,cAAc,gDAAiB;IAElB,SAAS,CAAC,QAAQ,CAAC,QAAQ,CAAC,EAAE,QAAQ,CAAA;cAEzC,SAAS;cAIT,YAAY;IAI/B,OAAO,CAAC,oBAAoB,CAE3B;IAED,SAAS,CAAC,QAAQ,CAAC,sBAAsB,EAAE,YAAY,CAAU;IAEjE,SAAS,CAAC,QAAQ,KAAK,YAAY,IAAI,QAAQ,GAAG,SAAS,CAA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cAAuB,QAAQ,uBAK9B;IAED,cAAuB,eAAe,uBAMrC;IAED,cAAuB,aAAa,uBAanC;IAED,SAAS,CAAC,QAAQ,CAAC,iBAAiB,CAAC,KAAK,EAAE,MAAM,GAAG,CAAC,GAAG,SAAS;uBACtC,iBAAiB,CAAC,KAAK,EAAE,CAAC,GAAG,SAAS,GAAG,MAAM;IAE3E,OAAO,KAAK,uBAAuB,GAYlC;IAED,SAAS,KAAK,0BAA0B,uBAOvC;IAED,SAAS,KAAK,eAAe,uBAkB5B;IAED,SAAS,KAAK,wBAAwB,uBAOrC;IAED,SAAS,CAAC,QAAQ,KAAK,eAAe,IAAI,kBAAkB,CAAA;IAE5D,SAAS,CAAC,iBAAiB,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,CAAC;IAYzD,OAAO,CAAC,gBAAgB;IAaxB,SAAS,KAAK,YAAY,uBAEzB;IAED,SAAS,KAAK,gBAAgB,uBAW7B;IAED,SAAS,CAAC,QAAQ,KAAK,aAAa,IAAI,aAAa,GAAG,SAAS,CAAA;IAEjE,OAAO,KAAK,YAAY,GAavB;IAED,OAAO,KAAK,gBAAgB,GAQ3B;IAED,OAAO,KAAK,kBAAkB,GAQ7B;IAED,OAAO,KAAK,kBAAkB,GAQ7B;IAED,SAAS,CAAC,wBAAwB,CAAC,IAAI,EAAE,QAAQ,EAAE,SAAS,EAAE,sBAAsB;CAMpF"}
@@ -3,7 +3,7 @@ import { cache, css, html, live, property, style, bind, state, query, ifDefined
3
3
  import { hasChanged } from '@a11d/equals';
4
4
  import { InputFieldComponent } from '@3mo/field';
5
5
  import { FieldDateTimePrecision } from './FieldDateTimePrecision.js';
6
- const dateTimeConverter = (value) => {
6
+ export const dateTimeConverter = (value) => {
7
7
  if (value instanceof DateTime) {
8
8
  return value;
9
9
  }
@@ -1,13 +1,52 @@
1
- import { FieldText } from '@3mo/text-fields';
1
+ import { FieldComponent } from '@3mo/field';
2
+ import { FieldDateTimePrecision } from './FieldDateTimePrecision.js';
3
+ import { FieldTimeController } from './FieldTimeController.js';
4
+ import { type HourCycle } from './segments/index.js';
2
5
  /**
6
+ * A time-of-day field. Its value is the 24-hour `HH:mm` (or `HH:mm:ss` at second precision) string the
7
+ * native time input uses, so that it drops in where one was, while the segments follow the language's
8
+ * own clock — "02:07 PM" for English, "14:07" for German — and the popover offers the hour and minute wheels.
9
+ *
10
+ * Its behaviour is {@link FieldTimeController}, for a time field of another design.
11
+ *
3
12
  * @element mo-field-time
4
13
  *
14
+ * @attr value - The time as `HH:mm` or `HH:mm:ss`
15
+ * @attr open - Whether the time picker is open
16
+ * @attr pickerHidden - Hide the time picker
17
+ * @attr dense - Whether the field is dense
18
+ * @attr precision - 'minute' (default) or 'second'
19
+ * @attr hourCycle - The hour cycle of the segments ('h11', 'h12', 'h23' or 'h24'). Defaults to the language's convention.
20
+ * @attr shortcutReferenceDate - The date the units the user leaves out are taken from. Defaults to now.
21
+ *
22
+ * @csspart segments - The group of segments making up the time
23
+ * @csspart segment - An editable unit of the time
24
+ * @csspart literal - A separator between the units
25
+ *
5
26
  * @i18n "Time"
6
27
  */
7
- export declare class FieldTime extends FieldText {
28
+ export declare class FieldTime extends FieldComponent<string> {
8
29
  label: string;
9
- get inputType(): string;
10
- protected get startSlotTemplate(): import("lit-html").HTMLTemplateResult;
30
+ value?: string;
31
+ open: boolean;
32
+ pickerHidden: boolean;
33
+ dense: boolean;
34
+ precision: FieldDateTimePrecision;
35
+ hourCycle?: HourCycle;
36
+ shortcutReferenceDate: import("@3mo/date-time/DateTime.js").DateTime;
37
+ readonly controller: FieldTimeController<this>;
38
+ protected get isActive(): boolean;
39
+ protected get isDense(): boolean;
40
+ protected get isPopulated(): boolean;
41
+ focus(): void;
42
+ setCustomValidity(error: string): void;
43
+ checkValidity(): Promise<boolean>;
44
+ reportValidity(): void;
45
+ static get styles(): import("@a11d/lit").CSSResult;
46
+ protected get template(): import("lit-html").HTMLTemplateResult;
47
+ protected get inputTemplate(): import("lit-html").HTMLTemplateResult;
48
+ protected get endSlotTemplate(): import("lit-html").HTMLTemplateResult;
49
+ protected get popoverTemplate(): import("lit-html").HTMLTemplateResult;
11
50
  }
12
51
  declare global {
13
52
  interface HTMLElementTagNameMap {
@@ -1 +1 @@
1
- {"version":3,"file":"FieldTime.d.ts","sourceRoot":"","sources":["../FieldTime.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAA;AAQ5C;;;;GAIG;AACH,qBACa,SAAU,SAAQ,SAAS;IAClB,KAAK,SAAY;IAEtC,IAAa,SAAS,WAErB;IAED,cAAuB,iBAAiB,0CAKvC;CACD;AAED,OAAO,CAAC,MAAM,CAAC;IACd,UAAU,qBAAqB;QAC9B,eAAe,EAAE,SAAS,CAAA;KAC1B;CACD"}
1
+ {"version":3,"file":"FieldTime.d.ts","sourceRoot":"","sources":["../FieldTime.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,MAAM,YAAY,CAAA;AAE3C,OAAO,EAAE,sBAAsB,EAAE,MAAM,6BAA6B,CAAA;AAEpE,OAAO,EAAE,mBAAmB,EAAE,MAAM,0BAA0B,CAAA;AAC9D,OAAO,EAAsC,KAAK,SAAS,EAAE,MAAM,qBAAqB,CAAA;AAMxF;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,qBACa,SAAU,SAAQ,cAAc,CAAC,MAAM,CAAC;IAC/B,KAAK,SAAY;IAC1B,KAAK,CAAC,EAAE,MAAM,CAAA;IACkB,IAAI,UAAQ;IAC3B,YAAY,UAAQ;IACpB,KAAK,UAAQ;IACwD,SAAS,yBAAgC;IAChF,SAAS,CAAC,EAAE,SAAS,CAAA;IACtB,qBAAqB,gDAAiB;IAEhG,QAAQ,CAAC,UAAU,4BAahB;IAEH,cAAuB,QAAQ,YAE9B;IAED,cAAuB,OAAO,YAE7B;IAED,cAAuB,WAAW,YAEjC;IAEQ,KAAK;IAIL,iBAAiB,CAAC,KAAK,EAAE,MAAM;IAIzB,aAAa;IAKnB,cAAc;IAIvB,WAAoB,MAAM,kCAgCzB;IAED,cAAuB,QAAQ,0CAK9B;IAED,cAAuB,aAAa,0CAMnC;IAED,cAAuB,eAAe,0CASrC;IAED,SAAS,KAAK,eAAe,0CAiB5B;CACD;AAED,OAAO,CAAC,MAAM,CAAC;IACd,UAAU,qBAAqB;QAC9B,eAAe,EAAE,SAAS,CAAA;KAC1B;CACD"}
package/dist/FieldTime.js CHANGED
@@ -1,34 +1,182 @@
1
1
  import { __decorate } from "tslib";
2
- import { component, html, property, style } from '@a11d/lit';
2
+ import { bind, cache, component, css, html, property, style } from '@a11d/lit';
3
+ import { FieldComponent } from '@3mo/field';
3
4
  import { Localizer } from '@3mo/localization';
4
- import { FieldText } from '@3mo/text-fields';
5
+ import { FieldDateTimePrecision } from './FieldDateTimePrecision.js';
6
+ import { dateTimeConverter } from './FieldDateTimeBase.js';
7
+ import { FieldTimeController } from './FieldTimeController.js';
8
+ import { hourCycleConverter, segmentsStyles } from './segments/index.js';
5
9
  Localizer.dictionaries.add('de', {
6
10
  'Time': 'Uhrzeit',
7
11
  });
8
- // TODO: Migrate to Temporal.PlainTime
9
12
  /**
13
+ * A time-of-day field. Its value is the 24-hour `HH:mm` (or `HH:mm:ss` at second precision) string the
14
+ * native time input uses, so that it drops in where one was, while the segments follow the language's
15
+ * own clock — "02:07 PM" for English, "14:07" for German — and the popover offers the hour and minute wheels.
16
+ *
17
+ * Its behaviour is {@link FieldTimeController}, for a time field of another design.
18
+ *
10
19
  * @element mo-field-time
11
20
  *
21
+ * @attr value - The time as `HH:mm` or `HH:mm:ss`
22
+ * @attr open - Whether the time picker is open
23
+ * @attr pickerHidden - Hide the time picker
24
+ * @attr dense - Whether the field is dense
25
+ * @attr precision - 'minute' (default) or 'second'
26
+ * @attr hourCycle - The hour cycle of the segments ('h11', 'h12', 'h23' or 'h24'). Defaults to the language's convention.
27
+ * @attr shortcutReferenceDate - The date the units the user leaves out are taken from. Defaults to now.
28
+ *
29
+ * @csspart segments - The group of segments making up the time
30
+ * @csspart segment - An editable unit of the time
31
+ * @csspart literal - A separator between the units
32
+ *
12
33
  * @i18n "Time"
13
34
  */
14
- let FieldTime = class FieldTime extends FieldText {
35
+ let FieldTime = class FieldTime extends FieldComponent {
15
36
  constructor() {
16
37
  super(...arguments);
17
38
  this.label = t('Time');
39
+ this.open = false;
40
+ this.pickerHidden = false;
41
+ this.dense = false;
42
+ this.precision = FieldDateTimePrecision.Minute;
43
+ this.shortcutReferenceDate = new DateTime();
44
+ this.controller = new FieldTimeController(this, host => ({
45
+ get value() { return host.value; },
46
+ get precision() { return host.precision; },
47
+ get hourCycle() { return host.hourCycle; },
48
+ get referenceDate() { return host.shortcutReferenceDate; },
49
+ get label() { return host._label; },
50
+ get disabled() { return host.disabled; },
51
+ get readonly() { return host.readonly; },
52
+ get required() { return host.required; },
53
+ get invalid() { return host.invalid; },
54
+ get handlePickerOpen() { return host.pickerHidden ? undefined : () => host.open = true; },
55
+ handleInput: value => host.handleInput(value),
56
+ handleChange: value => host.handleChange(value),
57
+ }));
58
+ }
59
+ get isActive() {
60
+ return super.isActive || this.open;
61
+ }
62
+ get isDense() {
63
+ return this.dense;
64
+ }
65
+ get isPopulated() {
66
+ return this.controller.isPopulated;
67
+ }
68
+ focus() {
69
+ this.controller.focus();
70
+ }
71
+ setCustomValidity(error) {
72
+ this.controller.setCustomValidity(error);
73
+ }
74
+ async checkValidity() {
75
+ await this.updateComplete;
76
+ return this.controller.checkValidity();
77
+ }
78
+ reportValidity() {
79
+ this.focus();
80
+ }
81
+ static get styles() {
82
+ return css `
83
+ ${super.styles}
84
+
85
+ :host {
86
+ position: relative;
87
+ }
88
+
89
+ :host([disabled]) {
90
+ pointer-events: none;
91
+ }
92
+
93
+ mo-field {
94
+ anchor-name: --mo-field-time;
95
+ }
96
+
97
+ mo-popover {
98
+ position-anchor: --mo-field-time;
99
+ position-visibility: anchors-visible;
100
+ background: var(--mo-color-background);
101
+ box-shadow: var(--mo-shadow);
102
+ border-radius: var(--mo-border-radius);
103
+ color: var(--mo-color-foreground);
104
+ font-size: 0.875em;
105
+ }
106
+
107
+ #selector {
108
+ height: clamp(175px, 100vh, 300px);
109
+ }
110
+
111
+ ${segmentsStyles}
112
+ `;
113
+ }
114
+ get template() {
115
+ return html `
116
+ ${super.template}
117
+ ${this.popoverTemplate}
118
+ `;
18
119
  }
19
- get inputType() {
20
- return 'time';
120
+ get inputTemplate() {
121
+ return html `
122
+ <div part='segments' ${this.controller.group.ref()}>
123
+ ${this.controller.segments.segments.map(segment => html `<span part=${segment.editable ? 'segment' : 'literal'} ${this.controller.segment.ref(segment)}></span>`)}
124
+ </div>
125
+ `;
21
126
  }
22
- get startSlotTemplate() {
127
+ get endSlotTemplate() {
23
128
  return html `
24
- <mo-icon slot='start' @click=${() => this.focus()} icon='schedule' ${style({ color: 'var(--mo-color-gray)' })}></mo-icon>
25
- ${super.startSlotTemplate}
129
+ ${super.endSlotTemplate}
130
+ ${this.pickerHidden ? html.nothing : html `
131
+ <mo-icon id='picker' slot='end' icon='schedule'
132
+ ${style({ color: this.isActive ? 'var(--mo-color-accent)' : 'var(--mo-color-gray)', cursor: 'pointer', userSelect: 'none' })}
133
+ ></mo-icon>
134
+ `}
135
+ `;
136
+ }
137
+ get popoverTemplate() {
138
+ const { controller } = this;
139
+ const navigate = (e) => controller.navigationDate = e.detail;
140
+ const pick = (e) => controller.pick(e.detail);
141
+ return this.pickerHidden ? html.nothing : html `
142
+ <mo-popover tabindex='-1' .anchor=${this} target='picker' ?open=${bind(this, 'open')}>
143
+ ${cache(!this.open ? html.nothing : html `
144
+ <mo-flex id='selector' direction='horizontal'>
145
+ <mo-hour-list style='flex: 1' .hourCycle=${this.hourCycle} .navigationDate=${controller.navigationDate} .value=${controller.selectedDate} @navigate=${navigate} @change=${pick}></mo-hour-list>
146
+ <mo-minute-list style='flex: 1' .navigationDate=${controller.navigationDate} .value=${controller.selectedDate} @navigate=${navigate} @change=${pick}></mo-minute-list>
147
+ ${this.precision < FieldDateTimePrecision.Second ? html.nothing : html `
148
+ <mo-second-list style='flex: 1' .navigationDate=${controller.navigationDate} .value=${controller.selectedDate} @navigate=${navigate} @change=${pick}></mo-second-list>
149
+ `}
150
+ </mo-flex>
151
+ `)}
152
+ </mo-popover>
26
153
  `;
27
154
  }
28
155
  };
29
156
  __decorate([
30
157
  property()
31
158
  ], FieldTime.prototype, "label", void 0);
159
+ __decorate([
160
+ property()
161
+ ], FieldTime.prototype, "value", void 0);
162
+ __decorate([
163
+ property({ type: Boolean, reflect: true })
164
+ ], FieldTime.prototype, "open", void 0);
165
+ __decorate([
166
+ property({ type: Boolean })
167
+ ], FieldTime.prototype, "pickerHidden", void 0);
168
+ __decorate([
169
+ property({ type: Boolean })
170
+ ], FieldTime.prototype, "dense", void 0);
171
+ __decorate([
172
+ property({ type: String, converter: value => FieldDateTimePrecision.parse(value || undefined) })
173
+ ], FieldTime.prototype, "precision", void 0);
174
+ __decorate([
175
+ property({ type: String, converter: hourCycleConverter })
176
+ ], FieldTime.prototype, "hourCycle", void 0);
177
+ __decorate([
178
+ property({ type: Object, converter: dateTimeConverter })
179
+ ], FieldTime.prototype, "shortcutReferenceDate", void 0);
32
180
  FieldTime = __decorate([
33
181
  component('mo-field-time')
34
182
  ], FieldTime);
@@ -0,0 +1,72 @@
1
+ import { Controller, type ReactiveControllerHost } from '@a11d/lit';
2
+ import { FieldDateTimePrecision } from './FieldDateTimePrecision.js';
3
+ import { DateTimeSegmentsController, type HourCycle } from './segments/index.js';
4
+ export type FieldTimeControllerOptions = {
5
+ /** `HH:mm`, or `HH:mm:ss` at second precision: what a native time input holds. */
6
+ readonly value?: string;
7
+ /** 'minute' (default) or 'second'. */
8
+ readonly precision?: FieldDateTimePrecision;
9
+ /** Defaults to the language's convention. */
10
+ readonly hourCycle?: HourCycle;
11
+ /** The day the time is edited on, and where the units the user leaves out are taken from. Defaults to now. */
12
+ readonly referenceDate?: DateTime;
13
+ /** The field's name, appended to every segment's own. */
14
+ readonly label?: string;
15
+ readonly disabled?: boolean;
16
+ readonly readonly?: boolean;
17
+ readonly required?: boolean;
18
+ readonly invalid?: boolean;
19
+ /** Fired while editing, once every unit is filled. */
20
+ handleInput?(value: string | undefined): void;
21
+ /** Fired on commit: leaving the segments, Enter, or a time picked. */
22
+ handleChange?(value: string | undefined): void;
23
+ /** Alt+ArrowDown in the segments, as on a native time input. Left out, the field has no picker. */
24
+ handlePickerOpen?(): void;
25
+ };
26
+ type OptionsOrFactory<THost> = FieldTimeControllerOptions | ((host: THost) => FieldTimeControllerOptions);
27
+ /**
28
+ * Everything a time-of-day field does, for a host which only renders it: the segments, the value as the
29
+ * `HH:mm` string of a native time input, where a picker stands, and the field's validity.
30
+ *
31
+ * ```html
32
+ * <div ${this.controller.group.ref()}>
33
+ * ${this.controller.segments.segments.map(segment => html`<span ${this.controller.segment.ref(segment)}></span>`)}
34
+ * </div>
35
+ * ```
36
+ *
37
+ * A picker is the host's to render: it opens on `handlePickerOpen`, scrolls with `navigationDate` and
38
+ * hands what was chosen to `pick`.
39
+ *
40
+ * @ssr false
41
+ */
42
+ export declare class FieldTimeController<THost extends ReactiveControllerHost = ReactiveControllerHost> extends Controller implements EventListenerObject {
43
+ protected readonly host: THost;
44
+ private static readonly pattern;
45
+ protected readonly options: FieldTimeControllerOptions;
46
+ readonly segments: DateTimeSegmentsController<THost>;
47
+ private customValidity;
48
+ private navigatedValue?;
49
+ private _navigationDate?;
50
+ constructor(host: THost, options: OptionsOrFactory<THost>);
51
+ get group(): import("@a11d/lit").ElementRef<HTMLElement, void>;
52
+ get segment(): import("@a11d/lit").ElementRefs<HTMLElement, import("./segments/DateTimeSegment.js").DateTimeSegment>;
53
+ get precision(): FieldDateTimePrecision;
54
+ /** The value as a time on the reference day, which is what the segments and a picker work with. */
55
+ get selectedDate(): DateTime | undefined;
56
+ /** Where a picker stands: at the value, until it is scrolled elsewhere, and back there whenever the value changes. */
57
+ get navigationDate(): import("@3mo/date-time/DateTime.js").DateTime;
58
+ set navigationDate(value: import("@3mo/date-time/DateTime.js").DateTime);
59
+ /** Whether the field shows anything: a value, or units typed on the way to one. */
60
+ get isPopulated(): boolean;
61
+ hostUpdate(): void;
62
+ hostUpdated(): void;
63
+ handleEvent(event: Event): void;
64
+ focus(): void;
65
+ /** Commits a time chosen outside the segments, e.g. in a picker. */
66
+ pick(date: DateTime): void;
67
+ setCustomValidity(message: string): void;
68
+ checkValidity(): boolean;
69
+ private toValue;
70
+ }
71
+ export {};
72
+ //# sourceMappingURL=FieldTimeController.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"FieldTimeController.d.ts","sourceRoot":"","sources":["../FieldTimeController.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,KAAK,sBAAsB,EAAE,MAAM,WAAW,CAAA;AACnE,OAAO,EAAE,sBAAsB,EAAE,MAAM,6BAA6B,CAAA;AACpE,OAAO,EAAE,0BAA0B,EAAE,KAAK,SAAS,EAAE,MAAM,qBAAqB,CAAA;AAEhF,MAAM,MAAM,0BAA0B,GAAG;IACxC,kFAAkF;IAClF,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,CAAA;IACvB,sCAAsC;IACtC,QAAQ,CAAC,SAAS,CAAC,EAAE,sBAAsB,CAAA;IAC3C,6CAA6C;IAC7C,QAAQ,CAAC,SAAS,CAAC,EAAE,SAAS,CAAA;IAC9B,8GAA8G;IAC9G,QAAQ,CAAC,aAAa,CAAC,EAAE,QAAQ,CAAA;IACjC,yDAAyD;IACzD,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,CAAA;IACvB,QAAQ,CAAC,QAAQ,CAAC,EAAE,OAAO,CAAA;IAC3B,QAAQ,CAAC,QAAQ,CAAC,EAAE,OAAO,CAAA;IAC3B,QAAQ,CAAC,QAAQ,CAAC,EAAE,OAAO,CAAA;IAC3B,QAAQ,CAAC,OAAO,CAAC,EAAE,OAAO,CAAA;IAC1B,sDAAsD;IACtD,WAAW,CAA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AAC,OAAO,EAAE,MAAM;IAIjC,aAAa;IAIb,OAAO,CAAC,OAAO;CAQf"}
@@ -0,0 +1,110 @@
1
+ import { Controller } from '@a11d/lit';
2
+ import { FieldDateTimePrecision } from './FieldDateTimePrecision.js';
3
+ import { DateTimeSegmentsController } from './segments/index.js';
4
+ /**
5
+ * Everything a time-of-day field does, for a host which only renders it: the segments, the value as the
6
+ * `HH:mm` string of a native time input, where a picker stands, and the field's validity.
7
+ *
8
+ * ```html
9
+ * <div ${this.controller.group.ref()}>
10
+ * ${this.controller.segments.segments.map(segment => html`<span ${this.controller.segment.ref(segment)}></span>`)}
11
+ * </div>
12
+ * ```
13
+ *
14
+ * A picker is the host's to render: it opens on `handlePickerOpen`, scrolls with `navigationDate` and
15
+ * hands what was chosen to `pick`.
16
+ *
17
+ * @ssr false
18
+ */
19
+ export class FieldTimeController extends Controller {
20
+ static { this.pattern = /^(\d{1,2}):(\d{2})(?::(\d{2}))?$/; }
21
+ constructor(host, options) {
22
+ super(host);
23
+ this.host = host;
24
+ this.customValidity = '';
25
+ this.options = typeof options === 'function' ? options(host) : options;
26
+ const controller = this;
27
+ this.segments = new DateTimeSegmentsController(host, {
28
+ timeOnly: true,
29
+ get value() { return controller.selectedDate; },
30
+ get precision() { return controller.precision; },
31
+ get referenceDate() { return controller.options.referenceDate; },
32
+ get hourCycle() { return controller.options.hourCycle; },
33
+ get label() { return controller.options.label; },
34
+ get disabled() { return controller.options.disabled; },
35
+ get readonly() { return controller.options.readonly; },
36
+ get required() { return controller.options.required; },
37
+ get invalid() { return controller.options.invalid; },
38
+ handleInput: value => controller.options.handleInput?.(controller.toValue(value)),
39
+ handleChange: value => controller.options.handleChange?.(controller.toValue(value)),
40
+ });
41
+ }
42
+ get group() {
43
+ return this.segments.group;
44
+ }
45
+ get segment() {
46
+ return this.segments.segment;
47
+ }
48
+ get precision() {
49
+ return this.options.precision ?? FieldDateTimePrecision.Minute;
50
+ }
51
+ /** The value as a time on the reference day, which is what the segments and a picker work with. */
52
+ get selectedDate() {
53
+ const match = this.options.value?.match(FieldTimeController.pattern);
54
+ if (!match) {
55
+ return undefined;
56
+ }
57
+ const [, hour, minute, second] = match.map(Number);
58
+ return (this.options.referenceDate ?? new DateTime()).dayStart.with({ hour, minute, second: second || 0 });
59
+ }
60
+ /** Where a picker stands: at the value, until it is scrolled elsewhere, and back there whenever the value changes. */
61
+ get navigationDate() {
62
+ return this._navigationDate ??= this.selectedDate ?? new DateTime();
63
+ }
64
+ set navigationDate(value) {
65
+ this._navigationDate = value;
66
+ this.host.requestUpdate();
67
+ }
68
+ /** Whether the field shows anything: a value, or units typed on the way to one. */
69
+ get isPopulated() {
70
+ return !!this.options.value || !this.segments.isEmpty;
71
+ }
72
+ hostUpdate() {
73
+ if (this.options.value !== this.navigatedValue) {
74
+ this.navigatedValue = this.options.value;
75
+ this._navigationDate = undefined;
76
+ }
77
+ }
78
+ // Idempotent for one and the same listener, so re-declaring it on every update costs nothing.
79
+ hostUpdated() {
80
+ this.group.value?.addEventListener('keydown', this);
81
+ }
82
+ handleEvent(event) {
83
+ const { key, altKey } = event;
84
+ if (altKey && key === 'ArrowDown' && this.options.handlePickerOpen) {
85
+ event.preventDefault();
86
+ this.options.handlePickerOpen();
87
+ }
88
+ }
89
+ focus() {
90
+ this.segments.focus();
91
+ }
92
+ /** Commits a time chosen outside the segments, e.g. in a picker. */
93
+ pick(date) {
94
+ this.options.handleChange?.(this.toValue(date));
95
+ }
96
+ setCustomValidity(message) {
97
+ this.customValidity = message;
98
+ }
99
+ checkValidity() {
100
+ return !this.customValidity && !(this.options.required && !this.options.value);
101
+ }
102
+ toValue(date) {
103
+ if (!date) {
104
+ return undefined;
105
+ }
106
+ const pad = (value) => String(value).padStart(2, '0');
107
+ const time = `${pad(date.hour)}:${pad(date.minute)}`;
108
+ return this.precision >= FieldDateTimePrecision.Second ? `${time}:${pad(date.second)}` : time;
109
+ }
110
+ }