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

Commit

Permalink
feat(autocomplete): adds a new attribute option md-select-on-match
Browse files Browse the repository at this point in the history
When enabled, an exact match in the `searchText` will automatically
select a matching item if (a) there is exactly one match found and (b)
the `searchText` matches the display value exactly

Closes #3324
  • Loading branch information
Robert Messerle committed Jul 20, 2015
1 parent d203d72 commit db58e5d
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 5 deletions.
58 changes: 53 additions & 5 deletions src/components/autocomplete/autocomplete.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe('<md-autocomplete>', function () {
}

describe('basic functionality', function () {
it('should update selected item and search text', inject(function ($timeout, $mdConstant, $rootElement) {
it('should update selected item and search text', inject(function ($timeout, $mdConstant) {
var scope = createScope();
var template = '\
<md-autocomplete\
Expand Down Expand Up @@ -136,7 +136,6 @@ describe('<md-autocomplete>', function () {
<span md-highlight-text="searchText">{{item.display}}</span>\
</md-autocomplete>';
var element = compile(template, scope);
var ctrl = element.controller('mdAutocomplete');
var ul = element.find('ul');

expect(scope.searchText).toBe('');
Expand All @@ -152,7 +151,7 @@ describe('<md-autocomplete>', function () {
});

describe('API access', function () {
it('should clear the selected item', inject(function ($timeout, $mdConstant) {
it('should clear the selected item', inject(function ($timeout) {
var scope = createScope();
var template = '\
<md-autocomplete\
Expand Down Expand Up @@ -187,7 +186,7 @@ describe('<md-autocomplete>', function () {
expect(scope.selectedItem).toBe(null);
}));

it('should notify selected item watchers', inject(function ($timeout, $mdConstant) {
it('should notify selected item watchers', inject(function ($timeout) {
var scope = createScope();
var scopeItemChanged = 1;
scope.itemChanged = jasmine.createSpy('itemChanged');
Expand Down Expand Up @@ -237,7 +236,7 @@ describe('<md-autocomplete>', function () {
expect(scope.itemChanged.calls.mostRecent().args[ 0 ]).toBeNull();
expect(scope.selectedItem).toBeNull();
}));
it('should pass value to item watcher', inject(function ($timeout, $mdConstant) {
it('should pass value to item watcher', inject(function ($timeout) {
var scope = createScope();
var itemValue = null;
var template = '\
Expand Down Expand Up @@ -272,4 +271,53 @@ describe('<md-autocomplete>', function () {
element.scope().$apply();
}));
});

describe('md-select-on-match', function () {
it('should select matching item on exact match when `md-select-on-match` is toggled', inject(function ($timeout) {
var scope = createScope();
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';
element.scope().$apply();
$timeout.flush();

expect(scope.selectedItem).not.toBe(null);
expect(scope.selectedItem.display).toBe('foo');
}));
});
it('should not select matching item on exact match when `md-select-on-match` is NOT toggled', inject(function ($timeout) {
var scope = createScope();
var template = '\
<md-autocomplete\
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';
element.scope().$apply();
$timeout.flush();

expect(scope.selectedItem).toBe(null);
}));
});
10 changes: 10 additions & 0 deletions src/components/autocomplete/js/autocompleteController.js
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,7 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $mdTheming,
if (searchText !== $scope.searchText) return; //-- just cache the results if old request
ctrl.matches = matches;
ctrl.hidden = shouldHide();
if ($scope.selectOnMatch) selectItemOnMatch();
updateMessages();
positionDropdown();
}
Expand Down Expand Up @@ -593,4 +594,13 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $mdTheming,
if (hasFocus) ctrl.hidden = shouldHide();
}

function selectItemOnMatch () {
var searchText = $scope.searchText,
matches = ctrl.matches,
item = matches[ 0 ];
if (matches.length === 1) getDisplayValue(item).then(function (displayValue) {
if (searchText == displayValue) select(0);
});
}

}
1 change: 1 addition & 0 deletions src/components/autocomplete/js/autocompleteDirective.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ function MdAutocomplete () {
itemText: '&mdItemText',
placeholder: '@placeholder',
noCache: '=?mdNoCache',
selectOnMatch: '=?mdSelectOnMatch',
itemChange: '&?mdSelectedItemChange',
textChange: '&?mdSearchTextChange',
minLength: '=?mdMinLength',
Expand Down

0 comments on commit db58e5d

Please sign in to comment.