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

Commit

Permalink
feat(autocomplete): support readonly attribute
Browse files Browse the repository at this point in the history
Fixes #5507

Closes #7107
  • Loading branch information
devversion authored and ThomasBurleson committed Mar 30, 2016
1 parent 210e8b0 commit 2a884c6
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
50 changes: 50 additions & 0 deletions src/components/autocomplete/autocomplete.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,56 @@ describe('<md-autocomplete>', function() {
element.remove();
}));

it('should allow allow using ng-readonly', inject(function() {
var scope = createScope(null, {inputId: 'custom-input-id'});
var template = '\
<md-autocomplete\
md-input-id="{{inputId}}"\
md-selected-item="selectedItem"\
md-search-text="searchText"\
md-items="item in match(searchText)"\
md-item-text="item.display"\
placeholder="placeholder"\
ng-readonly="readonly">\
<span md-highlight-text="searchText">{{item.display}}</span>\
</md-autocomplete>';
var element = compile(template, scope);
var input = element.find('input');

scope.readonly = true;
scope.$digest();

expect(input.attr('readonly')).toBe('readonly');

scope.readonly = false;
scope.$digest();

expect(input.attr('readonly')).toBeUndefined();

element.remove();
}));

it('should allow allow using an empty readonly attribute', inject(function() {
var scope = createScope(null, {inputId: 'custom-input-id'});
var template = '\
<md-autocomplete\
md-input-id="{{inputId}}"\
md-selected-item="selectedItem"\
md-search-text="searchText"\
md-items="item in match(searchText)"\
md-item-text="item.display"\
placeholder="placeholder"\
readonly>\
<span md-highlight-text="searchText">{{item.display}}</span>\
</md-autocomplete>';
var element = compile(template, scope);
var input = element.find('input');

expect(input.attr('readonly')).toBe('readonly');

element.remove();
}));

it('should allow you to set an input id with floating label', inject(function() {
var scope = createScope(null, {inputId: 'custom-input-id'});
var template = '\
Expand Down
2 changes: 2 additions & 0 deletions src/components/autocomplete/js/autocompleteController.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $mdTheming,
ctrl.id = $mdUtil.nextUid();
ctrl.isDisabled = null;
ctrl.isRequired = null;
ctrl.isReadonly = null;
ctrl.hasNotFound = false;

//-- public methods
Expand Down Expand Up @@ -166,6 +167,7 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $mdTheming,
var wait = parseInt($scope.delay, 10) || 0;
$attrs.$observe('disabled', function (value) { ctrl.isDisabled = $mdUtil.parseAttributeBoolean(value, false); });
$attrs.$observe('required', function (value) { ctrl.isRequired = $mdUtil.parseAttributeBoolean(value, false); });
$attrs.$observe('readonly', function (value) { ctrl.isReadonly = $mdUtil.parseAttributeBoolean(value, false); });
$scope.$watch('searchText', wait ? $mdUtil.debounce(handleSearchText, wait) : handleSearchText);
$scope.$watch('selectedItem', selectedItemChange);
angular.element($window).on('resize', positionDropdown);
Expand Down
2 changes: 2 additions & 0 deletions src/components/autocomplete/js/autocompleteDirective.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ function MdAutocomplete () {
name="{{inputName}}"\
autocomplete="off"\
ng-required="$mdAutocompleteCtrl.isRequired"\
ng-readonly="$mdAutocompleteCtrl.isReadonly"\
ng-minlength="inputMinlength"\
ng-maxlength="inputMaxlength"\
ng-disabled="$mdAutocompleteCtrl.isDisabled"\
Expand All @@ -263,6 +264,7 @@ function MdAutocomplete () {
autocomplete="off"\
ng-required="$mdAutocompleteCtrl.isRequired"\
ng-disabled="$mdAutocompleteCtrl.isDisabled"\
ng-readonly="$mdAutocompleteCtrl.isReadonly"\
ng-model="$mdAutocompleteCtrl.scope.searchText"\
ng-keydown="$mdAutocompleteCtrl.keydown($event)"\
ng-blur="$mdAutocompleteCtrl.blur()"\
Expand Down

0 comments on commit 2a884c6

Please sign in to comment.