mirror of
https://github.com/angular/angular.git
synced 2026-09-14 13:54:52 +08:00
fix(datepicker): able to open from readonly input using keyboard control (#12880)
Fixes users being able to open a datepicker through a `readonly` input by using the alt + down arrow key combination.
This commit is contained in:
committed by
Jeremy Elbourn
parent
5d0ec23b1d
commit
548d2b7c04
@@ -311,7 +311,9 @@ export class MatDatepickerInput<D> implements ControlValueAccessor, OnDestroy, V
|
||||
}
|
||||
|
||||
_onKeydown(event: KeyboardEvent) {
|
||||
if (this._datepicker && event.altKey && event.keyCode === DOWN_ARROW) {
|
||||
const isAltDownArrow = event.altKey && event.keyCode === DOWN_ARROW;
|
||||
|
||||
if (this._datepicker && isAltDownArrow && !this._elementRef.nativeElement.readOnly) {
|
||||
this._datepicker.open();
|
||||
event.preventDefault();
|
||||
}
|
||||
|
||||
@@ -454,7 +454,7 @@ describe('MatDatepicker', () => {
|
||||
expect(testComponent.datepicker.opened).toBe(false);
|
||||
}));
|
||||
|
||||
it('should open the datpeicker using ALT + DOWN_ARROW', fakeAsync(() => {
|
||||
it('should open the datepicker using ALT + DOWN_ARROW', fakeAsync(() => {
|
||||
expect(testComponent.datepicker.opened).toBe(false);
|
||||
|
||||
const event = createKeyboardEvent('keydown', DOWN_ARROW);
|
||||
@@ -468,6 +468,24 @@ describe('MatDatepicker', () => {
|
||||
expect(event.defaultPrevented).toBe(true);
|
||||
}));
|
||||
|
||||
it('should not open for ALT + DOWN_ARROW on readonly input', fakeAsync(() => {
|
||||
const input = fixture.nativeElement.querySelector('input');
|
||||
|
||||
expect(testComponent.datepicker.opened).toBe(false);
|
||||
|
||||
input.setAttribute('readonly', 'true');
|
||||
|
||||
const event = createKeyboardEvent('keydown', DOWN_ARROW);
|
||||
Object.defineProperty(event, 'altKey', {get: () => true});
|
||||
|
||||
dispatchEvent(input, event);
|
||||
fixture.detectChanges();
|
||||
flush();
|
||||
|
||||
expect(testComponent.datepicker.opened).toBe(false);
|
||||
expect(event.defaultPrevented).toBe(false);
|
||||
}));
|
||||
|
||||
});
|
||||
|
||||
describe('datepicker with too many inputs', () => {
|
||||
|
||||
Reference in New Issue
Block a user