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

Commit 2136994

Browse files
Marcy Suttonpetebacondarwin
Marcy Sutton
authored andcommitted
feat(ngAria): add roles to custom inputs
This change adds the missing roles: `slider`, `radio`, `checkbox` Closes #10012 Closes #10318
1 parent 190ea88 commit 2136994

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
@@ -193,6 +193,10 @@ ngAriaModule.directive('ngShow', ['$aria', function($aria) {
193193
return $aria.config(normalizedAttr) && !elem.attr(attr);
194194
}
195195

196+
function shouldAttachRole(role, elem) {
197+
return !elem.attr('role') && (elem.attr('type') === role) && (elem[0].nodeName !== 'INPUT');
198+
}
199+
196200
function getShape(attr, elem) {
197201
var type = attr.type,
198202
role = attr.role;
@@ -237,12 +241,18 @@ ngAriaModule.directive('ngShow', ['$aria', function($aria) {
237241
switch (shape) {
238242
case 'radio':
239243
case 'checkbox':
244+
if (shouldAttachRole(shape, elem)) {
245+
elem.attr('role', shape);
246+
}
240247
if (shouldAttachAttr('aria-checked', 'ariaChecked', elem)) {
241248
scope.$watch(ngAriaWatchModelValue, shape === 'radio' ?
242249
getRadioReaction() : ngAriaCheckboxReaction);
243250
}
244251
break;
245252
case 'range':
253+
if (shouldAttachRole(shape, elem)) {
254+
elem.attr('role', 'slider');
255+
}
246256
if ($aria.config('ariaValue')) {
247257
if (attr.min && !elem.attr('aria-valuemin')) {
248258
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_;
@@ -188,6 +192,41 @@ describe('$aria', function() {
188192
});
189193
});
190194

195+
describe('roles for custom inputs', function() {
196+
beforeEach(injectScopeAndCompiler);
197+
198+
it('should add missing role="checkbox" to custom input', function() {
199+
scope.$apply('val = true');
200+
compileInput('<div type="checkbox" ng-model="val"></div>');
201+
expect(element.attr('role')).toBe('checkbox');
202+
});
203+
it('should not add a role to a native checkbox', function() {
204+
scope.$apply('val = true');
205+
compileInput('<input type="checkbox" ng-model="val"></div>');
206+
expect(element.attr('role')).toBe(undefined);
207+
});
208+
it('should add missing role="radio" to custom input', function() {
209+
scope.$apply('val = true');
210+
compileInput('<div type="radio" ng-model="val"></div>');
211+
expect(element.attr('role')).toBe('radio');
212+
});
213+
it('should not add a role to a native radio button', function() {
214+
scope.$apply('val = true');
215+
compileInput('<input type="radio" ng-model="val"></div>');
216+
expect(element.attr('role')).toBe(undefined);
217+
});
218+
it('should add missing role="slider" to custom input', function() {
219+
scope.$apply('val = true');
220+
compileInput('<div type="range" ng-model="val"></div>');
221+
expect(element.attr('role')).toBe('slider');
222+
});
223+
it('should not add a role to a native range input', function() {
224+
scope.$apply('val = true');
225+
compileInput('<input type="range" ng-model="val"></div>');
226+
expect(element.attr('role')).toBe(undefined);
227+
});
228+
});
229+
191230
describe('aria-checked when disabled', function() {
192231
beforeEach(configAriaProvider({
193232
ariaChecked: false

0 commit comments

Comments
 (0)