Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

Commit

Permalink
feat(ngAria): Roles added to custom inputs
Browse files Browse the repository at this point in the history
Adds missing roles: slider, radio, checkbox
Closes #10012
  • Loading branch information
Marcy Sutton committed Feb 7, 2015
1 parent 9d53e5a commit 93253df
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/ngAria/aria.js
Expand Up @@ -192,6 +192,10 @@ ngAriaModule.directive('ngShow', ['$aria', function($aria) {
return $aria.config(normalizedAttr) && !elem.attr(attr);
}

function shouldAttachRole(role, elem) {
return !elem.attr('role') && (elem.attr('type') === role) && (elem[0].nodeName !== 'INPUT');
}

function getShape(attr, elem) {
var type = attr.type,
role = attr.role;
Expand Down Expand Up @@ -235,12 +239,18 @@ ngAriaModule.directive('ngShow', ['$aria', function($aria) {
switch (shape) {
case 'radio':
case 'checkbox':
if (shouldAttachRole(shape, elem)) {
elem.attr('role', shape);
}
if (shouldAttachAttr('aria-checked', 'ariaChecked', elem)) {
scope.$watch(ngAriaWatchModelValue, shape === 'radio' ?
getRadioReaction() : ngAriaCheckboxReaction);
}
break;
case 'range':
if (shouldAttachRole(shape, elem)) {
elem.attr('role', 'slider');
}
if ($aria.config('ariaValue')) {
if (attr.min && !elem.attr('aria-valuemin')) {
elem.attr('aria-valuemin', attr.min);
Expand Down
39 changes: 39 additions & 0 deletions test/ngAria/ariaSpec.js
Expand Up @@ -5,6 +5,10 @@ describe('$aria', function() {

beforeEach(module('ngAria'));

afterEach(function(){
dealoc(element);
});

function injectScopeAndCompiler() {
return inject(function(_$compile_, _$rootScope_) {
$compile = _$compile_;
Expand Down Expand Up @@ -136,6 +140,41 @@ describe('$aria', function() {
});
});

describe('roles for custom inputs', function() {
beforeEach(injectScopeAndCompiler);

it('should add missing role="checkbox" to custom input', function() {
scope.$apply('val = true');
compileInput('<div type="checkbox" ng-model="val"></div>');
expect(element.attr('role')).toBe('checkbox');
});
it('should not add a role to a native checkbox', function() {
scope.$apply('val = true');
compileInput('<input type="checkbox" ng-model="val"></div>');
expect(element.attr('role')).toBe(undefined);
});
it('should add missing role="radio" to custom input', function() {
scope.$apply('val = true');
compileInput('<div type="radio" ng-model="val"></div>');
expect(element.attr('role')).toBe('radio');
});
it('should not add a role to a native radio button', function() {
scope.$apply('val = true');
compileInput('<input type="radio" ng-model="val"></div>');
expect(element.attr('role')).toBe(undefined);
});
it('should add missing role="slider" to custom input', function() {
scope.$apply('val = true');
compileInput('<div type="range" ng-model="val"></div>');
expect(element.attr('role')).toBe('slider');
});
it('should not add a role to a native range input', function() {
scope.$apply('val = true');
compileInput('<input type="range" ng-model="val"></div>');
expect(element.attr('role')).toBe(undefined);
});
});

describe('aria-checked when disabled', function() {
beforeEach(configAriaProvider({
ariaChecked: false
Expand Down

0 comments on commit 93253df

Please sign in to comment.