Skip to content
This repository has been archived by the owner on May 29, 2019. It is now read-only.

Commit

Permalink
feat(buttons): allow toggling via spacebar when focused
Browse files Browse the repository at this point in the history
- Allow the spacebar to toggle the button state when focused

Closes #4252
Closes #4259
  • Loading branch information
wesleycho committed Aug 23, 2015
1 parent 5ee23a4 commit bdfb289
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/buttons/buttons.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ angular.module('ui.bootstrap.buttons', [])
};
})

.directive('btnCheckbox', function() {
.directive('btnCheckbox', ['$document', function($document) {
return {
require: ['btnCheckbox', 'ngModel'],
controller: 'ButtonsController',
Expand Down Expand Up @@ -79,6 +79,18 @@ angular.module('ui.bootstrap.buttons', [])
ngModelCtrl.$render();
});
});

//accessibility
element.on('keypress', function(e) {
if (attrs.disabled || e.which !== 32 || $document[0].activeElement !== element[0]) {
return;
}

scope.$apply(function() {
ngModelCtrl.$setViewValue(element.hasClass(buttonsCtrl.activeClass) ? getFalseValue() : getTrueValue());
ngModelCtrl.$render();
});
});
}
};
});
}]);
35 changes: 35 additions & 0 deletions src/buttons/test/buttons.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,27 @@ describe('buttons', function() {
expect(btn).not.toHaveClass('active');
});

it('should toggle custom model values on spacebar if focused', function() {
$scope.model = 0;
var btn = compileButton('<button ng-model="model" btn-checkbox btn-checkbox-true="1" btn-checkbox-false="0">click</button>', $scope);
$('body').append(btn);
var e = $.Event('keypress');
e.which = 32;

btn[0].focus();
btn.trigger(e);
$scope.$digest();
expect($scope.model).toEqual(1);
expect(btn).toHaveClass('active');

btn.trigger(e);
$scope.$digest();
expect($scope.model).toEqual(0);
expect(btn).not.toHaveClass('active');

btn.remove();
});

it('should monitor true / false value changes - issue 666', function() {

$scope.model = 1;
Expand All @@ -95,6 +116,7 @@ describe('buttons', function() {
$scope.model = 1;
$scope.falseVal = 0;
var btn = compileButton('<button disabled ng-model="model" btn-checkbox btn-checkbox-true="falseVal">click</button>', $scope);
$('body').append(btn);

expect(btn).not.toHaveClass('active');
expect($scope.model).toEqual(1);
Expand All @@ -106,6 +128,19 @@ describe('buttons', function() {
$scope.$digest();

expect(btn).not.toHaveClass('active');

btn[0].focus();
var e = $.Event('keypress');
e.which = 32;
btn.trigger(e);

expect(btn).not.toHaveClass('active');

$scope.$digest();

expect(btn).not.toHaveClass('active');

btn.remove();
});

describe('setting buttonConfig', function() {
Expand Down

0 comments on commit bdfb289

Please sign in to comment.