Skip to content

Commit

Permalink
fix(dropdown,calendar): avoid tab to enter input on disabled variant
Browse files Browse the repository at this point in the history
When a dropdown with search selection, or a calendar field, is disabled, the input fields were still accessible by using the tab key
  • Loading branch information
lubber-de authored Aug 9, 2020
1 parent eb3436f commit 9f496c3
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 5 deletions.
58 changes: 55 additions & 3 deletions src/definitions/modules/calendar.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ $.fn.calendar = function(parameters) {
'20': {'row': 3, 'column': 1 },
'30': {'row': 2, 'column': 1 }
},
numberText = ['','one','two','three','four','five','six','seven','eight'],
selectionComplete = false
numberText = ['','one','two','three','four','five','six','seven','eight']
;

$allModules
Expand Down Expand Up @@ -78,6 +77,8 @@ $.fn.calendar = function(parameters) {
isTouchDown = false,
isInverted = $module.hasClass(className.inverted),
focusDateUsedForRange = false,
selectionComplete = false,
classObserver,
module
;

Expand All @@ -95,6 +96,7 @@ $.fn.calendar = function(parameters) {
module.create.calendar();

module.bind.events();
module.observeChanges();
module.instantiate();
},

Expand All @@ -108,6 +110,7 @@ $.fn.calendar = function(parameters) {
module.verbose('Destroying previous calendar for', element);
$module.removeData(moduleNamespace);
module.unbind.events();
module.disconnect.classObserver();
},

setup: {
Expand Down Expand Up @@ -197,6 +200,7 @@ $.fn.calendar = function(parameters) {
if (settings.touchReadonly && $input.length && isTouch) {
$input.prop('readonly', true);
}
module.check.disabled();
},
date: function () {
var date;
Expand Down Expand Up @@ -639,6 +643,53 @@ $.fn.calendar = function(parameters) {
module.trigger.change();
selectionComplete = false;
}
},
class: {
mutation: function(mutations) {
mutations.forEach(function(mutation) {
if(mutation.attributeName === "class") {
module.check.disabled();
}
});
}
}
},

observeChanges: function() {
if('MutationObserver' in window) {
classObserver = new MutationObserver(module.event.class.mutation);
module.debug('Setting up mutation observer', classObserver);
module.observe.class();
}
},

disconnect: {
classObserver: function() {
if($input.length && classObserver) {
classObserver.disconnect();
}
}
},

observe: {
class: function() {
if($input.length && classObserver) {
classObserver.observe($module[0], {
attributes : true
});
}
}
},

is: {
disabled: function() {
return $module.hasClass(className.disabled);
}
},

check: {
disabled: function(){
$input.attr('tabindex',module.is.disabled() ? -1 : 0);
}
},

Expand Down Expand Up @@ -1694,7 +1745,8 @@ $.fn.calendar.settings = {
rangeCell: 'range',
focusCell: 'focus',
todayCell: 'today',
today: 'today link'
today: 'today link',
disabled: 'disabled'
},

metadata: {
Expand Down
32 changes: 30 additions & 2 deletions src/definitions/modules/dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ $.fn.dropdown = function(parameters) {
id,
selectObserver,
menuObserver,
classObserver,
module
;

Expand Down Expand Up @@ -162,15 +163,18 @@ $.fn.dropdown = function(parameters) {
;
module.disconnect.menuObserver();
module.disconnect.selectObserver();
module.disconnect.classObserver();
},

observeChanges: function() {
if('MutationObserver' in window) {
selectObserver = new MutationObserver(module.event.select.mutation);
menuObserver = new MutationObserver(module.event.menu.mutation);
module.debug('Setting up mutation observer', selectObserver, menuObserver);
classObserver = new MutationObserver(module.event.class.mutation);
module.debug('Setting up mutation observer', selectObserver, menuObserver, classObserver);
module.observe.select();
module.observe.menu();
module.observe.class();
}
},

Expand All @@ -184,6 +188,11 @@ $.fn.dropdown = function(parameters) {
if(selectObserver) {
selectObserver.disconnect();
}
},
classObserver: function() {
if(classObserver) {
classObserver.disconnect();
}
}
},
observe: {
Expand All @@ -202,6 +211,13 @@ $.fn.dropdown = function(parameters) {
subtree : true
});
}
},
class: function() {
if(module.has.search() && classObserver) {
classObserver.observe($module[0], {
attributes : true
});
}
}
},

Expand Down Expand Up @@ -1216,6 +1232,15 @@ $.fn.dropdown = function(parameters) {
}
}
},
class: {
mutation: function(mutations) {
mutations.forEach(function(mutation) {
if(mutation.attributeName === "class") {
module.check.disabled();
}
});
}
},
select: {
mutation: function(mutations) {
module.debug('<select> modified, recreating menu');
Expand Down Expand Up @@ -2136,6 +2161,9 @@ $.fn.dropdown = function(parameters) {
}
}
return true;
},
disabled: function(){
$search.attr('tabindex',module.is.disabled() ? -1 : 0);
}
},

Expand Down Expand Up @@ -2401,8 +2429,8 @@ $.fn.dropdown = function(parameters) {
module.debug('Added tabindex to searchable dropdown');
$search
.val('')
.attr('tabindex', 0)
;
module.check.disabled();
$menu
.attr('tabindex', -1)
;
Expand Down

0 comments on commit 9f496c3

Please sign in to comment.