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

Commit

Permalink
feat(autocomplete): allow select on match to be case insensitive.
Browse files Browse the repository at this point in the history
Fixes #5782

Closes #6935
  • Loading branch information
devversion authored and ThomasBurleson committed Feb 4, 2016
1 parent 9179c61 commit 3fffde7
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 22 deletions.
31 changes: 29 additions & 2 deletions src/components/autocomplete/autocomplete.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe('<md-autocomplete>', function() {
return container;
}

function createScope(items, obj) {
function createScope(items, obj, matchLowercase) {
var scope;
items = items || ['foo', 'bar', 'baz'].map(function(item) {
return {display: item};
Expand All @@ -20,7 +20,7 @@ describe('<md-autocomplete>', function() {
scope = $rootScope.$new();
scope.match = function(term) {
return items.filter(function(item) {
return item.display.indexOf(term) === 0;
return item.display.indexOf(matchLowercase ? term.toLowerCase() : term) === 0;
});
};
scope.searchText = '';
Expand Down Expand Up @@ -797,6 +797,33 @@ describe('<md-autocomplete>', function() {

element.remove();
}));

it('should select matching item using case insensitive', inject(function($timeout) {
var scope = createScope(null, null, true);
var template =
'<md-autocomplete ' +
'md-select-on-match ' +
'md-selected-item="selectedItem" ' +
'md-search-text="searchText" ' +
'md-items="item in match(searchText)" ' +
'md-item-text="item.display" ' +
'placeholder="placeholder" ' +
'md-match-case-insensitive="true">' +
'<span md-highlight-text="searchText">{{item.display}}</span>' +
'</md-autocomplete>';
var element = compile(template, scope);

expect(scope.searchText).toBe('');
expect(scope.selectedItem).toBe(null);

element.scope().searchText = 'FoO';
$timeout.flush();

expect(scope.selectedItem).not.toBe(null);
expect(scope.selectedItem.display).toBe('foo');

element.remove();
}));
});

describe('when required', function() {
Expand Down
7 changes: 6 additions & 1 deletion src/components/autocomplete/js/autocompleteController.js
Original file line number Diff line number Diff line change
Expand Up @@ -741,7 +741,12 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $mdTheming,
matches = ctrl.matches,
item = matches[ 0 ];
if (matches.length === 1) getDisplayValue(item).then(function (displayValue) {
if (searchText == displayValue) select(0);
var isMatching = searchText == displayValue;
if ($scope.matchInsensitive && !isMatching) {
isMatching = searchText.toLowerCase() == displayValue.toLowerCase();
}

if (isMatching) select(0);
});
}

Expand Down
41 changes: 22 additions & 19 deletions src/components/autocomplete/js/autocompleteDirective.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ angular
* @param {number=} md-input-maxlength The maximum length for the input's value for validation
* @param {boolean=} md-select-on-match When set, autocomplete will automatically select exact
* the item if the search text is an exact match
* @param {boolean=} md-match-case-insensitive When set and using `md-select-on-match`, autocomplete
* will select on case-insensitive match
*
* @usage
* ### Basic Example
Expand Down Expand Up @@ -123,25 +125,26 @@ function MdAutocomplete () {
controller: 'MdAutocompleteCtrl',
controllerAs: '$mdAutocompleteCtrl',
scope: {
inputName: '@mdInputName',
inputMinlength: '@mdInputMinlength',
inputMaxlength: '@mdInputMaxlength',
searchText: '=?mdSearchText',
selectedItem: '=?mdSelectedItem',
itemsExpr: '@mdItems',
itemText: '&mdItemText',
placeholder: '@placeholder',
noCache: '=?mdNoCache',
selectOnMatch: '=?mdSelectOnMatch',
itemChange: '&?mdSelectedItemChange',
textChange: '&?mdSearchTextChange',
minLength: '=?mdMinLength',
delay: '=?mdDelay',
autofocus: '=?mdAutofocus',
floatingLabel: '@?mdFloatingLabel',
autoselect: '=?mdAutoselect',
menuClass: '@?mdMenuClass',
inputId: '@?mdInputId'
inputName: '@mdInputName',
inputMinlength: '@mdInputMinlength',
inputMaxlength: '@mdInputMaxlength',
searchText: '=?mdSearchText',
selectedItem: '=?mdSelectedItem',
itemsExpr: '@mdItems',
itemText: '&mdItemText',
placeholder: '@placeholder',
noCache: '=?mdNoCache',
selectOnMatch: '=?mdSelectOnMatch',
matchInsensitive: '=?mdMatchCaseInsensitive',
itemChange: '&?mdSelectedItemChange',
textChange: '&?mdSearchTextChange',
minLength: '=?mdMinLength',
delay: '=?mdDelay',
autofocus: '=?mdAutofocus',
floatingLabel: '@?mdFloatingLabel',
autoselect: '=?mdAutoselect',
menuClass: '@?mdMenuClass',
inputId: '@?mdInputId'
},
link: function(scope, element, attrs, controller) {
controller.hasNotFound = element.hasNotFoundTemplate;
Expand Down

0 comments on commit 3fffde7

Please sign in to comment.