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

Commit c9a215b

Browse files
devversionkara
authored andcommitted
feat(autocomplete): support ng-pattern attribute (#9759)
* Added support for ng-pattern attribute on the autocomplete * Added ng-trim to supported attribute docs as well. Fixes #9755. References #4492.
1 parent 974acd3 commit c9a215b

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

src/components/autocomplete/autocomplete.spec.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,38 @@ describe('<md-autocomplete>', function() {
399399
element.remove();
400400
}));
401401

402+
it('should support ng-pattern for the search input', inject(function() {
403+
var scope = createScope(null, {inputId: 'custom-input-id'});
404+
var template =
405+
'<form name="testForm">' +
406+
'<md-autocomplete ' +
407+
'md-input-name="autocomplete" ' +
408+
'md-selected-item="selectedItem" ' +
409+
'md-search-text="searchText" ' +
410+
'md-items="item in match(searchText)" ' +
411+
'md-item-text="item.display" ' +
412+
'ng-pattern="/^[0-9]$/" ' +
413+
'placeholder="placeholder">' +
414+
'<span md-highlight-text="searchText">{{item.display}}</span>' +
415+
'</md-autocomplete>' +
416+
'</form>';
417+
418+
var element = compile(template, scope);
419+
var input = element.find('input');
420+
421+
expect(input.attr('ng-pattern')).toBeTruthy();
422+
423+
scope.$apply('searchText = "Test"');
424+
425+
expect(scope.testForm.autocomplete.$error['pattern']).toBeTruthy();
426+
427+
scope.$apply('searchText = "3"');
428+
429+
expect(scope.testForm.autocomplete.$error['pattern']).toBeFalsy();
430+
431+
element.remove();
432+
}));
433+
402434
it('forwards the tabindex to the input', inject(function() {
403435
var scope = createScope(null, {inputId: 'custom-input-id'});
404436
var template =

src/components/autocomplete/js/autocompleteDirective.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ angular
8888
* the dropdown.<br/><br/>
8989
* When the dropdown doesn't fit into the viewport, the dropdown will shrink
9090
* as less as possible.
91+
* @param {string=} ng-trim If set to false, the search text will be not trimmed automatically.
92+
* Defaults to true.
93+
* @param {string=} ng-pattern Adds the pattern validator to the ngModel of the search text.
94+
* [ngPattern Directive](https://docs.angularjs.org/api/ng/directive/ngPattern)
9195
*
9296
* @usage
9397
* ### Basic Example
@@ -203,7 +207,7 @@ function MdAutocomplete ($$mdSvgRegistry) {
203207
dropdownItems: '=?mdDropdownItems'
204208
},
205209
compile: function(tElement, tAttrs) {
206-
var attributes = ['md-select-on-focus', 'md-no-asterisk', 'ng-trim'];
210+
var attributes = ['md-select-on-focus', 'md-no-asterisk', 'ng-trim', 'ng-pattern'];
207211
var input = tElement.find('input');
208212

209213
attributes.forEach(function(attribute) {

0 commit comments

Comments
 (0)