Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix: Datetime-picker should recalculate currently shown calendar view when selected date is not contained in current month #1197

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading