@@ -3,7 +3,7 @@ import { NgControl } from '@angular/forms';
3
3
4
4
import { Config } from '../../config/config' ;
5
5
import { Ion } from '../ion' ;
6
- import { isPresent } from '../../util/util' ;
6
+ import { isPresent , isTrueProperty } from '../../util/util' ;
7
7
import { Debouncer } from '../../util/debouncer' ;
8
8
9
9
@@ -39,16 +39,19 @@ import { Debouncer } from '../../util/debouncer';
39
39
'</div>' +
40
40
'<button ion-button #cancelButton [tabindex]="_isActive ? 1 : -1" clear (click)="cancelSearchbar($event)" (mousedown)="cancelSearchbar($event)" class="searchbar-ios-cancel" type="button">{{cancelButtonText}}</button>' ,
41
41
host : {
42
+ '[class.searchbar-animated]' : 'animated' ,
42
43
'[class.searchbar-has-value]' : '_value' ,
43
44
'[class.searchbar-active]' : '_isActive' ,
44
45
'[class.searchbar-show-cancel]' : 'showCancelButton' ,
45
- '[class.searchbar-left-aligned]' : 'shouldAlignLeft() '
46
+ '[class.searchbar-left-aligned]' : '_shouldAlignLeft '
46
47
} ,
47
48
encapsulation : ViewEncapsulation . None
48
49
} )
49
50
export class Searchbar extends Ion {
50
51
_value : string | number = '' ;
51
52
_shouldBlur : boolean = true ;
53
+ _shouldAlignLeft : boolean = true ;
54
+ _isCancelVisible : boolean = false ;
52
55
_isActive : boolean = false ;
53
56
_searchbarInput : ElementRef ;
54
57
_debouncer : Debouncer = new Debouncer ( 250 ) ;
@@ -115,6 +118,11 @@ export class Searchbar extends Ion {
115
118
*/
116
119
@Input ( ) type : string = 'search' ;
117
120
121
+ /**
122
+ * @input {string|boolean} Set the input's spellcheck property. Values: `true`, `false`. Default `false`.
123
+ */
124
+ @Input ( ) animated : string | boolean = false ;
125
+
118
126
/**
119
127
* @output {event} When the Searchbar input has changed including cleared.
120
128
*/
@@ -217,7 +225,7 @@ export class Searchbar extends Ion {
217
225
* @private
218
226
* After View Checked position the elements
219
227
*/
220
- ngAfterViewChecked ( ) {
228
+ ngAfterContentInit ( ) {
221
229
this . positionElements ( ) ;
222
230
}
223
231
@@ -227,26 +235,31 @@ export class Searchbar extends Ion {
227
235
* based on the input value and if it is focused. (ios only)
228
236
*/
229
237
positionElements ( ) {
230
- if ( this . _config . get ( 'mode' ) !== 'ios' ) return ;
238
+ let isAnimated = isTrueProperty ( this . animated ) ;
239
+ let prevAlignLeft = this . _shouldAlignLeft ;
240
+ let shouldAlignLeft = ( ! isAnimated || ( this . _value && this . _value . toString ( ) . trim ( ) !== '' ) || this . _sbHasFocus === true ) ;
241
+ this . _shouldAlignLeft = shouldAlignLeft ;
231
242
232
- // Position the input placeholder & search icon
233
- if ( this . _searchbarInput && this . _searchbarIcon ) {
234
- this . positionInputPlaceholder ( this . _searchbarInput . nativeElement , this . _searchbarIcon . nativeElement ) ;
243
+ if ( this . _config . get ( 'mode' ) !== 'ios' ) {
244
+ return ;
235
245
}
236
246
237
- // Position the cancel button
238
- if ( this . _cancelButton && this . _cancelButton . nativeElement ) {
239
- this . positionCancelButton ( this . _cancelButton . nativeElement ) ;
247
+ if ( prevAlignLeft !== shouldAlignLeft ) {
248
+ this . positionPlaceholder ( ) ;
249
+ }
250
+ if ( isAnimated ) {
251
+ this . positionCancelButton ( ) ;
240
252
}
241
253
}
242
254
243
- /**
244
- * @private
245
- * Calculates the amount of padding/margin left for the elements
246
- * in order to center them based on the placeholder width
247
- */
248
- positionInputPlaceholder ( inputEle : HTMLElement , iconEle : HTMLElement ) {
249
- if ( this . shouldAlignLeft ( ) ) {
255
+ positionPlaceholder ( ) {
256
+ if ( ! this . _searchbarInput || ! this . _searchbarIcon ) {
257
+ return ;
258
+ }
259
+ let inputEle = this . _searchbarInput . nativeElement ;
260
+ let iconEle = this . _searchbarIcon . nativeElement ;
261
+
262
+ if ( this . _shouldAlignLeft ) {
250
263
inputEle . removeAttribute ( 'style' ) ;
251
264
iconEle . removeAttribute ( 'style' ) ;
252
265
} else {
@@ -273,23 +286,26 @@ export class Searchbar extends Ion {
273
286
* @private
274
287
* Show the iOS Cancel button on focus, hide it offscreen otherwise
275
288
*/
276
- positionCancelButton ( cancelButtonEle : HTMLElement ) {
277
- if ( cancelButtonEle . offsetWidth > 0 ) {
278
- if ( this . _sbHasFocus ) {
279
- cancelButtonEle . style . marginRight = '0' ;
289
+ positionCancelButton ( ) {
290
+ if ( ! this . _cancelButton || ! this . _cancelButton . nativeElement ) {
291
+ return ;
292
+ }
293
+ let showShowCancel = this . _sbHasFocus ;
294
+ if ( showShowCancel !== this . _isCancelVisible ) {
295
+ let cancelStyleEle = this . _cancelButton . nativeElement ;
296
+ let cancelStyle = cancelStyleEle . style ;
297
+ this . _isCancelVisible = showShowCancel ;
298
+ if ( showShowCancel ) {
299
+ cancelStyle . marginRight = '0' ;
280
300
} else {
281
- cancelButtonEle . style . marginRight = - cancelButtonEle . offsetWidth + 'px' ;
301
+ let offset = cancelStyleEle . offsetWidth ;
302
+ if ( offset > 0 ) {
303
+ cancelStyle . marginRight = - offset + 'px' ;
304
+ }
282
305
}
283
306
}
284
307
}
285
308
286
- /**
287
- * @private
288
- * Align the input placeholder left on focus or if a value exists
289
- */
290
- shouldAlignLeft ( ) {
291
- return ( ( this . _value && this . _value . toString ( ) . trim ( ) !== '' ) || this . _sbHasFocus === true ) ;
292
- }
293
309
294
310
/**
295
311
* @private
@@ -399,4 +415,8 @@ export class Searchbar extends Ion {
399
415
registerOnTouched ( fn : ( ) => { } ) : void {
400
416
this . onTouched = fn ;
401
417
}
418
+
419
+ focus ( ) {
420
+ this . getNativeElement ( ) . focus ( ) ;
421
+ }
402
422
}
0 commit comments