@sonny-ui/core 0.1.0-alpha.1 → 0.1.0-alpha.10
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 +101 -32
- package/fesm2022/sonny-ui-core.mjs +3031 -42
- package/fesm2022/sonny-ui-core.mjs.map +1 -1
- package/package.json +8 -5
- package/schematics/ng-add/schema.json +1 -1
- package/schematics/ng-generate/component/index.js +1 -1
- package/schematics/ng-generate/component/schema.json +1 -1
- package/src/lib/accordion/accordion.directives.spec.ts +173 -0
- package/src/lib/accordion/accordion.directives.ts +147 -0
- package/src/lib/accordion/index.ts +8 -0
- package/src/lib/alert/alert.directives.spec.ts +154 -0
- package/src/lib/alert/alert.directives.ts +70 -0
- package/src/lib/alert/alert.variants.ts +25 -0
- package/src/lib/alert/index.ts +6 -0
- package/src/lib/avatar/avatar.component.spec.ts +75 -0
- package/src/lib/avatar/avatar.component.ts +44 -0
- package/src/lib/avatar/avatar.variants.ts +26 -0
- package/src/lib/avatar/index.ts +2 -0
- package/src/lib/badge/badge.directive.spec.ts +74 -0
- package/src/lib/badge/badge.directive.ts +18 -0
- package/src/lib/badge/badge.variants.ts +29 -0
- package/src/lib/badge/index.ts +2 -0
- package/src/lib/breadcrumb/breadcrumb.directives.spec.ts +80 -0
- package/src/lib/breadcrumb/breadcrumb.directives.ts +84 -0
- package/src/lib/breadcrumb/index.ts +8 -0
- package/src/lib/button/button.directive.spec.ts +92 -0
- package/src/lib/button/button.directive.ts +29 -0
- package/src/lib/button/button.variants.ts +30 -0
- package/src/lib/button/index.ts +2 -0
- package/src/lib/button-group/button-group.directive.spec.ts +46 -0
- package/src/lib/button-group/button-group.directive.ts +20 -0
- package/src/lib/button-group/button-group.variants.ts +18 -0
- package/src/lib/button-group/index.ts +2 -0
- package/src/lib/calendar/calendar.component.spec.ts +105 -0
- package/src/lib/calendar/calendar.component.ts +231 -0
- package/src/lib/calendar/index.ts +1 -0
- package/src/lib/card/card.directives.spec.ts +104 -0
- package/src/lib/card/card.directives.ts +78 -0
- package/src/lib/card/card.variants.ts +28 -0
- package/src/lib/card/index.ts +9 -0
- package/src/lib/carousel/carousel.directives.spec.ts +85 -0
- package/src/lib/carousel/carousel.directives.ts +164 -0
- package/src/lib/carousel/index.ts +8 -0
- package/src/lib/chat-bubble/chat-bubble.directives.spec.ts +52 -0
- package/src/lib/chat-bubble/chat-bubble.directives.ts +102 -0
- package/src/lib/chat-bubble/index.ts +11 -0
- package/src/lib/checkbox/checkbox.directive.spec.ts +57 -0
- package/src/lib/checkbox/checkbox.directive.ts +17 -0
- package/src/lib/checkbox/checkbox.variants.ts +19 -0
- package/src/lib/checkbox/index.ts +2 -0
- package/src/lib/combobox/combobox.component.spec.ts +151 -0
- package/src/lib/combobox/combobox.component.ts +279 -0
- package/src/lib/combobox/combobox.variants.ts +19 -0
- package/src/lib/combobox/index.ts +2 -0
- package/src/lib/diff/diff.component.spec.ts +47 -0
- package/src/lib/diff/diff.component.ts +83 -0
- package/src/lib/diff/index.ts +1 -0
- package/src/lib/divider/divider.component.spec.ts +48 -0
- package/src/lib/divider/divider.component.ts +52 -0
- package/src/lib/divider/divider.variants.ts +22 -0
- package/src/lib/divider/index.ts +2 -0
- package/src/lib/dock/dock.directives.spec.ts +85 -0
- package/src/lib/dock/dock.directives.ts +83 -0
- package/src/lib/dock/index.ts +1 -0
- package/src/lib/drawer/drawer.directives.spec.ts +62 -0
- package/src/lib/drawer/drawer.directives.ts +83 -0
- package/src/lib/drawer/index.ts +8 -0
- package/src/lib/dropdown/dropdown.directives.spec.ts +106 -0
- package/src/lib/dropdown/dropdown.directives.ts +143 -0
- package/src/lib/dropdown/dropdown.variants.ts +27 -0
- package/src/lib/dropdown/index.ts +15 -0
- package/src/lib/fab/fab.directives.spec.ts +60 -0
- package/src/lib/fab/fab.directives.ts +80 -0
- package/src/lib/fab/index.ts +8 -0
- package/src/lib/fieldset/fieldset.directives.spec.ts +74 -0
- package/src/lib/fieldset/fieldset.directives.ts +52 -0
- package/src/lib/fieldset/fieldset.variants.ts +15 -0
- package/src/lib/fieldset/index.ts +6 -0
- package/src/lib/file-input/file-input.component.spec.ts +114 -0
- package/src/lib/file-input/file-input.component.ts +168 -0
- package/src/lib/file-input/file-input.variants.ts +25 -0
- package/src/lib/file-input/index.ts +6 -0
- package/src/lib/indicator/index.ts +6 -0
- package/src/lib/indicator/indicator.directives.spec.ts +64 -0
- package/src/lib/indicator/indicator.directives.ts +61 -0
- package/src/lib/input/index.ts +3 -0
- package/src/lib/input/input.directive.spec.ts +103 -0
- package/src/lib/input/input.directive.ts +26 -0
- package/src/lib/input/input.variants.ts +42 -0
- package/src/lib/input/label.directive.ts +17 -0
- package/src/lib/kbd/index.ts +2 -0
- package/src/lib/kbd/kbd.directive.spec.ts +42 -0
- package/src/lib/kbd/kbd.directive.ts +19 -0
- package/src/lib/kbd/kbd.variants.ts +19 -0
- package/src/lib/link/index.ts +2 -0
- package/src/lib/link/link.directive.spec.ts +41 -0
- package/src/lib/link/link.directive.ts +19 -0
- package/src/lib/link/link.variants.ts +20 -0
- package/src/lib/list/index.ts +8 -0
- package/src/lib/list/list.directives.spec.ts +65 -0
- package/src/lib/list/list.directives.ts +86 -0
- package/src/lib/loader/index.ts +2 -0
- package/src/lib/loader/loader.component.spec.ts +58 -0
- package/src/lib/loader/loader.component.ts +48 -0
- package/src/lib/loader/loader.variants.ts +21 -0
- package/src/lib/modal/dialog-ref.ts +19 -0
- package/src/lib/modal/dialog.directives.ts +90 -0
- package/src/lib/modal/dialog.service.spec.ts +52 -0
- package/src/lib/modal/dialog.service.ts +61 -0
- package/src/lib/modal/dialog.types.ts +16 -0
- package/src/lib/modal/index.ts +11 -0
- package/src/lib/navbar/index.ts +7 -0
- package/src/lib/navbar/navbar.directives.spec.ts +59 -0
- package/src/lib/navbar/navbar.directives.ts +61 -0
- package/src/lib/pagination/index.ts +6 -0
- package/src/lib/pagination/pagination.component.spec.ts +59 -0
- package/src/lib/pagination/pagination.component.ts +144 -0
- package/src/lib/pagination/pagination.variants.ts +31 -0
- package/src/lib/progress/index.ts +7 -0
- package/src/lib/progress/progress.component.spec.ts +117 -0
- package/src/lib/progress/progress.component.ts +65 -0
- package/src/lib/progress/progress.variants.ts +43 -0
- package/src/lib/radial-progress/index.ts +5 -0
- package/src/lib/radial-progress/radial-progress.component.spec.ts +41 -0
- package/src/lib/radial-progress/radial-progress.component.ts +71 -0
- package/src/lib/radio/index.ts +2 -0
- package/src/lib/radio/radio.directive.spec.ts +46 -0
- package/src/lib/radio/radio.directive.ts +17 -0
- package/src/lib/radio/radio.variants.ts +19 -0
- package/src/lib/rating/index.ts +2 -0
- package/src/lib/rating/rating.component.spec.ts +157 -0
- package/src/lib/rating/rating.component.ts +171 -0
- package/src/lib/rating/rating.variants.ts +20 -0
- package/src/lib/select/index.ts +2 -0
- package/src/lib/select/select.component.spec.ts +112 -0
- package/src/lib/select/select.component.ts +250 -0
- package/src/lib/select/select.variants.ts +19 -0
- package/src/lib/sheet/index.ts +10 -0
- package/src/lib/sheet/sheet-ref.ts +18 -0
- package/src/lib/sheet/sheet.component.spec.ts +67 -0
- package/src/lib/sheet/sheet.directives.ts +75 -0
- package/src/lib/sheet/sheet.service.ts +100 -0
- package/src/lib/sheet/sheet.types.ts +23 -0
- package/src/lib/skeleton/index.ts +2 -0
- package/src/lib/skeleton/skeleton.directive.spec.ts +63 -0
- package/src/lib/skeleton/skeleton.directive.ts +22 -0
- package/src/lib/skeleton/skeleton.variants.ts +27 -0
- package/src/lib/slider/index.ts +2 -0
- package/src/lib/slider/slider.component.spec.ts +104 -0
- package/src/lib/slider/slider.component.ts +188 -0
- package/src/lib/slider/slider.variants.ts +25 -0
- package/src/lib/stat/index.ts +8 -0
- package/src/lib/stat/stat.directives.spec.ts +60 -0
- package/src/lib/stat/stat.directives.ts +84 -0
- package/src/lib/status/index.ts +2 -0
- package/src/lib/status/status.directive.spec.ts +43 -0
- package/src/lib/status/status.directive.ts +38 -0
- package/src/lib/status/status.variants.ts +26 -0
- package/src/lib/steps/index.ts +8 -0
- package/src/lib/steps/steps.directives.spec.ts +52 -0
- package/src/lib/steps/steps.directives.ts +80 -0
- package/src/lib/switch/index.ts +2 -0
- package/src/lib/switch/switch.component.spec.ts +98 -0
- package/src/lib/switch/switch.component.ts +84 -0
- package/src/lib/switch/switch.variants.ts +31 -0
- package/src/lib/table/index.ts +12 -0
- package/src/lib/table/table.directives.spec.ts +111 -0
- package/src/lib/table/table.directives.ts +134 -0
- package/src/lib/table/table.variants.ts +36 -0
- package/src/lib/tabs/index.ts +8 -0
- package/src/lib/tabs/tabs.directives.spec.ts +136 -0
- package/src/lib/tabs/tabs.directives.ts +130 -0
- package/src/lib/tabs/tabs.variants.ts +17 -0
- package/src/lib/textarea/index.ts +7 -0
- package/src/lib/textarea/textarea.directive.spec.ts +84 -0
- package/src/lib/textarea/textarea.directive.ts +72 -0
- package/src/lib/textarea/textarea.variants.ts +34 -0
- package/src/lib/timeline/index.ts +11 -0
- package/src/lib/timeline/timeline.directives.spec.ts +55 -0
- package/src/lib/timeline/timeline.directives.ts +90 -0
- package/src/lib/toast/index.ts +3 -0
- package/src/lib/toast/toast.service.spec.ts +71 -0
- package/src/lib/toast/toast.service.ts +60 -0
- package/src/lib/toast/toast.variants.ts +38 -0
- package/src/lib/toast/toaster.component.spec.ts +38 -0
- package/src/lib/toast/toaster.component.ts +82 -0
- package/src/lib/toggle/index.ts +2 -0
- package/src/lib/toggle/toggle.directive.spec.ts +100 -0
- package/src/lib/toggle/toggle.directive.ts +73 -0
- package/src/lib/toggle/toggle.variants.ts +25 -0
- package/src/lib/tooltip/index.ts +2 -0
- package/src/lib/tooltip/tooltip.directive.spec.ts +113 -0
- package/src/lib/tooltip/tooltip.directive.ts +131 -0
- package/src/lib/tooltip/tooltip.variants.ts +20 -0
- package/src/lib/validator/index.ts +5 -0
- package/src/lib/validator/validator.directives.spec.ts +47 -0
- package/src/lib/validator/validator.directives.ts +52 -0
- package/types/sonny-ui-core.d.ts +878 -11
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
import { ChangeDetectionStrategy, Component, computed, effect, forwardRef, input, model, signal } from '@angular/core';
|
|
2
|
+
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
|
|
3
|
+
import { cn } from '../core/utils/cn';
|
|
4
|
+
import { ratingVariants, type RatingSize, type RatingVariant } from './rating.variants';
|
|
5
|
+
|
|
6
|
+
interface StarState {
|
|
7
|
+
index: number;
|
|
8
|
+
fill: 'full' | 'half' | 'empty';
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
@Component({
|
|
12
|
+
selector: 'sny-rating',
|
|
13
|
+
standalone: true,
|
|
14
|
+
changeDetection: ChangeDetectionStrategy.OnPush,
|
|
15
|
+
host: {
|
|
16
|
+
'role': 'slider',
|
|
17
|
+
'tabindex': '0',
|
|
18
|
+
'[attr.aria-valuenow]': 'value()',
|
|
19
|
+
'[attr.aria-valuemin]': '0',
|
|
20
|
+
'[attr.aria-valuemax]': 'max()',
|
|
21
|
+
'[attr.aria-label]': '"Rating"',
|
|
22
|
+
'[attr.aria-readonly]': 'readonly() || null',
|
|
23
|
+
'[class]': 'computedClass()',
|
|
24
|
+
'(keydown)': 'onKeydown($event)',
|
|
25
|
+
'(mouseleave)': 'onMouseLeave()',
|
|
26
|
+
'(blur)': 'onTouched()',
|
|
27
|
+
},
|
|
28
|
+
providers: [
|
|
29
|
+
{ provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(() => SnyRatingComponent), multi: true },
|
|
30
|
+
],
|
|
31
|
+
template: `
|
|
32
|
+
@for (star of stars(); track star.index) {
|
|
33
|
+
<svg
|
|
34
|
+
xmlns="http://www.w3.org/2000/svg"
|
|
35
|
+
viewBox="0 0 24 24"
|
|
36
|
+
[class]="starClass(star)"
|
|
37
|
+
(click)="onStarClick(star.index + 1)"
|
|
38
|
+
(mouseenter)="onStarHover(star.index + 1)"
|
|
39
|
+
>
|
|
40
|
+
@if (ratingVariant() === 'star') {
|
|
41
|
+
<polygon points="12 2 15.09 8.26 22 9.27 17 14.14 18.18 21.02 12 17.77 5.82 21.02 7 14.14 2 9.27 8.91 8.26 12 2" />
|
|
42
|
+
} @else {
|
|
43
|
+
<path d="M20.84 4.61a5.5 5.5 0 0 0-7.78 0L12 5.67l-1.06-1.06a5.5 5.5 0 0 0-7.78 7.78l1.06 1.06L12 21.23l7.78-7.78 1.06-1.06a5.5 5.5 0 0 0 0-7.78z" />
|
|
44
|
+
}
|
|
45
|
+
</svg>
|
|
46
|
+
}
|
|
47
|
+
`,
|
|
48
|
+
})
|
|
49
|
+
export class SnyRatingComponent implements ControlValueAccessor {
|
|
50
|
+
readonly value = model(0);
|
|
51
|
+
readonly max = input(5);
|
|
52
|
+
readonly readonly = input(false);
|
|
53
|
+
readonly size = input<RatingSize>('md');
|
|
54
|
+
readonly ratingVariant = input<RatingVariant>('star');
|
|
55
|
+
readonly half = input(false);
|
|
56
|
+
readonly class = input<string>('');
|
|
57
|
+
|
|
58
|
+
private readonly _disabledByCva = signal(false);
|
|
59
|
+
protected readonly isDisabled = computed(() => this.readonly() || this._disabledByCva());
|
|
60
|
+
|
|
61
|
+
readonly hoverValue = signal<number | null>(null);
|
|
62
|
+
|
|
63
|
+
private _onChange: (value: number) => void = () => {};
|
|
64
|
+
protected onTouched: () => void = () => {};
|
|
65
|
+
private _writing = false;
|
|
66
|
+
|
|
67
|
+
constructor() {
|
|
68
|
+
effect(() => {
|
|
69
|
+
const val = this.value();
|
|
70
|
+
if (this._writing) {
|
|
71
|
+
this._writing = false;
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
this._onChange(val);
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
writeValue(val: number): void {
|
|
79
|
+
this._writing = true;
|
|
80
|
+
this.value.set(val ?? 0);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
registerOnChange(fn: (value: number) => void): void {
|
|
84
|
+
this._onChange = fn;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
registerOnTouched(fn: () => void): void {
|
|
88
|
+
this.onTouched = fn;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
setDisabledState(isDisabled: boolean): void {
|
|
92
|
+
this._disabledByCva.set(isDisabled);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
readonly displayValue = computed(() => this.hoverValue() ?? this.value());
|
|
96
|
+
|
|
97
|
+
readonly stars = computed<StarState[]>(() => {
|
|
98
|
+
const display = this.displayValue();
|
|
99
|
+
const m = this.max();
|
|
100
|
+
return Array.from({ length: m }, (_, i) => {
|
|
101
|
+
let fill: 'full' | 'half' | 'empty';
|
|
102
|
+
if (i + 1 <= display) {
|
|
103
|
+
fill = 'full';
|
|
104
|
+
} else if (this.half() && i + 0.5 <= display) {
|
|
105
|
+
fill = 'half';
|
|
106
|
+
} else {
|
|
107
|
+
fill = 'empty';
|
|
108
|
+
}
|
|
109
|
+
return { index: i, fill };
|
|
110
|
+
});
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
protected readonly computedClass = computed(() =>
|
|
114
|
+
cn(
|
|
115
|
+
ratingVariants({ size: this.size() }),
|
|
116
|
+
this.isDisabled() && 'pointer-events-none',
|
|
117
|
+
this.class()
|
|
118
|
+
)
|
|
119
|
+
);
|
|
120
|
+
|
|
121
|
+
starClass(star: StarState): string {
|
|
122
|
+
const base = 'cursor-pointer transition-colors';
|
|
123
|
+
switch (star.fill) {
|
|
124
|
+
case 'full':
|
|
125
|
+
return cn(base, 'fill-yellow-400 stroke-yellow-400');
|
|
126
|
+
case 'half':
|
|
127
|
+
return cn(base, 'fill-yellow-400/50 stroke-yellow-400');
|
|
128
|
+
case 'empty':
|
|
129
|
+
return cn(base, 'fill-none stroke-muted-foreground');
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
onStarClick(index: number): void {
|
|
134
|
+
if (this.isDisabled()) return;
|
|
135
|
+
this.value.set(index);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
onStarHover(index: number): void {
|
|
139
|
+
if (this.isDisabled()) return;
|
|
140
|
+
this.hoverValue.set(index);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
onMouseLeave(): void {
|
|
144
|
+
this.hoverValue.set(null);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
onKeydown(event: KeyboardEvent): void {
|
|
148
|
+
if (this.isDisabled()) return;
|
|
149
|
+
const step = this.half() ? 0.5 : 1;
|
|
150
|
+
switch (event.key) {
|
|
151
|
+
case 'ArrowRight':
|
|
152
|
+
case 'ArrowUp':
|
|
153
|
+
event.preventDefault();
|
|
154
|
+
this.value.update((v) => Math.min(this.max(), v + step));
|
|
155
|
+
break;
|
|
156
|
+
case 'ArrowLeft':
|
|
157
|
+
case 'ArrowDown':
|
|
158
|
+
event.preventDefault();
|
|
159
|
+
this.value.update((v) => Math.max(0, v - step));
|
|
160
|
+
break;
|
|
161
|
+
case 'Home':
|
|
162
|
+
event.preventDefault();
|
|
163
|
+
this.value.set(0);
|
|
164
|
+
break;
|
|
165
|
+
case 'End':
|
|
166
|
+
event.preventDefault();
|
|
167
|
+
this.value.set(this.max());
|
|
168
|
+
break;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { cva } from 'class-variance-authority';
|
|
2
|
+
|
|
3
|
+
export const ratingVariants = cva(
|
|
4
|
+
'inline-flex items-center gap-0.5 focus:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 rounded',
|
|
5
|
+
{
|
|
6
|
+
variants: {
|
|
7
|
+
size: {
|
|
8
|
+
sm: '[&_svg]:w-4 [&_svg]:h-4',
|
|
9
|
+
md: '[&_svg]:w-5 [&_svg]:h-5',
|
|
10
|
+
lg: '[&_svg]:w-7 [&_svg]:h-7',
|
|
11
|
+
},
|
|
12
|
+
},
|
|
13
|
+
defaultVariants: {
|
|
14
|
+
size: 'md',
|
|
15
|
+
},
|
|
16
|
+
}
|
|
17
|
+
);
|
|
18
|
+
|
|
19
|
+
export type RatingSize = 'sm' | 'md' | 'lg';
|
|
20
|
+
export type RatingVariant = 'star' | 'heart';
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { Component, signal } from '@angular/core';
|
|
2
|
+
import { FormControl, ReactiveFormsModule } from '@angular/forms';
|
|
3
|
+
import { TestBed, ComponentFixture } from '@angular/core/testing';
|
|
4
|
+
import { SnySelectComponent, type SelectOption } from './select.component';
|
|
5
|
+
|
|
6
|
+
@Component({
|
|
7
|
+
standalone: true,
|
|
8
|
+
imports: [SnySelectComponent],
|
|
9
|
+
template: `<sny-select [options]="options" [(value)]="value" [placeholder]="placeholder()" />`,
|
|
10
|
+
})
|
|
11
|
+
class TestHostComponent {
|
|
12
|
+
options: SelectOption[] = [
|
|
13
|
+
{ value: 'a', label: 'Option A' },
|
|
14
|
+
{ value: 'b', label: 'Option B' },
|
|
15
|
+
{ value: 'c', label: 'Option C' },
|
|
16
|
+
];
|
|
17
|
+
value = signal('');
|
|
18
|
+
placeholder = signal('Pick one...');
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
describe('SnySelectComponent', () => {
|
|
22
|
+
let fixture: ComponentFixture<TestHostComponent>;
|
|
23
|
+
let trigger: HTMLButtonElement;
|
|
24
|
+
|
|
25
|
+
beforeEach(async () => {
|
|
26
|
+
await TestBed.configureTestingModule({
|
|
27
|
+
imports: [TestHostComponent],
|
|
28
|
+
}).compileComponents();
|
|
29
|
+
|
|
30
|
+
fixture = TestBed.createComponent(TestHostComponent);
|
|
31
|
+
fixture.detectChanges();
|
|
32
|
+
trigger = fixture.nativeElement.querySelector('button');
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it('should show placeholder when no value', () => {
|
|
36
|
+
expect(trigger.textContent).toContain('Pick one...');
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it('should have combobox role', () => {
|
|
40
|
+
expect(trigger.getAttribute('role')).toBe('combobox');
|
|
41
|
+
expect(trigger.getAttribute('aria-expanded')).toBe('false');
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it('should open dropdown on click', () => {
|
|
45
|
+
trigger.click();
|
|
46
|
+
fixture.detectChanges();
|
|
47
|
+
expect(trigger.getAttribute('aria-expanded')).toBe('true');
|
|
48
|
+
const options = fixture.nativeElement.querySelectorAll('[role="option"]');
|
|
49
|
+
expect(options.length).toBe(3);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it('should show selected label', () => {
|
|
53
|
+
fixture.componentInstance.value.set('b');
|
|
54
|
+
fixture.detectChanges();
|
|
55
|
+
expect(trigger.textContent).toContain('Option B');
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
@Component({
|
|
60
|
+
standalone: true,
|
|
61
|
+
imports: [ReactiveFormsModule, SnySelectComponent],
|
|
62
|
+
template: `<sny-select [options]="options" [formControl]="ctrl" />`,
|
|
63
|
+
})
|
|
64
|
+
class ReactiveFormHost {
|
|
65
|
+
options: SelectOption[] = [
|
|
66
|
+
{ value: 'a', label: 'Option A' },
|
|
67
|
+
{ value: 'b', label: 'Option B' },
|
|
68
|
+
{ value: 'c', label: 'Option C' },
|
|
69
|
+
];
|
|
70
|
+
ctrl = new FormControl('');
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
describe('SnySelectComponent — Reactive Forms', () => {
|
|
74
|
+
let fixture: ComponentFixture<ReactiveFormHost>;
|
|
75
|
+
let trigger: HTMLButtonElement;
|
|
76
|
+
|
|
77
|
+
beforeEach(async () => {
|
|
78
|
+
await TestBed.configureTestingModule({
|
|
79
|
+
imports: [ReactiveFormHost],
|
|
80
|
+
}).compileComponents();
|
|
81
|
+
fixture = TestBed.createComponent(ReactiveFormHost);
|
|
82
|
+
fixture.detectChanges();
|
|
83
|
+
trigger = fixture.nativeElement.querySelector('button');
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it('should update view when FormControl value changes (writeValue)', () => {
|
|
87
|
+
fixture.componentInstance.ctrl.setValue('b');
|
|
88
|
+
fixture.detectChanges();
|
|
89
|
+
expect(trigger.textContent).toContain('Option B');
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it('should update FormControl when user interacts (onChange)', () => {
|
|
93
|
+
trigger.click();
|
|
94
|
+
fixture.detectChanges();
|
|
95
|
+
const option = fixture.nativeElement.querySelector('[role="option"]') as HTMLElement;
|
|
96
|
+
option.dispatchEvent(new Event('mousedown'));
|
|
97
|
+
fixture.detectChanges();
|
|
98
|
+
expect(fixture.componentInstance.ctrl.value).toBe('a');
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it('should disable via FormControl.disable() (setDisabledState)', () => {
|
|
102
|
+
fixture.componentInstance.ctrl.disable();
|
|
103
|
+
fixture.detectChanges();
|
|
104
|
+
expect(trigger.disabled).toBe(true);
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it('should mark as touched on blur (onTouched)', () => {
|
|
108
|
+
expect(fixture.componentInstance.ctrl.touched).toBe(false);
|
|
109
|
+
trigger.dispatchEvent(new Event('blur'));
|
|
110
|
+
expect(fixture.componentInstance.ctrl.touched).toBe(true);
|
|
111
|
+
});
|
|
112
|
+
});
|
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ChangeDetectionStrategy,
|
|
3
|
+
Component,
|
|
4
|
+
computed,
|
|
5
|
+
effect,
|
|
6
|
+
ElementRef,
|
|
7
|
+
forwardRef,
|
|
8
|
+
HostListener,
|
|
9
|
+
inject,
|
|
10
|
+
input,
|
|
11
|
+
model,
|
|
12
|
+
OnDestroy,
|
|
13
|
+
signal,
|
|
14
|
+
viewChild,
|
|
15
|
+
} from '@angular/core';
|
|
16
|
+
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
|
|
17
|
+
import { cn } from '../core/utils/cn';
|
|
18
|
+
import { selectTriggerVariants, type SelectSize } from './select.variants';
|
|
19
|
+
|
|
20
|
+
export interface SelectOption {
|
|
21
|
+
value: string;
|
|
22
|
+
label: string;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
@Component({
|
|
26
|
+
selector: 'sny-select',
|
|
27
|
+
standalone: true,
|
|
28
|
+
changeDetection: ChangeDetectionStrategy.OnPush,
|
|
29
|
+
host: {
|
|
30
|
+
class: 'relative inline-block w-full',
|
|
31
|
+
},
|
|
32
|
+
providers: [
|
|
33
|
+
{ provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(() => SnySelectComponent), multi: true },
|
|
34
|
+
],
|
|
35
|
+
template: `
|
|
36
|
+
<button
|
|
37
|
+
#triggerEl
|
|
38
|
+
type="button"
|
|
39
|
+
role="combobox"
|
|
40
|
+
[attr.aria-expanded]="open()"
|
|
41
|
+
aria-haspopup="listbox"
|
|
42
|
+
[disabled]="isDisabled()"
|
|
43
|
+
[class]="triggerClass()"
|
|
44
|
+
(click)="toggle()"
|
|
45
|
+
(keydown)="onTriggerKeydown($event)"
|
|
46
|
+
(blur)="onTouched()"
|
|
47
|
+
>
|
|
48
|
+
<span [class]="selectedLabel() ? '' : 'text-muted-foreground'">
|
|
49
|
+
{{ selectedLabel() || placeholder() }}
|
|
50
|
+
</span>
|
|
51
|
+
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="shrink-0 opacity-50"><path d="m6 9 6 6 6-6"/></svg>
|
|
52
|
+
</button>
|
|
53
|
+
|
|
54
|
+
@if (open()) {
|
|
55
|
+
<div
|
|
56
|
+
#dropdownEl
|
|
57
|
+
class="fixed z-50 rounded-sm border border-border bg-popover text-popover-foreground shadow-md"
|
|
58
|
+
>
|
|
59
|
+
<ul role="listbox" class="max-h-60 overflow-auto p-1">
|
|
60
|
+
@for (opt of options(); track opt.value; let i = $index) {
|
|
61
|
+
<li
|
|
62
|
+
role="option"
|
|
63
|
+
[attr.aria-selected]="value() === opt.value"
|
|
64
|
+
[class]="optionClass(i)"
|
|
65
|
+
(mousedown)="select(opt); $event.preventDefault()"
|
|
66
|
+
(mouseenter)="activeIndex.set(i)"
|
|
67
|
+
>
|
|
68
|
+
<svg
|
|
69
|
+
xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24"
|
|
70
|
+
fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"
|
|
71
|
+
[class]="value() === opt.value ? 'mr-2 shrink-0 opacity-100' : 'mr-2 shrink-0 opacity-0'"
|
|
72
|
+
><path d="M20 6 9 17l-5-5"/></svg>
|
|
73
|
+
{{ opt.label }}
|
|
74
|
+
</li>
|
|
75
|
+
}
|
|
76
|
+
</ul>
|
|
77
|
+
</div>
|
|
78
|
+
}
|
|
79
|
+
`,
|
|
80
|
+
})
|
|
81
|
+
export class SnySelectComponent implements ControlValueAccessor, OnDestroy {
|
|
82
|
+
readonly options = input<SelectOption[]>([]);
|
|
83
|
+
readonly placeholder = input('Select...');
|
|
84
|
+
readonly size = input<SelectSize>('md');
|
|
85
|
+
readonly disabled = input(false);
|
|
86
|
+
readonly class = input<string>('');
|
|
87
|
+
readonly value = model<string>('');
|
|
88
|
+
|
|
89
|
+
readonly open = signal(false);
|
|
90
|
+
readonly activeIndex = signal(0);
|
|
91
|
+
|
|
92
|
+
private readonly _disabledByCva = signal(false);
|
|
93
|
+
protected readonly isDisabled = computed(() => this.disabled() || this._disabledByCva());
|
|
94
|
+
|
|
95
|
+
private readonly triggerRef = viewChild<ElementRef<HTMLButtonElement>>('triggerEl');
|
|
96
|
+
private readonly dropdownRef = viewChild<ElementRef<HTMLDivElement>>('dropdownEl');
|
|
97
|
+
private readonly elRef = inject(ElementRef);
|
|
98
|
+
|
|
99
|
+
private scrollHandler: (() => void) | null = null;
|
|
100
|
+
private resizeHandler: (() => void) | null = null;
|
|
101
|
+
|
|
102
|
+
private _onChange: (value: string) => void = () => {};
|
|
103
|
+
protected onTouched: () => void = () => {};
|
|
104
|
+
private _writing = false;
|
|
105
|
+
|
|
106
|
+
constructor() {
|
|
107
|
+
effect(() => {
|
|
108
|
+
const val = this.value();
|
|
109
|
+
if (this._writing) {
|
|
110
|
+
this._writing = false;
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
this._onChange(val);
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
writeValue(val: string): void {
|
|
118
|
+
this._writing = true;
|
|
119
|
+
this.value.set(val ?? '');
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
registerOnChange(fn: (value: string) => void): void {
|
|
123
|
+
this._onChange = fn;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
registerOnTouched(fn: () => void): void {
|
|
127
|
+
this.onTouched = fn;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
setDisabledState(isDisabled: boolean): void {
|
|
131
|
+
this._disabledByCva.set(isDisabled);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
readonly selectedLabel = computed(() => {
|
|
135
|
+
const v = this.value();
|
|
136
|
+
if (!v) return '';
|
|
137
|
+
const opt = this.options().find(o => o.value === v);
|
|
138
|
+
return opt?.label ?? '';
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
protected readonly triggerClass = computed(() =>
|
|
142
|
+
cn(selectTriggerVariants({ size: this.size() }), this.class())
|
|
143
|
+
);
|
|
144
|
+
|
|
145
|
+
optionClass(index: number): string {
|
|
146
|
+
const base = 'relative flex cursor-pointer select-none items-center rounded-sm px-2 py-1.5 text-sm outline-none transition-colors';
|
|
147
|
+
const active = index === this.activeIndex() ? 'bg-accent text-accent-foreground' : 'hover:bg-accent/50';
|
|
148
|
+
return cn(base, active);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
private updateDropdownPosition(): void {
|
|
152
|
+
const trigger = this.triggerRef()?.nativeElement;
|
|
153
|
+
if (!trigger) return;
|
|
154
|
+
const rect = trigger.getBoundingClientRect();
|
|
155
|
+
const dropdown = this.dropdownRef()?.nativeElement;
|
|
156
|
+
if (dropdown) {
|
|
157
|
+
dropdown.style.top = `${rect.bottom + 4}px`;
|
|
158
|
+
dropdown.style.left = `${rect.left}px`;
|
|
159
|
+
dropdown.style.width = `${rect.width}px`;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
private addGlobalListeners(): void {
|
|
164
|
+
this.removeGlobalListeners();
|
|
165
|
+
this.scrollHandler = () => {
|
|
166
|
+
requestAnimationFrame(() => this.updateDropdownPosition());
|
|
167
|
+
};
|
|
168
|
+
this.resizeHandler = () => {
|
|
169
|
+
requestAnimationFrame(() => this.updateDropdownPosition());
|
|
170
|
+
};
|
|
171
|
+
document.addEventListener('scroll', this.scrollHandler, { capture: true, passive: true });
|
|
172
|
+
window.addEventListener('resize', this.resizeHandler, { passive: true });
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
private removeGlobalListeners(): void {
|
|
176
|
+
if (this.scrollHandler) {
|
|
177
|
+
document.removeEventListener('scroll', this.scrollHandler, { capture: true } as EventListenerOptions);
|
|
178
|
+
this.scrollHandler = null;
|
|
179
|
+
}
|
|
180
|
+
if (this.resizeHandler) {
|
|
181
|
+
window.removeEventListener('resize', this.resizeHandler);
|
|
182
|
+
this.resizeHandler = null;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
ngOnDestroy(): void {
|
|
187
|
+
this.removeGlobalListeners();
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
toggle(): void {
|
|
191
|
+
if (this.open()) {
|
|
192
|
+
this.close();
|
|
193
|
+
} else {
|
|
194
|
+
this.open.set(true);
|
|
195
|
+
this.activeIndex.set(
|
|
196
|
+
Math.max(0, this.options().findIndex(o => o.value === this.value()))
|
|
197
|
+
);
|
|
198
|
+
this.addGlobalListeners();
|
|
199
|
+
setTimeout(() => this.updateDropdownPosition());
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
close(): void {
|
|
204
|
+
this.open.set(false);
|
|
205
|
+
this.removeGlobalListeners();
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
select(opt: SelectOption): void {
|
|
209
|
+
this.value.set(opt.value);
|
|
210
|
+
this.close();
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
onTriggerKeydown(event: KeyboardEvent): void {
|
|
214
|
+
const items = this.options();
|
|
215
|
+
if (!this.open()) {
|
|
216
|
+
if (event.key === 'ArrowDown' || event.key === 'Enter' || event.key === ' ') {
|
|
217
|
+
event.preventDefault();
|
|
218
|
+
this.toggle();
|
|
219
|
+
}
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
switch (event.key) {
|
|
223
|
+
case 'ArrowDown':
|
|
224
|
+
event.preventDefault();
|
|
225
|
+
this.activeIndex.update(i => Math.min(i + 1, items.length - 1));
|
|
226
|
+
break;
|
|
227
|
+
case 'ArrowUp':
|
|
228
|
+
event.preventDefault();
|
|
229
|
+
this.activeIndex.update(i => Math.max(i - 1, 0));
|
|
230
|
+
break;
|
|
231
|
+
case 'Enter':
|
|
232
|
+
case ' ':
|
|
233
|
+
event.preventDefault();
|
|
234
|
+
if (items[this.activeIndex()]) {
|
|
235
|
+
this.select(items[this.activeIndex()]);
|
|
236
|
+
}
|
|
237
|
+
break;
|
|
238
|
+
case 'Escape':
|
|
239
|
+
this.close();
|
|
240
|
+
break;
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
@HostListener('document:click', ['$event'])
|
|
245
|
+
onDocumentClick(event: MouseEvent): void {
|
|
246
|
+
if (!this.elRef.nativeElement.contains(event.target)) {
|
|
247
|
+
this.close();
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { cva } from 'class-variance-authority';
|
|
2
|
+
|
|
3
|
+
export const selectTriggerVariants = cva(
|
|
4
|
+
'inline-flex w-full items-center justify-between whitespace-nowrap rounded-sm border border-border bg-background px-3 py-2 text-sm ring-offset-background transition-colors hover:bg-accent hover:text-accent-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50',
|
|
5
|
+
{
|
|
6
|
+
variants: {
|
|
7
|
+
size: {
|
|
8
|
+
sm: 'h-9 text-xs',
|
|
9
|
+
md: 'h-10 text-sm',
|
|
10
|
+
lg: 'h-11 text-base',
|
|
11
|
+
},
|
|
12
|
+
},
|
|
13
|
+
defaultVariants: {
|
|
14
|
+
size: 'md',
|
|
15
|
+
},
|
|
16
|
+
}
|
|
17
|
+
);
|
|
18
|
+
|
|
19
|
+
export type SelectSize = 'sm' | 'md' | 'lg';
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export { SnySheetService, SNY_SHEET_DATA } from './sheet.service';
|
|
2
|
+
export { SnySheetRef } from './sheet-ref';
|
|
3
|
+
export {
|
|
4
|
+
SnySheetHeaderDirective,
|
|
5
|
+
SnySheetTitleDirective,
|
|
6
|
+
SnySheetDescriptionDirective,
|
|
7
|
+
SnySheetContentDirective,
|
|
8
|
+
SnySheetCloseDirective,
|
|
9
|
+
} from './sheet.directives';
|
|
10
|
+
export { type SnySheetConfig, type SheetSide } from './sheet.types';
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { Observable } from 'rxjs';
|
|
2
|
+
|
|
3
|
+
interface CdkDialogRefLike<R> {
|
|
4
|
+
close(result?: R): void;
|
|
5
|
+
readonly closed: Observable<R | undefined>;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export class SnySheetRef<R = unknown> {
|
|
9
|
+
constructor(private readonly cdkRef: CdkDialogRefLike<R>) {}
|
|
10
|
+
|
|
11
|
+
close(result?: R): void {
|
|
12
|
+
this.cdkRef.close(result);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
get closed(): Observable<R | undefined> {
|
|
16
|
+
return this.cdkRef.closed;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { Component, inject } from '@angular/core';
|
|
2
|
+
import { TestBed } from '@angular/core/testing';
|
|
3
|
+
import { DialogModule, DialogRef } from '@angular/cdk/dialog';
|
|
4
|
+
import { SnySheetService } from './sheet.service';
|
|
5
|
+
import {
|
|
6
|
+
SnySheetHeaderDirective,
|
|
7
|
+
SnySheetTitleDirective,
|
|
8
|
+
SnySheetDescriptionDirective,
|
|
9
|
+
SnySheetCloseDirective,
|
|
10
|
+
} from './sheet.directives';
|
|
11
|
+
import { SnyButtonDirective } from '../button/button.directive';
|
|
12
|
+
|
|
13
|
+
@Component({
|
|
14
|
+
standalone: true,
|
|
15
|
+
imports: [
|
|
16
|
+
SnySheetHeaderDirective,
|
|
17
|
+
SnySheetTitleDirective,
|
|
18
|
+
SnySheetDescriptionDirective,
|
|
19
|
+
SnySheetCloseDirective,
|
|
20
|
+
SnyButtonDirective,
|
|
21
|
+
],
|
|
22
|
+
template: `
|
|
23
|
+
<div>
|
|
24
|
+
<div snySheetHeader>
|
|
25
|
+
<h2 snySheetTitle>Test Sheet</h2>
|
|
26
|
+
<p snySheetDescription>A test sheet.</p>
|
|
27
|
+
</div>
|
|
28
|
+
<button snySheetClose aria-label="Close">X</button>
|
|
29
|
+
<button snyBtn (click)="dialogRef.close('done')">Done</button>
|
|
30
|
+
</div>
|
|
31
|
+
`,
|
|
32
|
+
})
|
|
33
|
+
class TestSheetComponent {
|
|
34
|
+
readonly dialogRef = inject(DialogRef);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
describe('SnySheetService', () => {
|
|
38
|
+
let service: SnySheetService;
|
|
39
|
+
|
|
40
|
+
beforeEach(async () => {
|
|
41
|
+
await TestBed.configureTestingModule({
|
|
42
|
+
imports: [DialogModule],
|
|
43
|
+
}).compileComponents();
|
|
44
|
+
|
|
45
|
+
service = TestBed.inject(SnySheetService);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it('should be created', () => {
|
|
49
|
+
expect(service).toBeTruthy();
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it('should open a sheet', () => {
|
|
53
|
+
const ref = service.open(TestSheetComponent, { side: 'right' });
|
|
54
|
+
expect(ref).toBeTruthy();
|
|
55
|
+
ref.close();
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it('should open from different sides', () => {
|
|
59
|
+
const refLeft = service.open(TestSheetComponent, { side: 'left' });
|
|
60
|
+
expect(refLeft).toBeTruthy();
|
|
61
|
+
refLeft.close();
|
|
62
|
+
|
|
63
|
+
const refTop = service.open(TestSheetComponent, { side: 'top' });
|
|
64
|
+
expect(refTop).toBeTruthy();
|
|
65
|
+
refTop.close();
|
|
66
|
+
});
|
|
67
|
+
});
|