Skip to content

Commit 451d970

Browse files
authored
Merge pull request #2471 from IgniteUI/sstoyanov/bug-fix-2470
Always use date from value
2 parents 10ce1ad + 5b8b237 commit 451d970

File tree

2 files changed

+56
-4
lines changed

2 files changed

+56
-4
lines changed

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

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -860,15 +860,59 @@ describe('IgxTimePicker', () => {
860860
tick();
861861
}));
862862

863-
it('TimePicker with retemplated input group (icon removed)', (async() => {
863+
it('TimePicker with retemplated input group (icon removed)', fakeAsync(() => {
864864
const fixture = TestBed.createComponent(IgxTimePickerRetemplatedComponent);
865-
wait();
865+
tick();
866866
fixture.detectChanges();
867867

868868
const dom = fixture.debugElement;
869869
expect(dom.query(By.css('.igx-input-group'))).not.toBeNull();
870870
expect(dom.query(By.css('.igx-icon'))).toBeNull();
871871
}));
872+
873+
// https://github.com/IgniteUI/igniteui-angular/issues/2470
874+
it('TimePicker always use date from value', fakeAsync(() => {
875+
const fixture = TestBed.createComponent(IgxTimePickerWithPassedTimeComponent);
876+
tick();
877+
fixture.detectChanges();
878+
const dom = fixture.debugElement;
879+
880+
const initialValue = (fixture.componentInstance.timePicker.value);
881+
const initialDate = getDateStringFromDateObject(initialValue);
882+
const initialTime = initialValue.getHours() + ':' + initialValue.getMinutes();
883+
884+
const timePickerTarget = dom.query(By.directive(IgxInputDirective));
885+
UIInteractions.clickElement(timePickerTarget);
886+
tick(100);
887+
fixture.detectChanges();
888+
889+
const hourColumn = dom.query(By.css('.igx-time-picker__hourList'));
890+
const selectHour = hourColumn.children[5];
891+
892+
const minutesColumn = dom.query(By.css('.igx-time-picker__minuteList'));
893+
const selectMinutes = minutesColumn.children[2];
894+
895+
UIInteractions.clickElement(selectHour);
896+
fixture.detectChanges();
897+
tick(100);
898+
UIInteractions.clickElement(selectMinutes);
899+
fixture.detectChanges();
900+
tick(100);
901+
902+
const OkButton = dom.queryAll(By.css('.igx-button--flat'))[1];
903+
UIInteractions.clickElement(OkButton);
904+
fixture.detectChanges();
905+
tick(100);
906+
907+
const changedValue = (fixture.componentInstance.timePicker.value);
908+
const changedDate = getDateStringFromDateObject(changedValue);
909+
const changedTime = changedValue.getHours() + ':' + changedValue.getMinutes();
910+
911+
expect(initialDate).toEqual(changedDate);
912+
expect(initialTime).not.toEqual(changedTime);
913+
expect(changedTime).toEqual('5:23');
914+
915+
}));
872916
});
873917

874918
@Component({
@@ -986,3 +1030,11 @@ function findByInnerText(collection, searchText) {
9861030
}
9871031
}
9881032
}
1033+
1034+
function getDateStringFromDateObject(date: Date): string {
1035+
const month = date.getMonth() + 1;
1036+
const day = date.getDate();
1037+
const year = date.getFullYear();
1038+
1039+
return year + '/' + month + '/' + day;
1040+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -757,7 +757,7 @@ export class IgxTimePickerComponent implements ControlValueAccessor, OnInit, OnD
757757
}
758758

759759
private _getSelectedTime(): Date {
760-
const date = new Date();
760+
const date = this.value ? new Date(this.value) : new Date();
761761
date.setHours(parseInt(this.selectedHour, 10));
762762
date.setMinutes(parseInt(this.selectedMinute, 10));
763763
date.setSeconds(0);
@@ -771,7 +771,7 @@ export class IgxTimePickerComponent implements ControlValueAccessor, OnInit, OnD
771771
}
772772

773773
private _convertMinMaxValue(value: string): Date {
774-
const date = new Date();
774+
const date = this.value ? new Date(this.value) : new Date();
775775
const sections = value.split(/[\s:]+/);
776776

777777
date.setHours(parseInt(sections[0], 10));

0 commit comments

Comments
 (0)