Skip to content

Commit a9d111a

Browse files
mustafashykhfetrarij
authored andcommitted
Added option for initialDates
1 parent c5482a5 commit a9d111a

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

src/daterangepicker/daterangepicker.component.ts

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,9 @@ interface VisibleCalendar {
157157
]
158158
})
159159
export class DaterangepickerComponent implements OnInit, OnChanges {
160+
@Input()
161+
initialDates: [string | Dayjs, string | Dayjs] = null;
162+
160163
@Input()
161164
startDate = dayjs().utc(true).startOf('day');
162165

@@ -413,9 +416,28 @@ export class DaterangepickerComponent implements OnInit, OnChanges {
413416
}
414417
}
415418
this.locale.daysOfWeek = daysOfWeek;
419+
420+
// Initialize with initialDates if provided
421+
let leftMonth: Dayjs;
422+
let rightMonth: Dayjs;
423+
424+
if (this.initialDates && this.initialDates.length === 2) {
425+
leftMonth = dayjs.isDayjs(this.initialDates[0]) ? this.initialDates[0].clone() : dayjs(this.initialDates[0]).utc(true);
426+
rightMonth = dayjs.isDayjs(this.initialDates[1]) ? this.initialDates[1].clone() : dayjs(this.initialDates[1]).utc(true);
427+
} else {
428+
leftMonth = dayjs().utc(true);
429+
rightMonth = leftMonth.clone().add(1, 'month');
430+
}
431+
432+
// Set calendar view without selecting dates
433+
if (!this.startDate || this.startDate.isSame(dayjs().utc(true).startOf('day'))) {
434+
this.leftCalendar.month = leftMonth.clone();
435+
this.rightCalendar.month = rightMonth.clone();
436+
}
437+
416438
if (this.inline) {
417-
this.cachedVersion.start = this.startDate.clone();
418-
this.cachedVersion.end = this.endDate.clone();
439+
this.cachedVersion.start = this.startDate?.clone();
440+
this.cachedVersion.end = this.endDate?.clone();
419441
}
420442

421443
if (this.startDate && this.timePicker) {
@@ -1455,7 +1477,7 @@ export class DaterangepickerComponent implements OnInit, OnChanges {
14551477
const minute = parseInt(String(this.timepickerVariables[side].selectedMinute), 10);
14561478
const second = this.timePickerSeconds ? parseInt(String(this.timepickerVariables[side].selectedSecond), 10) : 0;
14571479
return date.clone().hour(hour).minute(minute).second(second);
1458-
1480+
14591481
}else{
14601482
return;
14611483
}

src/daterangepicker/daterangepicker.directive.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,13 +146,16 @@ export class DaterangepickerDirective implements OnInit, OnChanges, DoCheck {
146146
@Input()
147147
timePickerSeconds = false;
148148

149+
@Input()
150+
initialDates: [string | dayjs.Dayjs, string | dayjs.Dayjs];
151+
149152
@Input() closeOnAutoApply = true;
150153
@Input()
151154
private endKeyHolder: string;
152155

153156
public picker: DaterangepickerComponent;
154157
private startKeyHolder: string;
155-
private notForChangesProperty: Array<string> = ['locale', 'endKey', 'startKey'];
158+
private notForChangesProperty: Array<string> = ['locale', 'endKey', 'startKey', 'initialDates'];
156159
private onChangeFn = Function.prototype;
157160
private onTouched = Function.prototype;
158161
private validatorChange = Function.prototype;
@@ -290,6 +293,9 @@ export class DaterangepickerDirective implements OnInit, OnChanges, DoCheck {
290293

291294
// eslint-disable-next-line @angular-eslint/no-conflicting-lifecycle
292295
ngOnInit(): void {
296+
if (this.initialDates) {
297+
this.picker.initialDates = this.initialDates;
298+
}
293299
this.picker.startDateChanged.asObservable().subscribe((itemChanged: StartDate) => {
294300
this.startDateChanged.emit(itemChanged);
295301
});

0 commit comments

Comments
 (0)