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

Commit 0032184

Browse files
devversionkara
authored andcommitted
feat(autocomplete): support ng-trim on the underlaying input (#9496)
* A lot of developers requested a way to pass the `ng-trim` attribute to the underlaying input element. This is reasonable, because ngTrim always trims the searchText and can cause deviations with the search results. * Also added a more generic way of forwarding the the (currently) three attributes to the input element, without doing this in the template. Fixes #4492
1 parent 0ad96ba commit 0032184

File tree

2 files changed

+42
-7
lines changed

2 files changed

+42
-7
lines changed

src/components/autocomplete/autocomplete.spec.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,31 @@ describe('<md-autocomplete>', function() {
374374
element.remove();
375375
}));
376376

377+
it('should support ng-trim for the search input', inject(function() {
378+
var scope = createScope(null, {inputId: 'custom-input-id'});
379+
var template =
380+
'<md-autocomplete ' +
381+
'md-selected-item="selectedItem" ' +
382+
'md-search-text="searchText" ' +
383+
'md-items="item in match(searchText)" ' +
384+
'md-item-text="item.display" ' +
385+
'ng-trim="false" ' +
386+
'placeholder="placeholder">' +
387+
'<span md-highlight-text="searchText">{{item.display}}</span>' +
388+
'</md-autocomplete>';
389+
390+
var element = compile(template, scope);
391+
var input = element.find('input');
392+
393+
expect(input.attr('ng-trim')).toBe("false");
394+
395+
scope.$apply('searchText = " Text "');
396+
397+
expect(scope.searchText).not.toBe('Text');
398+
399+
element.remove();
400+
}));
401+
377402
it('forwards the tabindex to the input', inject(function() {
378403
var scope = createScope(null, {inputId: 'custom-input-id'});
379404
var template =

src/components/autocomplete/js/autocompleteDirective.js

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,23 @@ function MdAutocomplete ($$mdSvgRegistry) {
167167
inputId: '@?mdInputId',
168168
escapeOptions: '@?mdEscapeOptions'
169169
},
170-
link: function(scope, element, attrs, controller) {
171-
// Retrieve the state of using a md-not-found template by using our attribute, which will
172-
// be added to the element in the template function.
173-
controller.hasNotFound = !!element.attr('md-has-not-found');
170+
compile: function(tElement, tAttrs) {
171+
var attributes = ['md-select-on-focus', 'md-no-asterisk', 'ng-trim'];
172+
var input = tElement.find('input');
173+
174+
attributes.forEach(function(attribute) {
175+
var attrValue = tAttrs[tAttrs.$normalize(attribute)];
176+
177+
if (attrValue !== null) {
178+
input.attr(attribute, attrValue);
179+
}
180+
});
181+
182+
return function(scope, element, attrs, ctrl) {
183+
// Retrieve the state of using a md-not-found template by using our attribute, which will
184+
// be added to the element in the template function.
185+
ctrl.hasNotFound = !!element.attr('md-has-not-found');
186+
}
174187
},
175188
template: function (element, attr) {
176189
var noItemsTemplate = getNoItemsTemplate(),
@@ -262,8 +275,6 @@ function MdAutocomplete ($$mdSvgRegistry) {
262275
ng-blur="$mdAutocompleteCtrl.blur($event)"\
263276
ng-focus="$mdAutocompleteCtrl.focus($event)"\
264277
aria-owns="ul-{{$mdAutocompleteCtrl.id}}"\
265-
' + (attr.mdNoAsterisk != null ? 'md-no-asterisk="' + attr.mdNoAsterisk + '"' : '') + '\
266-
' + (attr.mdSelectOnFocus != null ? 'md-select-on-focus=""' : '') + '\
267278
aria-label="{{floatingLabel}}"\
268279
aria-autocomplete="list"\
269280
role="combobox"\
@@ -289,7 +300,6 @@ function MdAutocomplete ($$mdSvgRegistry) {
289300
ng-focus="$mdAutocompleteCtrl.focus($event)"\
290301
placeholder="{{placeholder}}"\
291302
aria-owns="ul-{{$mdAutocompleteCtrl.id}}"\
292-
' + (attr.mdSelectOnFocus != null ? 'md-select-on-focus=""' : '') + '\
293303
aria-label="{{placeholder}}"\
294304
aria-autocomplete="list"\
295305
role="combobox"\

0 commit comments

Comments
 (0)