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

Commit 9931e2a

Browse files
author
Robert Messerle
committed
feat(autocomplete): adds md-input-id to allow the user to provide a custom ID for autocomplete input field
Closes #3481
1 parent eda4782 commit 9931e2a

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

src/components/autocomplete/autocomplete.spec.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,43 @@ describe('<md-autocomplete>', function () {
6969
expect(scope.searchText).toBe('foo');
7070
expect(scope.selectedItem).toBe(scope.match(scope.searchText)[ 0 ]);
7171
}));
72+
73+
it('should allow you to set an input id without floating label', inject(function () {
74+
var scope = createScope(null, { inputId: 'custom-input-id' });
75+
var template = '\
76+
<md-autocomplete\
77+
md-input-id="{{inputId}}"\
78+
md-selected-item="selectedItem"\
79+
md-search-text="searchText"\
80+
md-items="item in match(searchText)"\
81+
md-item-text="item.display"\
82+
placeholder="placeholder">\
83+
<span md-highlight-text="searchText">{{item.display}}</span>\
84+
</md-autocomplete>';
85+
var element = compile(template, scope);
86+
var input = element.find('input');
87+
88+
expect(input.attr('id')).toBe(scope.inputId);
89+
}));
90+
91+
it('should allow you to set an input id without floating label', inject(function () {
92+
var scope = createScope(null, { inputId: 'custom-input-id' });
93+
var template = '\
94+
<md-autocomplete\
95+
md-floating-label="Some Label"\
96+
md-input-id="{{inputId}}"\
97+
md-selected-item="selectedItem"\
98+
md-search-text="searchText"\
99+
md-items="item in match(searchText)"\
100+
md-item-text="item.display"\
101+
placeholder="placeholder">\
102+
<span md-highlight-text="searchText">{{item.display}}</span>\
103+
</md-autocomplete>';
104+
var element = compile(template, scope);
105+
var input = element.find('input');
106+
107+
expect(input.attr('id')).toBe(scope.inputId);
108+
}));
72109
});
73110

74111
describe('basic functionality with template', function () {

src/components/autocomplete/js/autocompleteDirective.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ angular
5656
* `md-input-container`
5757
* @param {string=} md-input-name The name attribute given to the input element to be used with
5858
* FormController
59+
* @param {string=} md-input-id An ID to be added to the input element
5960
* @param {number=} md-input-minlength The minimum length for the input's value for validation
6061
* @param {number=} md-input-maxlength The maximum length for the input's value for validation
6162
* @param {boolean=} md-select-on-match When set, autocomplete will automatically select exact
@@ -138,7 +139,8 @@ function MdAutocomplete () {
138139
autofocus: '=?mdAutofocus',
139140
floatingLabel: '@?mdFloatingLabel',
140141
autoselect: '=?mdAutoselect',
141-
menuClass: '@?mdMenuClass'
142+
menuClass: '@?mdMenuClass',
143+
inputId: '@?mdInputId'
142144
},
143145
template: function (element, attr) {
144146
var noItemsTemplate = getNoItemsTemplate(),
@@ -201,7 +203,7 @@ function MdAutocomplete () {
201203
<md-input-container flex ng-if="floatingLabel">\
202204
<label>{{floatingLabel}}</label>\
203205
<input type="search"\
204-
id="fl-input-{{$mdAutocompleteCtrl.id}}"\
206+
id="{{ inputId || \'fl-input-\' + $mdAutocompleteCtrl.id }}"\
205207
name="{{inputName}}"\
206208
autocomplete="off"\
207209
ng-required="$mdAutocompleteCtrl.isRequired"\
@@ -223,7 +225,7 @@ function MdAutocomplete () {
223225
} else {
224226
return '\
225227
<input flex type="search"\
226-
id="input-{{$mdAutocompleteCtrl.id}}"\
228+
id="{{ inputId || \'input-\' + $mdAutocompleteCtrl.id }}"\
227229
name="{{inputName}}"\
228230
ng-if="!floatingLabel"\
229231
autocomplete="off"\

0 commit comments

Comments
 (0)