Skip to content

Commit e820727

Browse files
authored
Merge pull request #8235 from IgniteUI/bpenkov/time-picker-editing
Set mask to TimePicker's editor on clear
2 parents 1598e52 + 23bac37 commit e820727

File tree

3 files changed

+36
-19
lines changed

3 files changed

+36
-19
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
</ng-template>
44

55
<ng-template #dropdownInputTemplate>
6-
<igx-input-group #group (mousedown)="mouseDown($event)">
6+
<igx-input-group #group (mousedown)="mouseDown($event)" [suppressInputAutofocus]="true">
77
<label igxLabel *ngIf="!labelDirective">Time</label>
88
<ng-container ngProjectAs="[igxLabel]" *ngTemplateOutlet="labelTemplate"></ng-container>
99
<igx-prefix (click)="openDialog(group.element.nativeElement)">

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1873,7 +1873,6 @@ describe('IgxTimePicker', () => {
18731873
fixture.componentInstance.format = 'hh tt';
18741874
fixture.componentInstance.customDate = new Date(2018, 10, 27, 17, 45, 0, 0);
18751875
fixture.detectChanges();
1876-
18771876
const clearTime = dom.queryAll(By.css('.igx-icon'))[1];
18781877

18791878
UIInteractions.simulateClickAndSelectEvent(clearTime);
@@ -1893,6 +1892,21 @@ describe('IgxTimePicker', () => {
18931892
expect(input.nativeElement.value).toBe('-- AM');
18941893
}));
18951894

1895+
it('should allow editing of input after clear', fakeAsync(() => {
1896+
fixture.componentInstance.format = 'hh tt';
1897+
fixture.componentInstance.customDate = new Date(2018, 10, 27, 17, 45, 0, 0);
1898+
fixture.detectChanges();
1899+
spyOn(fixture.componentInstance.timePicker, 'onInput');
1900+
1901+
const clearTime = dom.queryAll(By.css('.igx-icon'))[1];
1902+
UIInteractions.simulateClickAndSelectEvent(clearTime);
1903+
fixture.detectChanges();
1904+
const _input = fixture.debugElement.query(By.css('input'));
1905+
UIInteractions.simulateTyping('12 AM', _input);
1906+
expect(fixture.componentInstance.timePicker.onInput).not.toThrow();
1907+
expect(_input.nativeElement.value).toEqual('12 AM');
1908+
}));
1909+
18961910
it('Should navigate dropdown lists correctly when format contains only hours.', fakeAsync(() => {
18971911
fixture.componentInstance.format = 'hh tt';
18981912
fixture.componentInstance.customDate = new Date(2018, 10, 27, 17, 45, 0);

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

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ import { ITimePickerResourceStrings } from '../core/i18n/time-picker-resources';
5050
import { CurrentResourceStrings } from '../core/i18n/resources';
5151
import { KEYS, CancelableBrowserEventArgs, IBaseEventArgs } from '../core/utils';
5252
import { InteractionMode } from '../core/enums';
53-
import { IgxTextSelectionModule} from '../directives/text-selection/text-selection.directive';
53+
import { IgxTextSelectionModule } from '../directives/text-selection/text-selection.directive';
5454
import { IgxLabelDirective } from '../directives/label/label.directive';
5555

5656

@@ -1951,9 +1951,12 @@ export class IgxTimePickerComponent implements
19511951
this.isNotEmpty = false;
19521952

19531953
const oldVal = new Date(this.value);
1954-
1955-
this.displayValue = '';
1956-
this.value.setHours(0, 0);
1954+
this.displayValue = this.parseMask(false);
1955+
requestAnimationFrame(() => {
1956+
this._setCursorPosition(0);
1957+
});
1958+
// TODO: refactoring - this.value should be null #6585
1959+
this.value?.setHours(0, 0, 0);
19571960

19581961
if (oldVal.getTime() !== this.value.getTime()) {
19591962
const args: IgxTimePickerValueChangedEventArgs = {
@@ -1971,35 +1974,35 @@ export class IgxTimePickerComponent implements
19711974
* @hidden
19721975
*/
19731976
public onInput(event): void {
1974-
const val = event.target.value;
1977+
const inputMask: string = event.target.value;
19751978
const oldVal = new Date(this.value);
19761979

1977-
this.isNotEmpty = val !== this.parseMask(false);
1980+
this.isNotEmpty = inputMask !== this.parseMask(false);
19781981

19791982
// handle cases where all empty positions (promts) are filled and we want to update
19801983
// timepicker own value property if it is a valid Date
1981-
if (val.indexOf(this.promptChar) === -1) {
1982-
if (this._isEntryValid(val)) {
1983-
const newVal = this.convertMinMaxValue(val);
1984+
if (inputMask.indexOf(this.promptChar) === -1) {
1985+
if (this._isEntryValid(inputMask)) {
1986+
const newVal = this.convertMinMaxValue(inputMask);
19841987
if (oldVal.getTime() !== newVal.getTime()) {
19851988
this.value = newVal;
19861989
}
19871990
} else {
19881991
const args: IgxTimePickerValidationFailedEventArgs = {
19891992
timePicker: this,
1990-
currentValue: val,
1993+
currentValue: new Date(inputMask),
19911994
setThroughUI: false
19921995
};
19931996
this.onValidationFailed.emit(args);
19941997
}
19951998
// handle cases where the user deletes the display value (when pressing backspace or delete)
1996-
} else if (!this.value || !val || val === this.parseMask(false)) {
1999+
} else if (!this.value || inputMask.length === 0 || !this.isNotEmpty) {
19972000
this.isNotEmpty = false;
1998-
1999-
this.value.setHours(0, 0);
2000-
this.displayValue = val;
2001-
2002-
if (oldVal.getTime() !== this.value.getTime()) {
2001+
// TODO: refactoring - this.value should be null #6585
2002+
this.value?.setHours(0, 0, 0);
2003+
this.displayValue = inputMask;
2004+
if (oldVal.getTime() !== this.value?.getTime()) {
2005+
// TODO: Do not emit event when the editor is empty #6482
20032006
const args: IgxTimePickerValueChangedEventArgs = {
20042007
oldValue: oldVal,
20052008
newValue: this.value
@@ -2026,7 +2029,7 @@ export class IgxTimePickerComponent implements
20262029
this.isNotEmpty = value !== '';
20272030
this.displayValue = value;
20282031

2029-
if (value && value !== this.parseMask()) {
2032+
if (value && (value !== this.parseMask() || value !== this.parseMask(false))) {
20302033
if (this._isEntryValid(value)) {
20312034
const newVal = this.convertMinMaxValue(value);
20322035
if (!this.value || this.value.getTime() !== newVal.getTime()) {

0 commit comments

Comments
 (0)