@commmerce/ui 1.1.1 → 1.3.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 +38 -0
- package/dist/calendar.js +109 -0
- package/dist/cdn.js +1010 -533
- package/dist/components/board.d.ts +338 -0
- package/dist/components/board.js +1017 -0
- package/dist/components/button.d.ts +10 -0
- package/dist/components/button.js +25 -0
- package/dist/components/card.d.ts +7 -1
- package/dist/components/card.js +58 -7
- package/dist/components/color-field.d.ts +153 -0
- package/dist/components/color-field.js +369 -0
- package/dist/components/combobox.d.ts +17 -4
- package/dist/components/combobox.js +55 -23
- package/dist/components/date-picker.d.ts +49 -5
- package/dist/components/date-picker.js +211 -48
- package/dist/components/empty-state.d.ts +19 -4
- package/dist/components/empty-state.js +59 -5
- package/dist/components/file-upload.d.ts +33 -0
- package/dist/components/file-upload.js +80 -9
- package/dist/components/global-search.js +2 -7
- package/dist/components/metric.d.ts +97 -0
- package/dist/components/metric.js +168 -0
- package/dist/components/nested-list.d.ts +33 -1
- package/dist/components/nested-list.js +197 -18
- package/dist/components/option-group.d.ts +15 -6
- package/dist/components/option-group.js +25 -6
- package/dist/components/page-header.js +3 -2
- package/dist/components/resource-list.d.ts +14 -2
- package/dist/components/resource-list.js +48 -6
- package/dist/components/select.d.ts +20 -3
- package/dist/components/select.js +35 -11
- package/dist/components/table-header.d.ts +14 -0
- package/dist/components/table-header.js +21 -0
- package/dist/components/text-field.d.ts +36 -2
- package/dist/components/text-field.js +86 -14
- package/dist/components/timeline.d.ts +210 -0
- package/dist/components/timeline.js +380 -0
- package/dist/custom-elements.json +9371 -4963
- package/dist/index.d.ts +10 -0
- package/dist/index.js +5 -0
- package/dist/move-model.d.ts +93 -0
- package/dist/move-model.js +169 -0
- package/dist/popover.d.ts +24 -0
- package/dist/popover.js +59 -0
- package/package.json +3 -3
|
@@ -0,0 +1,369 @@
|
|
|
1
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
2
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
3
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
4
|
+
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");
|
|
5
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
6
|
+
};
|
|
7
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
8
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
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
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
|
+
};
|
|
12
|
+
var _CmColorField_instances, _CmColorField_internals, _CmColorField_id, _CmColorField_defaultValue, _CmColorField_typed, _CmColorField_emit, _CmColorField_pick, _CmColorField_onSwatchKey, _CmColorField_onHexInput, _CmColorField_commitHex, _CmColorField_onHexChange, _CmColorField_onHexKey, _CmColorField_onPickerInput, _CmColorField_onPickerChange;
|
|
13
|
+
import { LitElement, html, css, nothing } from 'lit';
|
|
14
|
+
import { live } from 'lit/directives/live.js';
|
|
15
|
+
import { base, define, nextId, logicalKey } from '../base.js';
|
|
16
|
+
import { CmLocalizedElement, defineStrings } from '../localize.js';
|
|
17
|
+
import './icon.js';
|
|
18
|
+
const strings = defineStrings('colorField', {
|
|
19
|
+
/** Accessible name of each swatch with a name: "Blue, #2e6faf". */
|
|
20
|
+
swatch: '{name}, {hex}',
|
|
21
|
+
/** Accessible name of the hex box. `hexNamed` is used when the field has a label. */
|
|
22
|
+
hex: 'Hex code',
|
|
23
|
+
hexNamed: '{label}, hex code',
|
|
24
|
+
/** Placeholder of the hex box. */
|
|
25
|
+
placeholder: '#RRGGBB',
|
|
26
|
+
/** Accessible name of the native colour picker button (`picker`). `pickerNamed` is used with a label. */
|
|
27
|
+
picker: 'Choose a custom colour',
|
|
28
|
+
pickerNamed: 'Choose a custom colour for {label}',
|
|
29
|
+
/** Validation message when `required` and no colour is chosen. */
|
|
30
|
+
required: 'Please choose a colour.',
|
|
31
|
+
/** Validation message for text that isn't a colour. */
|
|
32
|
+
invalid: 'Enter a colour as #RRGGBB, for example #2e6faf.',
|
|
33
|
+
/** Names of the default swatches. */
|
|
34
|
+
black: 'Black',
|
|
35
|
+
grey: 'Grey',
|
|
36
|
+
lightGrey: 'Light grey',
|
|
37
|
+
red: 'Red',
|
|
38
|
+
darkOrange: 'Dark orange',
|
|
39
|
+
orange: 'Orange',
|
|
40
|
+
yellow: 'Yellow',
|
|
41
|
+
lightGreen: 'Light green',
|
|
42
|
+
green: 'Green',
|
|
43
|
+
lightBlue: 'Light blue',
|
|
44
|
+
blue: 'Blue',
|
|
45
|
+
indigo: 'Indigo',
|
|
46
|
+
});
|
|
47
|
+
/**
|
|
48
|
+
* The default palette: the hues of Commmerce's light theme tokens (text-*, bg-*, icon-rating, nav-badge, gray-*), so
|
|
49
|
+
* colours picked for pick-list values sit well next to the admin. Names are built-in strings (translatable).
|
|
50
|
+
*/
|
|
51
|
+
const DEFAULT_SWATCHES = [
|
|
52
|
+
{ value: '#000000', key: 'black' },
|
|
53
|
+
{ value: '#707070', key: 'grey' },
|
|
54
|
+
{ value: '#c9c9c9', key: 'lightGrey' },
|
|
55
|
+
{ value: '#de0000', key: 'red' },
|
|
56
|
+
{ value: '#ad5500', key: 'darkOrange' },
|
|
57
|
+
{ value: '#f1a953', key: 'orange' },
|
|
58
|
+
{ value: '#ffc107', key: 'yellow' },
|
|
59
|
+
{ value: '#81bb47', key: 'lightGreen' },
|
|
60
|
+
{ value: '#3c7c00', key: 'green' },
|
|
61
|
+
{ value: '#5394d9', key: 'lightBlue' },
|
|
62
|
+
{ value: '#2e6faf', key: 'blue' },
|
|
63
|
+
{ value: '#446beb', key: 'indigo' },
|
|
64
|
+
];
|
|
65
|
+
/** `#RGB`, `RRGGBB`, `#RRGGBB` (any case, spaces around) -> `#rrggbb`; anything else -> null. */
|
|
66
|
+
export function normalizeHex(text) {
|
|
67
|
+
const m = /^#?([0-9a-f]{3}|[0-9a-f]{6})$/i.exec(text.trim());
|
|
68
|
+
if (!m)
|
|
69
|
+
return null;
|
|
70
|
+
const h = m[1].length === 3 ? [...m[1]].map((c) => c + c).join('') : m[1];
|
|
71
|
+
return `#${h.toLowerCase()}`;
|
|
72
|
+
}
|
|
73
|
+
const luminance = (hex) => {
|
|
74
|
+
const n = parseInt(hex.slice(1), 16);
|
|
75
|
+
return [(n >> 16) & 255, (n >> 8) & 255, n & 255]
|
|
76
|
+
.map((v) => v / 255)
|
|
77
|
+
.map((v) => (v <= 0.04045 ? v / 12.92 : ((v + 0.055) / 1.055) ** 2.4))
|
|
78
|
+
.reduce((s, v, i) => s + v * [0.2126, 0.7152, 0.0722][i], 0);
|
|
79
|
+
};
|
|
80
|
+
/**
|
|
81
|
+
* Black or white, whichever contrasts more with the swatch (always 4.5:1 or more). The swatch colour is data, not
|
|
82
|
+
* a theme colour, so this pairs it with plain black or white rather than a token that changes with the theme.
|
|
83
|
+
*/
|
|
84
|
+
export function checkColor(hex) {
|
|
85
|
+
const l = luminance(hex);
|
|
86
|
+
return (l + 0.05) / 0.05 >= 1.05 / (l + 0.05) ? '#000000' : '#ffffff';
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Colour field: a palette of swatches, a hex box and, optionally, the browser's colour picker. For colours that
|
|
90
|
+
* mean something to the merchant, such as the colour of each lead status. The value is lowercase `#rrggbb`.
|
|
91
|
+
* Participates in native forms.
|
|
92
|
+
* @element cm-color-field
|
|
93
|
+
* @fires input - When the colour changes: a swatch, a complete hex code typed, or the picker moving.
|
|
94
|
+
* @fires change - When the colour is committed: a swatch picked, the hex box left or Enter pressed, the picker closed.
|
|
95
|
+
* @csspart label - The label.
|
|
96
|
+
* @csspart swatches - The swatch radio group.
|
|
97
|
+
* @csspart swatch - Each swatch.
|
|
98
|
+
* @csspart input - The hex box.
|
|
99
|
+
*/
|
|
100
|
+
export class CmColorField extends CmLocalizedElement {
|
|
101
|
+
constructor() {
|
|
102
|
+
super(...arguments);
|
|
103
|
+
_CmColorField_instances.add(this);
|
|
104
|
+
/** Visible label above the swatches, e.g. "Colour". Without one, give the element an `aria-label`. */
|
|
105
|
+
this.label = '';
|
|
106
|
+
/** Lowercase bracketed note after the label, e.g. "shown on the status tag". */
|
|
107
|
+
this.hint = '';
|
|
108
|
+
/** One sentence under the field in text-tertiary. */
|
|
109
|
+
this.helpText = '';
|
|
110
|
+
/** Error message from your own validation; turns the hex box critical and is announced. */
|
|
111
|
+
this.error = '';
|
|
112
|
+
/** Form field name. */
|
|
113
|
+
this.name = '';
|
|
114
|
+
/** The colour as lowercase `#rrggbb`, or empty for none. `#ABC` and `ABCDEF` are accepted and normalised. */
|
|
115
|
+
this.value = '';
|
|
116
|
+
/** Adds a button that opens the browser's own colour picker, for colours outside the palette. */
|
|
117
|
+
this.picker = false;
|
|
118
|
+
/** Shows a red asterisk and blocks form submission until a colour is chosen. */
|
|
119
|
+
this.required = false;
|
|
120
|
+
/** Blocks interaction. Also set by a disabled `<fieldset>`. */
|
|
121
|
+
this.disabled = false;
|
|
122
|
+
/** The hex box holds text that isn't a colour (after it was committed). */
|
|
123
|
+
this._invalid = false;
|
|
124
|
+
_CmColorField_internals.set(this, this.attachInternals());
|
|
125
|
+
_CmColorField_id.set(this, nextId('cm-cf'));
|
|
126
|
+
_CmColorField_defaultValue.set(this, '');
|
|
127
|
+
/** The value this element set itself from the hex box; any other new value replaces the box's text. */
|
|
128
|
+
_CmColorField_typed.set(this, void 0);
|
|
129
|
+
}
|
|
130
|
+
connectedCallback() {
|
|
131
|
+
super.connectedCallback();
|
|
132
|
+
__classPrivateFieldSet(this, _CmColorField_defaultValue, normalizeHex(this.value) ?? this.value, "f");
|
|
133
|
+
}
|
|
134
|
+
get form() { return __classPrivateFieldGet(this, _CmColorField_internals, "f").form; }
|
|
135
|
+
get validity() { return __classPrivateFieldGet(this, _CmColorField_internals, "f").validity; }
|
|
136
|
+
get validationMessage() { return __classPrivateFieldGet(this, _CmColorField_internals, "f").validationMessage; }
|
|
137
|
+
checkValidity() { return __classPrivateFieldGet(this, _CmColorField_internals, "f").checkValidity(); }
|
|
138
|
+
reportValidity() { return __classPrivateFieldGet(this, _CmColorField_internals, "f").reportValidity(); }
|
|
139
|
+
formResetCallback() {
|
|
140
|
+
this.value = __classPrivateFieldGet(this, _CmColorField_defaultValue, "f");
|
|
141
|
+
this._draft = undefined;
|
|
142
|
+
this._invalid = false;
|
|
143
|
+
}
|
|
144
|
+
formDisabledCallback(d) { this.disabled = d; }
|
|
145
|
+
formStateRestoreCallback(state) { this.value = state; }
|
|
146
|
+
/** The swatches in use, normalised, with their names. Invalid colours are left out. */
|
|
147
|
+
get items() {
|
|
148
|
+
if (!this.swatches)
|
|
149
|
+
return DEFAULT_SWATCHES.map((s) => ({ value: s.value, name: this.t(s.key) }));
|
|
150
|
+
return this.swatches.flatMap((s) => {
|
|
151
|
+
const o = typeof s === 'string' ? { value: s } : s;
|
|
152
|
+
const value = normalizeHex(o?.value ?? '');
|
|
153
|
+
return value ? [{ value, name: o.name }] : [];
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
willUpdate(changed) {
|
|
157
|
+
if (changed.has('value')) {
|
|
158
|
+
const n = normalizeHex(this.value ?? '');
|
|
159
|
+
if (n && n !== this.value)
|
|
160
|
+
this.value = n;
|
|
161
|
+
if (this.hasUpdated && this.value !== __classPrivateFieldGet(this, _CmColorField_typed, "f")) {
|
|
162
|
+
this._draft = undefined;
|
|
163
|
+
this._invalid = false;
|
|
164
|
+
}
|
|
165
|
+
__classPrivateFieldSet(this, _CmColorField_typed, undefined, "f");
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
updated() {
|
|
169
|
+
const ok = !this.value || normalizeHex(this.value) !== null;
|
|
170
|
+
__classPrivateFieldGet(this, _CmColorField_internals, "f").setFormValue(this.value && ok ? this.value : null, this.value);
|
|
171
|
+
const anchor = this.renderRoot.querySelector('.input') ?? undefined;
|
|
172
|
+
if (this.error)
|
|
173
|
+
__classPrivateFieldGet(this, _CmColorField_internals, "f").setValidity({ customError: true }, this.error, anchor);
|
|
174
|
+
else if (this._invalid || !ok)
|
|
175
|
+
__classPrivateFieldGet(this, _CmColorField_internals, "f").setValidity({ patternMismatch: true }, this.t('invalid'), anchor);
|
|
176
|
+
else if (this.required && !this.value)
|
|
177
|
+
__classPrivateFieldGet(this, _CmColorField_internals, "f").setValidity({ valueMissing: true }, this.t('required'), anchor);
|
|
178
|
+
else
|
|
179
|
+
__classPrivateFieldGet(this, _CmColorField_internals, "f").setValidity({});
|
|
180
|
+
}
|
|
181
|
+
render() {
|
|
182
|
+
const items = this.items;
|
|
183
|
+
const current = normalizeHex(this.value ?? '');
|
|
184
|
+
const sel = items.findIndex((s) => s.value === current);
|
|
185
|
+
const message = this.error || (this._invalid || (this.value && !current) ? this.t('invalid') : '');
|
|
186
|
+
const describedBy = [message && `${__classPrivateFieldGet(this, _CmColorField_id, "f")}-err`, this.helpText && `${__classPrivateFieldGet(this, _CmColorField_id, "f")}-help`].filter(Boolean).join(' ') || nothing;
|
|
187
|
+
const hostLabel = this.getAttribute('aria-label');
|
|
188
|
+
const named = this.label || hostLabel || '';
|
|
189
|
+
return html `<div class="field">
|
|
190
|
+
${this.label
|
|
191
|
+
? html `<span class="label" id="${__classPrivateFieldGet(this, _CmColorField_id, "f")}-l" 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}</span>`
|
|
192
|
+
: nothing}
|
|
193
|
+
${items.length
|
|
194
|
+
? html `<div class="swatches" part="swatches" role="radiogroup"
|
|
195
|
+
aria-labelledby=${this.label ? `${__classPrivateFieldGet(this, _CmColorField_id, "f")}-l` : nothing} aria-label=${!this.label && hostLabel ? hostLabel : nothing}
|
|
196
|
+
aria-required=${this.required ? 'true' : nothing} aria-describedby=${describedBy}>
|
|
197
|
+
${items.map((s, i) => {
|
|
198
|
+
const name = s.name ? this.t('swatch', { name: s.name, hex: s.value }) : s.value;
|
|
199
|
+
return html `<button type="button" class="swatch" part="swatch" role="radio" aria-checked=${i === sel ? 'true' : 'false'}
|
|
200
|
+
aria-label=${name} title=${s.name || s.value} tabindex=${i === (sel >= 0 ? sel : 0) ? '0' : '-1'} ?disabled=${this.disabled}
|
|
201
|
+
style="background-color:${s.value};color:${checkColor(s.value)}"
|
|
202
|
+
@click=${() => __classPrivateFieldGet(this, _CmColorField_instances, "m", _CmColorField_pick).call(this, i)} @keydown=${(e) => __classPrivateFieldGet(this, _CmColorField_instances, "m", _CmColorField_onSwatchKey).call(this, e, i)}><cm-icon name="check" size="18"></cm-icon></button>`;
|
|
203
|
+
})}
|
|
204
|
+
</div>`
|
|
205
|
+
: nothing}
|
|
206
|
+
<div class="row">
|
|
207
|
+
${this.picker
|
|
208
|
+
? html `<label class="pick" part="picker" style=${current ? `background-color:${current}` : nothing}>
|
|
209
|
+
<input type="color" .value=${live(current ?? '#000000')} ?disabled=${this.disabled}
|
|
210
|
+
aria-label=${named ? this.t('pickerNamed', { label: named }) : this.t('picker')}
|
|
211
|
+
@input=${__classPrivateFieldGet(this, _CmColorField_instances, "m", _CmColorField_onPickerInput)} @change=${__classPrivateFieldGet(this, _CmColorField_instances, "m", _CmColorField_onPickerChange)} />
|
|
212
|
+
<cm-icon name="edit" size="12" aria-hidden="true"></cm-icon>
|
|
213
|
+
</label>`
|
|
214
|
+
: html `<span class="preview" part="preview" aria-hidden="true" style=${current ? `background-color:${current}` : nothing}></span>`}
|
|
215
|
+
<input class="input" part="input" id=${__classPrivateFieldGet(this, _CmColorField_id, "f")} type="text" inputmode="text" autocomplete="off" spellcheck="false"
|
|
216
|
+
maxlength="7" .value=${live(this._draft ?? this.value ?? '')} placeholder=${this.t('placeholder')}
|
|
217
|
+
aria-label=${named ? this.t('hexNamed', { label: named }) : this.t('hex')}
|
|
218
|
+
?required=${this.required} ?disabled=${this.disabled}
|
|
219
|
+
aria-invalid=${message ? 'true' : nothing} aria-describedby=${describedBy}
|
|
220
|
+
@input=${__classPrivateFieldGet(this, _CmColorField_instances, "m", _CmColorField_onHexInput)} @change=${__classPrivateFieldGet(this, _CmColorField_instances, "m", _CmColorField_onHexChange)} @keydown=${__classPrivateFieldGet(this, _CmColorField_instances, "m", _CmColorField_onHexKey)} />
|
|
221
|
+
</div>
|
|
222
|
+
${message ? html `<p class="error" id="${__classPrivateFieldGet(this, _CmColorField_id, "f")}-err" role="alert">${message}</p>` : nothing}
|
|
223
|
+
${this.helpText ? html `<p class="help" id="${__classPrivateFieldGet(this, _CmColorField_id, "f")}-help">${this.helpText}</p>` : nothing}
|
|
224
|
+
</div>`;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
_CmColorField_internals = new WeakMap(), _CmColorField_id = new WeakMap(), _CmColorField_defaultValue = new WeakMap(), _CmColorField_typed = new WeakMap(), _CmColorField_instances = new WeakSet(), _CmColorField_emit = function _CmColorField_emit(...types) {
|
|
228
|
+
for (const type of types)
|
|
229
|
+
this.dispatchEvent(new Event(type, { bubbles: true, composed: true }));
|
|
230
|
+
}, _CmColorField_pick = function _CmColorField_pick(i) {
|
|
231
|
+
const s = this.items[i];
|
|
232
|
+
if (!s || this.disabled)
|
|
233
|
+
return;
|
|
234
|
+
this.updateComplete.then(() => this.renderRoot.querySelectorAll('.swatch')[i]?.focus());
|
|
235
|
+
this._draft = undefined;
|
|
236
|
+
this._invalid = false;
|
|
237
|
+
if (s.value === this.value)
|
|
238
|
+
return;
|
|
239
|
+
this.value = s.value;
|
|
240
|
+
__classPrivateFieldGet(this, _CmColorField_instances, "m", _CmColorField_emit).call(this, 'input', 'change');
|
|
241
|
+
}, _CmColorField_onSwatchKey = function _CmColorField_onSwatchKey(e, i) {
|
|
242
|
+
const n = this.items.length;
|
|
243
|
+
const key = logicalKey(e, this);
|
|
244
|
+
let next;
|
|
245
|
+
if (key === 'ArrowRight' || e.key === 'ArrowDown')
|
|
246
|
+
next = (i + 1) % n;
|
|
247
|
+
else if (key === 'ArrowLeft' || e.key === 'ArrowUp')
|
|
248
|
+
next = (i - 1 + n) % n;
|
|
249
|
+
else if (e.key === 'Home')
|
|
250
|
+
next = 0;
|
|
251
|
+
else if (e.key === 'End')
|
|
252
|
+
next = n - 1;
|
|
253
|
+
else if (e.key === ' ' || e.key === 'Enter')
|
|
254
|
+
next = i;
|
|
255
|
+
else
|
|
256
|
+
return;
|
|
257
|
+
e.preventDefault();
|
|
258
|
+
__classPrivateFieldGet(this, _CmColorField_instances, "m", _CmColorField_pick).call(this, next);
|
|
259
|
+
}, _CmColorField_onHexInput = function _CmColorField_onHexInput(e) {
|
|
260
|
+
e.stopPropagation();
|
|
261
|
+
const text = e.target.value;
|
|
262
|
+
this._draft = text;
|
|
263
|
+
const n = /^#?[0-9a-f]{6}$/i.test(text.trim()) ? normalizeHex(text) : null; // live only for full codes
|
|
264
|
+
if (n && n !== this.value) {
|
|
265
|
+
__classPrivateFieldSet(this, _CmColorField_typed, n, "f");
|
|
266
|
+
this.value = n;
|
|
267
|
+
__classPrivateFieldGet(this, _CmColorField_instances, "m", _CmColorField_emit).call(this, 'input');
|
|
268
|
+
}
|
|
269
|
+
if (this._invalid && (n || !text.trim()))
|
|
270
|
+
this._invalid = false;
|
|
271
|
+
}, _CmColorField_commitHex = function _CmColorField_commitHex() {
|
|
272
|
+
if (this._draft === undefined)
|
|
273
|
+
return;
|
|
274
|
+
const text = this._draft.trim();
|
|
275
|
+
const n = text ? normalizeHex(text) : '';
|
|
276
|
+
if (n === null) {
|
|
277
|
+
this._invalid = true;
|
|
278
|
+
return;
|
|
279
|
+
}
|
|
280
|
+
const before = this.value;
|
|
281
|
+
this.value = n;
|
|
282
|
+
this._draft = undefined;
|
|
283
|
+
this._invalid = false;
|
|
284
|
+
if (n !== before)
|
|
285
|
+
__classPrivateFieldGet(this, _CmColorField_instances, "m", _CmColorField_emit).call(this, 'input');
|
|
286
|
+
__classPrivateFieldGet(this, _CmColorField_instances, "m", _CmColorField_emit).call(this, 'change');
|
|
287
|
+
}, _CmColorField_onHexChange = function _CmColorField_onHexChange(e) {
|
|
288
|
+
e.stopPropagation();
|
|
289
|
+
__classPrivateFieldGet(this, _CmColorField_instances, "m", _CmColorField_commitHex).call(this);
|
|
290
|
+
}, _CmColorField_onHexKey = function _CmColorField_onHexKey(e) {
|
|
291
|
+
if (e.key === 'Enter')
|
|
292
|
+
__classPrivateFieldGet(this, _CmColorField_instances, "m", _CmColorField_commitHex).call(this);
|
|
293
|
+
}, _CmColorField_onPickerInput = function _CmColorField_onPickerInput(e) {
|
|
294
|
+
e.stopPropagation();
|
|
295
|
+
const n = normalizeHex(e.target.value);
|
|
296
|
+
if (!n || n === this.value)
|
|
297
|
+
return;
|
|
298
|
+
this.value = n;
|
|
299
|
+
this._draft = undefined;
|
|
300
|
+
this._invalid = false;
|
|
301
|
+
__classPrivateFieldGet(this, _CmColorField_instances, "m", _CmColorField_emit).call(this, 'input');
|
|
302
|
+
}, _CmColorField_onPickerChange = function _CmColorField_onPickerChange(e) {
|
|
303
|
+
e.stopPropagation();
|
|
304
|
+
__classPrivateFieldGet(this, _CmColorField_instances, "m", _CmColorField_emit).call(this, 'change');
|
|
305
|
+
};
|
|
306
|
+
CmColorField.formAssociated = true;
|
|
307
|
+
CmColorField.shadowRootOptions = { ...LitElement.shadowRootOptions, delegatesFocus: true };
|
|
308
|
+
CmColorField.properties = {
|
|
309
|
+
label: {},
|
|
310
|
+
hint: {},
|
|
311
|
+
helpText: { attribute: 'help-text' },
|
|
312
|
+
error: { reflect: true },
|
|
313
|
+
name: { reflect: true },
|
|
314
|
+
value: {},
|
|
315
|
+
swatches: { type: Array },
|
|
316
|
+
picker: { type: Boolean },
|
|
317
|
+
required: { type: Boolean, reflect: true },
|
|
318
|
+
disabled: { type: Boolean, reflect: true },
|
|
319
|
+
_draft: { state: true },
|
|
320
|
+
_invalid: { state: true },
|
|
321
|
+
};
|
|
322
|
+
CmColorField.styles = [
|
|
323
|
+
base,
|
|
324
|
+
css `
|
|
325
|
+
:host { display: block; }
|
|
326
|
+
.field { display: flex; flex-direction: column; gap: var(--cm-space-10); }
|
|
327
|
+
.label { font-size: var(--cm-type-label-size); color: var(--cm-text-default); }
|
|
328
|
+
.req { color: var(--cm-text-critical); margin-inline-start: 2px; }
|
|
329
|
+
.hint { margin-inline-start: var(--cm-space-10); font-size: 12px; color: var(--cm-text-tertiary); text-transform: lowercase; }
|
|
330
|
+
.swatches { display: flex; flex-wrap: wrap; gap: var(--cm-space-10); padding: 4px; margin: -4px; }
|
|
331
|
+
.swatch {
|
|
332
|
+
position: relative; display: inline-flex; align-items: center; justify-content: center;
|
|
333
|
+
width: 32px; height: 32px; padding: 0; cursor: pointer; border-radius: var(--cm-radius-full);
|
|
334
|
+
border: var(--cm-border-1) solid var(--cm-border-input);
|
|
335
|
+
}
|
|
336
|
+
.swatch cm-icon { visibility: hidden; }
|
|
337
|
+
.swatch[aria-checked='true'] { box-shadow: 0 0 0 2px var(--cm-bg-surface), 0 0 0 4px var(--cm-border-strong); }
|
|
338
|
+
.swatch[aria-checked='true'] cm-icon { visibility: visible; }
|
|
339
|
+
.swatch:focus-visible { outline: 2px solid var(--cm-border-strong); outline-offset: 6px; }
|
|
340
|
+
.swatch:disabled { cursor: not-allowed; opacity: 0.4; }
|
|
341
|
+
.row { display: flex; align-items: center; gap: var(--cm-space-10); }
|
|
342
|
+
.preview, .pick {
|
|
343
|
+
position: relative; flex: none; width: 42px; height: 42px; border-radius: var(--cm-radius-sm);
|
|
344
|
+
border: var(--cm-border-1) solid var(--cm-border-input); background: var(--cm-bg-inset);
|
|
345
|
+
}
|
|
346
|
+
.pick { cursor: pointer; }
|
|
347
|
+
.pick input { position: absolute; inset: 0; width: 100%; height: 100%; opacity: 0; cursor: inherit; padding: 0; border: 0; }
|
|
348
|
+
.pick:focus-within { outline: 2px solid var(--cm-border-strong); outline-offset: 2px; }
|
|
349
|
+
.pick cm-icon {
|
|
350
|
+
position: absolute; inset-inline-end: -6px; bottom: -6px; padding: 2px; border-radius: var(--cm-radius-full);
|
|
351
|
+
background: var(--cm-bg-surface); color: var(--cm-text-default); border: var(--cm-border-1) solid var(--cm-border-input);
|
|
352
|
+
}
|
|
353
|
+
.input {
|
|
354
|
+
width: 11ch; min-width: 0; flex: 0 1 auto; height: 42px; padding: 0 var(--cm-space-10);
|
|
355
|
+
font: inherit; font-size: 16px; font-variant-numeric: tabular-nums; color: var(--cm-text-default);
|
|
356
|
+
background: var(--cm-bg-surface); border: var(--cm-border-1) solid var(--cm-border-input);
|
|
357
|
+
border-radius: var(--cm-radius-sm); outline: none; box-sizing: content-box;
|
|
358
|
+
}
|
|
359
|
+
.input::placeholder { color: var(--cm-text-tertiary); }
|
|
360
|
+
.input:focus { border-color: var(--cm-border-strong); box-shadow: 0 0 0 1px var(--cm-border-strong); }
|
|
361
|
+
.input[aria-invalid='true'] { border-color: var(--cm-border-critical); }
|
|
362
|
+
.input:disabled { background: var(--cm-bg-input-filled); color: var(--cm-text-secondary); border-color: var(--cm-border-default); cursor: not-allowed; }
|
|
363
|
+
:host([disabled]) .pick { cursor: not-allowed; opacity: 0.4; }
|
|
364
|
+
:host([disabled]) .preview { opacity: 0.4; }
|
|
365
|
+
.help { margin: 0; font-size: 12px; color: var(--cm-text-tertiary); }
|
|
366
|
+
.error { margin: 0; font-size: 12px; color: var(--cm-text-critical); }
|
|
367
|
+
`,
|
|
368
|
+
];
|
|
369
|
+
define('cm-color-field', CmColorField);
|
|
@@ -20,6 +20,8 @@ declare const strings: {
|
|
|
20
20
|
required: string;
|
|
21
21
|
/** Validation: `required`, `multiple` and nothing chosen. */
|
|
22
22
|
requiredMultiple: string;
|
|
23
|
+
/** The chosen option in the box or its tag with `show-group`: "Lead: Company". */
|
|
24
|
+
grouped: string;
|
|
23
25
|
};
|
|
24
26
|
declare module '../localize.js' {
|
|
25
27
|
interface CmStrings {
|
|
@@ -32,9 +34,11 @@ declare module '../localize.js' {
|
|
|
32
34
|
* tags). With `filter="none"` it doesn't filter by itself: listen for `cm-search`, fetch matches, set `options`
|
|
33
35
|
* and toggle `loading`. With `allow-custom` the merchant can add a value that isn't in the list (a new product tag).
|
|
34
36
|
*
|
|
35
|
-
* Options come from `<option>` children or the `options` property, as for `cm-select
|
|
37
|
+
* Options come from `<option>` children or the `options` property, as for `cm-select`, and can be grouped under
|
|
38
|
+
* headings with `<optgroup label="…">` or a `group` on each option (since 1.3). Filtering keeps the heading of
|
|
39
|
+
* every group that still has a match.
|
|
36
40
|
* @element cm-combobox
|
|
37
|
-
* @slot - `<option value="…">Label</option>` elements
|
|
41
|
+
* @slot - `<option value="…">Label</option>` elements, optionally inside `<optgroup label="…">`. `disabled` (also on an optgroup) and `selected` are honoured.
|
|
38
42
|
* @fires change - When the value changes (an option picked or removed, or a custom value added).
|
|
39
43
|
* @fires input - Same moment as `change`.
|
|
40
44
|
* @fires cm-search - As the merchant types, with `detail: { query }`. Use it to load matches from the server.
|
|
@@ -104,6 +108,10 @@ export declare class CmCombobox extends CmLocalizedElement {
|
|
|
104
108
|
type: BooleanConstructor;
|
|
105
109
|
reflect: boolean;
|
|
106
110
|
};
|
|
111
|
+
showGroup: {
|
|
112
|
+
type: BooleanConstructor;
|
|
113
|
+
attribute: string;
|
|
114
|
+
};
|
|
107
115
|
_query: {
|
|
108
116
|
state: boolean;
|
|
109
117
|
};
|
|
@@ -131,7 +139,7 @@ export declare class CmCombobox extends CmLocalizedElement {
|
|
|
131
139
|
values: string[];
|
|
132
140
|
/** Shown in the empty text box, e.g. "Search tags". */
|
|
133
141
|
placeholder: string;
|
|
134
|
-
/** Options as strings or `{ value, label, disabled }`. Used instead of `<option>` children when set. */
|
|
142
|
+
/** Options as strings or `{ value, label, disabled, group }`. Used instead of `<option>` children when set. */
|
|
135
143
|
options: Array<string | SelectOption>;
|
|
136
144
|
/** Lets the merchant choose several options, shown as removable tags. Backspace in the empty box removes the last one. */
|
|
137
145
|
multiple: boolean;
|
|
@@ -151,6 +159,11 @@ export declare class CmCombobox extends CmLocalizedElement {
|
|
|
151
159
|
disabled: boolean;
|
|
152
160
|
/** Whether the list is showing. */
|
|
153
161
|
open: boolean;
|
|
162
|
+
/**
|
|
163
|
+
* Shows the chosen option's group before its label in the box, or in its tag with `multiple` ("Lead: Company"),
|
|
164
|
+
* for grouped lists where labels repeat across groups. The list itself shows the bare label under its heading. Since 1.3.
|
|
165
|
+
*/
|
|
166
|
+
showGroup: boolean;
|
|
154
167
|
_query: string;
|
|
155
168
|
_active: number;
|
|
156
169
|
_slotted: SelectOption[];
|
|
@@ -167,7 +180,7 @@ export declare class CmCombobox extends CmLocalizedElement {
|
|
|
167
180
|
formResetCallback(): void;
|
|
168
181
|
formDisabledCallback(d: boolean): void;
|
|
169
182
|
formStateRestoreCallback(state: string | FormData): void;
|
|
170
|
-
/** All options in use, normalised (before filtering). */
|
|
183
|
+
/** All options in use, normalised (before filtering), in the order shown (options of a group together). */
|
|
171
184
|
get items(): SelectOption[];
|
|
172
185
|
/** The options shown for the current query. */
|
|
173
186
|
get filteredItems(): SelectOption[];
|
|
@@ -9,12 +9,12 @@ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (
|
|
|
9
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
10
|
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
11
11
|
};
|
|
12
|
-
var _CmCombobox_instances, _CmCombobox_internals, _CmCombobox_id, _CmCombobox_default, _CmCombobox_labels, _CmCombobox_observer, _CmCombobox_stopOutside, _CmCombobox_readChildren, _CmCombobox_labelOf, _CmCombobox_createText_get, _CmCombobox_rows, _CmCombobox_emit, _CmCombobox_show, _CmCombobox_hide, _CmCombobox_enabled, _CmCombobox_move, _CmCombobox_choose, _CmCombobox_create, _CmCombobox_focusInput, _CmCombobox_onInput, _CmCombobox_onKeyDown, _CmCombobox_onBlur, _CmCombobox_highlight;
|
|
12
|
+
var _CmCombobox_instances, _CmCombobox_internals, _CmCombobox_id, _CmCombobox_default, _CmCombobox_labels, _CmCombobox_groups, _CmCombobox_observer, _CmCombobox_stopOutside, _CmCombobox_readChildren, _CmCombobox_labelOf, _CmCombobox_displayOf, _CmCombobox_createText_get, _CmCombobox_rows, _CmCombobox_emit, _CmCombobox_show, _CmCombobox_hide, _CmCombobox_enabled, _CmCombobox_move, _CmCombobox_choose, _CmCombobox_create, _CmCombobox_focusInput, _CmCombobox_onInput, _CmCombobox_onKeyDown, _CmCombobox_onBlur, _CmCombobox_highlight;
|
|
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
16
|
import { CmLocalizedElement, defineStrings } from '../localize.js';
|
|
17
|
-
import { popoverStyles, closeOnOutsidePointer } from '../popover.js';
|
|
17
|
+
import { popoverStyles, groupStyles, sortByGroup, groupRuns, closeOnOutsidePointer } from '../popover.js';
|
|
18
18
|
import './icon.js';
|
|
19
19
|
import './tag.js';
|
|
20
20
|
import './spinner.js';
|
|
@@ -31,6 +31,8 @@ const strings = defineStrings('combobox', {
|
|
|
31
31
|
required: 'Please select an item in the list.',
|
|
32
32
|
/** Validation: `required`, `multiple` and nothing chosen. */
|
|
33
33
|
requiredMultiple: 'Choose at least one item.',
|
|
34
|
+
/** The chosen option in the box or its tag with `show-group`: "Lead: Company". */
|
|
35
|
+
grouped: '{group}: {label}',
|
|
34
36
|
});
|
|
35
37
|
/**
|
|
36
38
|
* Editable combobox: a text box that suggests matching options as the merchant types (the WAI-ARIA APG
|
|
@@ -38,9 +40,11 @@ const strings = defineStrings('combobox', {
|
|
|
38
40
|
* tags). With `filter="none"` it doesn't filter by itself: listen for `cm-search`, fetch matches, set `options`
|
|
39
41
|
* and toggle `loading`. With `allow-custom` the merchant can add a value that isn't in the list (a new product tag).
|
|
40
42
|
*
|
|
41
|
-
* Options come from `<option>` children or the `options` property, as for `cm-select
|
|
43
|
+
* Options come from `<option>` children or the `options` property, as for `cm-select`, and can be grouped under
|
|
44
|
+
* headings with `<optgroup label="…">` or a `group` on each option (since 1.3). Filtering keeps the heading of
|
|
45
|
+
* every group that still has a match.
|
|
42
46
|
* @element cm-combobox
|
|
43
|
-
* @slot - `<option value="…">Label</option>` elements
|
|
47
|
+
* @slot - `<option value="…">Label</option>` elements, optionally inside `<optgroup label="…">`. `disabled` (also on an optgroup) and `selected` are honoured.
|
|
44
48
|
* @fires change - When the value changes (an option picked or removed, or a custom value added).
|
|
45
49
|
* @fires input - Same moment as `change`.
|
|
46
50
|
* @fires cm-search - As the merchant types, with `detail: { query }`. Use it to load matches from the server.
|
|
@@ -67,7 +71,7 @@ export class CmCombobox extends CmLocalizedElement {
|
|
|
67
71
|
this.values = [];
|
|
68
72
|
/** Shown in the empty text box, e.g. "Search tags". */
|
|
69
73
|
this.placeholder = '';
|
|
70
|
-
/** Options as strings or `{ value, label, disabled }`. Used instead of `<option>` children when set. */
|
|
74
|
+
/** Options as strings or `{ value, label, disabled, group }`. Used instead of `<option>` children when set. */
|
|
71
75
|
this.options = [];
|
|
72
76
|
/** Lets the merchant choose several options, shown as removable tags. Backspace in the empty box removes the last one. */
|
|
73
77
|
this.multiple = false;
|
|
@@ -83,6 +87,11 @@ export class CmCombobox extends CmLocalizedElement {
|
|
|
83
87
|
this.disabled = false;
|
|
84
88
|
/** Whether the list is showing. */
|
|
85
89
|
this.open = false;
|
|
90
|
+
/**
|
|
91
|
+
* Shows the chosen option's group before its label in the box, or in its tag with `multiple` ("Lead: Company"),
|
|
92
|
+
* for grouped lists where labels repeat across groups. The list itself shows the bare label under its heading. Since 1.3.
|
|
93
|
+
*/
|
|
94
|
+
this.showGroup = false;
|
|
86
95
|
this._query = '';
|
|
87
96
|
this._active = -1;
|
|
88
97
|
this._slotted = [];
|
|
@@ -93,6 +102,8 @@ export class CmCombobox extends CmLocalizedElement {
|
|
|
93
102
|
_CmCombobox_default.set(this, null);
|
|
94
103
|
/** Labels for chosen values, so tags keep their text when async options change. */
|
|
95
104
|
_CmCombobox_labels.set(this, new Map());
|
|
105
|
+
/** Groups of chosen values, for the same reason (`show-group`). */
|
|
106
|
+
_CmCombobox_groups.set(this, new Map());
|
|
96
107
|
_CmCombobox_observer.set(this, new MutationObserver(() => __classPrivateFieldGet(this, _CmCombobox_instances, "m", _CmCombobox_readChildren).call(this)));
|
|
97
108
|
_CmCombobox_stopOutside.set(this, void 0);
|
|
98
109
|
_CmCombobox_onInput.set(this, (e) => {
|
|
@@ -174,7 +185,7 @@ export class CmCombobox extends CmLocalizedElement {
|
|
|
174
185
|
if (!this._typing)
|
|
175
186
|
return;
|
|
176
187
|
this._typing = false;
|
|
177
|
-
this._query = this.multiple ? '' : this.value ? __classPrivateFieldGet(this, _CmCombobox_instances, "m",
|
|
188
|
+
this._query = this.multiple ? '' : this.value ? __classPrivateFieldGet(this, _CmCombobox_instances, "m", _CmCombobox_displayOf).call(this, this.value) : '';
|
|
178
189
|
});
|
|
179
190
|
});
|
|
180
191
|
}
|
|
@@ -212,11 +223,11 @@ export class CmCombobox extends CmLocalizedElement {
|
|
|
212
223
|
this.value = state;
|
|
213
224
|
}
|
|
214
225
|
}
|
|
215
|
-
/** All options in use, normalised (before filtering). */
|
|
226
|
+
/** All options in use, normalised (before filtering), in the order shown (options of a group together). */
|
|
216
227
|
get items() {
|
|
217
228
|
if (!this.options?.length)
|
|
218
|
-
return this._slotted;
|
|
219
|
-
return this.options.map((o) => (typeof o === 'string' ? { value: o, label: o } : o));
|
|
229
|
+
return sortByGroup(this._slotted);
|
|
230
|
+
return sortByGroup(this.options.map((o) => (typeof o === 'string' ? { value: o, label: o } : o)));
|
|
220
231
|
}
|
|
221
232
|
/** The options shown for the current query. */
|
|
222
233
|
get filteredItems() {
|
|
@@ -232,10 +243,13 @@ export class CmCombobox extends CmLocalizedElement {
|
|
|
232
243
|
__classPrivateFieldSet(this, _CmCombobox_default, { value: this.value, values: [...this.values] }, "f");
|
|
233
244
|
}
|
|
234
245
|
willUpdate(changed) {
|
|
235
|
-
for (const o of this.items)
|
|
246
|
+
for (const o of this.items) {
|
|
236
247
|
__classPrivateFieldGet(this, _CmCombobox_labels, "f").set(o.value, o.label);
|
|
237
|
-
|
|
238
|
-
|
|
248
|
+
if (o.group)
|
|
249
|
+
__classPrivateFieldGet(this, _CmCombobox_groups, "f").set(o.value, o.group);
|
|
250
|
+
}
|
|
251
|
+
if ((changed.has('value') || changed.has('options') || changed.has('_slotted') || changed.has('showGroup')) && !this.multiple && !this._typing) {
|
|
252
|
+
this._query = this.value ? __classPrivateFieldGet(this, _CmCombobox_instances, "m", _CmCombobox_displayOf).call(this, this.value) : '';
|
|
239
253
|
}
|
|
240
254
|
if (changed.has('_query') || changed.has('options') || changed.has('_slotted') || changed.has('loading')) {
|
|
241
255
|
const rows = __classPrivateFieldGet(this, _CmCombobox_instances, "m", _CmCombobox_rows).call(this);
|
|
@@ -280,6 +294,12 @@ export class CmCombobox extends CmLocalizedElement {
|
|
|
280
294
|
const described = [this.error && `${__classPrivateFieldGet(this, _CmCombobox_id, "f")}-err`, this.helpText && `${__classPrivateFieldGet(this, _CmCombobox_id, "f")}-help`].filter(Boolean).join(' ');
|
|
281
295
|
const hostLabel = this.getAttribute('aria-label');
|
|
282
296
|
const selected = (v) => (this.multiple ? this.values.includes(v) : v === this.value);
|
|
297
|
+
const option = (o, i) => html `<li role="option" id="${__classPrivateFieldGet(this, _CmCombobox_id, "f")}-o${i}" class=${i === this._active ? 'active' : ''}
|
|
298
|
+
aria-selected=${o.create ? 'false' : selected(o.value) ? 'true' : 'false'} aria-disabled=${o.disabled ? 'true' : nothing}
|
|
299
|
+
@pointerenter=${() => !o.disabled && (this._active = i)} @click=${() => __classPrivateFieldGet(this, _CmCombobox_instances, "m", _CmCombobox_choose).call(this, i)}>
|
|
300
|
+
${o.create
|
|
301
|
+
? html `<cm-icon name="plus" size="16"></cm-icon><span class="create">${this.t('add', { value: o.label })}</span>`
|
|
302
|
+
: html `${this.multiple ? html `<cm-icon class="check" name="check" size="16"></cm-icon>` : nothing}<span>${__classPrivateFieldGet(this, _CmCombobox_instances, "m", _CmCombobox_highlight).call(this, o.label)}</span>`}</li>`;
|
|
283
303
|
const status = this.loading ? this.loadingText || this.t('loading')
|
|
284
304
|
: !this.open ? ''
|
|
285
305
|
: rows.length ? this.t('results', { count: rows.length })
|
|
@@ -291,8 +311,8 @@ export class CmCombobox extends CmLocalizedElement {
|
|
|
291
311
|
<div class="wrap">
|
|
292
312
|
<div class="box" part="box" @click=${(e) => { if (!e.target.closest('cm-tag'))
|
|
293
313
|
__classPrivateFieldGet(this, _CmCombobox_instances, "m", _CmCombobox_focusInput).call(this); }}>
|
|
294
|
-
${this.multiple ? this.values.map((v) => html `<cm-tag removable ?disabled=${this.disabled} remove-label=${this.t('remove', { label: __classPrivateFieldGet(this, _CmCombobox_instances, "m",
|
|
295
|
-
@cm-remove=${() => { this.removeValue(v); __classPrivateFieldGet(this, _CmCombobox_instances, "m", _CmCombobox_focusInput).call(this); }}>${__classPrivateFieldGet(this, _CmCombobox_instances, "m",
|
|
314
|
+
${this.multiple ? this.values.map((v) => html `<cm-tag removable ?disabled=${this.disabled} remove-label=${this.t('remove', { label: __classPrivateFieldGet(this, _CmCombobox_instances, "m", _CmCombobox_displayOf).call(this, v) })}
|
|
315
|
+
@cm-remove=${() => { this.removeValue(v); __classPrivateFieldGet(this, _CmCombobox_instances, "m", _CmCombobox_focusInput).call(this); }}>${__classPrivateFieldGet(this, _CmCombobox_instances, "m", _CmCombobox_displayOf).call(this, v)}</cm-tag>`) : nothing}
|
|
296
316
|
<input id="${__classPrivateFieldGet(this, _CmCombobox_id, "f")}-in" part="input" type="text" role="combobox" autocomplete="off" spellcheck="false"
|
|
297
317
|
.value=${live(this._query)} placeholder=${this.multiple && this.values.length ? nothing : this.placeholder || nothing}
|
|
298
318
|
aria-label=${!this.label && hostLabel ? hostLabel : nothing}
|
|
@@ -314,12 +334,10 @@ export class CmCombobox extends CmLocalizedElement {
|
|
|
314
334
|
<ul role="listbox" id="${__classPrivateFieldGet(this, _CmCombobox_id, "f")}-list" part="listbox" ?hidden=${!rows.length}
|
|
315
335
|
aria-labelledby=${this.label ? `${__classPrivateFieldGet(this, _CmCombobox_id, "f")}-l` : nothing} aria-label=${!this.label && hostLabel ? hostLabel : nothing}
|
|
316
336
|
aria-multiselectable=${this.multiple ? 'true' : nothing}>
|
|
317
|
-
${rows.map((
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
? html `<cm-icon name="plus" size="16"></cm-icon><span class="create">${this.t('add', { value: o.label })}</span>`
|
|
322
|
-
: html `${this.multiple ? html `<cm-icon class="check" name="check" size="16"></cm-icon>` : nothing}<span>${__classPrivateFieldGet(this, _CmCombobox_instances, "m", _CmCombobox_highlight).call(this, o.label)}</span>`}</li>`)}
|
|
337
|
+
${groupRuns(rows).map((run, gi) => run.group
|
|
338
|
+
? html `<li role="group" aria-labelledby="${__classPrivateFieldGet(this, _CmCombobox_id, "f")}-g${gi}"><div role="presentation" class="gh" id="${__classPrivateFieldGet(this, _CmCombobox_id, "f")}-g${gi}">${run.group}</div>
|
|
339
|
+
<ul role="none" class="gopts">${run.rows.map(({ row, index }) => option(row, index))}</ul></li>`
|
|
340
|
+
: run.rows.map(({ row, index }) => option(row, index)))}
|
|
323
341
|
</ul>
|
|
324
342
|
${this.loading
|
|
325
343
|
? html `<p class="message"><cm-spinner size="small" aria-hidden="true"></cm-spinner><span aria-hidden="true">${this.loadingText || this.t('loading')}</span></p>`
|
|
@@ -333,9 +351,15 @@ export class CmCombobox extends CmLocalizedElement {
|
|
|
333
351
|
</div>`;
|
|
334
352
|
}
|
|
335
353
|
}
|
|
336
|
-
_CmCombobox_internals = new WeakMap(), _CmCombobox_id = new WeakMap(), _CmCombobox_default = new WeakMap(), _CmCombobox_labels = new WeakMap(), _CmCombobox_observer = new WeakMap(), _CmCombobox_stopOutside = new WeakMap(), _CmCombobox_onInput = new WeakMap(), _CmCombobox_onKeyDown = new WeakMap(), _CmCombobox_onBlur = new WeakMap(), _CmCombobox_instances = new WeakSet(), _CmCombobox_readChildren = function _CmCombobox_readChildren() {
|
|
354
|
+
_CmCombobox_internals = new WeakMap(), _CmCombobox_id = new WeakMap(), _CmCombobox_default = new WeakMap(), _CmCombobox_labels = new WeakMap(), _CmCombobox_groups = new WeakMap(), _CmCombobox_observer = new WeakMap(), _CmCombobox_stopOutside = new WeakMap(), _CmCombobox_onInput = new WeakMap(), _CmCombobox_onKeyDown = new WeakMap(), _CmCombobox_onBlur = new WeakMap(), _CmCombobox_instances = new WeakSet(), _CmCombobox_readChildren = function _CmCombobox_readChildren() {
|
|
337
355
|
const opts = [...this.querySelectorAll('option')];
|
|
338
|
-
this._slotted = opts.map((o) =>
|
|
356
|
+
this._slotted = opts.map((o) => {
|
|
357
|
+
const g = o.parentElement?.localName === 'optgroup' ? o.parentElement : null;
|
|
358
|
+
const opt = { value: o.value, label: o.label || o.textContent.trim(), disabled: o.disabled || !!g?.disabled };
|
|
359
|
+
if (g?.label)
|
|
360
|
+
opt.group = g.label;
|
|
361
|
+
return opt;
|
|
362
|
+
});
|
|
339
363
|
if (!this.hasUpdated) {
|
|
340
364
|
const sel = opts.filter((o) => o.hasAttribute('selected')).map((o) => o.value);
|
|
341
365
|
if (this.multiple && !this.values.length && sel.length)
|
|
@@ -345,6 +369,12 @@ _CmCombobox_internals = new WeakMap(), _CmCombobox_id = new WeakMap(), _CmCombob
|
|
|
345
369
|
}
|
|
346
370
|
}, _CmCombobox_labelOf = function _CmCombobox_labelOf(v) {
|
|
347
371
|
return this.items.find((o) => o.value === v)?.label ?? __classPrivateFieldGet(this, _CmCombobox_labels, "f").get(v) ?? v;
|
|
372
|
+
}, _CmCombobox_displayOf = function _CmCombobox_displayOf(v) {
|
|
373
|
+
const label = __classPrivateFieldGet(this, _CmCombobox_instances, "m", _CmCombobox_labelOf).call(this, v);
|
|
374
|
+
if (!this.showGroup)
|
|
375
|
+
return label;
|
|
376
|
+
const group = this.items.find((o) => o.value === v)?.group ?? __classPrivateFieldGet(this, _CmCombobox_groups, "f").get(v);
|
|
377
|
+
return group ? this.t('grouped', { group, label }) : label;
|
|
348
378
|
}, _CmCombobox_createText_get = function _CmCombobox_createText_get() {
|
|
349
379
|
if (!this.allowCustom || !this._typing)
|
|
350
380
|
return '';
|
|
@@ -402,7 +432,7 @@ _CmCombobox_internals = new WeakMap(), _CmCombobox_id = new WeakMap(), _CmCombob
|
|
|
402
432
|
}
|
|
403
433
|
__classPrivateFieldGet(this, _CmCombobox_instances, "m", _CmCombobox_hide).call(this);
|
|
404
434
|
this._typing = false;
|
|
405
|
-
this._query = row.label;
|
|
435
|
+
this._query = this.showGroup && row.group ? this.t('grouped', { group: row.group, label: row.label }) : row.label;
|
|
406
436
|
if (row.value !== this.value) {
|
|
407
437
|
this.value = row.value;
|
|
408
438
|
__classPrivateFieldGet(this, _CmCombobox_instances, "m", _CmCombobox_emit).call(this);
|
|
@@ -454,6 +484,7 @@ CmCombobox.properties = {
|
|
|
454
484
|
required: { type: Boolean, reflect: true },
|
|
455
485
|
disabled: { type: Boolean, reflect: true },
|
|
456
486
|
open: { type: Boolean, reflect: true },
|
|
487
|
+
showGroup: { type: Boolean, attribute: 'show-group' },
|
|
457
488
|
_query: { state: true },
|
|
458
489
|
_active: { state: true },
|
|
459
490
|
_slotted: { state: true },
|
|
@@ -462,6 +493,7 @@ CmCombobox.properties = {
|
|
|
462
493
|
CmCombobox.styles = [
|
|
463
494
|
base,
|
|
464
495
|
popoverStyles,
|
|
496
|
+
groupStyles,
|
|
465
497
|
css `
|
|
466
498
|
:host { display: block; position: relative; }
|
|
467
499
|
.field { display: flex; flex-direction: column; gap: var(--cm-space-10); }
|