Skip to content

Commit 07739ba

Browse files
authored
Merge pull request #8236 from IgniteUI/bpenkov/time-picker-editing-10.1
Set mask to TimePicker's editor on clear - 10.1.x
2 parents 1c94cd2 + d1ee3cd commit 07739ba

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
@@ -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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1846,7 +1846,6 @@ describe('IgxTimePicker', () => {
18461846
fixture.componentInstance.format = 'hh tt';
18471847
fixture.componentInstance.customDate = new Date(2018, 10, 27, 17, 45, 0, 0);
18481848
fixture.detectChanges();
1849-
18501849
const clearTime = dom.queryAll(By.css('.igx-icon'))[1];
18511850

18521851
UIInteractions.simulateClickAndSelectEvent(clearTime);
@@ -1866,6 +1865,21 @@ describe('IgxTimePicker', () => {
18661865
expect(input.nativeElement.value).toBe('-- AM');
18671866
}));
18681867

1868+
it('should allow editing of input after clear', fakeAsync(() => {
1869+
fixture.componentInstance.format = 'hh tt';
1870+
fixture.componentInstance.customDate = new Date(2018, 10, 27, 17, 45, 0, 0);
1871+
fixture.detectChanges();
1872+
spyOn(fixture.componentInstance.timePicker, 'onInput');
1873+
1874+
const clearTime = dom.queryAll(By.css('.igx-icon'))[1];
1875+
UIInteractions.simulateClickAndSelectEvent(clearTime);
1876+
fixture.detectChanges();
1877+
const _input = fixture.debugElement.query(By.css('input'));
1878+
UIInteractions.simulateTyping('12 AM', _input);
1879+
expect(fixture.componentInstance.timePicker.onInput).not.toThrow();
1880+
expect(_input.nativeElement.value).toEqual('12 AM');
1881+
}));
1882+
18691883
it('Should navigate dropdown lists correctly when format contains only hours.', fakeAsync(() => {
18701884
fixture.componentInstance.format = 'hh tt';
18711885
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

5555

5656
let NEXT_ID = 0;
@@ -1944,9 +1944,12 @@ export class IgxTimePickerComponent implements
19441944
this.isNotEmpty = false;
19451945

19461946
const oldVal = new Date(this.value);
1947-
1948-
this.displayValue = '';
1949-
this.value.setHours(0, 0);
1947+
this.displayValue = this.parseMask(false);
1948+
requestAnimationFrame(() => {
1949+
this._setCursorPosition(0);
1950+
});
1951+
// TODO: refactoring - this.value should be null #6585
1952+
this.value?.setHours(0, 0, 0);
19501953

19511954
if (oldVal.getTime() !== this.value.getTime()) {
19521955
const args: IgxTimePickerValueChangedEventArgs = {
@@ -1964,35 +1967,35 @@ export class IgxTimePickerComponent implements
19641967
* @hidden
19651968
*/
19661969
public onInput(event): void {
1967-
const val = event.target.value;
1970+
const inputMask: string = event.target.value;
19681971
const oldVal = new Date(this.value);
19691972

1970-
this.isNotEmpty = val !== this.parseMask(false);
1973+
this.isNotEmpty = inputMask !== this.parseMask(false);
19711974

19721975
// handle cases where all empty positions (promts) are filled and we want to update
19731976
// timepicker own value property if it is a valid Date
1974-
if (val.indexOf(this.promptChar) === -1) {
1975-
if (this._isEntryValid(val)) {
1976-
const newVal = this.convertMinMaxValue(val);
1977+
if (inputMask.indexOf(this.promptChar) === -1) {
1978+
if (this._isEntryValid(inputMask)) {
1979+
const newVal = this.convertMinMaxValue(inputMask);
19771980
if (oldVal.getTime() !== newVal.getTime()) {
19781981
this.value = newVal;
19791982
}
19801983
} else {
19811984
const args: IgxTimePickerValidationFailedEventArgs = {
19821985
timePicker: this,
1983-
currentValue: val,
1986+
currentValue: new Date(inputMask),
19841987
setThroughUI: false
19851988
};
19861989
this.onValidationFailed.emit(args);
19871990
}
19881991
// handle cases where the user deletes the display value (when pressing backspace or delete)
1989-
} else if (!this.value || !val || val === this.parseMask(false)) {
1992+
} else if (!this.value || inputMask.length === 0 || !this.isNotEmpty) {
19901993
this.isNotEmpty = false;
1991-
1992-
this.value.setHours(0, 0);
1993-
this.displayValue = val;
1994-
1995-
if (oldVal.getTime() !== this.value.getTime()) {
1994+
// TODO: refactoring - this.value should be null #6585
1995+
this.value?.setHours(0, 0, 0);
1996+
this.displayValue = inputMask;
1997+
if (oldVal.getTime() !== this.value?.getTime()) {
1998+
// TODO: Do not emit event when the editor is empty #6482
19961999
const args: IgxTimePickerValueChangedEventArgs = {
19972000
oldValue: oldVal,
19982001
newValue: this.value
@@ -2019,7 +2022,7 @@ export class IgxTimePickerComponent implements
20192022
this.isNotEmpty = value !== '';
20202023
this.displayValue = value;
20212024

2022-
if (value && value !== this.parseMask()) {
2025+
if (value && (value !== this.parseMask() || value !== this.parseMask(false))) {
20232026
if (this._isEntryValid(value)) {
20242027
const newVal = this.convertMinMaxValue(value);
20252028
if (!this.value || this.value.getTime() !== newVal.getTime()) {

0 commit comments

Comments
 (0)