Skip to content

Commit 86ba0a6

Browse files
JLHwungvlio20
authored andcommitted
fix(date-picker): register onTouched callback on blurred (#424)
The onTouched callback should be register to the `blur` event handler of the input element. Fixes #426
1 parent 30c59a2 commit 86ba0a6

3 files changed

Lines changed: 22 additions & 0 deletions

File tree

src/app/date-picker/date-picker.component.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
[ngModel]="inputElementValue"
99
(ngModelChange)="onViewDateChange($event)"
1010
(focus)="inputFocused()"
11+
(blur)="inputBlurred()"
1112
[readonly]="componentConfig.disableKeypress"
1213
[disabled]="disabled"/>
1314
</div>

src/app/date-picker/date-picker.component.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {MonthCalendarComponent} from '../month-calendar/month-calendar.component
1212
import {DayCalendarService} from '../day-calendar/day-calendar.service';
1313
import {TimeSelectService} from '../time-select/time-select.service';
1414
import {UtilsService} from '../common/services/utils/utils.service';
15+
import {By} from '@angular/platform-browser';
1516

1617
describe('Component: DatePickerComponent', () => {
1718
let component: DatePickerComponent;
@@ -77,4 +78,15 @@ describe('Component: DatePickerComponent', () => {
7778
component.dayTimeCalendarRef.onGoToCurrent.emit();
7879
expect(component.onGoToCurrent.emit).toHaveBeenCalledWith();
7980
});
81+
82+
it('should call onTouched when input is blurred', () => {
83+
setComponentMode('day');
84+
spyOn(component, 'onTouchedCallback');
85+
component.registerOnTouched(component.onTouchedCallback);
86+
87+
const inputElement = fixture.debugElement.query(By.css('.dp-picker-input'));
88+
inputElement.triggerEventHandler('blur', {});
89+
90+
expect(component.onTouchedCallback).toHaveBeenCalledWith();
91+
});
8092
});

src/app/date-picker/date-picker.component.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,11 @@ export class DatePickerComponent implements OnChanges,
266266
}
267267

268268
registerOnTouched(fn: any): void {
269+
this.onTouchedCallback = fn;
270+
}
271+
272+
onTouchedCallback() {
273+
269274
}
270275

271276
validate(formControl: FormControl): ValidationErrors {
@@ -389,6 +394,10 @@ export class DatePickerComponent implements OnChanges,
389394
}, this.componentConfig.onOpenDelay);
390395
}
391396

397+
inputBlurred() {
398+
this.onTouchedCallback();
399+
}
400+
392401
showCalendars() {
393402
this.hideStateHelper = true;
394403
this.areCalendarsShown = true;

0 commit comments

Comments
 (0)