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

Commit

Permalink
feat(mdRadioGroup): Radio submits on keydown/enter
Browse files Browse the repository at this point in the history
Closes #577
  • Loading branch information
Marcy Sutton committed Jan 8, 2015
1 parent bfc947f commit 03c7592
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/components/radioButton/demoBasicUsage/index.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

<div ng-controller="AppCtrl" >
<form ng-submit="submit()" ng-controller="AppCtrl" >
<p>Selected Value: <span class="radioValue">{{ data.group1 }}</span> </p>

<md-radio-group ng-model="data.group1">
Expand Down Expand Up @@ -30,4 +30,4 @@
<md-button ng-click="removeItem()">Remove</md-button>
</p>

</div>
</form>
4 changes: 4 additions & 0 deletions src/components/radioButton/demoBasicUsage/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ angular.module('radioDemo1', ['ngMaterial'])
{ label: '4', value: '4' }
];

$scope.submit = function() {
alert('submit');
};

$scope.addItem = function() {
var r = Math.ceil(Math.random() * 1000);
$scope.radioData.push({ label: r, value: r });
Expand Down
26 changes: 19 additions & 7 deletions src/components/radioButton/radioButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,25 @@ function mdRadioGroupDirective($mdUtil, $mdConstant, $mdTheming) {
var ngModelCtrl = ctrls[1] || $mdUtil.fakeNgModel();

function keydownListener(ev) {
if (ev.keyCode === $mdConstant.KEY_CODE.LEFT_ARROW || ev.keyCode === $mdConstant.KEY_CODE.UP_ARROW) {
ev.preventDefault();
rgCtrl.selectPrevious();
}
else if (ev.keyCode === $mdConstant.KEY_CODE.RIGHT_ARROW || ev.keyCode === $mdConstant.KEY_CODE.DOWN_ARROW) {
ev.preventDefault();
rgCtrl.selectNext();
switch(ev.keyCode) {
case $mdConstant.KEY_CODE.LEFT_ARROW:
case $mdConstant.KEY_CODE.UP_ARROW:
ev.preventDefault();
rgCtrl.selectPrevious();
break;

case $mdConstant.KEY_CODE.RIGHT_ARROW:
case $mdConstant.KEY_CODE.DOWN_ARROW:
ev.preventDefault();
rgCtrl.selectNext();
break;

case $mdConstant.KEY_CODE.ENTER:
var form = angular.element($mdUtil.getClosest(element[0], 'form'));
if (form.length > 0) {
form.triggerHandler('submit');
}
break;
}
}

Expand Down
23 changes: 23 additions & 0 deletions src/components/radioButton/radioButton.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,29 @@ describe('radioButton', function() {
expect($rootScope.color).toBe('blue');
}));

it('should trigger a submit', inject(function($compile, $rootScope, $mdConstant) {

$rootScope.testValue = false;
$rootScope.submitFn = function(){
$rootScope.testValue = true;
};
var element = $compile('<div><form ng-submit="submitFn()">' +
'<md-radio-group ng-model="color">' +
'<md-radio-button value="white"></md-radio-button>' +
'</md-radio-group>' +
'</form></div>')($rootScope);

var formElement = element.find('form'),
rbGroupElement = element.find('md-radio-group');

rbGroupElement.triggerHandler({
type: 'keydown',
keyCode: $mdConstant.KEY_CODE.ENTER
});

expect($rootScope.testValue).toBe(true);
}));

it('should be disabled', inject(function($compile, $rootScope) {
var element = $compile('<md-radio-group ng-model="color">' +
'<md-radio-button value="white" ng-disabled="isDisabled"></md-radio-button>' +
Expand Down
15 changes: 15 additions & 0 deletions src/core/util/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,21 @@ angular.module('material.core')
} else {
parent.$$childHead = parent.$$childTail = child;
}
},
/*
* getClosest replicates jQuery.closest() to walk up the DOM tree until it finds a matching nodeName
*
* @param el Element to start walking the DOM from
* @param tagName Tag name to find closest to el, such as 'form'
*/
getClosest: function getClosest(el, tagName) {
tagName = tagName.toUpperCase();
do {
if (el.nodeName === tagName) {
return el;
}
} while (el = el.parentNode);
return null;
}
};

Expand Down

0 comments on commit 03c7592

Please sign in to comment.