@@ -87,6 +87,7 @@ angular
8787 * make suggestions
8888 * @param {number= } md-delay Specifies the amount of time (in milliseconds) to wait before looking
8989 * for results
90+ * @param {boolean= } md-clear-button Whether the clear button for the autocomplete input should show up or not.
9091 * @param {boolean= } md-autofocus If true, the autocomplete will be automatically focused when a `$mdDialog`,
9192 * `$mdBottomsheet` or `$mdSidenav`, which contains the autocomplete, is opening. <br/><br/>
9293 * Also the autocomplete will immediately focus the input element.
@@ -151,6 +152,17 @@ angular
151152 * In this example, our code utilizes `md-item-template` and `md-not-found` to specify the
152153 * different parts that make up our component.
153154 *
155+ * ### Clear button for the input
156+ * By default, for floating label autocomplete's the clear button is not showing up
157+ * ([See specs](https://material.google.com/components/text-fields.html#text-fields-auto-complete-text-field))
158+ *
159+ * Nevertheless, developers are able to explicitly toggle the clear button for all types of autocomplete's.
160+ *
161+ * <hljs lang="html">
162+ * <md-autocomplete ... md-clear-button="true"></md-autocomplete>
163+ * <md-autocomplete ... md-clear-button="false"></md-autocomplete>
164+ * </hljs>
165+ *
154166 * ### Example with validation
155167 * <hljs lang="html">
156168 * <form name="autocompleteForm">
@@ -232,7 +244,8 @@ function MdAutocomplete ($$mdSvgRegistry) {
232244 inputId : '@?mdInputId' ,
233245 escapeOptions : '@?mdEscapeOptions' ,
234246 dropdownItems : '=?mdDropdownItems' ,
235- dropdownPosition : '@?mdDropdownPosition'
247+ dropdownPosition : '@?mdDropdownPosition' ,
248+ clearButton : '=?mdClearButton'
236249 } ,
237250 compile : function ( tElement , tAttrs ) {
238251 var attributes = [ 'md-select-on-focus' , 'md-no-asterisk' , 'ng-trim' , 'ng-pattern' ] ;
@@ -250,6 +263,11 @@ function MdAutocomplete ($$mdSvgRegistry) {
250263 // Retrieve the state of using a md-not-found template by using our attribute, which will
251264 // be added to the element in the template function.
252265 ctrl . hasNotFound = ! ! element . attr ( 'md-has-not-found' ) ;
266+
267+ // By default the inset autocomplete should show the clear button when not explicitly overwritten.
268+ if ( ! angular . isDefined ( attrs . mdClearButton ) && ! scope . floatingLabel ) {
269+ scope . clearButton = true ;
270+ }
253271 }
254272 } ,
255273 template : function ( element , attr ) {
@@ -269,8 +287,11 @@ function MdAutocomplete ($$mdSvgRegistry) {
269287
270288 return '\
271289 <md-autocomplete-wrap\
272- ng-class="{ \'md-whiteframe-z1\': !floatingLabel, \'md-menu-showing\': !$mdAutocompleteCtrl.hidden }">\
290+ ng-class="{ \'md-whiteframe-z1\': !floatingLabel, \
291+ \'md-menu-showing\': !$mdAutocompleteCtrl.hidden, \
292+ \'md-show-clear-button\': !!clearButton }">\
273293 ' + getInputElement ( ) + '\
294+ ' + getClearButton ( ) + '\
274295 <md-progress-linear\
275296 class="' + ( attr . mdFloatingLabel ? 'md-inline' : '' ) + '"\
276297 ng-if="$mdAutocompleteCtrl.loadingIsVisible()"\
@@ -366,18 +387,21 @@ function MdAutocomplete ($$mdSvgRegistry) {
366387 role="combobox"\
367388 aria-haspopup="true"\
368389 aria-activedescendant=""\
369- aria-expanded="{{!$mdAutocompleteCtrl.hidden}}"/>\
370- <button\
371- type="button"\
372- tabindex="-1"\
373- ng-if="$mdAutocompleteCtrl.scope.searchText && !$mdAutocompleteCtrl.isDisabled"\
374- ng-click="$mdAutocompleteCtrl.clear($event)">\
375- <md-icon md-svg-src="' + $$mdSvgRegistry . mdClose + '"></md-icon>\
376- <span class="md-visually-hidden">Clear</span>\
377- </button>\
378- ' ;
390+ aria-expanded="{{!$mdAutocompleteCtrl.hidden}}"/>' ;
379391 }
380392 }
393+
394+ function getClearButton ( ) {
395+ return '' +
396+ '<button ' +
397+ 'type="button" ' +
398+ 'aria-label="Clear Input" ' +
399+ 'tabindex="-1" ' +
400+ 'ng-if="clearButton && $mdAutocompleteCtrl.scope.searchText && !$mdAutocompleteCtrl.isDisabled" ' +
401+ 'ng-click="$mdAutocompleteCtrl.clear($event)">' +
402+ '<md-icon md-svg-src="' + $$mdSvgRegistry . mdClose + '"></md-icon>' +
403+ '</button>' ;
404+ }
381405 }
382406 } ;
383407}
0 commit comments