-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathselect.ts
More file actions
499 lines (433 loc) · 11.9 KB
/
select.ts
File metadata and controls
499 lines (433 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
import { LitElement, html, css, PropertyValues } from "lit";
import { property, state } from "lit/decorators.js";
import { ComponentStyles as TailwindStyles } from "./tw-styles.js";
import { GlobalStyles } from "../../global.js";
function cn(...classes: (string | undefined | false)[]) {
return classes.filter(Boolean).join(" ");
}
// Global state manager for select
const selectState = new Map<
string,
{
isOpen: boolean;
value: string;
onSelect: (value: string) => void;
}
>();
/**
* EccUtilsDesignSelect - Root select component
*/
export class EccUtilsDesignSelect extends LitElement {
static styles = [
css`
${TailwindStyles}
`,
css`
${GlobalStyles}
`,
];
@state() private selectId = `select-${Math.random()
.toString(36)
.substring(2, 9)}`;
@property({ type: String }) value = "";
@property({ type: Boolean }) disabled = false;
@state() private isOpen = false;
connectedCallback() {
super.connectedCallback();
selectState.set(this.selectId, {
isOpen: this.isOpen,
value: this.value,
onSelect: (value) => this._handleSelect(value),
});
document.addEventListener("click", this._handleOutsideClick, true);
}
disconnectedCallback() {
super.disconnectedCallback();
selectState.delete(this.selectId);
document.removeEventListener("click", this._handleOutsideClick, true);
}
updated(changedProperties: PropertyValues) {
if (changedProperties.has("value") || changedProperties.has("isOpen")) {
selectState.set(this.selectId, {
...selectState.get(this.selectId)!,
value: this.value,
isOpen: this.isOpen,
});
}
}
private _handleOutsideClick = (e: MouseEvent) => {
if (this.isOpen && !this.contains(e.target as Node)) {
this.isOpen = false;
e.preventDefault();
}
};
toggleOpen() {
if (this.disabled) return;
this.isOpen = !this.isOpen;
this._updateState();
}
_handleSelect(value: string) {
this.value = value;
this.isOpen = false;
this.dispatchEvent(
new CustomEvent("ecc-input-changed", {
detail: { value },
bubbles: true,
composed: true,
})
);
}
private _updateState() {
selectState.set(this.selectId, {
...selectState.get(this.selectId)!,
isOpen: this.isOpen,
value: this.value,
});
}
getSelectId(): string {
return this.selectId;
}
render() {
return html`
<div
part="base"
class="relative"
data-state=${this.isOpen ? "open" : "closed"}
>
<slot></slot>
</div>
`;
}
}
/**
* EccUtilsDesignSelectTrigger - Trigger component for the select
*/
export class EccUtilsDesignSelectTrigger extends LitElement {
static styles = [
css`
${TailwindStyles}
`,
css`
${GlobalStyles}
`,
];
@property({ type: Boolean }) disabled = false;
@state() private isOpen = false;
private _getParentSelect(): EccUtilsDesignSelect | null {
return this.closest("ecc-utils-design-select") as EccUtilsDesignSelect;
}
private _handleClick() {
const select = this._getParentSelect();
if (select) {
select.toggleOpen();
}
}
render() {
const classes = cn(
"flex h-9 w-full items-center justify-between whitespace-nowrap rounded-md border border-input bg-transparent px-3 py-2 text-sm shadow-sm ring-offset-background focus:outline-none focus:ring-1 focus:ring-ring disabled:cursor-not-allowed disabled:opacity-50 [&>span]:line-clamp-1",
this.disabled && "opacity-50 cursor-not-allowed"
);
return html`
<button
part="base"
type="button"
class=${classes}
?disabled=${this.disabled}
data-state=${this.isOpen ? "open" : "closed"}
@click=${this._handleClick}
>
<slot></slot>
<svg
xmlns="http://www.w3.org/2000/svg"
width="24"
height="24"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
class="h-4 w-4 opacity-50"
>
<path d="m6 9 6 6 6-6" />
</svg>
</button>
`;
}
}
/**
* EccUtilsDesignSelectContent - Content component for the select
*/
export class EccUtilsDesignSelectContent extends LitElement {
static styles = [
css`
${TailwindStyles}
`,
css`
${GlobalStyles}
`,
];
@property({ type: String }) position = "popper";
@state() private isOpen = false;
connectedCallback() {
super.connectedCallback();
this._updateState();
this._updateInterval = window.setInterval(() => this._updateState(), 100);
}
disconnectedCallback() {
super.disconnectedCallback();
if (this._updateInterval) {
window.clearInterval(this._updateInterval);
}
}
private _updateInterval: number | undefined;
private _getParentSelect(): EccUtilsDesignSelect | null {
return this.closest("ecc-utils-design-select") as EccUtilsDesignSelect;
}
private _updateState() {
const select = this._getParentSelect();
if (select && typeof select.getSelectId === "function") {
const selectStateData = selectState.get(select.getSelectId());
if (selectStateData) {
this.isOpen = selectStateData.isOpen;
}
}
}
render() {
if (!this.isOpen) return html``;
const classes = cn(
"absolute z-50 w-full min-w-[8rem] overflow-hidden rounded-md border bg-popover text-popover-foreground shadow-md animate-in fade-in-0 zoom-in-95 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2",
this.position === "popper" &&
"top-full mt-1 data-[side=bottom]:translate-y-1 data-[side=left]:-translate-x-1 data-[side=right]:translate-x-1 data-[side=top]:-translate-y-1"
);
return html`
<div
part="base"
class=${classes}
data-state=${this.isOpen ? "open" : "closed"}
>
<div class="relative max-h-[300px] overflow-y-auto">
<div class="p-1">
<slot></slot>
</div>
</div>
</div>
`;
}
}
/**
* EccUtilsDesignSelectItem - Item component for the select
*/
export class EccUtilsDesignSelectItem extends LitElement {
static styles = [
css`
${TailwindStyles}
`,
css`
${GlobalStyles}
`,
];
@property({ type: String }) value = "";
@property({ type: Boolean }) disabled = false;
@state() private selected = false;
connectedCallback() {
super.connectedCallback();
this._updateSelected();
this._updateInterval = window.setInterval(
() => this._updateSelected(),
100
);
}
disconnectedCallback() {
super.disconnectedCallback();
if (this._updateInterval) {
window.clearInterval(this._updateInterval);
}
}
private _updateInterval: number | undefined;
private _getParentSelect(): EccUtilsDesignSelect | null {
return this.closest("ecc-utils-design-select") as EccUtilsDesignSelect;
}
private _updateSelected() {
const select = this._getParentSelect();
if (select && typeof select.getSelectId === "function") {
const selectStateData = selectState.get(select.getSelectId());
if (selectStateData) {
this.selected = selectStateData.value === this.value;
}
}
}
private _handleSelect() {
if (this.disabled) return;
const select = this._getParentSelect();
if (select) {
select._handleSelect(this.value);
}
}
private _handleKeyDown(e: KeyboardEvent) {
if (this.disabled) return;
// Handle Enter key press
if (e.key === "Enter") {
e.preventDefault();
this._handleSelect();
}
}
render() {
const classes = cn(
"relative flex w-full select-none items-center rounded-sm py-1.5 pl-2 pr-8 text-sm outline-none",
!this.disabled &&
"cursor-pointer hover:bg-accent hover:text-accent-foreground focus:bg-accent focus:text-accent-foreground",
this.disabled && "pointer-events-none opacity-50",
this.selected && "bg-accent text-accent-foreground"
);
return html`
<div
part="base"
class=${classes}
aria-disabled=${this.disabled}
aria-selected=${this.selected}
@click=${this._handleSelect}
@keydown=${this._handleKeyDown}
tabindex=${this.disabled ? "-1" : "0"}
role="option"
>
<span
class="absolute right-2 flex h-3.5 w-3.5 items-center justify-center"
>
${this.selected
? html`
<svg
xmlns="http://www.w3.org/2000/svg"
width="24"
height="24"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
class="h-4 w-4"
>
<path d="M20 6 9 17l-5-5" />
</svg>
`
: ""}
</span>
<slot></slot>
</div>
`;
}
}
/**
* EccUtilsDesignSelectGroup - Group component
*/
export class EccUtilsDesignSelectGroup extends LitElement {
static styles = [
css`
${TailwindStyles}
`,
css`
${GlobalStyles}
`,
];
render() {
return html`
<div part="base" class="overflow-hidden p-1">
<slot></slot>
</div>
`;
}
}
/**
* EccUtilsDesignSelectLabel - Label component
*/
export class EccUtilsDesignSelectLabel extends LitElement {
static styles = [
css`
${TailwindStyles}
`,
css`
${GlobalStyles}
`,
];
render() {
const classes = cn("px-2 py-1.5 text-sm font-semibold");
return html`<div part="base" class=${classes}><slot></slot></div>`;
}
}
/**
* EccUtilsDesignSelectSeparator - Separator component
*/
export class EccUtilsDesignSelectSeparator extends LitElement {
static styles = [
css`
${TailwindStyles}
`,
css`
${GlobalStyles}
`,
];
render() {
const classes = cn("-mx-1 my-1 h-px bg-muted");
return html`<div part="base" class=${classes}></div>`;
}
}
/**
* EccUtilsDesignSelectValue - Value component for the select
*/
export class EccUtilsDesignSelectValue extends LitElement {
static styles = [
css`
${TailwindStyles}
`,
css`
${GlobalStyles}
`,
];
@property({ type: String }) placeholder = "Select an option";
@state() private selectedLabel = "";
connectedCallback() {
super.connectedCallback();
this._updateState();
this._updateInterval = window.setInterval(() => this._updateState(), 100);
}
disconnectedCallback() {
super.disconnectedCallback();
if (this._updateInterval) {
window.clearInterval(this._updateInterval);
}
}
private _updateInterval: number | undefined;
private _getParentSelect(): EccUtilsDesignSelect | null {
return this.closest("ecc-utils-design-select") as EccUtilsDesignSelect;
}
private _updateState() {
const select = this._getParentSelect();
if (select && typeof select.getSelectId === "function") {
const selectStateData = selectState.get(select.getSelectId());
if (selectStateData && selectStateData.value) {
const options = select.querySelectorAll("ecc-utils-design-select-item");
this.selectedLabel =
Array.from(options)
.find(
(option) =>
(option as EccUtilsDesignSelectItem).value ===
selectStateData.value
)
?.textContent?.trim() || this.placeholder;
} else {
this.selectedLabel = this.placeholder;
}
}
}
render() {
const classes = cn(
"line-clamp-1",
this.selectedLabel === this.placeholder && "text-muted-foreground"
);
return html`<span part="base" class=${classes}
>${this.selectedLabel}</span
>`;
}
}
export default EccUtilsDesignSelect;