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

feat(buttons): allow toggling via spacebar when focused #4259

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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