Skip to content
This repository was archived by the owner on Sep 5, 2024. It is now read-only.

Commit b903153

Browse files
NgaiKaKitkara
authored andcommitted
fix(calendar): boundKeyHandler preventDefault on other input elements (#9746)
1 parent 2c3b546 commit b903153

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

src/components/datepicker/js/calendar.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,12 +197,26 @@
197197

198198
var boundKeyHandler = angular.bind(this, this.handleKeyEvent);
199199

200+
201+
202+
// If use the md-calendar directly in the body without datepicker,
203+
// handleKeyEvent will disable other inputs on the page.
204+
// So only apply the handleKeyEvent on the body when the md-calendar inside datepicker,
205+
// otherwise apply on the calendar element only.
206+
207+
var handleKeyElement;
208+
if ($element.parent().hasClass('md-datepicker-calendar')) {
209+
handleKeyElement = angular.element(document.body);
210+
} else {
211+
handleKeyElement = $element;
212+
}
213+
200214
// Bind the keydown handler to the body, in order to handle cases where the focused
201215
// element gets removed from the DOM and stops propagating click events.
202-
angular.element(document.body).on('keydown', boundKeyHandler);
216+
handleKeyElement.on('keydown', boundKeyHandler);
203217

204218
$scope.$on('$destroy', function() {
205-
angular.element(document.body).off('keydown', boundKeyHandler);
219+
handleKeyElement.off('keydown', boundKeyHandler);
206220
});
207221

208222
if (this.minDate && this.minDate > $mdDateLocale.firstRenderableDate) {

src/components/datepicker/js/calendar.spec.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,8 @@ describe('md-calendar', function() {
114114
function dispatchKeyEvent(keyCode, opt_modifiers) {
115115
var mod = opt_modifiers || {};
116116

117-
angular.element(document.body).triggerHandler({
117+
118+
angular.element(element).triggerHandler({
118119
type: 'keydown',
119120
keyCode: keyCode,
120121
which: keyCode,

0 commit comments

Comments
 (0)