@rcarls/rc-select 0.1.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/README.md +70 -0
- package/dist/custom-elements.json +856 -0
- package/dist/demo.css +85 -0
- package/dist/demo.js +40 -0
- package/dist/rc-select-BumY2uQl.js +600 -0
- package/dist/rc-select-BumY2uQl.js.map +1 -0
- package/dist/rc-select-define.js +7 -0
- package/dist/rc-select-define.js.map +1 -0
- package/dist/rc-select.js +5 -0
- package/dist/rc-select.js.map +1 -0
- package/dist/types/packages/rc-select/src/define.d.ts +1 -0
- package/dist/types/packages/rc-select/src/index.d.ts +1 -0
- package/dist/types/packages/rc-select/src/rc-select.d.ts +130 -0
- package/dist/types/packages/rc-select/src/rc-select.styles.d.ts +2 -0
- package/dist/types/packages/rc-select/src/rc-select.test.d.ts +0 -0
- package/package.json +69 -0
|
@@ -0,0 +1,600 @@
|
|
|
1
|
+
import { css as v, LitElement as f, nothing as _, html as c } from "lit";
|
|
2
|
+
import { property as n, query as h, state as p } from "lit/decorators.js";
|
|
3
|
+
import { ActiveDescendantController as b, AnchorController as m } from "@rcarls/rc-common";
|
|
4
|
+
const g = v`
|
|
5
|
+
:host {
|
|
6
|
+
display: inline-block;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
#anchor {
|
|
10
|
+
display: flex;
|
|
11
|
+
align-items: center;
|
|
12
|
+
flex-wrap: wrap;
|
|
13
|
+
gap: var(--rc-select-gap, var(--rc-control-gap, 0.25em));
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
#trigger {
|
|
17
|
+
display: flex;
|
|
18
|
+
align-items: center;
|
|
19
|
+
flex-wrap: wrap;
|
|
20
|
+
gap: var(--rc-select-gap, var(--rc-control-gap, 0.25em));
|
|
21
|
+
min-block-size: var(--rc-select-control-block-size, var(--rc-control-block-size, auto));
|
|
22
|
+
padding: var(--rc-select-padding-block, var(--rc-control-padding-block, 0.25em))
|
|
23
|
+
var(--rc-select-padding-inline, var(--rc-control-padding-inline, 0.5em));
|
|
24
|
+
min-width: 8em;
|
|
25
|
+
cursor: default;
|
|
26
|
+
user-select: none;
|
|
27
|
+
border: var(--rc-select-border, var(--rc-border, 1px solid var(--rc-border-color, ButtonBorder)));
|
|
28
|
+
border-radius: var(--rc-select-radius, var(--rc-control-radius, var(--rc-radius-sm, 0.125em)));
|
|
29
|
+
background: var(--rc-field, Field);
|
|
30
|
+
color: var(--rc-field-text, FieldText);
|
|
31
|
+
font-family: var(--rc-font-family, inherit);
|
|
32
|
+
font-size: var(--rc-font-size, inherit);
|
|
33
|
+
line-height: var(--rc-line-height, normal);
|
|
34
|
+
transition:
|
|
35
|
+
background-color var(--rc-motion-duration, 120ms),
|
|
36
|
+
border-color var(--rc-motion-duration, 120ms),
|
|
37
|
+
box-shadow var(--rc-motion-duration, 120ms);
|
|
38
|
+
|
|
39
|
+
/* Keyboard focus indicator */
|
|
40
|
+
outline: none;
|
|
41
|
+
&:focus-visible {
|
|
42
|
+
outline: var(--rc-focus-ring, auto);
|
|
43
|
+
outline-offset: var(--rc-focus-ring-offset, 0);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
[part='value-display'] {
|
|
48
|
+
flex: 1;
|
|
49
|
+
overflow: hidden;
|
|
50
|
+
text-overflow: ellipsis;
|
|
51
|
+
white-space: nowrap;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
[part='toggle-icon'] {
|
|
55
|
+
flex-shrink: 0;
|
|
56
|
+
font-size: 0.75em;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/* Chips — the whole chip is the remove button for a larger touch target */
|
|
60
|
+
[part='chip'] {
|
|
61
|
+
display: inline-flex;
|
|
62
|
+
align-items: center;
|
|
63
|
+
gap: var(--rc-select-chip-gap, calc(var(--rc-control-gap, 0.25em) * 0.8));
|
|
64
|
+
padding: var(--rc-select-chip-padding-block, 0.1em)
|
|
65
|
+
var(--rc-select-chip-padding-inline, 0.3em);
|
|
66
|
+
border: var(--rc-select-chip-border, var(--rc-border, 1px solid var(--rc-border-color, ButtonBorder)));
|
|
67
|
+
border-radius: var(--rc-select-chip-radius, var(--rc-radius-md, 0.25em));
|
|
68
|
+
background: var(--rc-button-bg, ButtonFace);
|
|
69
|
+
color: var(--rc-button-text, ButtonText);
|
|
70
|
+
font: inherit;
|
|
71
|
+
font-size: 0.875em;
|
|
72
|
+
cursor: pointer;
|
|
73
|
+
|
|
74
|
+
&:hover {
|
|
75
|
+
background: var(--rc-highlight, Highlight);
|
|
76
|
+
color: var(--rc-highlight-text, HighlightText);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
&:focus-visible {
|
|
80
|
+
outline: var(--rc-focus-ring, auto);
|
|
81
|
+
outline-offset: var(--rc-focus-ring-offset, 0);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/* Decorative × icon inside the chip — no interaction, aria-hidden */
|
|
86
|
+
[part='chip-remove'] {
|
|
87
|
+
display: inline-flex;
|
|
88
|
+
align-items: center;
|
|
89
|
+
justify-content: center;
|
|
90
|
+
width: 1em;
|
|
91
|
+
font-size: 0.85em;
|
|
92
|
+
pointer-events: none;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/* Hidden native select slot */
|
|
96
|
+
slot[name='select'] {
|
|
97
|
+
display: none;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/* Listbox popup — positioned by AnchorController via adoptedStyleSheets */
|
|
101
|
+
rc-listbox {
|
|
102
|
+
max-height: var(--rc-select-max-height, 20em);
|
|
103
|
+
overflow-y: auto;
|
|
104
|
+
background: var(--rc-surface, Canvas);
|
|
105
|
+
border: var(--rc-select-listbox-border, var(--rc-border, 1px solid var(--rc-border-color, ButtonBorder)));
|
|
106
|
+
border-radius: var(--rc-select-listbox-radius, var(--rc-control-radius, 0));
|
|
107
|
+
box-shadow: var(--rc-select-shadow, var(--rc-shadow, 0 2px 8px color-mix(in srgb, CanvasText 15%, transparent)));
|
|
108
|
+
color: var(--rc-field-text, FieldText);
|
|
109
|
+
padding-block: var(--rc-select-listbox-padding-block, var(--rc-control-padding-block, 0.25em));
|
|
110
|
+
|
|
111
|
+
&:not(:popover-open) {
|
|
112
|
+
display: none;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/* Options within the listbox */
|
|
117
|
+
rc-listbox [part~='option'] {
|
|
118
|
+
display: flex;
|
|
119
|
+
align-items: center;
|
|
120
|
+
gap: var(--rc-item-gap, 0.4em);
|
|
121
|
+
padding: var(--rc-item-padding-block, 0.3em) var(--rc-item-padding-inline, 0.75em);
|
|
122
|
+
cursor: default;
|
|
123
|
+
|
|
124
|
+
/* display: flex overrides [hidden]'s browser-default display:none — restore it explicitly */
|
|
125
|
+
&[hidden] { display: none; }
|
|
126
|
+
|
|
127
|
+
&:not([hidden]):not([aria-disabled='true']):hover {
|
|
128
|
+
background: var(--rc-highlight, Highlight);
|
|
129
|
+
color: var(--rc-highlight-text, HighlightText);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
&[data-active]:not([aria-disabled='true']) {
|
|
133
|
+
background: var(--rc-highlight, Highlight);
|
|
134
|
+
color: var(--rc-highlight-text, HighlightText);
|
|
135
|
+
outline: var(--rc-focus-ring, 2px solid var(--rc-accent, Highlight));
|
|
136
|
+
outline-offset: -2px;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
&[aria-disabled='true'] {
|
|
140
|
+
opacity: var(--rc-disabled-opacity, 0.5);
|
|
141
|
+
cursor: not-allowed;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/* Selection checkmark — reserves space whether visible or not */
|
|
146
|
+
rc-listbox [part~='option-checkmark'] {
|
|
147
|
+
flex-shrink: 0;
|
|
148
|
+
width: 1em;
|
|
149
|
+
text-align: center;
|
|
150
|
+
font-size: 0.85em;
|
|
151
|
+
visibility: hidden;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
rc-listbox [part~='option'][aria-selected='true'] [part~='option-checkmark'] {
|
|
155
|
+
visibility: visible;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
rc-listbox [part~='create-option'] {
|
|
159
|
+
font-style: italic;
|
|
160
|
+
border-top: 1px solid var(--rc-border-color, ButtonBorder);
|
|
161
|
+
margin-top: 0.25em;
|
|
162
|
+
padding-top: 0.3em;
|
|
163
|
+
}
|
|
164
|
+
`;
|
|
165
|
+
var y = Object.defineProperty, a = (u, e, t, i) => {
|
|
166
|
+
for (var s = void 0, r = u.length - 1, l; r >= 0; r--)
|
|
167
|
+
(l = u[r]) && (s = l(e, t, s) || s);
|
|
168
|
+
return s && y(e, t, s), s;
|
|
169
|
+
};
|
|
170
|
+
const d = class d extends f {
|
|
171
|
+
constructor() {
|
|
172
|
+
super(...arguments), this.open = !1, this.multiple = !1, this.disabled = !1, this.placeholder = "", this.display = "auto", this._selectedValues = /* @__PURE__ */ new Set(), this._chipNavIndex = -1, this._options = [], this._selectRef = null, this._mutationObserver = null, this._selectionInitialized = !1, this._typeAheadBuffer = "", this._typeAheadTimer = 0, this._adc = new b(this, {
|
|
173
|
+
host: () => this._$trigger ?? null,
|
|
174
|
+
items: () => this._$listbox?.navigableItems ?? []
|
|
175
|
+
}), this._anchorCtrl = new m(this, {
|
|
176
|
+
anchor: () => this._$anchor ?? null,
|
|
177
|
+
floating: () => this._$listbox ?? null,
|
|
178
|
+
shadowHost: () => this,
|
|
179
|
+
placement: "bottom-start",
|
|
180
|
+
offset: 2
|
|
181
|
+
}), this._onDocClick = (e) => {
|
|
182
|
+
this.open && (e.composedPath().includes(this) || this.closePopup(!1));
|
|
183
|
+
}, this._onDocKeyDown = (e) => {
|
|
184
|
+
this.open && e.key === "Escape" && (e.preventDefault(), this.closePopup());
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
connectedCallback() {
|
|
188
|
+
super.connectedCallback(), document.addEventListener("click", this._onDocClick, { capture: !0 }), document.addEventListener("keydown", this._onDocKeyDown, { capture: !0 });
|
|
189
|
+
}
|
|
190
|
+
disconnectedCallback() {
|
|
191
|
+
super.disconnectedCallback(), document.removeEventListener("click", this._onDocClick, { capture: !0 }), document.removeEventListener("keydown", this._onDocKeyDown, {
|
|
192
|
+
capture: !0
|
|
193
|
+
}), this._mutationObserver?.disconnect();
|
|
194
|
+
}
|
|
195
|
+
// ── Popup ────────────────────────────────────────────────────────────────────
|
|
196
|
+
openPopup() {
|
|
197
|
+
this.open || this.disabled || (this.open = !0, this._$listbox.showPopover(), this._anchorCtrl.update(), this.dispatchEvent(
|
|
198
|
+
new CustomEvent("rc-select-open", { bubbles: !0, composed: !0 })
|
|
199
|
+
), this.requestUpdate());
|
|
200
|
+
}
|
|
201
|
+
closePopup(e = !0) {
|
|
202
|
+
this.open && (this.open = !1, this._$listbox.hidePopover(), this._adc.clear(), e && this._$trigger?.focus(), this.dispatchEvent(
|
|
203
|
+
new CustomEvent("rc-select-close", { bubbles: !0, composed: !0 })
|
|
204
|
+
));
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Current selection. Host writes update selection silently; user interaction
|
|
208
|
+
* emits `rc-select-change`.
|
|
209
|
+
*/
|
|
210
|
+
get value() {
|
|
211
|
+
return this.multiple ? this.selectedValues : this.selectedValues[0] ?? "";
|
|
212
|
+
}
|
|
213
|
+
set value(e) {
|
|
214
|
+
const t = this._value;
|
|
215
|
+
if (this._value = e, e === void 0) {
|
|
216
|
+
this.requestUpdate("value", t);
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
219
|
+
this._selectionInitialized = !0, this._applySelection(this._normalizeValue(e)), this.requestUpdate("value", t);
|
|
220
|
+
}
|
|
221
|
+
/** Initial uncontrolled selection, applied before user or native state owns the value. */
|
|
222
|
+
get defaultValue() {
|
|
223
|
+
return this._defaultValue;
|
|
224
|
+
}
|
|
225
|
+
set defaultValue(e) {
|
|
226
|
+
const t = this._defaultValue;
|
|
227
|
+
this._defaultValue = e, !this._selectionInitialized && this._value === void 0 && e !== void 0 && this._applySelection(this._normalizeValue(e)), this.requestUpdate("defaultValue", t);
|
|
228
|
+
}
|
|
229
|
+
/** Programmatic option source. Omit to derive options from the slotted `<select>`. */
|
|
230
|
+
get options() {
|
|
231
|
+
return this._propertyOptions ? [...this._propertyOptions] : void 0;
|
|
232
|
+
}
|
|
233
|
+
set options(e) {
|
|
234
|
+
const t = this._propertyOptions;
|
|
235
|
+
this._propertyOptions = e ? [...e] : void 0, this._syncOptions(this._currentOptions()), this._mirrorOptionsToNativeSelect(), this.requestUpdate("options", t);
|
|
236
|
+
}
|
|
237
|
+
/** Selected values as a consistently-array-shaped convenience getter. */
|
|
238
|
+
get selectedValues() {
|
|
239
|
+
return [...this._selectedValues];
|
|
240
|
+
}
|
|
241
|
+
get selectedOptions() {
|
|
242
|
+
return this._selectedOptionsFor(this.selectedValues);
|
|
243
|
+
}
|
|
244
|
+
_applySelection(e) {
|
|
245
|
+
const t = this.multiple ? e : e.slice(0, 1);
|
|
246
|
+
this._selectedValues = new Set(t), this._$listbox?.setSelectedValues(t), this._syncNativeSelect(), this.requestUpdate();
|
|
247
|
+
}
|
|
248
|
+
// ── Slot: native <select> ────────────────────────────────────────────────────
|
|
249
|
+
_handleSelectSlotChange(e) {
|
|
250
|
+
const i = e.target.assignedElements().find(
|
|
251
|
+
(s) => s instanceof HTMLSelectElement
|
|
252
|
+
) ?? null;
|
|
253
|
+
this._mutationObserver?.disconnect(), this._mutationObserver = null, this._selectRef = i ? new WeakRef(i) : null, i && queueMicrotask(() => {
|
|
254
|
+
i.isConnected && (this.multiple = i.multiple, this.disabled = i.disabled, this._syncOptionsFromSelect(i), this._mutationObserver = new MutationObserver(() => {
|
|
255
|
+
const s = this._selectRef?.deref();
|
|
256
|
+
if (s) {
|
|
257
|
+
if (this.multiple = s.multiple, this.disabled = s.disabled, this._propertyOptions !== void 0) {
|
|
258
|
+
this._applySelectionFromCurrentSource();
|
|
259
|
+
return;
|
|
260
|
+
}
|
|
261
|
+
this._syncOptionsFromSelect(s);
|
|
262
|
+
}
|
|
263
|
+
}), this._mutationObserver.observe(i, {
|
|
264
|
+
childList: !0,
|
|
265
|
+
subtree: !0,
|
|
266
|
+
attributes: !0
|
|
267
|
+
}), this._syncAccessibleName(i));
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
_syncOptionsFromSelect(e) {
|
|
271
|
+
if (this._propertyOptions !== void 0) {
|
|
272
|
+
this._syncOptions(this._propertyOptions), this._mirrorOptionsToNativeSelect(), this._applySelectionFromCurrentSource();
|
|
273
|
+
return;
|
|
274
|
+
}
|
|
275
|
+
const t = this._optionsFromSelect(e);
|
|
276
|
+
this._syncOptions(t), this._applySelectionFromCurrentSource(), this._syncAccessibleName(e);
|
|
277
|
+
}
|
|
278
|
+
_applySelectionFromCurrentSource() {
|
|
279
|
+
if (this._value !== void 0) {
|
|
280
|
+
this._applySelection(this._normalizeValue(this._value));
|
|
281
|
+
return;
|
|
282
|
+
}
|
|
283
|
+
if (!this._selectionInitialized && this._defaultValue !== void 0) {
|
|
284
|
+
this._selectionInitialized = !0, this._applySelection(this._normalizeValue(this._defaultValue));
|
|
285
|
+
return;
|
|
286
|
+
}
|
|
287
|
+
const e = this._selectionInitialized ? this._selectedValuesFromNativeSelect() : this._defaultSelectedValuesFromNativeSelect();
|
|
288
|
+
this._selectionInitialized = !0, this._applySelection(e);
|
|
289
|
+
}
|
|
290
|
+
_syncOptions(e) {
|
|
291
|
+
this._options = [...e], this._$listbox && (this._$listbox.options = this._options);
|
|
292
|
+
}
|
|
293
|
+
_currentOptions() {
|
|
294
|
+
if (this._propertyOptions !== void 0) return this._propertyOptions;
|
|
295
|
+
const e = this._selectRef?.deref();
|
|
296
|
+
return e ? this._optionsFromSelect(e) : [];
|
|
297
|
+
}
|
|
298
|
+
_optionsFromSelect(e) {
|
|
299
|
+
const t = [];
|
|
300
|
+
for (const i of e.options)
|
|
301
|
+
i.value && t.push({
|
|
302
|
+
value: i.value,
|
|
303
|
+
label: i.text,
|
|
304
|
+
disabled: i.disabled
|
|
305
|
+
});
|
|
306
|
+
return t;
|
|
307
|
+
}
|
|
308
|
+
_selectedValuesFromNativeSelect() {
|
|
309
|
+
const e = this._selectRef?.deref();
|
|
310
|
+
return e ? Array.from(e.selectedOptions).map((t) => t.value).filter(Boolean) : this.selectedValues;
|
|
311
|
+
}
|
|
312
|
+
_defaultSelectedValuesFromNativeSelect() {
|
|
313
|
+
const e = this._selectRef?.deref();
|
|
314
|
+
return e ? Array.from(e.options).filter((t) => t.defaultSelected).map((t) => t.value).filter(Boolean) : this.selectedValues;
|
|
315
|
+
}
|
|
316
|
+
_mirrorOptionsToNativeSelect() {
|
|
317
|
+
const e = this._selectRef?.deref();
|
|
318
|
+
if (!(!e || this._propertyOptions === void 0)) {
|
|
319
|
+
this._mutationObserver?.disconnect(), e.replaceChildren();
|
|
320
|
+
for (const t of this._propertyOptions) {
|
|
321
|
+
const i = document.createElement("option");
|
|
322
|
+
i.value = t.value, i.text = t.label, i.disabled = t.disabled ?? !1, i.selected = this._selectedValues.has(t.value), e.add(i);
|
|
323
|
+
}
|
|
324
|
+
this._mutationObserver?.observe(e, {
|
|
325
|
+
childList: !0,
|
|
326
|
+
subtree: !0,
|
|
327
|
+
attributes: !0
|
|
328
|
+
});
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
_addOption(e) {
|
|
332
|
+
const t = this._options.some((r) => r.value === e.value) ? this._options.map((r) => r.value === e.value ? e : r) : [...this._options, e];
|
|
333
|
+
this._propertyOptions !== void 0 && (this._propertyOptions = t), this._syncOptions(t);
|
|
334
|
+
const i = this._selectRef?.deref();
|
|
335
|
+
if (!i || Array.from(i.options).some((r) => r.value === e.value)) return;
|
|
336
|
+
const s = document.createElement("option");
|
|
337
|
+
s.value = e.value, s.text = e.label, s.disabled = e.disabled ?? !1, s.selected = this._selectedValues.has(e.value), i.add(s);
|
|
338
|
+
}
|
|
339
|
+
_normalizeValue(e) {
|
|
340
|
+
return Array.isArray(e) ? e : e ? [e] : [];
|
|
341
|
+
}
|
|
342
|
+
_syncAccessibleName(e) {
|
|
343
|
+
if (!this._$trigger || this._$trigger.hasAttribute("aria-label")) return;
|
|
344
|
+
const t = e.getAttribute("aria-label") ?? e.labels?.[0]?.textContent?.trim() ?? null;
|
|
345
|
+
t ? this._$trigger.setAttribute("aria-label", t) : this._$trigger.removeAttribute("aria-label");
|
|
346
|
+
}
|
|
347
|
+
// ── Selection ────────────────────────────────────────────────────────────────
|
|
348
|
+
_handleListboxChange(e) {
|
|
349
|
+
const { optionValue: t, value: i, selected: s } = e.detail, r = t ?? (Array.isArray(i) ? i.at(-1) : i);
|
|
350
|
+
if (r) {
|
|
351
|
+
if (e.stopPropagation(), this.multiple) {
|
|
352
|
+
const l = new Set(this._selectedValues);
|
|
353
|
+
s ? l.add(r) : l.delete(r), this._selectedValues = l;
|
|
354
|
+
} else
|
|
355
|
+
this._selectedValues = s ? /* @__PURE__ */ new Set([r]) : /* @__PURE__ */ new Set(), s && this.closePopup();
|
|
356
|
+
this._selectionInitialized = !0, this._syncNativeSelect(), this._$listbox.setSelectedValues(this.selectedValues), this._dispatchChange();
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
_removeValue(e) {
|
|
360
|
+
const t = new Set(this._selectedValues);
|
|
361
|
+
t.delete(e), this._selectedValues = t, this._$listbox.setSelectedValues([...t]), this._selectionInitialized = !0, this._syncNativeSelect(), this._dispatchChange();
|
|
362
|
+
}
|
|
363
|
+
_syncNativeSelect() {
|
|
364
|
+
const e = this._selectRef?.deref();
|
|
365
|
+
if (e)
|
|
366
|
+
for (const t of e.options)
|
|
367
|
+
t.selected = this._selectedValues.has(t.value);
|
|
368
|
+
}
|
|
369
|
+
_dispatchChange() {
|
|
370
|
+
this.dispatchEvent(
|
|
371
|
+
new CustomEvent("rc-select-change", {
|
|
372
|
+
bubbles: !0,
|
|
373
|
+
composed: !0,
|
|
374
|
+
detail: {
|
|
375
|
+
value: this.value,
|
|
376
|
+
selectedValues: this.selectedValues,
|
|
377
|
+
selectedOptions: this.selectedOptions
|
|
378
|
+
}
|
|
379
|
+
})
|
|
380
|
+
);
|
|
381
|
+
}
|
|
382
|
+
_selectedOptionsFor(e) {
|
|
383
|
+
return e.map((t) => this._options.find((i) => i.value === t) ?? {
|
|
384
|
+
value: t,
|
|
385
|
+
label: t
|
|
386
|
+
});
|
|
387
|
+
}
|
|
388
|
+
// ── Display helpers ──────────────────────────────────────────────────────────
|
|
389
|
+
get _effectiveDisplay() {
|
|
390
|
+
return this.display === "chips" ? "chips" : this.display === "compact" || window.matchMedia("(pointer: coarse)").matches ? "compact" : "chips";
|
|
391
|
+
}
|
|
392
|
+
_labelFor(e) {
|
|
393
|
+
const t = this._selectRef?.deref();
|
|
394
|
+
if (t) {
|
|
395
|
+
for (const i of t.options)
|
|
396
|
+
if (i.value === e) return i.text;
|
|
397
|
+
}
|
|
398
|
+
return e;
|
|
399
|
+
}
|
|
400
|
+
get _displayLabel() {
|
|
401
|
+
if (this._selectedValues.size === 0) return this.placeholder;
|
|
402
|
+
if (this._effectiveDisplay === "chips" && this.multiple)
|
|
403
|
+
return this.placeholder;
|
|
404
|
+
if (this._selectedValues.size === 1)
|
|
405
|
+
return this._labelFor([...this._selectedValues][0]);
|
|
406
|
+
const e = [...this._selectedValues];
|
|
407
|
+
return `${this._labelFor(e[0])}, +${e.length - 1} more`;
|
|
408
|
+
}
|
|
409
|
+
// ── Keyboard: trigger ────────────────────────────────────────────────────────
|
|
410
|
+
_handleTriggerKeyDown(e) {
|
|
411
|
+
switch (e.key) {
|
|
412
|
+
case "ArrowDown":
|
|
413
|
+
e.preventDefault(), this.open ? this._adc.navigate(1) : (this.openPopup(), this._adc.navigateToFirst());
|
|
414
|
+
break;
|
|
415
|
+
case "ArrowUp":
|
|
416
|
+
e.preventDefault(), this.open ? this._adc.navigate(-1) : (this.openPopup(), this._adc.navigateToLast());
|
|
417
|
+
break;
|
|
418
|
+
case "Home":
|
|
419
|
+
this.open && (e.preventDefault(), this._adc.navigateToFirst());
|
|
420
|
+
break;
|
|
421
|
+
case "End":
|
|
422
|
+
this.open && (e.preventDefault(), this._adc.navigateToLast());
|
|
423
|
+
break;
|
|
424
|
+
case " ":
|
|
425
|
+
case "Enter":
|
|
426
|
+
e.preventDefault(), this.open ? this._activateActive() : (this.openPopup(), this._adc.navigateToFirst());
|
|
427
|
+
break;
|
|
428
|
+
case "Tab":
|
|
429
|
+
this.open && this.closePopup(!1);
|
|
430
|
+
break;
|
|
431
|
+
case "ArrowLeft":
|
|
432
|
+
!this.open && this.multiple && this._selectedValues.size > 0 && (e.preventDefault(), this._enterChipNav());
|
|
433
|
+
break;
|
|
434
|
+
default:
|
|
435
|
+
e.key.length === 1 && !e.ctrlKey && !e.metaKey && !e.altKey && this._handleTypeAhead(e.key);
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
_activateActive() {
|
|
439
|
+
const e = this._adc.activeItem;
|
|
440
|
+
e && e.dispatchEvent(
|
|
441
|
+
new PointerEvent("pointerdown", { bubbles: !0, cancelable: !0 })
|
|
442
|
+
);
|
|
443
|
+
}
|
|
444
|
+
// ── Type-ahead ───────────────────────────────────────────────────────────────
|
|
445
|
+
_handleTypeAhead(e) {
|
|
446
|
+
clearTimeout(this._typeAheadTimer), this._typeAheadBuffer += e.toLowerCase(), this._typeAheadTimer = window.setTimeout(() => {
|
|
447
|
+
this._typeAheadBuffer = "";
|
|
448
|
+
}, 500);
|
|
449
|
+
const i = (this._$listbox?.allOptions ?? []).find(
|
|
450
|
+
(s) => !s.disabled && s.label.toLowerCase().startsWith(this._typeAheadBuffer)
|
|
451
|
+
);
|
|
452
|
+
if (i)
|
|
453
|
+
if (!this.open)
|
|
454
|
+
this.multiple || (this._selectionInitialized = !0, this._applySelection([i.value]), this._dispatchChange());
|
|
455
|
+
else {
|
|
456
|
+
const r = this._$listbox.navigableItems.find(
|
|
457
|
+
(l) => l.getAttribute("data-value") === i.value
|
|
458
|
+
);
|
|
459
|
+
r && this._adc.navigateToItem(r);
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
// ── Chip keyboard navigation ─────────────────────────────────────────────────
|
|
463
|
+
_enterChipNav() {
|
|
464
|
+
const e = this._getChipButtons();
|
|
465
|
+
e.length !== 0 && (this._chipNavIndex = e.length - 1, e[this._chipNavIndex].focus());
|
|
466
|
+
}
|
|
467
|
+
_getChipButtons() {
|
|
468
|
+
return Array.from(
|
|
469
|
+
this.renderRoot.querySelectorAll(
|
|
470
|
+
'button[part~="chip"]'
|
|
471
|
+
)
|
|
472
|
+
);
|
|
473
|
+
}
|
|
474
|
+
_handleChipKeyDown(e, t) {
|
|
475
|
+
const i = this._getChipButtons();
|
|
476
|
+
switch (e.key) {
|
|
477
|
+
case "ArrowLeft":
|
|
478
|
+
e.preventDefault(), this._chipNavIndex > 0 && (this._chipNavIndex--, i[this._chipNavIndex]?.focus());
|
|
479
|
+
break;
|
|
480
|
+
case "ArrowRight":
|
|
481
|
+
e.preventDefault(), this._chipNavIndex < i.length - 1 ? (this._chipNavIndex++, i[this._chipNavIndex]?.focus()) : (this._chipNavIndex = -1, this._$trigger?.focus());
|
|
482
|
+
break;
|
|
483
|
+
case "Escape":
|
|
484
|
+
e.preventDefault(), this._chipNavIndex = -1, this._$trigger?.focus();
|
|
485
|
+
break;
|
|
486
|
+
case "Delete":
|
|
487
|
+
case "Backspace":
|
|
488
|
+
case "Enter":
|
|
489
|
+
case " ":
|
|
490
|
+
e.preventDefault(), this._removeValue(t), this._chipNavIndex = -1, this._$trigger?.focus();
|
|
491
|
+
break;
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
// ── Trigger click ────────────────────────────────────────────────────────────
|
|
495
|
+
_handleTriggerClick() {
|
|
496
|
+
this.open ? this.closePopup() : this.openPopup();
|
|
497
|
+
}
|
|
498
|
+
// ── Render ───────────────────────────────────────────────────────────────────
|
|
499
|
+
render() {
|
|
500
|
+
const e = this.multiple && this._effectiveDisplay === "chips" && this._selectedValues.size > 0;
|
|
501
|
+
return c`
|
|
502
|
+
<div id="anchor" part="anchor">
|
|
503
|
+
<div
|
|
504
|
+
id="trigger"
|
|
505
|
+
part="trigger"
|
|
506
|
+
role="combobox"
|
|
507
|
+
tabindex=${this.disabled ? "-1" : "0"}
|
|
508
|
+
aria-haspopup="listbox"
|
|
509
|
+
aria-expanded=${this.open ? "true" : "false"}
|
|
510
|
+
aria-controls="listbox"
|
|
511
|
+
aria-disabled=${this.disabled ? "true" : "false"}
|
|
512
|
+
aria-label=${this.placeholder || _}
|
|
513
|
+
@click=${this._handleTriggerClick}
|
|
514
|
+
@keydown=${this._handleTriggerKeyDown}
|
|
515
|
+
>
|
|
516
|
+
${e ? this._renderChips() : _}
|
|
517
|
+
<span part="value-display">${this._displayLabel}</span>
|
|
518
|
+
<span part="toggle-icon" aria-hidden="true">
|
|
519
|
+
<slot name="toggle-icon">▼</slot>
|
|
520
|
+
</span>
|
|
521
|
+
</div>
|
|
522
|
+
</div>
|
|
523
|
+
|
|
524
|
+
<rc-listbox
|
|
525
|
+
id="listbox"
|
|
526
|
+
part="listbox"
|
|
527
|
+
popover="manual"
|
|
528
|
+
?multiple=${this.multiple}
|
|
529
|
+
checkmark
|
|
530
|
+
@rc-listbox-change=${this._handleListboxChange}
|
|
531
|
+
></rc-listbox>
|
|
532
|
+
|
|
533
|
+
<slot name="select" @slotchange=${this._handleSelectSlotChange}></slot>
|
|
534
|
+
`;
|
|
535
|
+
}
|
|
536
|
+
_renderChips() {
|
|
537
|
+
return c`
|
|
538
|
+
<span part="chips" role="group" aria-label="Selected items">
|
|
539
|
+
${[...this._selectedValues].map((e) => {
|
|
540
|
+
const t = this._labelFor(e);
|
|
541
|
+
return c`
|
|
542
|
+
<button
|
|
543
|
+
type="button"
|
|
544
|
+
part="chip"
|
|
545
|
+
data-value=${e}
|
|
546
|
+
tabindex="-1"
|
|
547
|
+
aria-label=${`Remove ${t}`}
|
|
548
|
+
@click=${(i) => {
|
|
549
|
+
i.stopPropagation(), this._removeValue(e);
|
|
550
|
+
}}
|
|
551
|
+
@keydown=${(i) => this._handleChipKeyDown(i, e)}
|
|
552
|
+
>
|
|
553
|
+
<span part="chip-label">${t}</span
|
|
554
|
+
><span part="chip-remove" aria-hidden="true">×</span>
|
|
555
|
+
</button>
|
|
556
|
+
`;
|
|
557
|
+
})}
|
|
558
|
+
</span>
|
|
559
|
+
`;
|
|
560
|
+
}
|
|
561
|
+
};
|
|
562
|
+
d.styles = g;
|
|
563
|
+
let o = d;
|
|
564
|
+
a([
|
|
565
|
+
n({ type: Boolean, reflect: !0 })
|
|
566
|
+
], o.prototype, "open");
|
|
567
|
+
a([
|
|
568
|
+
n({ type: Boolean, reflect: !0 })
|
|
569
|
+
], o.prototype, "multiple");
|
|
570
|
+
a([
|
|
571
|
+
n({ type: Boolean, reflect: !0 })
|
|
572
|
+
], o.prototype, "disabled");
|
|
573
|
+
a([
|
|
574
|
+
n()
|
|
575
|
+
], o.prototype, "placeholder");
|
|
576
|
+
a([
|
|
577
|
+
n()
|
|
578
|
+
], o.prototype, "display");
|
|
579
|
+
a([
|
|
580
|
+
h("#anchor")
|
|
581
|
+
], o.prototype, "_$anchor");
|
|
582
|
+
a([
|
|
583
|
+
h("#trigger")
|
|
584
|
+
], o.prototype, "_$trigger");
|
|
585
|
+
a([
|
|
586
|
+
h("#listbox")
|
|
587
|
+
], o.prototype, "_$listbox");
|
|
588
|
+
a([
|
|
589
|
+
p()
|
|
590
|
+
], o.prototype, "_selectedValues");
|
|
591
|
+
a([
|
|
592
|
+
p()
|
|
593
|
+
], o.prototype, "_chipNavIndex");
|
|
594
|
+
a([
|
|
595
|
+
p()
|
|
596
|
+
], o.prototype, "_options");
|
|
597
|
+
export {
|
|
598
|
+
o as R
|
|
599
|
+
};
|
|
600
|
+
//# sourceMappingURL=rc-select-BumY2uQl.js.map
|