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

Commit bdfb289

Browse files
committed
feat(buttons): allow toggling via spacebar when focused
- Allow the spacebar to toggle the button state when focused Closes #4252 Closes #4259
1 parent 5ee23a4 commit bdfb289

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

Diff for: src/buttons/buttons.js

+14-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ angular.module('ui.bootstrap.buttons', [])
4242
};
4343
})
4444

45-
.directive('btnCheckbox', function() {
45+
.directive('btnCheckbox', ['$document', function($document) {
4646
return {
4747
require: ['btnCheckbox', 'ngModel'],
4848
controller: 'ButtonsController',
@@ -79,6 +79,18 @@ angular.module('ui.bootstrap.buttons', [])
7979
ngModelCtrl.$render();
8080
});
8181
});
82+
83+
//accessibility
84+
element.on('keypress', function(e) {
85+
if (attrs.disabled || e.which !== 32 || $document[0].activeElement !== element[0]) {
86+
return;
87+
}
88+
89+
scope.$apply(function() {
90+
ngModelCtrl.$setViewValue(element.hasClass(buttonsCtrl.activeClass) ? getFalseValue() : getTrueValue());
91+
ngModelCtrl.$render();
92+
});
93+
});
8294
}
8395
};
84-
});
96+
}]);

Diff for: src/buttons/test/buttons.spec.js

+35
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,27 @@ describe('buttons', function() {
7474
expect(btn).not.toHaveClass('active');
7575
});
7676

77+
it('should toggle custom model values on spacebar if focused', function() {
78+
$scope.model = 0;
79+
var btn = compileButton('<button ng-model="model" btn-checkbox btn-checkbox-true="1" btn-checkbox-false="0">click</button>', $scope);
80+
$('body').append(btn);
81+
var e = $.Event('keypress');
82+
e.which = 32;
83+
84+
btn[0].focus();
85+
btn.trigger(e);
86+
$scope.$digest();
87+
expect($scope.model).toEqual(1);
88+
expect(btn).toHaveClass('active');
89+
90+
btn.trigger(e);
91+
$scope.$digest();
92+
expect($scope.model).toEqual(0);
93+
expect(btn).not.toHaveClass('active');
94+
95+
btn.remove();
96+
});
97+
7798
it('should monitor true / false value changes - issue 666', function() {
7899

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

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

108130
expect(btn).not.toHaveClass('active');
131+
132+
btn[0].focus();
133+
var e = $.Event('keypress');
134+
e.which = 32;
135+
btn.trigger(e);
136+
137+
expect(btn).not.toHaveClass('active');
138+
139+
$scope.$digest();
140+
141+
expect(btn).not.toHaveClass('active');
142+
143+
btn.remove();
109144
});
110145

111146
describe('setting buttonConfig', function() {

0 commit comments

Comments
 (0)