Skip to content

Commit

Permalink
fix(checkbox): support onChange for cursor keynav
Browse files Browse the repository at this point in the history
Changing Radio-Button Selections via Cursor Key-Navigation was not triggering the onChange Event
  • Loading branch information
lubber-de authored and Sean committed Dec 21, 2018
1 parent 11da1e3 commit 3fec1ec
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion src/definitions/modules/checkbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,11 @@ $.fn.checkbox = function(parameters) {
},

event: {
change: function(event) {
if( !module.should.ignoreCallbacks() ) {
settings.onChange.call(input);
}
},
click: function(event) {
var
$target = $(event.target)
Expand All @@ -197,9 +202,36 @@ $.fn.checkbox = function(parameters) {
keyCode = {
enter : 13,
space : 32,
escape : 27
escape : 27,
left : 37,
up : 38,
right : 39,
down : 40
}
;

var r = module.get.radios(),
rIndex = r.index($module),
rLen = r.length,
checkIndex = false;

if(key == keyCode.left || key == keyCode.up) {
checkIndex = (rIndex === 0 ? rLen : rIndex) - 1;
} else if(key == keyCode.right || key == keyCode.down) {
checkIndex = rIndex === rLen-1 ? 0 : rIndex+1;
}

if (!module.should.ignoreCallbacks() && checkIndex !== false) {
if(!settings.beforeUnchecked.apply(input)) {
module.verbose('Option not allowed to be unchecked, cancelling key navigation');
return false;
}
if (!settings.beforeChecked.apply($(r[checkIndex]).children(selector.input)[0])) {
module.verbose('Next option should not allow check, cancelling key navigation');
return false;
}
}

if(key == keyCode.escape) {
module.verbose('Escape key pressed blurring field');
$input.blur();
Expand Down Expand Up @@ -550,6 +582,7 @@ $.fn.checkbox = function(parameters) {
module.verbose('Attaching checkbox events');
$module
.on('click' + eventNamespace, module.event.click)
.on('change' + eventNamespace, module.event.change)
.on('keydown' + eventNamespace, selector.input, module.event.keydown)
.on('keyup' + eventNamespace, selector.input, module.event.keyup)
;
Expand Down

0 comments on commit 3fec1ec

Please # to comment.