Skip to content

Commit a80c3fa

Browse files
authored
Merge pull request #8237 from IgniteUI/bpenkov/time-picker-editing-9.1
Set mask to TimePicker's editor on clear - 9.1.x
2 parents c58a10d + c8b9243 commit a80c3fa

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<ng-template #dropdownInputTemplate>
2-
<igx-input-group #group (mousedown)="mouseDown($event)">
2+
<igx-input-group #group (mousedown)="mouseDown($event)" [suppressInputAutofocus]="true">
33
<label igxLabel>Time</label>
44
<igx-prefix (click)="openDialog(group.element.nativeElement)">
55
<igx-icon>access_time</igx-icon>

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

+15-1
Original file line numberDiff line numberDiff line change
@@ -1798,7 +1798,6 @@ describe('IgxTimePicker', () => {
17981798
fixture.componentInstance.format = 'hh tt';
17991799
fixture.componentInstance.customDate = new Date(2018, 10, 27, 17, 45, 0, 0);
18001800
fixture.detectChanges();
1801-
18021801
const clearTime = dom.queryAll(By.css('.igx-icon'))[1];
18031802

18041803
UIInteractions.simulateClickAndSelectEvent(clearTime);
@@ -1818,6 +1817,21 @@ describe('IgxTimePicker', () => {
18181817
expect(input.nativeElement.value).toBe('-- AM');
18191818
}));
18201819

1820+
it('should allow editing of input after clear', fakeAsync(() => {
1821+
fixture.componentInstance.format = 'hh tt';
1822+
fixture.componentInstance.customDate = new Date(2018, 10, 27, 17, 45, 0, 0);
1823+
fixture.detectChanges();
1824+
spyOn(fixture.componentInstance.timePicker, 'onInput');
1825+
1826+
const clearTime = dom.queryAll(By.css('.igx-icon'))[1];
1827+
UIInteractions.simulateClickAndSelectEvent(clearTime);
1828+
fixture.detectChanges();
1829+
const _input = fixture.debugElement.query(By.css('input'));
1830+
UIInteractions.simulateTyping('12 AM', _input);
1831+
expect(fixture.componentInstance.timePicker.onInput).not.toThrow();
1832+
expect(_input.nativeElement.value).toEqual('12 AM');
1833+
}));
1834+
18211835
it('Should navigate dropdown lists correctly when format contains only hours.', fakeAsync(() => {
18221836
fixture.componentInstance.format = 'hh tt';
18231837
fixture.componentInstance.customDate = new Date(2018, 10, 27, 17, 45, 0);

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

+20-17
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

5555
let NEXT_ID = 0;
5656
const ITEMS_COUNT = 7;
@@ -1894,9 +1894,12 @@ export class IgxTimePickerComponent implements
18941894
this.isNotEmpty = false;
18951895

18961896
const oldVal = new Date(this.value);
1897-
1898-
this.displayValue = '';
1899-
this.value.setHours(0, 0);
1897+
this.displayValue = this.parseMask(false);
1898+
requestAnimationFrame(() => {
1899+
this._setCursorPosition(0);
1900+
});
1901+
// TODO: refactoring - this.value should be null #6585
1902+
this.value?.setHours(0, 0, 0);
19001903

19011904
if (oldVal.getTime() !== this.value.getTime()) {
19021905
const args: IgxTimePickerValueChangedEventArgs = {
@@ -1914,35 +1917,35 @@ export class IgxTimePickerComponent implements
19141917
* @hidden
19151918
*/
19161919
public onInput(event): void {
1917-
const val = event.target.value;
1920+
const inputMask: string = event.target.value;
19181921
const oldVal = new Date(this.value);
19191922

1920-
this.isNotEmpty = val !== this.parseMask(false);
1923+
this.isNotEmpty = inputMask !== this.parseMask(false);
19211924

19221925
// handle cases where all empty positions (promts) are filled and we want to update
19231926
// timepicker own value property if it is a valid Date
1924-
if (val.indexOf(this.promptChar) === -1) {
1925-
if (this._isEntryValid(val)) {
1926-
const newVal = this._convertMinMaxValue(val);
1927+
if (inputMask.indexOf(this.promptChar) === -1) {
1928+
if (this._isEntryValid(inputMask)) {
1929+
const newVal = this._convertMinMaxValue(inputMask);
19271930
if (oldVal.getTime() !== newVal.getTime()) {
19281931
this.value = newVal;
19291932
}
19301933
} else {
19311934
const args: IgxTimePickerValidationFailedEventArgs = {
19321935
timePicker: this,
1933-
currentValue: val,
1936+
currentValue: new Date(inputMask),
19341937
setThroughUI: false
19351938
};
19361939
this.onValidationFailed.emit(args);
19371940
}
19381941
// handle cases where the user deletes the display value (when pressing backspace or delete)
1939-
} else if (!this.value || !val || val === this.parseMask(false)) {
1942+
} else if (!this.value || inputMask.length === 0 || !this.isNotEmpty) {
19401943
this.isNotEmpty = false;
1941-
1942-
this.value.setHours(0, 0);
1943-
this.displayValue = val;
1944-
1945-
if (oldVal.getTime() !== this.value.getTime()) {
1944+
// TODO: refactoring - this.value should be null #6585
1945+
this.value?.setHours(0, 0, 0);
1946+
this.displayValue = inputMask;
1947+
if (oldVal.getTime() !== this.value?.getTime()) {
1948+
// TODO: Do not emit event when the editor is empty #6482
19461949
const args: IgxTimePickerValueChangedEventArgs = {
19471950
oldValue: oldVal,
19481951
newValue: this.value
@@ -1969,7 +1972,7 @@ export class IgxTimePickerComponent implements
19691972
this.isNotEmpty = value !== '';
19701973
this.displayValue = value;
19711974

1972-
if (value && value !== this.parseMask()) {
1975+
if (value && (value !== this.parseMask() || value !== this.parseMask(false))) {
19731976
if (this._isEntryValid(value)) {
19741977
const newVal = this._convertMinMaxValue(value);
19751978
if (!this.value || this.value.getTime() !== newVal.getTime()) {

0 commit comments

Comments
 (0)