Skip to content

Commit 545625a

Browse files
authored
Merge pull request #8069 from IgniteUI/ddincheva/monthPicker-9.1.x
igx-month-picker should emit onSelection only when the selected month is changed - 9.1.x
2 parents ee976ca + 55df97c commit 545625a

File tree

3 files changed

+77
-51
lines changed

3 files changed

+77
-51
lines changed

projects/igniteui-angular/src/lib/calendar/month-picker/month-picker.component.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<div *ngIf="isDefaultView" [@animateView]="activeView" class="igx-calendar__body" (swiperight)="previousYear()" (swipeleft)="nextYear()">
22
<div class="igx-calendar-picker">
3-
<div tabindex="0" class="igx-calendar-picker__prev" (click)="previousYear()" (keydown)="previousYearKB($event)" [ngStyle]="{
3+
<div tabindex="0" class="igx-calendar-picker__prev" (click)="previousYear()" (keydown)="changeYearKB($event, false)" [ngStyle]="{
44
'min-width.%': 25,
55
'left': 0
66
}">
@@ -11,7 +11,7 @@
1111
{{ formattedYear(viewDate) }}
1212
</span>
1313
</div>
14-
<div tabindex="0" class="igx-calendar-picker__next" (click)="nextYear()" (keydown)="nextYearKB($event)" [ngStyle]="{
14+
<div tabindex="0" class="igx-calendar-picker__next" (click)="nextYear()" (keydown)="changeYearKB($event)" [ngStyle]="{
1515
'min-width.%': 25,
1616
'right': 0
1717
}">

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

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,73 @@ describe('IgxMonthPicker', () => {
275275
expect(yearBtn.nativeElement.textContent.trim()).toMatch('2021');
276276
});
277277

278+
it('should not emit onSelection when navigating to the next year', () => {
279+
const fixture = TestBed.createComponent(IgxMonthPickerSampleComponent);
280+
fixture.detectChanges();
281+
282+
const dom = fixture.debugElement;
283+
const monthPicker = fixture.componentInstance.monthPicker;
284+
spyOn(monthPicker.onSelection, 'emit').and.callThrough();
285+
286+
const next = dom.query(By.css('.igx-calendar-picker__next'));
287+
let yearBtn = dom.query(By.css('.igx-calendar-picker__date'));
288+
expect(yearBtn.nativeElement.textContent.trim()).toMatch('2019');
289+
290+
UIInteractions.simulateClickEvent(next.nativeElement);
291+
fixture.detectChanges();
292+
UIInteractions.triggerKeyDownEvtUponElem('Enter', next.nativeElement);
293+
fixture.detectChanges();
294+
295+
expect(monthPicker.onSelection.emit).toHaveBeenCalledTimes(0);
296+
yearBtn = dom.query(By.css('.igx-calendar-picker__date'));
297+
expect(yearBtn.nativeElement.textContent.trim()).toMatch('2021');
298+
});
299+
300+
it('should not emit onSelection when navigating to the previous year', () => {
301+
const fixture = TestBed.createComponent(IgxMonthPickerSampleComponent);
302+
fixture.detectChanges();
303+
304+
const dom = fixture.debugElement;
305+
const monthPicker = fixture.componentInstance.monthPicker;
306+
spyOn(monthPicker.onSelection, 'emit').and.callThrough();
307+
308+
const prev = dom.query(By.css('.igx-calendar-picker__prev'));
309+
let yearBtn = dom.query(By.css('.igx-calendar-picker__date'));
310+
expect(yearBtn.nativeElement.textContent.trim()).toMatch('2019');
311+
312+
UIInteractions.triggerKeyDownEvtUponElem('Enter', prev.nativeElement);
313+
fixture.detectChanges();
314+
UIInteractions.simulateClickEvent(prev.nativeElement);
315+
fixture.detectChanges();
316+
317+
expect(monthPicker.onSelection.emit).toHaveBeenCalledTimes(0);
318+
yearBtn = dom.query(By.css('.igx-calendar-picker__date'));
319+
expect(yearBtn.nativeElement.textContent.trim()).toMatch('2017');
320+
});
321+
322+
it('should not emit onSelection when changing the year', () => {
323+
const fixture = TestBed.createComponent(IgxMonthPickerSampleComponent);
324+
fixture.detectChanges();
325+
326+
const dom = fixture.debugElement;
327+
const monthPicker = fixture.componentInstance.monthPicker;
328+
spyOn(monthPicker.onSelection, 'emit').and.callThrough();
329+
330+
let yearBtn = dom.query(By.css('.igx-calendar-picker__date'));
331+
expect(yearBtn.nativeElement.textContent.trim()).toMatch('2019');
332+
333+
UIInteractions.simulateClickEvent(yearBtn.nativeElement);
334+
fixture.detectChanges();
335+
336+
const year = dom.nativeElement.querySelector('.igx-calendar__year');
337+
UIInteractions.simulateClickEvent(year);
338+
fixture.detectChanges();
339+
340+
expect(monthPicker.onSelection.emit).toHaveBeenCalledTimes(0);
341+
yearBtn = dom.query(By.css('.igx-calendar-picker__date'));
342+
expect(yearBtn.nativeElement.textContent.trim()).toMatch('2016');
343+
});
344+
278345
it('should open years view, navigate through and select an year via KB.', () => {
279346
const fixture = TestBed.createComponent(IgxMonthPickerSampleComponent);
280347
fixture.detectChanges();

projects/igniteui-angular/src/lib/calendar/month-picker/month-picker.component.ts

Lines changed: 8 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,11 @@ export class IgxMonthPickerComponent extends IgxMonthPickerBaseDirective {
110110
super.activeViewDecadeKB(event);
111111

112112
if (event.key === KEYS.RIGHT_ARROW || event.key === KEYS.RIGHT_ARROW_IE) {
113-
event.preventDefault();
114-
this.nextYear();
113+
this.nextYear(event);
115114
}
116115

117116
if (event.key === KEYS.LEFT_ARROW || event.key === KEYS.LEFT_ARROW_IE) {
118-
event.preventDefault();
119-
this.previousYear();
117+
this.previousYear(event);
120118
}
121119

122120
requestAnimationFrame(() => {
@@ -138,46 +136,10 @@ export class IgxMonthPickerComponent extends IgxMonthPickerBaseDirective {
138136
/**
139137
* @hidden
140138
*/
141-
public nextYear() {
142-
this.yearAction = 'next';
143-
this.viewDate = this.calendarModel.getNextYear(this.viewDate);
144-
145-
this.selectDate(this.viewDate);
146-
this.onSelection.emit(this.selectedDates);
147-
}
148-
149-
/**
150-
* @hidden
151-
*/
152-
public nextYearKB(event) {
139+
public changeYearKB(event, next = true) {
153140
if (event.key === KEYS.SPACE || event.key === KEYS.SPACE_IE || event.key === KEYS.ENTER) {
154-
event.preventDefault();
155141
event.stopPropagation();
156-
157-
this.nextYear();
158-
}
159-
}
160-
161-
/**
162-
* @hidden
163-
*/
164-
public previousYear() {
165-
this.yearAction = 'prev';
166-
this.viewDate = this.calendarModel.getPrevYear(this.viewDate);
167-
168-
this.selectDate(this.viewDate);
169-
this.onSelection.emit(this.selectedDates);
170-
}
171-
172-
/**
173-
* @hidden
174-
*/
175-
public previousYearKB(event) {
176-
if (event.key === KEYS.SPACE || event.key === KEYS.SPACE_IE || event.key === KEYS.ENTER) {
177-
event.preventDefault();
178-
event.stopPropagation();
179-
180-
this.previousYear();
142+
next ? this.nextYear(event) : this.previousYear(event);
181143
}
182144
}
183145

@@ -188,9 +150,6 @@ export class IgxMonthPickerComponent extends IgxMonthPickerBaseDirective {
188150
this.viewDate = new Date(event.getFullYear(), event.getMonth(), event.getDate());
189151
this.activeView = CalendarView.DEFAULT;
190152

191-
this.selectDate(event);
192-
this.onSelection.emit(this.selectedDates);
193-
194153
requestAnimationFrame(() => {
195154
if (this.yearsBtn) { this.yearsBtn.nativeElement.focus(); }
196155
});
@@ -235,8 +194,8 @@ export class IgxMonthPickerComponent extends IgxMonthPickerBaseDirective {
235194
* @hidden
236195
*/
237196
@HostListener('keydown.pageup', ['$event'])
238-
public onKeydownPageUp(event: KeyboardEvent) {
239-
event.preventDefault();
197+
public previousYear(event?: KeyboardEvent) {
198+
event?.preventDefault();
240199
this.yearAction = 'prev';
241200
this.viewDate = this.calendarModel.getPrevYear(this.viewDate);
242201
}
@@ -245,8 +204,8 @@ export class IgxMonthPickerComponent extends IgxMonthPickerBaseDirective {
245204
* @hidden
246205
*/
247206
@HostListener('keydown.pagedown', ['$event'])
248-
public onKeydownPageDown(event: KeyboardEvent) {
249-
event.preventDefault();
207+
public nextYear(event?: KeyboardEvent) {
208+
event?.preventDefault();
250209
this.yearAction = 'next';
251210
this.viewDate = this.calendarModel.getNextYear(this.viewDate);
252211
}

0 commit comments

Comments
 (0)