Skip to content

Commit 93253df

Browse files
author
Marcy Sutton
committed
feat(ngAria): Roles added to custom inputs
Adds missing roles: slider, radio, checkbox Closes angular#10012
1 parent 9d53e5a commit 93253df

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

src/ngAria/aria.js

+10
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,10 @@ ngAriaModule.directive('ngShow', ['$aria', function($aria) {
192192
return $aria.config(normalizedAttr) && !elem.attr(attr);
193193
}
194194

195+
function shouldAttachRole(role, elem) {
196+
return !elem.attr('role') && (elem.attr('type') === role) && (elem[0].nodeName !== 'INPUT');
197+
}
198+
195199
function getShape(attr, elem) {
196200
var type = attr.type,
197201
role = attr.role;
@@ -235,12 +239,18 @@ ngAriaModule.directive('ngShow', ['$aria', function($aria) {
235239
switch (shape) {
236240
case 'radio':
237241
case 'checkbox':
242+
if (shouldAttachRole(shape, elem)) {
243+
elem.attr('role', shape);
244+
}
238245
if (shouldAttachAttr('aria-checked', 'ariaChecked', elem)) {
239246
scope.$watch(ngAriaWatchModelValue, shape === 'radio' ?
240247
getRadioReaction() : ngAriaCheckboxReaction);
241248
}
242249
break;
243250
case 'range':
251+
if (shouldAttachRole(shape, elem)) {
252+
elem.attr('role', 'slider');
253+
}
244254
if ($aria.config('ariaValue')) {
245255
if (attr.min && !elem.attr('aria-valuemin')) {
246256
elem.attr('aria-valuemin', attr.min);

test/ngAria/ariaSpec.js

+39
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ describe('$aria', function() {
55

66
beforeEach(module('ngAria'));
77

8+
afterEach(function(){
9+
dealoc(element);
10+
});
11+
812
function injectScopeAndCompiler() {
913
return inject(function(_$compile_, _$rootScope_) {
1014
$compile = _$compile_;
@@ -136,6 +140,41 @@ describe('$aria', function() {
136140
});
137141
});
138142

143+
describe('roles for custom inputs', function() {
144+
beforeEach(injectScopeAndCompiler);
145+
146+
it('should add missing role="checkbox" to custom input', function() {
147+
scope.$apply('val = true');
148+
compileInput('<div type="checkbox" ng-model="val"></div>');
149+
expect(element.attr('role')).toBe('checkbox');
150+
});
151+
it('should not add a role to a native checkbox', function() {
152+
scope.$apply('val = true');
153+
compileInput('<input type="checkbox" ng-model="val"></div>');
154+
expect(element.attr('role')).toBe(undefined);
155+
});
156+
it('should add missing role="radio" to custom input', function() {
157+
scope.$apply('val = true');
158+
compileInput('<div type="radio" ng-model="val"></div>');
159+
expect(element.attr('role')).toBe('radio');
160+
});
161+
it('should not add a role to a native radio button', function() {
162+
scope.$apply('val = true');
163+
compileInput('<input type="radio" ng-model="val"></div>');
164+
expect(element.attr('role')).toBe(undefined);
165+
});
166+
it('should add missing role="slider" to custom input', function() {
167+
scope.$apply('val = true');
168+
compileInput('<div type="range" ng-model="val"></div>');
169+
expect(element.attr('role')).toBe('slider');
170+
});
171+
it('should not add a role to a native range input', function() {
172+
scope.$apply('val = true');
173+
compileInput('<input type="range" ng-model="val"></div>');
174+
expect(element.attr('role')).toBe(undefined);
175+
});
176+
});
177+
139178
describe('aria-checked when disabled', function() {
140179
beforeEach(configAriaProvider({
141180
ariaChecked: false

0 commit comments

Comments
 (0)