|
| 1 | +import { |
| 2 | + Component, |
| 3 | + computed, |
| 4 | + inject, |
| 5 | + signal, |
| 6 | + Signal, |
| 7 | + untracked, |
| 8 | + viewChildren, |
| 9 | + WritableSignal, |
| 10 | +} from '@angular/core'; |
| 11 | +import { |
| 12 | + DateAdapter, |
| 13 | + MAT_DATE_FORMATS, |
| 14 | + MatDateFormats, |
| 15 | + provideNativeDateAdapter, |
| 16 | +} from '@angular/material/core'; |
| 17 | +import {Grid, GridRow, GridCell, GridCellWidget} from '@angular/aria/grid'; |
| 18 | + |
| 19 | +const DAYS_PER_WEEK = 7; |
| 20 | + |
| 21 | +interface CalendarCell<D = any> { |
| 22 | + displayName: string; |
| 23 | + ariaLabel: string; |
| 24 | + date: D; |
| 25 | + selected: WritableSignal<boolean>; |
| 26 | + day: number; |
| 27 | +} |
| 28 | + |
| 29 | +@Component({ |
| 30 | + selector: 'app-root', |
| 31 | + templateUrl: 'app.html', |
| 32 | + styleUrl: 'app.css', |
| 33 | + providers: [provideNativeDateAdapter()], |
| 34 | + imports: [Grid, GridRow, GridCell, GridCellWidget], |
| 35 | +}) |
| 36 | +export class App<D> { |
| 37 | + private readonly _dayButtons = viewChildren(GridCellWidget); |
| 38 | + private readonly _dateAdapter = inject<DateAdapter<D>>(DateAdapter, {optional: true})!; |
| 39 | + private readonly _dateFormats = inject<MatDateFormats>(MAT_DATE_FORMATS, {optional: true})!; |
| 40 | + private readonly _firstWeekOffset = computed(() => { |
| 41 | + const firstDayOfMonth = this._dateAdapter.createDate( |
| 42 | + this._dateAdapter.getYear(this.viewMonth()), |
| 43 | + this._dateAdapter.getMonth(this.viewMonth()), |
| 44 | + 1, |
| 45 | + ); |
| 46 | + |
| 47 | + return ( |
| 48 | + (DAYS_PER_WEEK + |
| 49 | + this._dateAdapter.getDayOfWeek(firstDayOfMonth) - |
| 50 | + this._dateAdapter.getFirstDayOfWeek()) % |
| 51 | + DAYS_PER_WEEK |
| 52 | + ); |
| 53 | + }); |
| 54 | + |
| 55 | + protected readonly monthYearLabel = computed(() => |
| 56 | + this._dateAdapter |
| 57 | + .format(this.viewMonth(), this._dateFormats.display.monthYearLabel) |
| 58 | + .toLocaleUpperCase(), |
| 59 | + ); |
| 60 | + |
| 61 | + protected readonly daysFromPrevMonth: Signal<number[]> = computed(() => { |
| 62 | + const prevMonthNumDays = this._dateAdapter.getNumDaysInMonth( |
| 63 | + this._dateAdapter.addCalendarMonths(this.viewMonth(), -1), |
| 64 | + ); |
| 65 | + |
| 66 | + const days: number[] = []; |
| 67 | + for (let i = this._firstWeekOffset() - 1; i >= 0; i--) { |
| 68 | + days.push(prevMonthNumDays - i); |
| 69 | + } |
| 70 | + return days; |
| 71 | + }); |
| 72 | + |
| 73 | + readonly weekdays: Signal<{long: string; narrow: string}[]> = computed(() => { |
| 74 | + const firstDayOfWeek = this._dateAdapter.getFirstDayOfWeek(); |
| 75 | + const narrowWeekdays = this._dateAdapter.getDayOfWeekNames('narrow'); |
| 76 | + const longWeekdays = this._dateAdapter.getDayOfWeekNames('long'); |
| 77 | + |
| 78 | + const weekdays = longWeekdays.map((long, i) => { |
| 79 | + return {long, narrow: narrowWeekdays[i]}; |
| 80 | + }); |
| 81 | + return weekdays.slice(firstDayOfWeek).concat(weekdays.slice(0, firstDayOfWeek)); |
| 82 | + }); |
| 83 | + |
| 84 | + /** The current selected date. */ |
| 85 | + readonly selectedDate: WritableSignal<D> = signal(this._dateAdapter.today()); |
| 86 | + |
| 87 | + /** The current display month. */ |
| 88 | + readonly viewMonth: WritableSignal<D> = signal(this.selectedDate()); |
| 89 | + |
| 90 | + /** Calendar day cells. */ |
| 91 | + readonly calendar = computed(() => { |
| 92 | + const month = this.viewMonth(); |
| 93 | + |
| 94 | + const daysInMonth = this._dateAdapter.getNumDaysInMonth(month); |
| 95 | + const dateNames = this._dateAdapter.getDateNames(); |
| 96 | + |
| 97 | + const calendar: CalendarCell[][] = [[]]; |
| 98 | + for (let i = 0, cell = this._firstWeekOffset(); i < daysInMonth; i++, cell++) { |
| 99 | + if (cell == DAYS_PER_WEEK) { |
| 100 | + calendar.push([]); |
| 101 | + cell = 0; |
| 102 | + } |
| 103 | + const date = this._dateAdapter.createDate( |
| 104 | + this._dateAdapter.getYear(month), |
| 105 | + this._dateAdapter.getMonth(month), |
| 106 | + i + 1, |
| 107 | + ); |
| 108 | + const ariaLabel = this._dateAdapter.format(date, this._dateFormats.display.dateA11yLabel); |
| 109 | + |
| 110 | + calendar[calendar.length - 1].push({ |
| 111 | + displayName: dateNames[i], |
| 112 | + ariaLabel, |
| 113 | + date, |
| 114 | + selected: signal( |
| 115 | + this._dateAdapter.compareDate( |
| 116 | + date, |
| 117 | + untracked(() => this.selectedDate()), |
| 118 | + ) === 0, |
| 119 | + ), |
| 120 | + day: i + 1, |
| 121 | + }); |
| 122 | + } |
| 123 | + |
| 124 | + return calendar; |
| 125 | + }); |
| 126 | + |
| 127 | + nextMonth(): void { |
| 128 | + this.viewMonth.set(this._dateAdapter.addCalendarMonths(this.viewMonth(), 1)); |
| 129 | + } |
| 130 | + |
| 131 | + prevMonth(): void { |
| 132 | + this.viewMonth.set(this._dateAdapter.addCalendarMonths(this.viewMonth(), -1)); |
| 133 | + } |
| 134 | + |
| 135 | + scrollDown(): void { |
| 136 | + this.nextMonth(); |
| 137 | + setTimeout(() => this._dayButtons()[0]?.element.focus()); |
| 138 | + } |
| 139 | + |
| 140 | + scrollUp(): void { |
| 141 | + this.prevMonth(); |
| 142 | + setTimeout(() => this._dayButtons()[this._dayButtons().length - 1]?.element.focus()); |
| 143 | + } |
| 144 | + |
| 145 | + onKeyDown(event: KeyboardEvent): void { |
| 146 | + const day = Number((event.target as Element).getAttribute('data-day')); |
| 147 | + if (!day) return; |
| 148 | + |
| 149 | + const viewMonthNumDays = this._dateAdapter.getNumDaysInMonth(this.viewMonth()); |
| 150 | + if (day > 7 && day <= viewMonthNumDays - 7) return; |
| 151 | + |
| 152 | + const arrowLeft = event.key === 'ArrowLeft'; |
| 153 | + const arrowRight = event.key === 'ArrowRight'; |
| 154 | + const arrowUp = event.key === 'ArrowUp'; |
| 155 | + const arrowDown = event.key === 'ArrowDown'; |
| 156 | + |
| 157 | + if ((day === 1 && arrowLeft) || (day <= 7 && arrowUp)) { |
| 158 | + this.scrollUp(); |
| 159 | + } |
| 160 | + |
| 161 | + if ((day === viewMonthNumDays && arrowRight) || (day > viewMonthNumDays - 7 && arrowDown)) { |
| 162 | + this.scrollDown(); |
| 163 | + } |
| 164 | + } |
| 165 | +} |
0 commit comments