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

Commit 78f802c

Browse files
committed
feat(autocomplete): add md-menu-container-class for styling (#11194)
1 parent b80bc8e commit 78f802c

File tree

4 files changed

+62
-36
lines changed

4 files changed

+62
-36
lines changed

src/components/autocomplete/autocomplete.spec.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,26 @@ describe('<md-autocomplete>', function() {
250250
element.remove();
251251
}));
252252

253+
it('should allow you to set a class to the md-virtual-repeat-container element', inject(function() {
254+
var scope = createScope(null, {menuContainerClass: 'custom-menu-container-class'});
255+
var template = '\
256+
<md-autocomplete\
257+
md-menu-container-class="{{menuContainerClass}}"\
258+
md-selected-item="selectedItem"\
259+
md-search-text="searchText"\
260+
md-items="item in match(searchText)"\
261+
md-item-text="item.display"\
262+
placeholder="placeholder">\
263+
<span md-highlight-text="searchText">{{item.display}}</span>\
264+
</md-autocomplete>';
265+
var element = compile(template, scope);
266+
var repeatContainer = element.find('md-virtual-repeat-container');
267+
268+
expect(repeatContainer.attr('class')).toContain(scope.menuContainerClass);
269+
270+
element.remove();
271+
}));
272+
253273
it('allows using ng-readonly', inject(function() {
254274
var scope = createScope(null, {inputId: 'custom-input-id'});
255275
var template = '\

src/components/autocomplete/demoCustomTemplate/index.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<form ng-submit="$event.preventDefault()">
44
<p>Use <code>&lt;md-autocomplete&gt;</code> with custom templates to show styled autocomplete results.</p>
55
<md-autocomplete
6+
id="custom-template"
67
ng-disabled="ctrl.isDisabled"
78
md-no-cache="ctrl.noCache"
89
md-selected-item="ctrl.selectedItem"
@@ -13,7 +14,8 @@
1314
md-item-text="item.name"
1415
md-min-length="0"
1516
placeholder="Pick an Angular repository"
16-
md-menu-class="autocomplete-custom-template">
17+
md-menu-class="autocomplete-custom-template"
18+
md-menu-container-class="custom-container">
1719
<md-item-template>
1820
<span class="item-title">
1921
<md-icon md-svg-icon="img/icons/octicon-repo.svg"></md-icon>
@@ -32,4 +34,3 @@
3234
</form>
3335
</md-content>
3436
</div>
35-

src/components/autocomplete/demoCustomTemplate/style.global.css

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
md-autocomplete#custom-template {
2+
width: 200px;
3+
}
14
.autocomplete-custom-template li {
25
border-bottom: 1px solid #ccc;
36
height: auto;
@@ -16,4 +19,7 @@
1619
.autocomplete-custom-template .item-title md-icon {
1720
height: 18px;
1821
width: 18px;
19-
}
22+
}
23+
.custom-container {
24+
min-width: 300px !important;
25+
}

src/components/autocomplete/js/autocompleteDirective.js

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ angular
7474
* </hljs>
7575
*
7676
*
77-
*
7877
* @param {expression} md-items An expression in the format of `item in results` to iterate over
7978
* matches for your search.<br/><br/>
8079
* The `results` expression can be also a function, which returns the results synchronously
@@ -102,12 +101,11 @@ angular
102101
* @param {boolean=} md-no-asterisk When present, asterisk will not be appended to the floating label.
103102
* @param {boolean=} md-autoselect If set to true, the first item will be automatically selected
104103
* in the dropdown upon open.
105-
* @param {string=} md-menu-class This will be applied to the dropdown menu for styling.
106-
* @param {string=} md-floating-label This will add a floating label to autocomplete and wrap it in
107-
* `md-input-container`.
108104
* @param {string=} md-input-name The name attribute given to the input element to be used with
109105
* FormController.
110-
* @param {string=} md-menu-class This will be applied to the dropdown menu for styling
106+
* @param {string=} md-menu-class This class will be applied to the dropdown menu for styling.
107+
* @param {string=} md-menu-container-class This class will be applied to the parent container
108+
* of the dropdown panel.
111109
* @param {string=} md-input-class This will be applied to the input for styling. This attribute is only valid when a `md-floating-label` is defined
112110
* @param {string=} md-floating-label This will add a floating label to autocomplete and wrap it in
113111
* `md-input-container`
@@ -229,39 +227,39 @@ angular
229227
* </hljs>
230228
*
231229
*/
232-
233230
function MdAutocomplete ($$mdSvgRegistry) {
234231

235232
return {
236233
controller: 'MdAutocompleteCtrl',
237234
controllerAs: '$mdAutocompleteCtrl',
238235
scope: {
239-
inputName: '@mdInputName',
240-
inputMinlength: '@mdInputMinlength',
241-
inputMaxlength: '@mdInputMaxlength',
242-
searchText: '=?mdSearchText',
243-
selectedItem: '=?mdSelectedItem',
244-
itemsExpr: '@mdItems',
245-
itemText: '&mdItemText',
246-
placeholder: '@placeholder',
247-
noCache: '=?mdNoCache',
248-
requireMatch: '=?mdRequireMatch',
249-
selectOnMatch: '=?mdSelectOnMatch',
250-
matchInsensitive: '=?mdMatchCaseInsensitive',
251-
itemChange: '&?mdSelectedItemChange',
252-
textChange: '&?mdSearchTextChange',
253-
minLength: '=?mdMinLength',
254-
delay: '=?mdDelay',
255-
autofocus: '=?mdAutofocus',
256-
floatingLabel: '@?mdFloatingLabel',
257-
autoselect: '=?mdAutoselect',
258-
menuClass: '@?mdMenuClass',
259-
inputClass: '@?mdInputClass',
260-
inputId: '@?mdInputId',
261-
escapeOptions: '@?mdEscapeOptions',
262-
dropdownItems: '=?mdDropdownItems',
263-
dropdownPosition: '@?mdDropdownPosition',
264-
clearButton: '=?mdClearButton'
236+
inputName: '@mdInputName',
237+
inputMinlength: '@mdInputMinlength',
238+
inputMaxlength: '@mdInputMaxlength',
239+
searchText: '=?mdSearchText',
240+
selectedItem: '=?mdSelectedItem',
241+
itemsExpr: '@mdItems',
242+
itemText: '&mdItemText',
243+
placeholder: '@placeholder',
244+
noCache: '=?mdNoCache',
245+
requireMatch: '=?mdRequireMatch',
246+
selectOnMatch: '=?mdSelectOnMatch',
247+
matchInsensitive: '=?mdMatchCaseInsensitive',
248+
itemChange: '&?mdSelectedItemChange',
249+
textChange: '&?mdSearchTextChange',
250+
minLength: '=?mdMinLength',
251+
delay: '=?mdDelay',
252+
autofocus: '=?mdAutofocus',
253+
floatingLabel: '@?mdFloatingLabel',
254+
autoselect: '=?mdAutoselect',
255+
menuClass: '@?mdMenuClass',
256+
menuContainerClass: '@?mdMenuContainerClass',
257+
inputClass: '@?mdInputClass',
258+
inputId: '@?mdInputId',
259+
escapeOptions: '@?mdEscapeOptions',
260+
dropdownItems: '=?mdDropdownItems',
261+
dropdownPosition: '@?mdDropdownPosition',
262+
clearButton: '=?mdClearButton'
265263
},
266264
compile: function(tElement, tAttrs) {
267265
var attributes = ['md-select-on-focus', 'md-no-asterisk', 'ng-trim', 'ng-pattern'];
@@ -291,6 +289,7 @@ function MdAutocomplete ($$mdSvgRegistry) {
291289
itemTemplate = getItemTemplate(),
292290
leftover = element.html(),
293291
tabindex = attr.tabindex;
292+
var menuContainerClass = attr.mdMenuContainerClass ? ' ' + attr.mdMenuContainerClass : '';
294293

295294
// Set our attribute for the link function above which runs later.
296295
// We will set an attribute, because otherwise the stored variables will be trashed when
@@ -319,7 +318,7 @@ function MdAutocomplete ($$mdSvgRegistry) {
319318
ng-mouseleave="$mdAutocompleteCtrl.listLeave()"\
320319
ng-mouseup="$mdAutocompleteCtrl.mouseUp()"\
321320
ng-hide="$mdAutocompleteCtrl.hidden"\
322-
class="md-autocomplete-suggestions-container md-whiteframe-z1"\
321+
class="md-autocomplete-suggestions-container md-whiteframe-z1' + menuContainerClass + '"\
323322
ng-class="{ \'md-not-found\': $mdAutocompleteCtrl.notFoundVisible() }"\
324323
role="presentation">\
325324
<ul class="md-autocomplete-suggestions"\

0 commit comments

Comments
 (0)