Skip to content

Commit 4a1e63a

Browse files
committed
feat(date-picker): add selectedDate property to highlight selected month and year in views
1 parent 3cb6c3a commit 4a1e63a

5 files changed

Lines changed: 49 additions & 28 deletions

File tree

src/components/date-picker/components/month-view.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export interface MonthViewConfig {
44
formatter: IDateFormatter;
55
locale: string;
66
viewDate: Date;
7+
selectedDate?: Date | null; // Add selectedDate to determine which month to highlight
78
}
89

910
export interface MonthViewCallbacks {
@@ -25,7 +26,13 @@ export class MonthView {
2526
const monthNames = this.getMonthNames('short');
2627
const currentYear = this.config.viewDate.getFullYear();
2728
const currentMonth = new Date().getFullYear() === currentYear ? new Date().getMonth() : -1;
28-
const selectedMonth = this.config.viewDate.getMonth();
29+
30+
// Determine which month to highlight as selected
31+
// This is the key change: only mark a month as selected if the selectedDate is in the current view year
32+
let selectedMonth = -1;
33+
if (this.config.selectedDate && this.config.selectedDate.getFullYear() === currentYear) {
34+
selectedMonth = this.config.selectedDate.getMonth();
35+
}
2936

3037
// Month grid is 4 rows x 3 columns
3138
const rows = 4;

src/components/date-picker/components/year-view.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export interface YearViewConfig {
22
viewDate: Date;
3+
selectedDate?: Date | null; // Add selectedDate property to decide what to highlight
34
}
45

56
export interface YearViewCallbacks {
@@ -25,12 +26,22 @@ export class YearView {
2526
const rows = 5;
2627
const cols = 3;
2728

29+
// Only highlight the year if it's in the current view range
30+
let yearToHighlight = -1;
31+
if (this.config.selectedDate) {
32+
const selectedYear = this.config.selectedDate.getFullYear();
33+
// Only highlight if the selected year is within the display range
34+
if (selectedYear >= startYear && selectedYear <= startYear + (rows * cols - 1)) {
35+
yearToHighlight = selectedYear;
36+
}
37+
}
38+
2839
for (let i = 0; i < rows; i++) {
2940
yearsContent += '<div class="date-picker-row" role="row">';
3041

3142
for (let j = 0; j < cols; j++) {
3243
const yearValue = startYear + i * cols + j;
33-
const isSelected = currentYear === yearValue;
44+
const isSelected = yearValue === yearToHighlight;
3445
const isCurrent = new Date().getFullYear() === yearValue;
3546

3647
let cellClass = 'date-picker-cell year-cell';

src/components/date-picker/date-picker.ts

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -516,27 +516,20 @@ export class DatePicker extends HTMLElement implements EventListenerObject {
516516

517517
// Use setTimeout to ensure animation is visible
518518
setTimeout(() => {
519-
// Perform the actual navigation
520-
const currentDate = this.stateService.viewDate;
521-
const newDate = new Date(currentDate);
522-
523519
if (direction === 'previous') {
524-
newDate.setMonth(currentDate.getMonth() - 1);
520+
this.stateService.navigateToPreviousPeriod();
525521
} else if (direction === 'next') {
526-
newDate.setMonth(currentDate.getMonth() + 1);
522+
this.stateService.navigateToNextPeriod();
527523
} else if (direction === 'previous-year') {
528-
newDate.setFullYear(currentDate.getFullYear() - 1);
524+
this.stateService.navigateToPreviousPeriod();
529525
} else if (direction === 'next-year') {
530-
newDate.setFullYear(currentDate.getFullYear() + 1);
526+
this.stateService.navigateToNextPeriod();
531527
} else if (direction === 'previous-decade') {
532-
newDate.setFullYear(currentDate.getFullYear() - 10);
528+
this.stateService.navigateToPreviousPeriod();
533529
} else if (direction === 'next-decade') {
534-
newDate.setFullYear(currentDate.getFullYear() + 10);
530+
this.stateService.navigateToNextPeriod();
535531
}
536532

537-
// Update state
538-
this.stateService.viewDate = newDate;
539-
540533
// Remove animation class after animation completes
541534
setTimeout(() => {
542535
this.calendarElement.classList.remove(animationClass);

src/components/date-picker/services/ui-updater.service.ts

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,11 @@ export class UIUpdaterService {
628628
monthsContainer.classList.add('date-picker-months-grid');
629629
monthsContainer.setAttribute('role', 'grid');
630630

631-
// Create months grid (3x4)
631+
// Current date for highlighting today's month if in current year
632+
const today = new Date();
633+
const isCurrentYear = today.getFullYear() === year;
634+
635+
// Create months grid (4x3)
632636
for (let row = 0; row < 4; row++) {
633637
const monthRow = document.createElement('div');
634638
monthRow.classList.add('date-picker-row');
@@ -639,8 +643,14 @@ export class UIUpdaterService {
639643
const monthElement = document.createElement('div');
640644
monthElement.classList.add('date-picker-cell', 'month-cell');
641645
monthElement.setAttribute('role', 'gridcell');
646+
monthElement.setAttribute('data-month', monthIndex.toString());
647+
648+
// Only add selected class if we're looking at the current month in the current view
649+
// This is the key fix: Only highlight the month if it's the selected month AND in the current year
650+
const currentDate = new Date();
651+
currentDate.setFullYear(year);
652+
currentDate.setMonth(monthIndex);
642653

643-
// Check if month is selected
644654
if (monthIndex === currentMonth) {
645655
monthElement.classList.add('selected');
646656
monthElement.setAttribute('tabindex', '0');
@@ -649,8 +659,7 @@ export class UIUpdaterService {
649659
}
650660

651661
// Check if it's current month in current year
652-
const today = new Date();
653-
if (today.getMonth() === monthIndex && today.getFullYear() === year) {
662+
if (isCurrentYear && today.getMonth() === monthIndex) {
654663
monthElement.classList.add('current');
655664
}
656665

@@ -687,8 +696,8 @@ export class UIUpdaterService {
687696
yearsContainer.classList.add('date-picker-years-grid');
688697
yearsContainer.setAttribute('role', 'grid');
689698

690-
// Track if the selected year is in the displayed range
691-
let selectedYearVisible = false;
699+
// Only highlight the year if it's within the current view range
700+
const shouldHighlight = currentYear >= startYear && currentYear <= endYear;
692701

693702
// Create years grid (4x3)
694703
let yearIndex = startYear;
@@ -703,13 +712,14 @@ export class UIUpdaterService {
703712
const yearElement = document.createElement('div');
704713
yearElement.classList.add('date-picker-cell', 'year-cell');
705714
yearElement.setAttribute('role', 'gridcell');
715+
yearElement.setAttribute('data-year', yearIndex.toString());
716+
717+
// Only highlight if the year is in the current view range
718+
const isSelected = shouldHighlight && (yearIndex === currentYear);
706719

707-
// Check if year is selected
708-
const isSelected = yearIndex === currentYear;
709720
if (isSelected) {
710721
yearElement.classList.add('selected');
711722
yearElement.setAttribute('tabindex', '0');
712-
selectedYearVisible = true;
713723
} else {
714724
yearElement.setAttribute('tabindex', '-1');
715725
}
@@ -729,8 +739,6 @@ export class UIUpdaterService {
729739
// Prevent the event from bubbling up which might cause the calendar to close
730740
e.stopPropagation();
731741
e.preventDefault();
732-
// Log which year was selected for debugging
733-
console.log(`Year selected: ${actualYear}`);
734742
onSelectYear(actualYear);
735743
});
736744

src/components/date-picker/services/ui.service.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,8 @@ export class UIService{
341341
{
342342
formatter: this.formatter,
343343
locale: this.state.locale,
344-
viewDate: this.state.viewDate
344+
viewDate: this.state.viewDate,
345+
selectedDate: this.state.selectedDate // Pass selected date to determine which month to highlight
345346
},
346347
{
347348
onMonthSelect: this.handleMonthSelect.bind(this)
@@ -357,7 +358,8 @@ export class UIService{
357358
private renderYearView(): void {
358359
const yearView = new YearView(
359360
{
360-
viewDate: this.state.viewDate
361+
viewDate: this.state.viewDate,
362+
selectedDate: this.state.selectedDate // Pass selected date to determine which year to highlight
361363
},
362364
{
363365
onYearSelect: this.handleYearSelect.bind(this)

0 commit comments

Comments
 (0)