Skip to content

fix(form-field): fixes for outline appearance #9759

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions src/lib/form-field/_form-field-fill-theme.scss
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,6 @@ $mat-form-field-fill-dedupe: 0;
$infix-margin-top);
}

.mat-form-field-autofill-control:-webkit-autofill + .mat-form-field-label-wrapper
.mat-form-field-label {
@include _mat-form-field-fill-label-floating(
$subscript-font-scale, $infix-padding-top + $fill-appearance-label-offset,
$infix-margin-top);
}

// Server-side rendered matInput with a label attribute but label not shown
// (used as a pure CSS stand-in for mat-form-field-should-float).
.mat-input-server[label]:not(:label-shown) + .mat-form-field-label-wrapper
Expand Down
1 change: 1 addition & 0 deletions src/lib/form-field/_form-field-legacy-theme.scss
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ $mat-form-field-legacy-dedupe: 0;
$subscript-font-scale, $infix-padding, $infix-margin-top);
}

// @deletion-target 7.0.0 will rely on AutofillMonitor instead.
.mat-form-field-autofill-control:-webkit-autofill + .mat-form-field-label-wrapper
.mat-form-field-label {
@include _mat-form-field-legacy-label-floating(
Expand Down
7 changes: 0 additions & 7 deletions src/lib/form-field/_form-field-outline-theme.scss
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,6 @@ $mat-form-field-outline-dedupe: 0;
$infix-margin-top);
}

.mat-form-field-autofill-control:-webkit-autofill + .mat-form-field-label-wrapper
.mat-form-field-label {
@include _mat-form-field-outline-label-floating(
$subscript-font-scale, $infix-padding + $outline-appearance-label-offset,
$infix-margin-top);
}

// Server-side rendered matInput with a label attribute but label not shown
// (used as a pure CSS stand-in for mat-form-field-should-float).
.mat-input-server[label]:not(:label-shown) + .mat-form-field-label-wrapper
Expand Down
6 changes: 0 additions & 6 deletions src/lib/form-field/_form-field-theme.scss
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,6 @@ $mat-form-field-dedupe: 0;
$subscript-font-scale, $infix-padding, $infix-margin-top);
}

.mat-form-field-autofill-control:-webkit-autofill + .mat-form-field-label-wrapper
.mat-form-field-label {
@include _mat-form-field-label-floating(
$subscript-font-scale, $infix-padding, $infix-margin-top);
}

// Server-side rendered matInput with a label attribute but label not shown
// (used as a pure CSS stand-in for mat-form-field-should-float).
.mat-input-server[label]:not(:label-shown) + .mat-form-field-label-wrapper
Expand Down
6 changes: 6 additions & 0 deletions src/lib/form-field/form-field-control.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ export abstract class MatFormFieldControl<T> {
*/
readonly controlType?: string;

/**
* Whether the input is currently in an autofilled state. If property is not present on the
* control it is assumed to be false.
*/
readonly autofilled?: boolean;

/** Sets the list of element IDs that currently describe this control. */
abstract setDescribedByIds(ids: string[]): void;

Expand Down
1 change: 1 addition & 0 deletions src/lib/form-field/form-field.scss
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ $mat-form-field-default-infix-width: 180px !default;
// This is necessary because these browsers do not actually fire any events when a form auto-fill is
// occurring. Once the autofill is committed, a change event happen and the regular mat-form-field
// classes take over to fulfill this behaviour.
// @deletion-target 7.0.0 will rely on AutofillMonitor instead.
.mat-form-field-autofill-control:-webkit-autofill + .mat-form-field-label-wrapper
.mat-form-field-label {
// The form field will be considered empty if it is autofilled, and therefore the label will
Expand Down
5 changes: 3 additions & 2 deletions src/lib/form-field/form-field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ export type MatFormFieldAppearance = 'legacy' | 'standard' | 'fill' | 'outline';
'[class.mat-form-field-should-float]': '_shouldLabelFloat()',
'[class.mat-form-field-hide-placeholder]': '_hideControlPlaceholder()',
'[class.mat-form-field-disabled]': '_control.disabled',
'[class.mat-form-field-autofilled]': '_control.autofilled',
'[class.mat-focused]': '_control.focused',
'[class.mat-accent]': 'color == "accent"',
'[class.mat-warn]': 'color == "warn"',
Expand Down Expand Up @@ -266,7 +267,7 @@ export class MatFormField extends _MatFormFieldMixinBase
});

Promise.resolve().then(() => {
this._updateOutlineGap();
this.updateOutlineGap();
this._changeDetectorRef.detectChanges();
});
}
Expand Down Expand Up @@ -413,7 +414,7 @@ export class MatFormField extends _MatFormFieldMixinBase
* Updates the width and position of the gap in the outline. Only relevant for the outline
* appearance.
*/
private _updateOutlineGap() {
updateOutlineGap() {
if (this.appearance === 'outline' && this._label && this._label.nativeElement.children.length) {
const containerStart = this._getStartEnd(
this._connectionContainerRef.nativeElement.getBoundingClientRect());
Expand Down
27 changes: 24 additions & 3 deletions src/lib/input/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,17 @@ import {
ElementRef,
Inject,
Input,
NgZone,
OnChanges,
OnDestroy,
Optional,
Self,
} from '@angular/core';
import {FormGroupDirective, NgControl, NgForm} from '@angular/forms';
import {ErrorStateMatcher, mixinErrorState, CanUpdateErrorState} from '@angular/material/core';
import {CanUpdateErrorState, ErrorStateMatcher, mixinErrorState} from '@angular/material/core';
import {MatFormFieldControl} from '@angular/material/form-field';
import {Subject} from 'rxjs/Subject';
import {AutofillMonitor} from './autofill';
import {getMatInputUnsupportedTypeError} from './input-errors';
import {MAT_INPUT_VALUE_ACCESSOR} from './input-value-accessor';

Expand Down Expand Up @@ -58,6 +60,9 @@ export const _MatInputMixinBase = mixinErrorState(MatInputBase);
selector: `input[matInput], textarea[matInput]`,
exportAs: 'matInput',
host: {
/**
* @deletion-target 7.0.0 remove .mat-form-field-autofill-control in favor of AutofillMonitor.
*/
'class': 'mat-input-element mat-form-field-autofill-control',
'[class.mat-input-server]': '_isServer',
// Native input properties that are overwritten by Angular inputs need to be synced with
Expand Down Expand Up @@ -105,6 +110,12 @@ export class MatInput extends _MatInputMixinBase implements MatFormFieldControl<
*/
controlType: string = 'mat-input';

/**
* Implemented as part of MatFormFieldControl.
* @docs-private
*/
autofilled = false;

/**
* Implemented as part of MatFormFieldControl.
* @docs-private
Expand Down Expand Up @@ -206,7 +217,9 @@ export class MatInput extends _MatInputMixinBase implements MatFormFieldControl<
@Optional() _parentForm: NgForm,
@Optional() _parentFormGroup: FormGroupDirective,
_defaultErrorStateMatcher: ErrorStateMatcher,
@Optional() @Self() @Inject(MAT_INPUT_VALUE_ACCESSOR) inputValueAccessor: any) {
@Optional() @Self() @Inject(MAT_INPUT_VALUE_ACCESSOR) inputValueAccessor: any,
private _autofillMonitor: AutofillMonitor,
ngZone: NgZone) {
super(_defaultErrorStateMatcher, _parentForm, _parentFormGroup, ngControl);
// If no input value accessor was explicitly specified, use the element as the input value
// accessor.
Expand All @@ -233,6 +246,12 @@ export class MatInput extends _MatInputMixinBase implements MatFormFieldControl<
});
}

this._autofillMonitor.monitor(this._elementRef.nativeElement)
.subscribe(event => ngZone.run(() => {
this.autofilled = event.isAutofilled;
this.stateChanges.next();
}));

this._isServer = !this._platform.isBrowser;
}

Expand All @@ -242,6 +261,7 @@ export class MatInput extends _MatInputMixinBase implements MatFormFieldControl<

ngOnDestroy() {
this.stateChanges.complete();
this._autofillMonitor.stopMonitoring(this._elementRef.nativeElement);
}

ngDoCheck() {
Expand Down Expand Up @@ -324,7 +344,8 @@ export class MatInput extends _MatInputMixinBase implements MatFormFieldControl<
* @docs-private
*/
get empty(): boolean {
return !this._isNeverEmpty() && !this._elementRef.nativeElement.value && !this._isBadInput();
return !this._isNeverEmpty() && !this._elementRef.nativeElement.value && !this._isBadInput() &&
!this.autofilled;
}

/**
Expand Down