Skip to content

Added option for initialDates #558

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

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
30 changes: 26 additions & 4 deletions src/daterangepicker/daterangepicker.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,9 @@ interface VisibleCalendar {
]
})
export class DaterangepickerComponent implements OnInit, OnChanges {
@Input()
initialDates: [string | Dayjs, string | Dayjs] = null;

@Input()
startDate = dayjs().utc(true).startOf('day');

Expand Down Expand Up @@ -405,9 +408,28 @@ export class DaterangepickerComponent implements OnInit, OnChanges {
}
}
this.locale.daysOfWeek = daysOfWeek;

// Initialize with initialDates if provided
let leftMonth: Dayjs;
let rightMonth: Dayjs;

if (this.initialDates && this.initialDates.length === 2) {
leftMonth = dayjs.isDayjs(this.initialDates[0]) ? this.initialDates[0].clone() : dayjs(this.initialDates[0]).utc(true);
rightMonth = dayjs.isDayjs(this.initialDates[1]) ? this.initialDates[1].clone() : dayjs(this.initialDates[1]).utc(true);
} else {
leftMonth = dayjs().utc(true);
rightMonth = leftMonth.clone().add(1, 'month');
}

// Set calendar view without selecting dates
if (!this.startDate || this.startDate.isSame(dayjs().utc(true).startOf('day'))) {
this.leftCalendar.month = leftMonth.clone();
this.rightCalendar.month = rightMonth.clone();
}

if (this.inline) {
this.cachedVersion.start = this.startDate.clone();
this.cachedVersion.end = this.endDate.clone();
this.cachedVersion.start = this.startDate?.clone();
this.cachedVersion.end = this.endDate?.clone();
}

if (this.startDate && this.timePicker) {
Expand Down Expand Up @@ -844,7 +866,7 @@ export class DaterangepickerComponent implements OnInit, OnChanges {
return;
}
if (this.startDate) {
// we want to stay on whatever months are in view if date range is set and both calendar sides have a month already. e.g. when
// we want to stay on whatever months are in view if date range is set and both calendar sides have a month already. e.g. when
// user clicks on the end date, we want to stay on current month in view
if (this.leftCalendar.month && this.rightCalendar.month) {
return;
Expand Down Expand Up @@ -1438,7 +1460,7 @@ export class DaterangepickerComponent implements OnInit, OnChanges {
const minute = parseInt(String(this.timepickerVariables[side].selectedMinute), 10);
const second = this.timePickerSeconds ? parseInt(String(this.timepickerVariables[side].selectedSecond), 10) : 0;
return date.clone().hour(hour).minute(minute).second(second);

}else{
return;
}
Expand Down
8 changes: 7 additions & 1 deletion src/daterangepicker/daterangepicker.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,16 @@ export class DaterangepickerDirective implements OnInit, OnChanges, DoCheck {
@Input()
timePickerSeconds = false;

@Input()
initialDates: [string | dayjs.Dayjs, string | dayjs.Dayjs];

@Input() closeOnAutoApply = true;
@Input()
private endKeyHolder: string;

public picker: DaterangepickerComponent;
private startKeyHolder: string;
private notForChangesProperty: Array<string> = ['locale', 'endKey', 'startKey'];
private notForChangesProperty: Array<string> = ['locale', 'endKey', 'startKey', 'initialDates'];
private onChangeFn = Function.prototype;
private onTouched = Function.prototype;
private validatorChange = Function.prototype;
Expand Down Expand Up @@ -290,6 +293,9 @@ export class DaterangepickerDirective implements OnInit, OnChanges, DoCheck {

// eslint-disable-next-line @angular-eslint/no-conflicting-lifecycle
ngOnInit(): void {
if (this.initialDates) {
this.picker.initialDates = this.initialDates;
}
this.picker.startDateChanged.asObservable().subscribe((itemChanged: StartDate) => {
this.startDateChanged.emit(itemChanged);
});
Expand Down