I'll help you create a comprehensive date picker component in Angular that meets all your requirements. This will be a complete implementation with all the functionality you've specified.
// datepicker.component.ts
import { Component, input, output, signal, computed, forwardRef, inject, DestroyRef, effect } from '@angular/core';
import { CommonModule } from '@angular/common';
import { ControlValueAccessor, NG_VALUE_ACCESSOR, FormControl, ReactiveFormsModule } from '@angular/forms';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
interface DateRange {
start: Date | null;
end: Date | null;
}
interface PresetRange {
label: string;
getValue: () => DateRange;
}
@Component({
selector: 'gsd-datepicker',
standalone: true,
imports: [CommonModule, ReactiveFormsModule],
providers: [
{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => DatepickerComponent),
multi: true
}
],
template: `
<div class="datepicker-container" [class.open]="showCalendar()">
<div class="datepicker-input" (click)="toggleCalendar()">
<svg class="icon-calendar" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor">
<rect x="3" y="4" width="18" height="18" rx="2" ry="2"></rect>
<line x1="16" y1="2" x2="16" y2="6"></line>
<line x1="8" y1="2" x2="8" y2="6"></line>
<line x1="3" y1="10" x2="21" y2="10"></line>
</svg>
<input
type="text"
[value]="displayValue()"
readonly
class="input-field"
[disabled]="disabled"
/>
<svg class="icon-chevron" [class.rotated]="showCalendar()" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor">
<polyline points="6 9 12 15 18 9"></polyline>
</svg>
</div>
@if (showCalendar()) {
<div class="datepicker-dropdown" (click)="$event.stopPropagation()">
<div class="datepicker-content">
<div class="sidebar">
@for (preset of presetRanges; track preset.label) {
<button
class="preset-button"
[class.active]="isPresetActive(preset)"
(click)="selectPreset(preset)"
>
{{ preset.label }}
</button>
}
</div>
<div class="calendar-section">
<div class="calendar-header">
<button
class="nav-button"
(click)="previousMonth()"
[disabled]="!canNavigatePrevious()"
>
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor">
<polyline points="15 18 9 12 15 6"></polyline>
</svg>
</button>
<div class="month-year-selector">
<select class="month-select" [(ngModel)]="currentMonth" (change)="updateCalendar()">
@for (month of availableMonths(); track month.value) {
<option [value]="month.value">{{ month.label }}</option>
}
</select>
<select class="year-select" [(ngModel)]="currentYear" (change)="updateCalendar()">
@for (year of availableYears(); track year) {
<option [value]="year">{{ year }}</option>
}
</select>
</div>
<button
class="nav-button"
(click)="nextMonth()"
[disabled]="!canNavigateNext()"
>
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor">
<polyline points="9 18 15 12 9 6"></polyline>
</svg>
</button>
</div>
<div class="calendar-grid">
<div class="weekday-header">
@for (day of weekDays; track day) {
<div class="weekday">{{ day }}</div>
}
</div>
<div class="days-grid">
@for (week of calendarDays(); track $index) {
@for (day of week; track day.date) {
<button
class="day-cell"
[class.other-month]="!day.isCurrentMonth"
[class.weekend]="day.isWeekend"
[class.today]="day.isToday"
[class.selected]="isSelected(day.date)"
[class.in-range]="isInRange(day.date)"
[class.range-start]="isRangeStart(day.date)"
[class.range-end]="isRangeEnd(day.date)"
[class.hover-range]="isInHoverRange(day.date)"
[disabled]="!isDateSelectable(day.date)"
(click)="selectDate(day.date)"
(mouseenter)="onDateHover(day.date)"
(mouseleave)="onDateLeave()"
>
{{ day.day }}
</button>
}
}
</div>
</div>
<div class="calendar-footer">
<button class="btn-reset" (click)="reset()">Reset</button>
<div class="footer-actions">
<button class="btn-cancel" (click)="cancel()">Cancel</button>
<button class="btn-save" (click)="save()">Save</button>
</div>
</div>
</div>
</div>
</div>
}
</div>
`,
styles: [`
.datepicker-container {
position: relative;
width: 100%;
}
.datepicker-input {
display: flex;
align-items: center;
gap: 8px;
padding: 8px 12px;
border: 1px solid #ddd;
border-radius: 4px;
background: white;
cursor: pointer;
transition: all 0.2s;
}
.datepicker-container.open .datepicker-input {
border-color: #3b82f6;
box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
}
.datepicker-input:hover {
border-color: #bbb;
}
.input-field {
flex: 1;
border: none;
outline: none;
background: transparent;
cursor: pointer;
font-size: 14px;
}
.icon-calendar, .icon-chevron {
color: #666;
flex-shrink: 0;
}
.icon-chevron {
transition: transform 0.2s;
}
.icon-chevron.rotated {
transform: rotate(180deg);
}
.datepicker-dropdown {
position: absolute;
top: 100%;
left: 0;
margin-top: 4px;
background: white;
border: 1px solid #ddd;
border-radius: 8px;
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
z-index: 1000;
}
.datepicker-content {
display: flex;
min-width: 600px;
}
.sidebar {
width: 160px;
padding: 16px;
border-right: 1px solid #e5e7eb;
display: flex;
flex-direction: column;
gap: 4px;
}
.preset-button {
padding: 8px 12px;
text-align: left;
border: none;
background: transparent;
cursor: pointer;
border-radius: 4px;
font-size: 14px;
transition: all 0.2s;
}
.preset-button:hover {
background: #f3f4f6;
}
.preset-button.active {
background: #3b82f6;
color: white;
}
.calendar-section {
flex: 1;
padding: 16px;
}
.calendar-header {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 16px;
}
.nav-button {
padding: 4px;
border: none;
background: transparent;
cursor: pointer;
border-radius: 4px;
transition: background 0.2s;
}
.nav-button:hover:not(:disabled) {
background: #f3f4f6;
}
.nav-button:disabled {
opacity: 0.3;
cursor: not-allowed;
}
.month-year-selector {
display: flex;
gap: 8px;
}
.month-select, .year-select {
padding: 4px 8px;
border: 1px solid #ddd;
border-radius: 4px;
background: white;
cursor: pointer;
font-size: 14px;
}
.calendar-grid {
margin-bottom: 16px;
}
.weekday-header {
display: grid;
grid-template-columns: repeat(7, 1fr);
gap: 4px;
margin-bottom: 8px;
}
.weekday {
text-align: center;
font-size: 12px;
font-weight: 500;
color: #666;
padding: 4px;
}
.days-grid {
display: grid;
grid-template-columns: repeat(7, 1fr);
gap: 4px;
}
.day-cell {
aspect-ratio: 1;
border: none;
background: transparent;
cursor: pointer;
border-radius: 4px;
font-size: 14px;
transition: all 0.2s;
position: relative;
}
.day-cell:hover:not(:disabled) {
background: #f3f4f6;
}
.day-cell.other-month {
color: #d1d5db;
}
.day-cell.weekend {
color: #9ca3af;
}
.day-cell.today {
color: #3b82f6;
font-weight: 600;
}
.day-cell.selected {
background: #3b82f6;
color: white;
}
.day-cell.in-range, .day-cell.hover-range {
background: #dbeafe;
color: #1e293b;
}
.day-cell.range-start {
border-top-right-radius: 0;
border-bottom-right-radius: 0;
}
.day-cell.range-end {
border-top-left-radius: 0;
border-bottom-left-radius: 0;
}
.day-cell:disabled {
opacity: 0.3;
cursor: not-allowed;
}
.calendar-footer {
display: flex;
justify-content: space-between;
align-items: center;
padding-top: 16px;
border-top: 1px solid #e5e7eb;
}
.footer-actions {
display: flex;
gap: 8px;
}
.btn-reset, .btn-cancel, .btn-save {
padding: 8px 16px;
border: none;
border-radius: 4px;
font-size: 14px;
cursor: pointer;
transition: all 0.2s;
}
.btn-reset {
background: transparent;
color: #666;
}
.btn-reset:hover {
background: #f3f4f6;
}
.btn-cancel {
background: #f3f4f6;
color: #1e293b;
}
.btn-cancel:hover {
background: #e5e7eb;
}
.btn-save {
background: #3b82f6;
color: white;
}
.btn-save:hover {
background: #2563eb;
}
`]
})
export class DatepickerComponent implements ControlValueAccessor {
private destroyRef = inject(DestroyRef);
// Inputs
public rangeMode = input<boolean>(false);
public minDate = input<Date | null>(null);
public maxDate = input<Date | null>(null);
// Outputs
public dateChange = output<Date | DateRange | null>();
public rangeChange = output<DateRange | null>();
// Internal state
protected showCalendar = signal(false);
protected currentMonth = new Date().getMonth();
protected currentYear = new Date().getFullYear();
protected selectedDate = signal<Date | null>(null);
protected selectedRange = signal<DateRange>({ start: null, end: null });
protected tempRange = signal<DateRange>({ start: null, end: null });
protected hoverDate = signal<Date | null>(null);
protected previousValue: Date | DateRange | null = null;
// Form control
protected disabled = false;
private onChange: (value: any) => void = () => {};
private onTouched: () => void = () => {};
protected weekDays = ['Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa', 'Su'];
protected presetRanges: PresetRange[] = [
{
label: 'Today',
getValue: () => {
const today = new Date();
today.setHours(0, 0, 0, 0);
return { start: today, end: today };
}
},
{
label: 'Yesterday',
getValue: () => {
const yesterday = new Date();
yesterday.setDate(yesterday.getDate() - 1);
yesterday.setHours(0, 0, 0, 0);
return { start: yesterday, end: yesterday };
}
},
{
label: 'Last week',
getValue: () => {
const end = new Date();
const start = new Date();
start.setDate(start.getDate() - 7);
start.setHours(0, 0, 0, 0);
end.setHours(23, 59, 59, 999);
return { start, end };
}
},
{
label: 'Last 7 days',
getValue: () => {
const end = new Date();
const start = new Date();
start.setDate(start.getDate() - 6);
start.setHours(0, 0, 0, 0);
end.setHours(23, 59, 59, 999);
return { start, end };
}
},
{
label: 'This month',
getValue: () => {
const now = new Date();
const start = new Date(now.getFullYear(), now.getMonth(), 1);
const end = new Date(now.getFullYear(), now.getMonth() + 1, 0);
end.setHours(23, 59, 59, 999);
return { start, end };
}
},
{
label: 'Last 30 days',
getValue: () => {
const end = new Date();
const start = new Date();
start.setDate(start.getDate() - 29);
start.setHours(0, 0, 0, 0);
end.setHours(23, 59, 59, 999);
return { start, end };
}
},
{
label: 'Custom Range',
getValue: () => ({ start: null, end: null })
}
];
protected displayValue = computed(() => {
if (this.rangeMode()) {
const range = this.selectedRange();
if (range.start && range.end) {
return `${this.formatDate(range.start)} - ${this.formatDate(range.end)}`;
}
return '';
} else {
const date = this.selectedDate();
return date ? this.formatDate(date) : '';
}
});
protected calendarDays = computed(() => {
const year = this.currentYear;
const month = this.currentMonth;
const firstDay = new Date(year, month, 1);
const lastDay = new Date(year, month + 1, 0);
const startDate = new Date(firstDay);
const startDay = startDate.getDay() || 7;
startDate.setDate(startDate.getDate() - startDay + 1);
const days = [];
const today = new Date();
today.setHours(0, 0, 0, 0);
for (let week = 0; week < 6; week++) {
const weekDays = [];
for (let day = 0; day < 7; day++) {
const date = new Date(startDate);
const isWeekend = date.getDay() === 0 || date.getDay() === 6;
const isCurrentMonth = date.getMonth() === month;
const isToday = date.getTime() === today.getTime();
weekDays.push({
date,
day: date.getDate(),
isCurrentMonth,
isWeekend,
isToday
});
startDate.setDate(startDate.getDate() + 1);
}
days.push(weekDays);
}
return days;
});
protected availableMonths = computed(() => {
const months = [
{ value: 0, label: 'January' },
{ value: 1, label: 'February' },
{ value: 2, label: 'March' },
{ value: 3, label: 'April' },
{ value: 4, label: 'May' },
{ value: 5, label: 'June' },
{ value: 6, label: 'July' },
{ value: 7, label: 'August' },
{ value: 8, label: 'September' },
{ value: 9, label: 'October' },
{ value: 10, label: 'November' },
{ value: 11, label: 'December' }
];
const minDate = this.minDate();
const maxDate = this.maxDate();
return months.filter(month => {
const monthStart = new Date(this.currentYear, month.value, 1);
const monthEnd = new Date(this.currentYear, month.value + 1, 0);
if (minDate && monthEnd < minDate) return false;
if (maxDate && monthStart > maxDate) return false;
return true;
});
});
protected availableYears = computed(() => {
const currentYear = new Date().getFullYear();
const minDate = this.minDate();
const maxDate = this.maxDate();
const minYear = minDate ? minDate.getFullYear() : currentYear - 100;
const maxYear = maxDate ? maxDate.getFullYear() : currentYear + 100;
const years = [];
for (let year = minYear; year <= maxYear; year++) {
years.push(year);
}
return years;
});
// ControlValueAccessor implementation
writeValue(value: Date | DateRange | null): void {
if (this.rangeMode() && value && 'start' in value) {
this.selectedRange.set(value as DateRange);
this.tempRange.set(value as DateRange);
} else if (!this.rangeMode() && value instanceof Date) {
this.selectedDate.set(value);
}
}
registerOnChange(fn: any): void {
this.onChange = fn;
}
registerOnTouched(fn: any): void {
this.onTouched = fn;
}
setDisabledState(isDisabled: boolean): void {
this.disabled = isDisabled;
}
// Public methods
protected toggleCalendar(): void {
if (this.disabled) return;
if (!this.showCalendar()) {
this.previousValue = this.rangeMode()
? { ...this.selectedRange() }
: this.selectedDate();
}
this.showCalendar.update(show => !show);
this.onTouched();
}
protected selectDate(date: Date): void {
if (!this.isDateSelectable(date)) return;
if (this.rangeMode()) {
const range = this.tempRange();
if (!range.start || (range.start && range.end)) {
this.tempRange.set({ start: date, end: null });
} else {
if (date < range.start) {
this.tempRange.set({ start: date, end: range.start });
} else {
this.tempRange.set({ start: range.start, end: date });
}
}
} else {
this.selectedDate.set(date);
this.onChange(date);
this.dateChange.emit(date);
this.showCalendar.set(false);
}
}
protected selectPreset(preset: PresetRange): void {
const range = preset.getValue();
if (preset.label === 'Custom Range') {
this.tempRange.set({ start: null, end: null });
} else {
this.tempRange.set(range);
}
}
protected isPresetActive(preset: PresetRange): boolean {
const range = this.tempRange();
if (!range.start || !range.end) return false;
if (preset.label === 'Custom Range') {
return !this.presetRanges.slice(0, -1).some(p => {
const presetRange = p.getValue();
return this.datesEqual(range.start, presetRange.start) &&
this.datesEqual(range.end, presetRange.end);
});
}
const presetRange = preset.getValue();
return this.datesEqual(range.start, presetRange.start) &&
this.datesEqual(range.end, presetRange.end);
}
protected reset(): void {
if (this.rangeMode()) {
this.tempRange.set(this.previousValue as DateRange || { start: null, end: null });
} else {
this.selectedDate.set(this.previousValue as Date || null);
}
}
protected cancel(): void {
this.reset();
this.showCalendar.set(false);
}
protected save(): void {
if (this.rangeMode()) {
const range = this.tempRange();
this.selectedRange.set(range);
this.onChange(range);
this.rangeChange.emit(range);
this.dateChange.emit(range);
}
this.showCalendar.set(false);
}
protected onDateHover(date: Date): void {
if (this.rangeMode() && this.tempRange().start && !this.tempRange().end) {
this.hoverDate.set(date);
}
}
protected onDateLeave(): void {
this.hoverDate.set(null);
}
protected isSelected(date: Date): boolean {
if (this.rangeMode()) {
const range = this.tempRange();
return (range.start && this.isSameDay(date, range.start)) ||
(range.end && this.isSameDay(date, range.end));
} else {
return this.selectedDate() ? this.isSameDay(date, this.selectedDate()!) : false;
}
}
protected isInRange(date: Date): boolean {
if (!this.rangeMode()) return false;
const range = this.tempRange();
if (!range.start || !range.end) return false;
return date > range.start && date < range.end;
}
protected isInHoverRange(date: Date): boolean {
if (!this.rangeMode() || !this.hoverDate()) return false;
const range = this.tempRange();
if (!range.start || range.end) return false;
const hoverDate = this.hoverDate()!;
const start = range.start;
if (hoverDate < start) {
return date > hoverDate && date < start;
} else {
return date > start && date < hoverDate;
}
}
protected isRangeStart(date: Date): boolean {
if (!this.rangeMode()) return false;
const range = this.tempRange();
return range.start ? this.isSameDay(date, range.start) : false;
}
protected isRangeEnd(date: Date): boolean {
if (!this.rangeMode()) return false;
const range = this.tempRange();
return range.end ? this.isSameDay(date, range.end) : false;
}
protected isDateSelectable(date: Date): boolean {
const minDate = this.minDate();
const maxDate = this.maxDate();
if (minDate && date < minDate) return false;
if (maxDate && date > maxDate) return false;
return true;
}
protected canNavigatePrevious(): boolean {
const minDate = this.minDate();
if (!minDate) return true;
const prevMonth = new Date(this.currentYear, this.currentMonth - 1, 1);
return prevMonth >= minDate;
}
protected canNavigateNext(): boolean {
const maxDate = this.maxDate();
if (!maxDate) return true;
const nextMonth = new Date(this.currentYear, this.currentMonth + 1, 0);
return nextMonth <= maxDate;
}
protected previousMonth(): void {
if (!this.canNavigatePrevious()) return;
if (this.currentMonth === 0) {
this.currentMonth = 11;
this.currentYear--;
} else {
this.currentMonth--;
}
}
protected nextMonth(): void {
if (!this.canNavigateNext()) return;
if (this.currentMonth === 11) {
this.currentMonth = 0;
this.currentYear++;
} else {
this.currentMonth++;
}
}
protected updateCalendar(): void {
// Calendar will auto-update via computed signal
}
// Helper methods
private formatDate(date: Date): string {
const day = date.getDate().toString().padStart(2, '0');
const month = (date.getMonth() + 1).toString().padStart(2, '0');
const year = date.getFullYear();
return `${day}-${month}-${year}`;
}
private isSameDay(date1: Date, date2: Date): boolean {
return date1.getFullYear() === date2.getFullYear() &&
date1.getMonth() === date2.getMonth() &&
date1.getDate() === date2.getDate();
}
private datesEqual(date1: Date | null, date2: Date | null): boolean {
if (!date1 || !date2) return false;
return this.isSameDay(date1, date2);
}
}