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

Commit 0fe831a

Browse files
Splaktarmmalerba
authored andcommitted
fix(autocomplete): don't show the menu panel when readonly (#11245)
this could sometimes happen when no value was selected Fixes #11231
1 parent e0078d7 commit 0fe831a

File tree

2 files changed

+62
-8
lines changed

2 files changed

+62
-8
lines changed

src/components/autocomplete/autocomplete.spec.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,45 @@ describe('<md-autocomplete>', function() {
299299
element.remove();
300300
}));
301301

302+
it('does not open panel when ng-readonly is true', inject(function() {
303+
var scope = createScope(null, {inputId: 'custom-input-id'});
304+
var template = '\
305+
<md-autocomplete\
306+
md-input-id="{{inputId}}"\
307+
md-selected-item="selectedItem"\
308+
md-search-text="searchText"\
309+
md-items="item in match(searchText)"\
310+
md-item-text="item.display"\
311+
placeholder="placeholder"\
312+
md-min-length="0"\
313+
ng-readonly="readonly">\
314+
<span md-highlight-text="searchText">{{item.display}}</span>\
315+
</md-autocomplete>';
316+
var element = compile(template, scope);
317+
var ctrl = element.controller('mdAutocomplete');
318+
var input = element.find('input');
319+
320+
scope.readonly = false;
321+
scope.$digest();
322+
ctrl.focus();
323+
waitForVirtualRepeat();
324+
325+
expect(input.attr('readonly')).toBeUndefined();
326+
expect(ctrl.hidden).toBe(false);
327+
328+
ctrl.blur();
329+
scope.readonly = true;
330+
scope.$digest();
331+
expect(ctrl.hidden).toBe(true);
332+
ctrl.focus();
333+
waitForVirtualRepeat();
334+
335+
expect(input.attr('readonly')).toBe('readonly');
336+
expect(ctrl.hidden).toBe(true);
337+
338+
element.remove();
339+
}));
340+
302341
it('should forward focus to the input element with md-autofocus', inject(function($timeout) {
303342

304343
var scope = createScope();

src/components/autocomplete/js/autocompleteController.js

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -660,21 +660,28 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $mdTheming,
660660

661661
/**
662662
* Determines if the menu should be hidden.
663-
* @returns {boolean}
663+
* @returns {boolean} true if the menu should be hidden
664664
*/
665665
function shouldHide () {
666-
if (!isSearchable()) return true; // Hide when not able to query
667-
else return !shouldShow(); // Hide when the dropdown is not able to show.
666+
return !shouldShow();
668667
}
669668

670669
/**
671670
* Determines whether the autocomplete is able to query within the current state.
672-
* @returns {boolean}
671+
* @returns {boolean} true if the query can be run
673672
*/
674673
function isSearchable() {
675-
if (ctrl.loading && !hasMatches()) return false; // No query when query is in progress.
676-
else if (hasSelection()) return false; // No query if there is already a selection
677-
else if (!hasFocus) return false; // No query if the input does not have focus
674+
if (ctrl.loading && !hasMatches()) {
675+
// No query when query is in progress.
676+
return false;
677+
} else if (hasSelection()) {
678+
// No query if there is already a selection
679+
return false;
680+
}
681+
else if (!hasFocus) {
682+
// No query if the input does not have focus
683+
return false;
684+
}
678685
return true;
679686
}
680687

@@ -696,9 +703,17 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $mdTheming,
696703

697704
/**
698705
* Determines if the menu should be shown.
699-
* @returns {boolean}
706+
* @returns {boolean} true if the menu should be shown
700707
*/
701708
function shouldShow() {
709+
if (ctrl.isReadonly) {
710+
// Don't show if read only is set
711+
return false;
712+
} else if (!isSearchable()) {
713+
// Don't show if a query is in progress, there is already a selection,
714+
// or the input is not focused.
715+
return false;
716+
}
702717
return (isMinLengthMet() && hasMatches()) || notFoundVisible();
703718
}
704719

0 commit comments

Comments
 (0)