Skip to content

Commit efba47a

Browse files
authored
Merge branch 'master' into ibarakov/fix-7878-master
2 parents 76848da + 31993da commit efba47a

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

projects/igniteui-angular/src/lib/calendar/calendar-base.ts

+15-1
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,16 @@ export class IgxCalendarBaseDirective implements ControlValueAccessor {
156156
*/
157157
public set value(value: Date | Date[]) {
158158
if (!value || !!value && (value as Date[]).length === 0) {
159+
this.selectedDatesWithoutFocus = new Date();
159160
return;
160161
}
161-
162+
if (!this.selectedDatesWithoutFocus) {
163+
const valueDate = value[0] ? Math.min.apply(null, value) : value;
164+
const date = this.getDateOnly(new Date(valueDate)).setDate(1);
165+
this.viewDate = new Date(date);
166+
}
162167
this.selectDate(value);
168+
this.selectedDatesWithoutFocus = value;
163169
}
164170

165171
/**
@@ -175,6 +181,9 @@ export class IgxCalendarBaseDirective implements ControlValueAccessor {
175181
* Sets the date that will be presented in the default view when the component renders.
176182
*/
177183
public set viewDate(value: Date) {
184+
if (this._viewDate) {
185+
this.selectedDatesWithoutFocus = value;
186+
}
178187
const date = this.getDateOnly(value).setDate(1);
179188
this._viewDate = new Date(date);
180189
}
@@ -380,6 +389,11 @@ export class IgxCalendarBaseDirective implements ControlValueAccessor {
380389
*/
381390
public selectedDates;
382391

392+
/**
393+
* @hidden
394+
*/
395+
private selectedDatesWithoutFocus;
396+
383397
/**
384398
* @hidden
385399
*/

projects/igniteui-angular/src/lib/calendar/calendar.component.spec.ts

+33-1
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,8 @@ describe('IgxCalendar - ', () => {
174174
configureTestSuite();
175175
beforeAll(async(() => {
176176
TestBed.configureTestingModule({
177-
declarations: [IgxCalendarSampleComponent, IgxCalendarRangeComponent, IgxCalendarDisabledSpecialDatesComponent],
177+
declarations: [IgxCalendarSampleComponent, IgxCalendarRangeComponent, IgxCalendarDisabledSpecialDatesComponent,
178+
IgxCalendarValueComponent],
178179
imports: [IgxCalendarModule, FormsModule, NoopAnimationsModule]
179180
}).compileComponents();
180181
}));
@@ -291,6 +292,27 @@ describe('IgxCalendar - ', () => {
291292
expect(bodyMonth.nativeElement.textContent.trim()).toMatch('8');
292293
});
293294

295+
it('Should show right month when value is set', () => {
296+
fixture = TestBed.createComponent(IgxCalendarValueComponent);
297+
fixture.detectChanges();
298+
calendar = fixture.componentInstance.calendar;
299+
300+
expect(calendar.weekStart).toEqual(WEEKDAYS.SUNDAY);
301+
expect(calendar.selection).toEqual('single');
302+
expect(calendar.viewDate.getMonth()).toEqual(calendar.value.getMonth());
303+
304+
const date = new Date(2020, 8, 28);
305+
calendar.viewDate = date;
306+
fixture.detectChanges();
307+
308+
expect(calendar.viewDate.getMonth()).toEqual(date.getMonth());
309+
310+
calendar.value = new Date(2020, 9, 15);
311+
fixture.detectChanges();
312+
313+
expect(calendar.viewDate.getMonth()).toEqual(date.getMonth());
314+
});
315+
294316
it('Should properly set locale', () => {
295317
fixture.componentInstance.viewDate = new Date(2018, 8, 17);
296318
fixture.componentInstance.model = new Date();
@@ -1998,6 +2020,16 @@ export class IgxCalendarDisabledSpecialDatesComponent {
19982020
@ViewChild(IgxCalendarComponent, { static: true }) public calendar: IgxCalendarComponent;
19992021
}
20002022

2023+
@Component({
2024+
template: `
2025+
<igx-calendar [value]="value"></igx-calendar>
2026+
`
2027+
})
2028+
export class IgxCalendarValueComponent {
2029+
public value = new Date(2020, 7, 13);
2030+
@ViewChild(IgxCalendarComponent, { static: true }) public calendar: IgxCalendarComponent;
2031+
}
2032+
20012033
class DateTester {
20022034
// tests whether a date is disabled or not
20032035
static testDatesAvailability(dates: IgxDayItemComponent[], disabled: boolean) {

0 commit comments

Comments
 (0)