@sonny-ui/core 0.1.0-alpha.13 → 0.1.0-alpha.15
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/fesm2022/sonny-ui-core.mjs +2239 -632
- package/fesm2022/sonny-ui-core.mjs.map +1 -1
- package/package.json +1 -1
- package/src/lib/calendar/calendar.component.spec.ts +87 -0
- package/src/lib/calendar/calendar.component.ts +184 -61
- package/src/lib/calendar/calendar.types.ts +24 -0
- package/src/lib/calendar/index.ts +6 -0
- package/src/lib/data-table/data-table.component.spec.ts +443 -0
- package/src/lib/data-table/data-table.component.ts +603 -0
- package/src/lib/data-table/data-table.directives.ts +35 -0
- package/src/lib/data-table/data-table.types.ts +20 -0
- package/src/lib/data-table/index.ts +13 -0
- package/src/lib/date-picker/date-picker.component.spec.ts +131 -0
- package/src/lib/date-picker/date-picker.component.ts +220 -0
- package/src/lib/date-picker/date-picker.variants.ts +17 -0
- package/src/lib/date-picker/index.ts +2 -0
- package/src/lib/date-range-picker/date-range-picker.component.spec.ts +151 -0
- package/src/lib/date-range-picker/date-range-picker.component.ts +340 -0
- package/src/lib/date-range-picker/index.ts +1 -0
- package/types/sonny-ui-core.d.ts +234 -7
|
@@ -0,0 +1,340 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ChangeDetectionStrategy,
|
|
3
|
+
Component,
|
|
4
|
+
computed,
|
|
5
|
+
ElementRef,
|
|
6
|
+
forwardRef,
|
|
7
|
+
HostListener,
|
|
8
|
+
inject,
|
|
9
|
+
input,
|
|
10
|
+
model,
|
|
11
|
+
OnDestroy,
|
|
12
|
+
signal,
|
|
13
|
+
viewChild,
|
|
14
|
+
} from '@angular/core';
|
|
15
|
+
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
|
|
16
|
+
import { cn } from '../core/utils/cn';
|
|
17
|
+
import { SnyCalendarComponent } from '../calendar/calendar.component';
|
|
18
|
+
import type { DateRange, DatePickerPreset } from '../calendar/calendar.types';
|
|
19
|
+
import { datePickerTriggerVariants, type DatePickerSize } from '../date-picker/date-picker.variants';
|
|
20
|
+
|
|
21
|
+
@Component({
|
|
22
|
+
selector: 'sny-date-range-picker',
|
|
23
|
+
standalone: true,
|
|
24
|
+
changeDetection: ChangeDetectionStrategy.OnPush,
|
|
25
|
+
imports: [SnyCalendarComponent],
|
|
26
|
+
host: { class: 'relative inline-block w-full' },
|
|
27
|
+
providers: [
|
|
28
|
+
{ provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(() => SnyDateRangePickerComponent), multi: true },
|
|
29
|
+
],
|
|
30
|
+
template: `
|
|
31
|
+
<button
|
|
32
|
+
#triggerEl
|
|
33
|
+
type="button"
|
|
34
|
+
role="combobox"
|
|
35
|
+
[attr.aria-expanded]="open()"
|
|
36
|
+
aria-haspopup="dialog"
|
|
37
|
+
[disabled]="isDisabled()"
|
|
38
|
+
[class]="triggerClass()"
|
|
39
|
+
(click)="toggle()"
|
|
40
|
+
(blur)="onTouched()"
|
|
41
|
+
>
|
|
42
|
+
<span [class]="displayValue() ? 'truncate' : 'text-muted-foreground truncate'">
|
|
43
|
+
{{ displayValue() || placeholder() }}
|
|
44
|
+
</span>
|
|
45
|
+
<div class="flex items-center gap-1 shrink-0">
|
|
46
|
+
@if (clearable() && value()?.start) {
|
|
47
|
+
<button
|
|
48
|
+
type="button"
|
|
49
|
+
class="rounded-sm p-0.5 hover:bg-muted transition-colors text-muted-foreground hover:text-foreground"
|
|
50
|
+
(click)="clear($event)"
|
|
51
|
+
aria-label="Clear date range"
|
|
52
|
+
>
|
|
53
|
+
<svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M18 6 6 18"/><path d="m6 6 12 12"/></svg>
|
|
54
|
+
</button>
|
|
55
|
+
}
|
|
56
|
+
<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 text-muted-foreground"><path d="M8 2v4"/><path d="M16 2v4"/><rect width="18" height="18" x="3" y="4" rx="2"/><path d="M3 10h18"/></svg>
|
|
57
|
+
</div>
|
|
58
|
+
</button>
|
|
59
|
+
|
|
60
|
+
@if (open()) {
|
|
61
|
+
<div
|
|
62
|
+
#dropdownEl
|
|
63
|
+
role="dialog"
|
|
64
|
+
aria-modal="true"
|
|
65
|
+
aria-label="Choose date range"
|
|
66
|
+
class="fixed z-50 rounded-md border border-border bg-popover text-popover-foreground shadow-lg animate-in fade-in-0 zoom-in-95"
|
|
67
|
+
>
|
|
68
|
+
<div class="flex flex-col sm:flex-row">
|
|
69
|
+
<!-- Presets sidebar -->
|
|
70
|
+
@if (presets().length > 0) {
|
|
71
|
+
<div class="border-b sm:border-b-0 sm:border-r border-border p-3 space-y-0.5 sm:min-w-[150px]">
|
|
72
|
+
<p class="px-3 py-1.5 text-xs font-semibold text-muted-foreground uppercase tracking-wider">Presets</p>
|
|
73
|
+
@for (preset of presets(); track preset.label) {
|
|
74
|
+
<button
|
|
75
|
+
type="button"
|
|
76
|
+
class="w-full text-left px-3 py-2 text-sm rounded-md hover:bg-accent hover:text-accent-foreground transition-colors cursor-pointer"
|
|
77
|
+
(mousedown)="selectPreset(preset); $event.preventDefault()"
|
|
78
|
+
>
|
|
79
|
+
{{ preset.label }}
|
|
80
|
+
</button>
|
|
81
|
+
}
|
|
82
|
+
</div>
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
<!-- Calendar(s) -->
|
|
86
|
+
<div class="flex flex-col sm:flex-row">
|
|
87
|
+
@if (dualCalendar()) {
|
|
88
|
+
<!-- Left calendar -->
|
|
89
|
+
<div class="p-1">
|
|
90
|
+
<div class="flex items-center justify-between px-3 py-2">
|
|
91
|
+
<button
|
|
92
|
+
type="button"
|
|
93
|
+
class="inline-flex items-center justify-center rounded-md h-8 w-8 hover:bg-accent hover:text-accent-foreground transition-colors"
|
|
94
|
+
(click)="prevMonth()"
|
|
95
|
+
aria-label="Previous month"
|
|
96
|
+
>
|
|
97
|
+
<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"><path d="m15 18-6-6 6-6"/></svg>
|
|
98
|
+
</button>
|
|
99
|
+
<span class="text-sm font-semibold tracking-tight">{{ leftMonthLabel() }}</span>
|
|
100
|
+
<div class="w-8"></div>
|
|
101
|
+
</div>
|
|
102
|
+
<sny-calendar
|
|
103
|
+
mode="range"
|
|
104
|
+
[(rangeValue)]="internalRange"
|
|
105
|
+
[min]="min()"
|
|
106
|
+
[max]="max()"
|
|
107
|
+
[locale]="locale()"
|
|
108
|
+
[showNavigation]="false"
|
|
109
|
+
[borderless]="true"
|
|
110
|
+
[initialViewDate]="leftViewDate()"
|
|
111
|
+
(rangeValueChange)="onRangeChanged($event)"
|
|
112
|
+
/>
|
|
113
|
+
</div>
|
|
114
|
+
<div class="border-t sm:border-t-0 sm:border-l border-border"></div>
|
|
115
|
+
<!-- Right calendar -->
|
|
116
|
+
<div class="p-1">
|
|
117
|
+
<div class="flex items-center justify-between px-3 py-2">
|
|
118
|
+
<div class="w-8"></div>
|
|
119
|
+
<span class="text-sm font-semibold tracking-tight">{{ rightMonthLabel() }}</span>
|
|
120
|
+
<button
|
|
121
|
+
type="button"
|
|
122
|
+
class="inline-flex items-center justify-center rounded-md h-8 w-8 hover:bg-accent hover:text-accent-foreground transition-colors"
|
|
123
|
+
(click)="nextMonth()"
|
|
124
|
+
aria-label="Next month"
|
|
125
|
+
>
|
|
126
|
+
<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"><path d="m9 18 6-6-6-6"/></svg>
|
|
127
|
+
</button>
|
|
128
|
+
</div>
|
|
129
|
+
<sny-calendar
|
|
130
|
+
mode="range"
|
|
131
|
+
[(rangeValue)]="internalRange"
|
|
132
|
+
[min]="min()"
|
|
133
|
+
[max]="max()"
|
|
134
|
+
[locale]="locale()"
|
|
135
|
+
[showNavigation]="false"
|
|
136
|
+
[borderless]="true"
|
|
137
|
+
[initialViewDate]="rightViewDate()"
|
|
138
|
+
(rangeValueChange)="onRangeChanged($event)"
|
|
139
|
+
/>
|
|
140
|
+
</div>
|
|
141
|
+
} @else {
|
|
142
|
+
<!-- Single calendar -->
|
|
143
|
+
<sny-calendar
|
|
144
|
+
mode="range"
|
|
145
|
+
[(rangeValue)]="internalRange"
|
|
146
|
+
[min]="min()"
|
|
147
|
+
[max]="max()"
|
|
148
|
+
[locale]="locale()"
|
|
149
|
+
(rangeValueChange)="onRangeChanged($event)"
|
|
150
|
+
/>
|
|
151
|
+
}
|
|
152
|
+
</div>
|
|
153
|
+
</div>
|
|
154
|
+
</div>
|
|
155
|
+
}
|
|
156
|
+
`,
|
|
157
|
+
})
|
|
158
|
+
export class SnyDateRangePickerComponent implements ControlValueAccessor, OnDestroy {
|
|
159
|
+
readonly value = model<DateRange | null>(null);
|
|
160
|
+
readonly placeholder = input('Pick a date range...');
|
|
161
|
+
readonly size = input<DatePickerSize>('md');
|
|
162
|
+
readonly locale = input('en-US');
|
|
163
|
+
readonly dateFormat = input<Intl.DateTimeFormatOptions>({
|
|
164
|
+
month: 'short',
|
|
165
|
+
day: 'numeric',
|
|
166
|
+
year: 'numeric',
|
|
167
|
+
});
|
|
168
|
+
readonly separator = input(' \u2014 ');
|
|
169
|
+
readonly dualCalendar = input(false);
|
|
170
|
+
readonly presets = input<DatePickerPreset[]>([]);
|
|
171
|
+
readonly min = input<Date | undefined>(undefined);
|
|
172
|
+
readonly max = input<Date | undefined>(undefined);
|
|
173
|
+
readonly clearable = input(true);
|
|
174
|
+
readonly disabled = input(false);
|
|
175
|
+
readonly class = input<string>('');
|
|
176
|
+
|
|
177
|
+
readonly open = signal(false);
|
|
178
|
+
readonly internalRange = signal<DateRange | null>(null);
|
|
179
|
+
readonly leftViewDate = signal(new Date());
|
|
180
|
+
|
|
181
|
+
private readonly _disabledByCva = signal(false);
|
|
182
|
+
protected readonly isDisabled = computed(() => this.disabled() || this._disabledByCva());
|
|
183
|
+
|
|
184
|
+
private readonly triggerRef = viewChild<ElementRef<HTMLButtonElement>>('triggerEl');
|
|
185
|
+
private readonly dropdownRef = viewChild<ElementRef<HTMLDivElement>>('dropdownEl');
|
|
186
|
+
private readonly elRef = inject(ElementRef);
|
|
187
|
+
|
|
188
|
+
private scrollHandler: (() => void) | null = null;
|
|
189
|
+
private resizeHandler: (() => void) | null = null;
|
|
190
|
+
|
|
191
|
+
private _onChange: (value: DateRange | null) => void = () => {};
|
|
192
|
+
protected onTouched: () => void = () => {};
|
|
193
|
+
|
|
194
|
+
// Computed
|
|
195
|
+
readonly rightViewDate = computed(() => {
|
|
196
|
+
const d = this.leftViewDate();
|
|
197
|
+
return new Date(d.getFullYear(), d.getMonth() + 1, 1);
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
readonly leftMonthLabel = computed(() =>
|
|
201
|
+
this.leftViewDate().toLocaleDateString(this.locale(), { month: 'long', year: 'numeric' })
|
|
202
|
+
);
|
|
203
|
+
|
|
204
|
+
readonly rightMonthLabel = computed(() =>
|
|
205
|
+
this.rightViewDate().toLocaleDateString(this.locale(), { month: 'long', year: 'numeric' })
|
|
206
|
+
);
|
|
207
|
+
|
|
208
|
+
readonly displayValue = computed(() => {
|
|
209
|
+
const r = this.value();
|
|
210
|
+
if (!r?.start) return '';
|
|
211
|
+
const fmt = (d: Date) => d.toLocaleDateString(this.locale(), this.dateFormat());
|
|
212
|
+
if (!r.end) return fmt(r.start) + this.separator() + '...';
|
|
213
|
+
return fmt(r.start) + this.separator() + fmt(r.end);
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
protected readonly triggerClass = computed(() =>
|
|
217
|
+
cn(datePickerTriggerVariants({ size: this.size() }), this.class())
|
|
218
|
+
);
|
|
219
|
+
|
|
220
|
+
// CVA
|
|
221
|
+
writeValue(val: DateRange | null): void {
|
|
222
|
+
this.value.set(val ?? null);
|
|
223
|
+
this.internalRange.set(val ?? null);
|
|
224
|
+
if (val?.start) {
|
|
225
|
+
this.leftViewDate.set(new Date(val.start.getFullYear(), val.start.getMonth(), 1));
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
registerOnChange(fn: (value: DateRange | null) => void): void {
|
|
230
|
+
this._onChange = fn;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
registerOnTouched(fn: () => void): void {
|
|
234
|
+
this.onTouched = fn;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
setDisabledState(isDisabled: boolean): void {
|
|
238
|
+
this._disabledByCva.set(isDisabled);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
// Actions
|
|
242
|
+
onRangeChanged(range: DateRange | null): void {
|
|
243
|
+
this.internalRange.set(range);
|
|
244
|
+
if (range?.start && range?.end) {
|
|
245
|
+
this.value.set(range);
|
|
246
|
+
this._onChange(range);
|
|
247
|
+
setTimeout(() => this.close(), 150);
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
selectPreset(preset: DatePickerPreset): void {
|
|
252
|
+
this.value.set(preset.range);
|
|
253
|
+
this.internalRange.set(preset.range);
|
|
254
|
+
this._onChange(preset.range);
|
|
255
|
+
this.close();
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
clear(event: Event): void {
|
|
259
|
+
event.stopPropagation();
|
|
260
|
+
this.value.set(null);
|
|
261
|
+
this.internalRange.set(null);
|
|
262
|
+
this._onChange(null);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
prevMonth(): void {
|
|
266
|
+
this.leftViewDate.update((d) => new Date(d.getFullYear(), d.getMonth() - 1, 1));
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
nextMonth(): void {
|
|
270
|
+
this.leftViewDate.update((d) => new Date(d.getFullYear(), d.getMonth() + 1, 1));
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
toggle(): void {
|
|
274
|
+
if (this.open()) {
|
|
275
|
+
this.close();
|
|
276
|
+
} else {
|
|
277
|
+
this.internalRange.set(this.value());
|
|
278
|
+
this.updateDropdownPosition();
|
|
279
|
+
this.open.set(true);
|
|
280
|
+
this.addGlobalListeners();
|
|
281
|
+
setTimeout(() => this.updateDropdownPosition());
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
close(): void {
|
|
286
|
+
this.open.set(false);
|
|
287
|
+
this.removeGlobalListeners();
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
// Positioning
|
|
291
|
+
private updateDropdownPosition(): void {
|
|
292
|
+
const trigger = this.triggerRef()?.nativeElement;
|
|
293
|
+
if (!trigger) return;
|
|
294
|
+
const rect = trigger.getBoundingClientRect();
|
|
295
|
+
const dropdown = this.dropdownRef()?.nativeElement;
|
|
296
|
+
if (dropdown) {
|
|
297
|
+
dropdown.style.top = `${rect.bottom + 4}px`;
|
|
298
|
+
dropdown.style.left = `${rect.left}px`;
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
private addGlobalListeners(): void {
|
|
303
|
+
this.removeGlobalListeners();
|
|
304
|
+
this.scrollHandler = () => {
|
|
305
|
+
requestAnimationFrame(() => this.updateDropdownPosition());
|
|
306
|
+
};
|
|
307
|
+
this.resizeHandler = () => {
|
|
308
|
+
requestAnimationFrame(() => this.updateDropdownPosition());
|
|
309
|
+
};
|
|
310
|
+
document.addEventListener('scroll', this.scrollHandler, { capture: true, passive: true });
|
|
311
|
+
window.addEventListener('resize', this.resizeHandler, { passive: true });
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
private removeGlobalListeners(): void {
|
|
315
|
+
if (this.scrollHandler) {
|
|
316
|
+
document.removeEventListener('scroll', this.scrollHandler, { capture: true } as EventListenerOptions);
|
|
317
|
+
this.scrollHandler = null;
|
|
318
|
+
}
|
|
319
|
+
if (this.resizeHandler) {
|
|
320
|
+
window.removeEventListener('resize', this.resizeHandler);
|
|
321
|
+
this.resizeHandler = null;
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
ngOnDestroy(): void {
|
|
326
|
+
this.removeGlobalListeners();
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
@HostListener('document:click', ['$event'])
|
|
330
|
+
onDocumentClick(event: MouseEvent): void {
|
|
331
|
+
if (!this.elRef.nativeElement.contains(event.target)) {
|
|
332
|
+
this.close();
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
@HostListener('keydown.escape')
|
|
337
|
+
onEscape(): void {
|
|
338
|
+
this.close();
|
|
339
|
+
}
|
|
340
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { SnyDateRangePickerComponent } from './date-range-picker.component';
|