Skip to content

Commit

Permalink
check if current month is the same as selected date
Browse files Browse the repository at this point in the history
  • Loading branch information
ardentia committed Mar 19, 2024
1 parent 8afdfa5 commit dde4930
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 24 deletions.
2 changes: 1 addition & 1 deletion projects/ng-datetime-picker/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sq-ui/ng-datetime-picker",
"version": "2.0.1",
"version": "2.0.4",
"license": "MIT",
"private": false,
"description": "Simple Quality UI Datetime-Picker for Angular",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ export class DatetimePickerComponent extends InputCoreComponent implements OnIni
this.selectedDates = List([now.clone()]);
this.weekdays = this.calendarManager.getWeekdays();
this.calendar = this.getMonthCalendar(now.clone());
this.initializeAuthorValuesIfAny();
}

ngAfterViewInit() {
Expand All @@ -80,6 +79,32 @@ export class DatetimePickerComponent extends InputCoreComponent implements OnIni
}
}

override writeValue(newValue: any): void {
super.writeValue(newValue);

if (this.selectedDates.size === 1 && this.selectedDates.get(0).isSame(moment(), 'day')) {
if (newValue) {
this.deselectAll();

if (Array.isArray(newValue)) {
newValue.forEach((date) => {
if (!this.currentMonth.isSame(moment(date), 'month')) {
this.calendar = this.getMonthCalendar(moment(date));
}
const convertedDate = this.calendarManager.findADateFromCalendar(moment(date), this.calendar);
this.markDateAsSelected(convertedDate);
});
} else {
if (!this.currentMonth.isSame(moment(newValue), 'month')) {
this.calendar = this.getMonthCalendar(moment(newValue));
}
const calendarDay = this.calendarManager.findADateFromCalendar(moment(newValue), this.calendar);
this.markDateAsSelected(calendarDay);
}
}
}
}

onDateClick(date: CalendarDay) {
switch (date.relativityToCurrentMonth) {
case CalendarPeriodRelativityEnum.After:
Expand Down Expand Up @@ -196,28 +221,6 @@ export class DatetimePickerComponent extends InputCoreComponent implements OnIni
this.dateSelectionChange.emit(this.value);
}

private initializeAuthorValuesIfAny() {
const subscription = this._modelToViewChange.subscribe((newValue) => {
if (this.selectedDates.size === 1 && this.selectedDates.get(0).isSame(moment(), 'day')) {
if (newValue) {
this.deselectAll();

if (Array.isArray(newValue)) {
newValue.forEach((date) => {
const convertedDate = this.calendarManager.findADateFromCalendar(moment(date), this.calendar);
this.markDateAsSelected(convertedDate);
});
} else {
const calendarDay = this.calendarManager.findADateFromCalendar(moment(newValue), this.calendar);
this.markDateAsSelected(calendarDay);
}
}
}

subscription.unsubscribe();
});
}

private markDateAsSelected(date: CalendarDay) {
const selectedMomentObj = moment(date.momentObj);
const selectedIndex = this.calendarManager.getSelectedItemIndex(selectedMomentObj, this.selectedDates.toArray());
Expand Down

0 comments on commit dde4930

Please sign in to comment.