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

feat(autocomplete): allow select on match to be case insensitive. #6935

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 81 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 @@ -685,6 +685,85 @@ describe('<md-autocomplete>', function() {

element.remove();
}));

it('should ignore case when matching by default', 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">' +
'<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();
}));

it('should not select matching item when not ignoring case', 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="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).toBe(null);

element.remove();
}));

it('should select matching item when ignoring case', 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="false">' +
'<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
2 changes: 2 additions & 0 deletions src/components/autocomplete/demoBasicUsage/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
md-items="item in ctrl.querySearch(ctrl.searchText)"
md-item-text="item.display"
md-min-length="0"
md-select-on-match="ctrl.selectOnMatch"
placeholder="What is your favorite US state?">
<md-item-template>
<span md-highlight-text="ctrl.searchText" md-highlight-flags="^i">{{item.display}}</span>
Expand All @@ -25,6 +26,7 @@
<md-checkbox ng-model="ctrl.simulateQuery">Simulate query for results?</md-checkbox>
<md-checkbox ng-model="ctrl.noCache">Disable caching of queries?</md-checkbox>
<md-checkbox ng-model="ctrl.isDisabled">Disable the input?</md-checkbox>
<md-checkbox ng-model="ctrl.selectOnMatch">Select on Match?</md-checkbox>

<p>By default, <code>md-autocomplete</code> will cache results when performing a query. After the initial call is performed, it will use the cached results to eliminate unnecessary server requests or lookup logic. This can be disabled above.</p>
</form>
Expand Down
1 change: 1 addition & 0 deletions src/components/autocomplete/demoBasicUsage/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

self.simulateQuery = false;
self.isDisabled = false;
self.selectOnMatch = false;

// list of `state` value/display objects
self.states = loadAll();
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 @@ -738,7 +738,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.toLowerCase() == displayValue.toLowerCase();
if ($scope.matchCase) {
isMatching = searchText == displayValue;
}

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

Expand Down
3 changes: 3 additions & 0 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 When set and using `md-select-on-match`, autocomplete
* will match case sensitive. By default case will be ignored.
*
* @usage
* ### Basic Example
Expand Down Expand Up @@ -134,6 +136,7 @@ function MdAutocomplete () {
placeholder: '@placeholder',
noCache: '=?mdNoCache',
selectOnMatch: '=?mdSelectOnMatch',
matchCase: '=?mdMatchCase',
itemChange: '&?mdSelectedItemChange',
textChange: '&?mdSearchTextChange',
minLength: '=?mdMinLength',
Expand Down