@@ -91,9 +91,9 @@ let nextUniqueId = 0;
91
91
(change)="onChange($event)"
92
92
(blur)="onBlur()" />
93
93
<ng-content></ng-content>
94
- <label mdcFloatingLabel [for]="id" *ngIf="!this.placeholder">{{label}}</label>
94
+ <label mdcFloatingLabel [for]="id" *ngIf="!this.placeholder && !outlined ">{{label}}</label>
95
95
<mdc-line-ripple *ngIf="!this.outlined && !this.textarea"></mdc-line-ripple>
96
- <mdc-notched-outline *ngIf="outlined"></mdc-notched-outline>
96
+ <mdc-notched-outline *ngIf="outlined" [label]="label" [for]="id" ></mdc-notched-outline>
97
97
` ,
98
98
providers : [
99
99
MdcRipple ,
@@ -138,7 +138,6 @@ export class MdcTextField extends _MdcTextFieldMixinBase implements AfterViewIni
138
138
const newValue = toBoolean ( value ) ;
139
139
if ( newValue !== this . _outlined ) {
140
140
this . _outlined = toBoolean ( newValue ) ;
141
- this . _reinitialize ( ) ;
142
141
this . layout ( ) ;
143
142
}
144
143
}
@@ -227,9 +226,9 @@ export class MdcTextField extends _MdcTextFieldMixinBase implements AfterViewIni
227
226
@Output ( ) readonly blur = new EventEmitter < any > ( ) ;
228
227
229
228
@ViewChild ( 'input' ) _input ! : ElementRef < HTMLInputElement | HTMLTextAreaElement > ;
230
- @ViewChild ( MdcFloatingLabel ) _floatingLabel ! : MdcFloatingLabel ;
231
229
@ViewChild ( MdcLineRipple ) _lineRipple ! : MdcLineRipple ;
232
230
@ViewChild ( MdcNotchedOutline ) _notchedOutline ! : MdcNotchedOutline ;
231
+ @ViewChild ( MdcFloatingLabel ) _floatingLabel ! : MdcFloatingLabel ;
233
232
@ContentChildren ( MdcTextFieldIcon , { descendants : true } ) _icons ! : QueryList < MdcTextFieldIcon > ;
234
233
235
234
/** View -> model callback called when value changes */
@@ -248,9 +247,7 @@ export class MdcTextField extends _MdcTextFieldMixinBase implements AfterViewIni
248
247
addClass : ( className : string ) => this . _getHostElement ( ) . classList . add ( className ) ,
249
248
removeClass : ( className : string ) => this . _getHostElement ( ) . classList . remove ( className ) ,
250
249
hasClass : ( className : string ) => this . _getHostElement ( ) . classList . contains ( className ) ,
251
- isFocused : ( ) => this . _platform . isBrowser ? document . activeElement ! === this . _getInputElement ( ) : false ,
252
- isRtl : ( ) =>
253
- this . _platform . isBrowser ? window . getComputedStyle ( this . _getHostElement ( ) ) . getPropertyValue ( 'direction' ) === 'rtl' : false
250
+ isFocused : ( ) => this . _platform . isBrowser ? document . activeElement ! === this . _getInputElement ( ) : false
254
251
} ,
255
252
this . _getInputAdapterMethods ( ) ,
256
253
this . _getLabelAdapterMethods ( ) ,
@@ -267,7 +264,7 @@ export class MdcTextField extends _MdcTextFieldMixinBase implements AfterViewIni
267
264
value : this . _value ,
268
265
disabled : this . _disabled ,
269
266
validity : {
270
- valid : this . _valid ? this . _valid : this . _platform . isBrowser ? this . _input . nativeElement . validity . valid : true ,
267
+ valid : this . _hasErrorState ( ) ,
271
268
badInput : this . _platform . isBrowser ? this . _input . nativeElement . validity . badInput : false
272
269
}
273
270
} ;
@@ -277,10 +274,10 @@ export class MdcTextField extends _MdcTextFieldMixinBase implements AfterViewIni
277
274
278
275
private _getLabelAdapterMethods ( ) {
279
276
return {
280
- shakeLabel : ( shouldShake : boolean ) => this . _floatingLabel . shake ( shouldShake ) ,
281
- floatLabel : ( shouldFloat : boolean ) => this . _floatingLabel . float ( shouldFloat ) ,
282
- hasLabel : ( ) => this . _floatingLabel ,
283
- getLabelWidth : ( ) => this . _floatingLabel ? this . _floatingLabel . getWidth ( ) : 0
277
+ shakeLabel : ( shouldShake : boolean ) => this . _getFloatingLabel ( ) . shake ( shouldShake ) ,
278
+ floatLabel : ( shouldFloat : boolean ) => this . _getFloatingLabel ( ) . float ( shouldFloat ) ,
279
+ hasLabel : ( ) => this . _hasFloatingLabel ( ) ,
280
+ getLabelWidth : ( ) => this . _hasFloatingLabel ( ) ? this . _getFloatingLabel ( ) . getWidth ( ) : 0
284
281
} ;
285
282
}
286
283
@@ -307,7 +304,7 @@ export class MdcTextField extends _MdcTextFieldMixinBase implements AfterViewIni
307
304
private _getOutlineAdapterMethods ( ) {
308
305
return {
309
306
hasOutline : ( ) => this . _notchedOutline ,
310
- notchOutline : ( labelWidth : number , isRtl : boolean ) => this . _notchedOutline . notch ( labelWidth , isRtl ) ,
307
+ notchOutline : ( labelWidth : number ) => this . _notchedOutline . notch ( labelWidth ) ,
311
308
closeOutline : ( ) => this . _notchedOutline . closeNotch ( )
312
309
} ;
313
310
}
@@ -328,18 +325,18 @@ export class MdcTextField extends _MdcTextFieldMixinBase implements AfterViewIni
328
325
readonly shouldFloat : boolean ,
329
326
notchOutline ( openNotch : boolean ) : void ,
330
327
setUseNativeValidation ( useNativeValidation : boolean ) : void ,
331
- setTransformOrigin ( evt : Event ) : void ,
328
+ setTransformOrigin ( evt : Event | TouchEvent ) : void ,
332
329
handleTextFieldInteraction ( ) : void ,
333
330
activateFocus ( ) : void ,
334
331
deactivateFocus ( ) : void ,
335
332
handleValidationAttributeChange ( attributesList ?: string [ ] ) : void
336
333
} = new MDCTextFieldFoundation ( ) ;
337
334
338
335
constructor (
339
- public _defaultErrorStateMatcher : ErrorStateMatcher ,
340
336
private _platform : Platform ,
341
337
private _changeDetectorRef : ChangeDetectorRef ,
342
338
public elementRef : ElementRef < HTMLElement > ,
339
+ public _defaultErrorStateMatcher : ErrorStateMatcher ,
343
340
@Optional ( ) private _parentFormField : MdcFormField ,
344
341
@Optional ( ) private _ripple : MdcRipple ,
345
342
@Self ( ) @Optional ( ) public ngControl : NgControl ,
@@ -367,10 +364,10 @@ export class MdcTextField extends _MdcTextFieldMixinBase implements AfterViewIni
367
364
}
368
365
369
366
ngOnDestroy ( ) : void {
370
- this . _destroyTextField ( ) ;
367
+ this . _destroy ( ) ;
371
368
}
372
369
373
- ngDoCheck ( ) {
370
+ ngDoCheck ( ) : void {
374
371
if ( this . ngControl ) {
375
372
// We need to re-evaluate this on every change detection cycle, because there are some
376
373
// error triggers that we can't subscribe to (e.g. parent form submissions). This means
@@ -475,6 +472,22 @@ export class MdcTextField extends _MdcTextFieldMixinBase implements AfterViewIni
475
472
}
476
473
}
477
474
475
+ /** Initializes Text Field's internal state based on the environment state */
476
+ private layout ( ) : void {
477
+ this . _destroy ( ) ;
478
+ this . init ( ) ;
479
+ this . _changeDetectorRef . markForCheck ( ) ;
480
+
481
+ setTimeout ( ( ) => {
482
+ if ( this . _outlined ) {
483
+ this . _foundation . notchOutline ( this . _foundation . shouldFloat ) ;
484
+ }
485
+ if ( this . _hasFloatingLabel ( ) ) {
486
+ this . _getFloatingLabel ( ) . float ( this . _foundation . shouldFloat ) ;
487
+ }
488
+ } ) ;
489
+ }
490
+
478
491
/** Implemented as part of ControlValueAccessor. */
479
492
setDisabledState ( isDisabled : boolean ) {
480
493
const newValue = toBoolean ( isDisabled ) ;
@@ -486,19 +499,6 @@ export class MdcTextField extends _MdcTextFieldMixinBase implements AfterViewIni
486
499
this . _changeDetectorRef . markForCheck ( ) ;
487
500
}
488
501
489
- /** Recomputes the outline SVG path for the outline element. */
490
- layout ( ) : void {
491
- setTimeout ( ( ) => {
492
- if ( this . _outlined ) {
493
- this . _foundation . notchOutline ( this . _foundation . shouldFloat ) ;
494
- }
495
-
496
- if ( this . _floatingLabel ) {
497
- this . _floatingLabel . float ( this . _foundation . shouldFloat ) ;
498
- }
499
- } ) ;
500
- }
501
-
502
502
private _checkCustomValidity ( ) : void {
503
503
Promise . resolve ( ) . then ( ( ) => {
504
504
if ( this . _valid !== undefined ) {
@@ -533,7 +533,7 @@ export class MdcTextField extends _MdcTextFieldMixinBase implements AfterViewIni
533
533
}
534
534
}
535
535
536
- private _destroyTextField ( ) : void {
536
+ private _destroy ( ) : void {
537
537
if ( this . _lineRipple ) {
538
538
this . _lineRipple . destroy ( ) ;
539
539
}
@@ -543,12 +543,21 @@ export class MdcTextField extends _MdcTextFieldMixinBase implements AfterViewIni
543
543
this . _foundation . destroy ( ) ;
544
544
}
545
545
546
- private _reinitialize ( ) : void {
547
- if ( this . _initialized ) {
548
- this . _destroyTextField ( ) ;
549
- this . init ( ) ;
550
- this . _changeDetectorRef . markForCheck ( ) ;
546
+ private _hasErrorState ( ) : boolean {
547
+ if ( this . ngControl ) {
548
+ return ! this . errorState ;
551
549
}
550
+
551
+ return this . _valid ? this . _valid : this . _platform . isBrowser ?
552
+ this . _input . nativeElement . validity . valid : true ;
553
+ }
554
+
555
+ private _hasFloatingLabel ( ) : boolean {
556
+ return this . label && ( this . _floatingLabel || this . _notchedOutline ) ? true : false ;
557
+ }
558
+
559
+ private _getFloatingLabel ( ) : MdcFloatingLabel {
560
+ return this . _floatingLabel || this . _notchedOutline . floatingLabel ;
552
561
}
553
562
554
563
private _getInputElement ( ) : HTMLInputElement | HTMLTextAreaElement {
0 commit comments