@@ -87,6 +87,7 @@ angular
87
87
* make suggestions
88
88
* @param {number= } md-delay Specifies the amount of time (in milliseconds) to wait before looking
89
89
* for results
90
+ * @param {boolean= } md-clear-button Whether the clear button for the autocomplete input should show up or not.
90
91
* @param {boolean= } md-autofocus If true, the autocomplete will be automatically focused when a `$mdDialog`,
91
92
* `$mdBottomsheet` or `$mdSidenav`, which contains the autocomplete, is opening. <br/><br/>
92
93
* Also the autocomplete will immediately focus the input element.
@@ -151,6 +152,17 @@ angular
151
152
* In this example, our code utilizes `md-item-template` and `md-not-found` to specify the
152
153
* different parts that make up our component.
153
154
*
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
+ *
154
166
* ### Example with validation
155
167
* <hljs lang="html">
156
168
* <form name="autocompleteForm">
@@ -232,7 +244,8 @@ function MdAutocomplete ($$mdSvgRegistry) {
232
244
inputId : '@?mdInputId' ,
233
245
escapeOptions : '@?mdEscapeOptions' ,
234
246
dropdownItems : '=?mdDropdownItems' ,
235
- dropdownPosition : '@?mdDropdownPosition'
247
+ dropdownPosition : '@?mdDropdownPosition' ,
248
+ clearButton : '=?mdClearButton'
236
249
} ,
237
250
compile : function ( tElement , tAttrs ) {
238
251
var attributes = [ 'md-select-on-focus' , 'md-no-asterisk' , 'ng-trim' , 'ng-pattern' ] ;
@@ -250,6 +263,11 @@ function MdAutocomplete ($$mdSvgRegistry) {
250
263
// Retrieve the state of using a md-not-found template by using our attribute, which will
251
264
// be added to the element in the template function.
252
265
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
+ }
253
271
}
254
272
} ,
255
273
template : function ( element , attr ) {
@@ -269,8 +287,11 @@ function MdAutocomplete ($$mdSvgRegistry) {
269
287
270
288
return '\
271
289
<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 }">\
273
293
' + getInputElement ( ) + '\
294
+ ' + getClearButton ( ) + '\
274
295
<md-progress-linear\
275
296
class="' + ( attr . mdFloatingLabel ? 'md-inline' : '' ) + '"\
276
297
ng-if="$mdAutocompleteCtrl.loadingIsVisible()"\
@@ -366,18 +387,21 @@ function MdAutocomplete ($$mdSvgRegistry) {
366
387
role="combobox"\
367
388
aria-haspopup="true"\
368
389
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}}"/>' ;
379
391
}
380
392
}
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
+ }
381
405
}
382
406
} ;
383
407
}
0 commit comments