@commmerce/ui 1.1.1 → 1.2.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.
@@ -9,17 +9,33 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
9
9
  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");
10
10
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
11
  };
12
- var _CmTextField_instances, _CmTextField_internals, _CmTextField_id, _CmTextField_defaultValue, _CmTextField_onInput, _CmTextField_onChange;
12
+ var _CmTextField_instances, _CmTextField_internals, _CmTextField_id, _CmTextField_defaultValue, _CmTextField_validate, _CmTextField_show, _CmTextField_nearest, _CmTextField_onInput, _CmTextField_onBlur, _CmTextField_onChange;
13
13
  import { LitElement, html, css, nothing } from 'lit';
14
14
  import { live } from 'lit/directives/live.js';
15
15
  import { base, define, nextId } from '../base.js';
16
- import { CmLocalizedElement, defineStrings } from '../localize.js';
16
+ import { CmLocalizedElement, defineStrings, formatDate } from '../localize.js';
17
17
  const strings = defineStrings('textField', {
18
18
  /** Validation message when `required` and empty. */
19
19
  required: 'Please fill in this field.',
20
+ /** Validation: a date or time before `min`. `{min}` is the min in the page language, e.g. "16 Nov 2026, 3:30 pm". */
21
+ rangeUnderflow: 'Enter {min} or later.',
22
+ /** Validation: a date or time after `max`. */
23
+ rangeOverflow: 'Enter {max} or earlier.',
24
+ /** Validation: a number below `min`. */
25
+ numberUnderflow: 'Enter {min} or more.',
26
+ /** Validation: a number above `max`. */
27
+ numberOverflow: 'Enter {max} or less.',
28
+ /** Validation: a value off the `step`. `{lower}` and `{upper}` are the nearest allowed values. */
29
+ stepMismatch: 'Enter a valid value. The two nearest are {lower} and {upper}.',
30
+ /** Validation, `type="time"`: the time is only partly filled in. */
31
+ badTime: 'Enter a complete time.',
32
+ /** Validation, `type="datetime-local"`: the date and time are only partly filled in. */
33
+ badDateTime: 'Enter a complete date and time.',
20
34
  });
21
35
  /**
22
36
  * Labelled single- or multi-line input. Participates in native forms.
37
+ * `type="date"`, `"time"` and `"datetime-local"` use the browser's own picker; their values are ISO and local
38
+ * (`yyyy-mm-dd`, `hh:mm`, `yyyy-mm-ddThh:mm`), with no time zone.
23
39
  * @element cm-text-field
24
40
  * @fires input - On every keystroke (bubbles, composed).
25
41
  * @fires change - When the value is committed.
@@ -42,7 +58,10 @@ export class CmTextField extends CmLocalizedElement {
42
58
  this.value = '';
43
59
  /** Example input, shown in text-tertiary. Never a substitute for the label. */
44
60
  this.placeholder = '';
45
- /** Native input type. Ignored when `multiline`. */
61
+ /**
62
+ * Native input type. Ignored when `multiline`. `time` and `datetime-local` (since 1.2) give the browser's time and
63
+ * date-and-time pickers; their value is local time without a time zone, `hh:mm` and `yyyy-mm-ddThh:mm`.
64
+ */
46
65
  this.type = 'text';
47
66
  /** Renders a resizable textarea. */
48
67
  this.multiline = false;
@@ -67,8 +86,8 @@ export class CmTextField extends CmLocalizedElement {
67
86
  get form() { return __classPrivateFieldGet(this, _CmTextField_internals, "f").form; }
68
87
  get validity() { return __classPrivateFieldGet(this, _CmTextField_internals, "f").validity; }
69
88
  get validationMessage() { return __classPrivateFieldGet(this, _CmTextField_internals, "f").validationMessage; }
70
- checkValidity() { return __classPrivateFieldGet(this, _CmTextField_internals, "f").checkValidity(); }
71
- reportValidity() { return __classPrivateFieldGet(this, _CmTextField_internals, "f").reportValidity(); }
89
+ checkValidity() { __classPrivateFieldGet(this, _CmTextField_instances, "m", _CmTextField_validate).call(this); return __classPrivateFieldGet(this, _CmTextField_internals, "f").checkValidity(); }
90
+ reportValidity() { __classPrivateFieldGet(this, _CmTextField_instances, "m", _CmTextField_validate).call(this); return __classPrivateFieldGet(this, _CmTextField_internals, "f").reportValidity(); }
72
91
  formResetCallback() {
73
92
  this.value = __classPrivateFieldGet(this, _CmTextField_defaultValue, "f");
74
93
  this.error = '';
@@ -76,13 +95,7 @@ export class CmTextField extends CmLocalizedElement {
76
95
  formDisabledCallback(disabled) { this.disabled = disabled; }
77
96
  updated() {
78
97
  __classPrivateFieldGet(this, _CmTextField_internals, "f").setFormValue(this.value);
79
- const control = this.renderRoot.querySelector('.input');
80
- if (this.error)
81
- __classPrivateFieldGet(this, _CmTextField_internals, "f").setValidity({ customError: true }, this.error, control ?? undefined);
82
- else if (this.required && !this.value)
83
- __classPrivateFieldGet(this, _CmTextField_internals, "f").setValidity({ valueMissing: true }, this.t('required'), control ?? undefined);
84
- else
85
- __classPrivateFieldGet(this, _CmTextField_internals, "f").setValidity({});
98
+ __classPrivateFieldGet(this, _CmTextField_instances, "m", _CmTextField_validate).call(this);
86
99
  }
87
100
  render() {
88
101
  const describedBy = [this.error && `${__classPrivateFieldGet(this, _CmTextField_id, "f")}-err`, this.helpText && `${__classPrivateFieldGet(this, _CmTextField_id, "f")}-help`].filter(Boolean).join(' ') || nothing;
@@ -99,9 +112,10 @@ export class CmTextField extends CmLocalizedElement {
99
112
  : html `<input class="input" part="input" id=${common.id} type=${this.type}
100
113
  .value=${live(this.value)} placeholder=${this.placeholder || nothing}
101
114
  autocomplete=${this.autocomplete ?? nothing}
115
+ min=${this.min || nothing} max=${this.max || nothing} step=${this.step || nothing}
102
116
  ?required=${this.required} ?disabled=${this.disabled} ?readonly=${this.readonly}
103
117
  aria-invalid=${common.invalid} aria-describedby=${describedBy}
104
- @input=${__classPrivateFieldGet(this, _CmTextField_instances, "m", _CmTextField_onInput)} @change=${__classPrivateFieldGet(this, _CmTextField_instances, "m", _CmTextField_onChange)} />`;
118
+ @input=${__classPrivateFieldGet(this, _CmTextField_instances, "m", _CmTextField_onInput)} @change=${__classPrivateFieldGet(this, _CmTextField_instances, "m", _CmTextField_onChange)} @blur=${__classPrivateFieldGet(this, _CmTextField_instances, "m", _CmTextField_onBlur)} />`;
105
119
  return html `<div class="field">
106
120
  ${this.label
107
121
  ? html `<label for=${__classPrivateFieldGet(this, _CmTextField_id, "f")} part="label">${this.label}${this.required ? html `<span class="req" aria-hidden="true">*</span>` : nothing}${this.hint ? html `<span class="hint">[${this.hint}]</span>` : nothing}</label>`
@@ -112,8 +126,63 @@ export class CmTextField extends CmLocalizedElement {
112
126
  </div>`;
113
127
  }
114
128
  }
115
- _CmTextField_internals = new WeakMap(), _CmTextField_id = new WeakMap(), _CmTextField_defaultValue = new WeakMap(), _CmTextField_instances = new WeakSet(), _CmTextField_onInput = function _CmTextField_onInput(e) {
129
+ _CmTextField_internals = new WeakMap(), _CmTextField_id = new WeakMap(), _CmTextField_defaultValue = new WeakMap(), _CmTextField_instances = new WeakSet(), _CmTextField_validate = function _CmTextField_validate() {
130
+ const control = this.renderRoot.querySelector('.input');
131
+ const native = control instanceof HTMLInputElement ? control.validity : null;
132
+ const dated = this.type === 'time' || this.type === 'datetime-local';
133
+ if (this.error)
134
+ __classPrivateFieldGet(this, _CmTextField_internals, "f").setValidity({ customError: true }, this.error, control ?? undefined);
135
+ // A half-typed time reads as an empty value; say so instead of "fill in this field".
136
+ else if (dated && native?.badInput)
137
+ __classPrivateFieldGet(this, _CmTextField_internals, "f").setValidity({ badInput: true }, this.t(this.type === 'time' ? 'badTime' : 'badDateTime'), control ?? undefined);
138
+ else if (this.required && !this.value)
139
+ __classPrivateFieldGet(this, _CmTextField_internals, "f").setValidity({ valueMissing: true }, this.t('required'), control ?? undefined);
140
+ else if (!this.multiline && native?.rangeUnderflow)
141
+ __classPrivateFieldGet(this, _CmTextField_internals, "f").setValidity({ rangeUnderflow: true }, this.t(this.type === 'number' ? 'numberUnderflow' : 'rangeUnderflow', { min: __classPrivateFieldGet(this, _CmTextField_instances, "m", _CmTextField_show).call(this, this.min ?? '') }), control ?? undefined);
142
+ else if (!this.multiline && native?.rangeOverflow)
143
+ __classPrivateFieldGet(this, _CmTextField_internals, "f").setValidity({ rangeOverflow: true }, this.t(this.type === 'number' ? 'numberOverflow' : 'rangeOverflow', { max: __classPrivateFieldGet(this, _CmTextField_instances, "m", _CmTextField_show).call(this, this.max ?? '') }), control ?? undefined);
144
+ // Only with an explicit `step`: a number like 12.50 stays valid, as before 1.2.
145
+ else if (!this.multiline && this.step && native?.stepMismatch)
146
+ __classPrivateFieldGet(this, _CmTextField_internals, "f").setValidity({ stepMismatch: true }, this.t('stepMismatch', __classPrivateFieldGet(this, _CmTextField_instances, "m", _CmTextField_nearest).call(this, control)), control ?? undefined);
147
+ else
148
+ __classPrivateFieldGet(this, _CmTextField_internals, "f").setValidity({});
149
+ }, _CmTextField_show = function _CmTextField_show(v) {
150
+ const locale = this.locale;
151
+ if (this.type === 'number' && v !== '' && Number.isFinite(Number(v)))
152
+ return this.formatNumber(Number(v));
153
+ if (this.type === 'time') {
154
+ const t = /^(\d{2}):(\d{2})/.exec(v);
155
+ return t ? formatDate(new Date(2000, 0, 1, +t[1], +t[2]), { hour: 'numeric', minute: '2-digit' }, locale) : v;
156
+ }
157
+ const m = /^(\d{4})-(\d{2})-(\d{2})(?:T(\d{2}):(\d{2}))?/.exec(v);
158
+ if (!m)
159
+ return v;
160
+ const d = new Date(+m[1], +m[2] - 1, +m[3], +(m[4] ?? 0), +(m[5] ?? 0));
161
+ return this.type === 'datetime-local'
162
+ ? formatDate(d, { day: 'numeric', month: 'short', year: 'numeric', hour: 'numeric', minute: '2-digit' }, locale)
163
+ : formatDate(d, { day: 'numeric', month: 'short', year: 'numeric' }, locale);
164
+ }, _CmTextField_nearest = function _CmTextField_nearest(input) {
165
+ const unit = this.type === 'date' ? 86_400_000 : this.type === 'time' || this.type === 'datetime-local' ? 1000 : 1;
166
+ const step = Number(this.step) * unit || (this.type === 'date' ? 86_400_000 : this.type === 'number' ? 1 : 60_000);
167
+ const v = input.valueAsNumber;
168
+ const probe = document.createElement('input');
169
+ probe.type = this.type;
170
+ probe.value = this.min ?? '';
171
+ const base = Number.isFinite(probe.valueAsNumber) ? probe.valueAsNumber : 0;
172
+ const lower = base + Math.floor((v - base) / step) * step;
173
+ const show = (n) => {
174
+ if (this.type === 'number')
175
+ return this.formatNumber(Number(n.toPrecision(12)));
176
+ const d = new Date(n);
177
+ const iso = d.toISOString();
178
+ return __classPrivateFieldGet(this, _CmTextField_instances, "m", _CmTextField_show).call(this, this.type === 'time' ? iso.slice(11, 16) : this.type === 'date' ? iso.slice(0, 10) : iso.slice(0, 16));
179
+ };
180
+ return { lower: show(lower), upper: show(lower + step) };
181
+ }, _CmTextField_onInput = function _CmTextField_onInput(e) {
116
182
  this.value = e.target.value;
183
+ }, _CmTextField_onBlur = function _CmTextField_onBlur() {
184
+ // A half-typed date or time doesn't fire input; check it when the merchant leaves the box.
185
+ __classPrivateFieldGet(this, _CmTextField_instances, "m", _CmTextField_validate).call(this);
117
186
  }, _CmTextField_onChange = function _CmTextField_onChange() {
118
187
  // native change does not cross the shadow boundary; re-dispatch it
119
188
  this.dispatchEvent(new Event('change', { bubbles: true, composed: true }));
@@ -136,6 +205,9 @@ CmTextField.properties = {
136
205
  readonly: { type: Boolean, reflect: true },
137
206
  autocomplete: {},
138
207
  size: { reflect: true },
208
+ min: {},
209
+ max: {},
210
+ step: {},
139
211
  };
140
212
  CmTextField.styles = [
141
213
  base,
@@ -0,0 +1,210 @@
1
+ import { type PropertyValues } from 'lit';
2
+ import { type Tone } from '../base.js';
3
+ import { CmLocalizedElement } from '../localize.js';
4
+ import './icon.js';
5
+ import './button.js';
6
+ import './skeleton.js';
7
+ import './tooltip.js';
8
+ declare const itemStrings: {
9
+ /** When it happened, relative to now. `{count}` is a whole number; past and future wording are separate keys. */
10
+ justNow: string;
11
+ minutesAgo: {
12
+ one: string;
13
+ other: string;
14
+ };
15
+ hoursAgo: {
16
+ one: string;
17
+ other: string;
18
+ };
19
+ daysAgo: {
20
+ one: string;
21
+ other: string;
22
+ };
23
+ inMinutes: {
24
+ one: string;
25
+ other: string;
26
+ };
27
+ inHours: {
28
+ one: string;
29
+ other: string;
30
+ };
31
+ inDays: {
32
+ one: string;
33
+ other: string;
34
+ };
35
+ /** The button under a clamped body. */
36
+ showAll: string;
37
+ showLess: string;
38
+ };
39
+ declare const strings: {
40
+ /** Accessible name of the list. */
41
+ label: string;
42
+ /** The button at the end that asks for older events. */
43
+ showMore: string;
44
+ /** Announced while `loading`. */
45
+ loading: string;
46
+ };
47
+ declare module '../localize.js' {
48
+ interface CmStrings {
49
+ timelineItem: typeof itemStrings;
50
+ timeline: typeof strings;
51
+ }
52
+ }
53
+ /**
54
+ * One event in a `cm-timeline`: an icon for its kind, a title, when it happened, a meta line, an optional badge,
55
+ * an optional body (clamped to a few lines with Show all / Show less) and optional actions.
56
+ *
57
+ * The time is shown relative to now ("2 hours ago", then a date after a week), with the full date and time in a
58
+ * tooltip on hover and keyboard focus, and read out to screen readers after the relative time.
59
+ * @element cm-timeline-item
60
+ * @slot - The body: an email excerpt, a note, a call summary. Clamped to `lines` lines.
61
+ * @slot heading - Rich title (e.g. with a link), instead of `heading`.
62
+ * @slot meta - Rich meta line, instead of `meta`.
63
+ * @slot badge - A `cm-badge` for the kind or state, after the title.
64
+ * @slot actions - Links or buttons under the body, e.g. "Read email".
65
+ * @csspart icon - The round icon.
66
+ * @csspart title - The title.
67
+ * @csspart time - The `<time>` element.
68
+ * @csspart meta - The meta line.
69
+ * @csspart body - The body.
70
+ * @csspart toggle - The Show all / Show less button.
71
+ */
72
+ export declare class CmTimelineItem extends CmLocalizedElement {
73
+ #private;
74
+ static properties: {
75
+ icon: {
76
+ reflect: boolean;
77
+ };
78
+ tone: {
79
+ reflect: boolean;
80
+ };
81
+ heading: {};
82
+ datetime: {
83
+ reflect: boolean;
84
+ };
85
+ time: {};
86
+ meta: {};
87
+ lines: {
88
+ type: NumberConstructor;
89
+ };
90
+ expanded: {
91
+ type: BooleanConstructor;
92
+ reflect: boolean;
93
+ };
94
+ _overflow: {
95
+ state: boolean;
96
+ };
97
+ _body: {
98
+ state: boolean;
99
+ };
100
+ _meta: {
101
+ state: boolean;
102
+ };
103
+ _actions: {
104
+ state: boolean;
105
+ };
106
+ };
107
+ /** Icon name from `@commmerce/icons` for the kind of event ("mail", "note", "edit", "chat"). Empty shows a small dot. */
108
+ icon: string;
109
+ /** Colours the icon: `neutral` (default), `information`, `attention`, `success` or `critical`. */
110
+ tone: Tone | 'neutral';
111
+ /** What happened: "Email sent", "Status changed". Use the `heading` slot for a link. */
112
+ heading: string;
113
+ /** When it happened, as an ISO date-time (`2026-11-16T10:30:00+05:30`). Shown relative to now; used for `<time datetime>`. */
114
+ datetime: string;
115
+ /** The time as shown, instead of the relative time worked out from `datetime` (which still gives the full date on hover). */
116
+ time: string;
117
+ /** A line under the title: "by Rahul Kumar · sent". Use the `meta` slot for links. */
118
+ meta: string;
119
+ /** Lines of body shown before "Show all". 0 shows the whole body. */
120
+ lines: number;
121
+ /** The whole body is shown (after "Show all"). */
122
+ expanded: boolean;
123
+ _overflow: boolean;
124
+ _body: boolean;
125
+ _meta: boolean;
126
+ _actions: boolean;
127
+ static styles: import("lit").CSSResult[];
128
+ connectedCallback(): void;
129
+ disconnectedCallback(): void;
130
+ firstUpdated(): void;
131
+ updated(changed: PropertyValues): void;
132
+ /** The `datetime` as a Date, or undefined when missing or invalid. */
133
+ get date(): Date | undefined;
134
+ /** The time as shown: `time`, or `datetime` relative to now ("2 hours ago"), or a date when over a week away. */
135
+ get relativeTime(): string;
136
+ /** The full date and time, for the tooltip and screen readers. */
137
+ get fullTime(): string;
138
+ render(): import("lit-html").TemplateResult<1>;
139
+ }
140
+ /**
141
+ * An activity feed for a record page: one `cm-timeline-item` per event (emails, calls, notes, field changes,
142
+ * system events), newest first, joined by a vertical line, with "Show more" at the end for older events.
143
+ *
144
+ * It is an ordered list: the component renders the `<ol>` and one `<li>` per item, and each item is shown inside its
145
+ * `<li>`. The line between icons is decoration only.
146
+ * @element cm-timeline
147
+ * @slot - `cm-timeline-item` elements, in the order to show them. Other children are not shown.
148
+ * @slot empty - Shown when there are no items and nothing is loading, e.g. a `cm-empty-state`.
149
+ * @fires cm-load-more - When "Show more" is pressed. Fetch the next page, append the items and clear `loading`; clear `has-more` on the last page.
150
+ * @csspart list - The `<ol>`.
151
+ * @csspart more - The Show more button.
152
+ */
153
+ export declare class CmTimeline extends CmLocalizedElement {
154
+ #private;
155
+ static shadowRootOptions: {
156
+ slotAssignment: SlotAssignmentMode;
157
+ delegatesFocus?: boolean;
158
+ mode: ShadowRootMode;
159
+ serializable?: boolean;
160
+ };
161
+ static properties: {
162
+ label: {
163
+ string: string;
164
+ };
165
+ hasMore: {
166
+ type: BooleanConstructor;
167
+ attribute: string;
168
+ reflect: boolean;
169
+ };
170
+ loading: {
171
+ type: BooleanConstructor;
172
+ reflect: boolean;
173
+ };
174
+ moreLabel: {
175
+ attribute: string;
176
+ string: string;
177
+ };
178
+ _items: {
179
+ state: boolean;
180
+ };
181
+ _empty: {
182
+ state: boolean;
183
+ };
184
+ };
185
+ /** Accessible name of the list, e.g. "Activity for Asha Rao". Default: the built-in string `label` ("Activity"). */
186
+ label: string;
187
+ /** There are older events: shows the "Show more" button, which fires `cm-load-more`. */
188
+ hasMore: boolean;
189
+ /** Events are loading: the Show more button shows a spinner, or placeholder rows show when there are no items yet. Announced politely. */
190
+ loading: boolean;
191
+ /** Text of the Show more button. Default: the built-in string `showMore` ("Show more"). */
192
+ moreLabel: string;
193
+ _items: CmTimelineItem[];
194
+ _empty: Element[];
195
+ static styles: import("lit").CSSResult[];
196
+ connectedCallback(): void;
197
+ disconnectedCallback(): void;
198
+ /** The `cm-timeline-item` children, in order. */
199
+ get items(): CmTimelineItem[];
200
+ protected willUpdate(): void;
201
+ protected updated(): void;
202
+ render(): import("lit-html").TemplateResult<1>;
203
+ }
204
+ declare global {
205
+ interface HTMLElementTagNameMap {
206
+ 'cm-timeline': CmTimeline;
207
+ 'cm-timeline-item': CmTimelineItem;
208
+ }
209
+ }
210
+ export {};