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

Commit

Permalink
fix(textfloat): improve ARIA pairing links between label and input.
Browse files Browse the repository at this point in the history
Closes #483.
  • Loading branch information
ThomasBurleson committed Oct 28, 2014
1 parent 3674a51 commit 457b375
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
19 changes: 11 additions & 8 deletions src/components/textField/textField.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
angular.module('material.components.textField', ['material.core', 'material.services.theming'])
.directive('mdInputGroup', [ mdInputGroupDirective ])
.directive('mdInput', ['$mdUtil', mdInputDirective ])
.directive('mdTextFloat', [ '$mdTheming', mdTextFloatDirective ]);
.directive('mdTextFloat', [ '$mdTheming', '$mdUtil', mdTextFloatDirective ]);



Expand All @@ -21,9 +21,10 @@ angular.module('material.components.textField', ['material.core', 'material.serv
* @description
* Use the `<md-text-float>` directive to quickly construct `Floating Label` text fields
*
* @param {string} fid Attribute used for accessibility link pairing between the Label and Input elements
* @param {string=} type Optional value to define the type of input field. Defaults to string.
* @param {string} label Attribute to specify the input text field hint.
* @param {string=} ng-model Optional value to assign as existing input text string
* @param {string=} type Optional value to define the type of input field. Defaults to string.
*
* @usage
* <hljs lang="html">
Expand All @@ -36,7 +37,7 @@ angular.module('material.components.textField', ['material.core', 'material.serv
* <md-text-float label="eMail" ng-model="user.email" type="email" ></md-text-float>
* </hljs>
*/
function mdTextFloatDirective($mdTheming) {
function mdTextFloatDirective($mdTheming, $mdUtil) {
return {
restrict: 'E',
replace: true,
Expand All @@ -45,7 +46,12 @@ function mdTextFloatDirective($mdTheming) {
label : '@?',
value : '=ngModel'
},
compile : function() {
compile : function(element, attr) {

if ( angular.isUndefined(attr.fid) ) {
attr.fid = $mdUtil.nextUid();
}

return {
pre : function(scope, element, attrs) {
// transpose `disabled` flag
Expand All @@ -54,13 +60,10 @@ function mdTextFloatDirective($mdTheming) {
scope.isDisabled = true;
}

// transpose the `label`, type, and fid properties
scope.fid = scope.fid || scope.label;

scope.inputType = attrs.type || "text";
element.removeAttr('type');

// transpose optional `type` and `class` settings
// transpose optional `class` settings
element.attr('class', attrs.class );

},
Expand Down
27 changes: 26 additions & 1 deletion src/components/textField/textField.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,6 @@ describe('Text Field directives', function() {
input.triggerHandler('input');
expect(el.hasClass('md-input-has-value')).toBe(true);
});

it('should set input class for ngModel changes', function() {
var expressions = {label:"Password", model:"user.password", type:"password"},
el = setupTextFloat( expressions, model),
Expand All @@ -188,6 +187,32 @@ describe('Text Field directives', function() {
expect(el.hasClass('md-input-has-value')).toBe(true);
});

it('should pair input and label for accessibility.', function() {
var markup ='<md-text-float ' +
' fid="093" ' +
' label="{{labels.firstName}}" ' +
' ng-model="user.firstName" >' +
'</md-text-float>';
var el = buildElement( markup, model);
var input = el.find('input');
var label = el.find('label')

expect( label.attr('for') ).toBe( "093" );
expect( input.attr('id') ).toBe( label.attr('for') );
});
it('should auto-pair input and label for accessibility.', function() {
var markup ='<md-text-float ' +
' label="{{labels.firstName}}" ' +
' ng-model="user.firstName" >' +
'</md-text-float>';
var el = buildElement( markup, model);
var input = el.find('input');
var label = el.find('label')

expect( label.attr('for') == "" ).toBe( false );
expect( input.attr('id') ).toBe( label.attr('for') );
});

});

// ****************************************************************
Expand Down

1 comment on commit 457b375

@marcysutton
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!! Love the unit tests! 👍

Please sign in to comment.