Skip to content

fix(form-field): always use native input value to determine whether control is empty #13307

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 1 commit into from
Sep 27, 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
55 changes: 50 additions & 5 deletions src/lib/input/input.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,15 @@ import {
wrappedErrorMessage,
MockNgZone,
} from '@angular/cdk/testing';
import {ChangeDetectionStrategy, Component, ViewChild, Type, Provider, NgZone} from '@angular/core';
import {
ChangeDetectionStrategy,
Component,
ViewChild,
Type,
Provider,
NgZone,
Directive,
} from '@angular/core';
import {ComponentFixture, fakeAsync, flush, TestBed} from '@angular/core/testing';
import {
FormControl,
Expand Down Expand Up @@ -35,8 +43,7 @@ import {By} from '@angular/platform-browser';
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
import {MatStepperModule} from '@angular/material/stepper';
import {MatTabsModule} from '@angular/material/tabs';
import {MatInputModule} from './index';
import {MatInput} from './input';
import {MatInputModule, MatInput, MAT_INPUT_VALUE_ACCESSOR} from './index';
import {MatTextareaAutosize} from './autosize';

describe('MatInput without forms', () => {
Expand Down Expand Up @@ -889,6 +896,20 @@ describe('MatInput without forms', () => {
expect(formField.classList).not.toContain('mat-form-field-type-mat-native-select');
});

it('should use the native input value when determining whether ' +
'the element is empty with a custom accessor', fakeAsync(() => {
let fixture = createComponent(MatInputWithCustomAccessor, [], [], [CustomMatInputAccessor]);
fixture.detectChanges();
let label = fixture.debugElement.query(By.css('label')).nativeElement;

expect(label.classList).toContain('mat-form-field-empty');

fixture.nativeElement.querySelector('input').value = 'abc';
fixture.detectChanges();

expect(label.classList).not.toContain('mat-form-field-empty');
}));

});

describe('MatInput with forms', () => {
Expand Down Expand Up @@ -1368,7 +1389,8 @@ describe('MatInput with textarea autosize', () => {

function createComponent<T>(component: Type<T>,
providers: Provider[] = [],
imports: any[] = []): ComponentFixture<T> {
imports: any[] = [],
declarations: any[] = []): ComponentFixture<T> {
TestBed.configureTestingModule({
imports: [
FormsModule,
Expand All @@ -1379,7 +1401,7 @@ function createComponent<T>(component: Type<T>,
ReactiveFormsModule,
...imports
],
declarations: [component],
declarations: [component, ...declarations],
providers,
}).compileComponents();

Expand Down Expand Up @@ -1876,3 +1898,26 @@ class MatInputSelectWithLabel {}
</mat-form-field>`
})
class MatInputSelectWithInnerHtml {}

@Component({
template: `
<mat-form-field floatLabel="never">
<input matInput customInputAccessor placeholder="Placeholder">
</mat-form-field>`
})
class MatInputWithCustomAccessor {}


/** Custom component that never has a value. Used for testing the `MAT_INPUT_VALUE_ACCESSOR`. */
@Directive({
selector: 'input[customInputAccessor]',
providers: [{
provide: MAT_INPUT_VALUE_ACCESSOR,
useExisting: CustomMatInputAccessor
}]
})
class CustomMatInputAccessor {
get value() { return this._value; }
set value(_value: any) {}
private _value = null;
}
2 changes: 1 addition & 1 deletion src/lib/input/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ export class MatInput extends _MatInputMixinBase implements MatFormFieldControl<

/** Does some manual dirty checking on the native input `value` property. */
protected _dirtyCheckNativeValue() {
const newValue = this.value;
const newValue = this._elementRef.nativeElement.value;

if (this._previousNativeValue !== newValue) {
this._previousNativeValue = newValue;
Expand Down