Skip to content

Commit 191013c

Browse files
Ensure hours stay clamped within 0-23 when interacting with AM/PM - 18.2.x (#15369)
1 parent f9afd2a commit 191013c

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

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

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ const CSS_CLASS_SECONDSLIST = '.igx-time-picker__secondsList';
3636
const CSS_CLASS_AMPMLIST = 'igx-time-picker__ampmList';
3737
const CSS_CLASS_SELECTED_ITEM = '.igx-time-picker__item--selected';
3838
const CSS_CLASS_HEADER_HOUR = '.igx-time-picker__header-hour';
39-
const CSS_CLASS_OVERLAY = 'igx-overlay';
4039
const CSS_CLASS_OVERLAY_WRAPPER = 'igx-overlay__wrapper';
4140
const TIME_PICKER_TOGGLE_ICON = 'access_time';
4241
const TIME_PICKER_CLEAR_ICON = 'clear';
@@ -937,7 +936,6 @@ describe('IgxTimePicker', () => {
937936
expect(document.activeElement.children[3].innerHTML.trim()).toBe('10');
938937
});
939938

940-
941939
it('should navigate through items with arrow keys', () => {
942940
timePicker.itemsDelta = { hours: 4, minutes: 7, seconds: 1 };
943941
fixture.detectChanges();
@@ -1087,6 +1085,51 @@ describe('IgxTimePicker', () => {
10871085
expect(selectedMinutes).toEqual('00');
10881086
expect(selectedAMPM).toEqual('AM');
10891087
});
1088+
1089+
it('should not reset the time when clicking on AM/PM - GH#15158', fakeAsync(() => {
1090+
timePicker.itemsDelta = { hours: 1, minutes: 15, seconds: 1 };
1091+
timePicker.mode = "dialog";
1092+
1093+
timePicker.open();
1094+
fixture.detectChanges();
1095+
1096+
const amElement = ampmColumn.query(e => e.nativeElement.textContent === 'AM');
1097+
const pmElement = ampmColumn.query(e => e.nativeElement.textContent === 'PM');
1098+
1099+
UIInteractions.simulateClickEvent(amElement.nativeElement);
1100+
fixture.detectChanges();
1101+
1102+
let selectedItems = fixture.debugElement.queryAll(By.css(CSS_CLASS_SELECTED_ITEM));
1103+
let selectedHour = selectedItems[0].nativeElement.innerText;
1104+
let selectedMinutes = selectedItems[1].nativeElement.innerText;
1105+
let selectedAMPM = selectedItems[2].nativeElement.innerText;
1106+
1107+
expect(selectedHour).toBe('11');
1108+
expect(selectedMinutes).toEqual('45');
1109+
expect(selectedAMPM).toEqual('AM');
1110+
1111+
UIInteractions.simulateClickEvent(pmElement.nativeElement); // move to the PM element
1112+
fixture.detectChanges();
1113+
UIInteractions.simulateClickEvent(pmElement.nativeElement); // click again to reproduce the issue
1114+
fixture.detectChanges();
1115+
1116+
selectedItems = fixture.debugElement.queryAll(By.css(CSS_CLASS_SELECTED_ITEM));
1117+
selectedHour = selectedItems[0].nativeElement.innerText;
1118+
selectedMinutes = selectedItems[1].nativeElement.innerText;
1119+
1120+
expect(selectedItems[2]).toBeDefined(); // if the minutes column has no elements, this will be undefined
1121+
selectedAMPM = selectedItems[2].nativeElement.innerText;
1122+
expect(selectedHour).toBe('11');
1123+
expect(selectedMinutes).toEqual('45');
1124+
expect(selectedAMPM).toEqual('PM');
1125+
1126+
// ensure there is content in each element of the spinners
1127+
// '08', '09', '10', '11', '12', '01', '02'
1128+
expect(hourColumn.queryAll(By.css('span')).every(e => !!e.nativeElement.innerText)).toBeTrue();
1129+
1130+
// '00', '15', '30', '45', '', '', '' - three empty elements to align the minutes spinner length with the hours spinner length
1131+
expect(minutesColumn.queryAll(By.css('span')).filter(e => !!e.nativeElement.innerText).length).toEqual(4);
1132+
}));
10901133
});
10911134

10921135
describe('Rendering tests', () => {

projects/igniteui-angular/src/lib/time-picker/time-picker.component.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -945,7 +945,10 @@ export class IgxTimePickerComponent extends PickerBaseDirective
945945
}
946946
case 'ampmList': {
947947
let hour = this._selectedDate.getHours();
948-
hour = DateTimeUtil.isAm(item) ? hour - 12 : hour + 12;
948+
hour = DateTimeUtil.isAm(item)
949+
? hour % 12
950+
: (hour % 12) + 12;
951+
949952
date.setHours(hour);
950953
date = this.validateDropdownValue(date, true);
951954
this.setSelectedValue(date);

0 commit comments

Comments
 (0)