Skip to content

Commit

Permalink
fix(form-field): outline gap not being recalculated on direction chan…
Browse files Browse the repository at this point in the history
…ges (#13478)

Since some of the calculations for the outline gap depend on the direction, we need to re-run them of the direction changes.
  • Loading branch information
crisbeto authored and mmalerba committed Dec 6, 2018
1 parent b15392d commit b78a750
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 6 deletions.
6 changes: 3 additions & 3 deletions src/lib/autocomplete/autocomplete.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import {MatOption, MatOptionSelectionChange} from '@angular/material/core';
import {MatFormField, MatFormFieldModule} from '@angular/material/form-field';
import {By} from '@angular/platform-browser';
import {NoopAnimationsModule} from '@angular/platform-browser/animations';
import {Observable, Subject, Subscription} from 'rxjs';
import {Observable, Subject, Subscription, EMPTY} from 'rxjs';
import {map, startWith} from 'rxjs/operators';
import {MatInputModule} from '../input/index';
import {
Expand Down Expand Up @@ -485,7 +485,7 @@ describe('MatAutocomplete', () => {

it('should have the correct text direction in RTL', () => {
const rtlFixture = createComponent(SimpleAutocomplete, [
{provide: Directionality, useFactory: () => ({value: 'rtl'})},
{provide: Directionality, useFactory: () => ({value: 'rtl', change: EMPTY})},
]);

rtlFixture.detectChanges();
Expand All @@ -498,7 +498,7 @@ describe('MatAutocomplete', () => {
});

it('should update the panel direction if it changes for the trigger', () => {
const dirProvider = {value: 'rtl'};
const dirProvider = {value: 'rtl', change: EMPTY};
const rtlFixture = createComponent(SimpleAutocomplete, [
{provide: Directionality, useFactory: () => dirProvider},
]);
Expand Down
5 changes: 5 additions & 0 deletions src/lib/form-field/form-field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ export class MatFormField extends _MatFormFieldMixinBase

/** Whether the outline gap needs to be calculated next time the zone has stabilized. */
private _outlineGapCalculationNeededOnStable = false;

private _destroyed = new Subject<void>();

/** The form-field appearance style. */
Expand Down Expand Up @@ -325,6 +326,10 @@ export class MatFormField extends _MatFormFieldMixinBase
this._syncDescribedByIds();
this._changeDetectorRef.markForCheck();
});

if (this._dir) {
this._dir.change.pipe(takeUntil(this._destroyed)).subscribe(() => this.updateOutlineGap());
}
}

ngAfterContentChecked() {
Expand Down
1 change: 1 addition & 0 deletions src/lib/input/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ ng_test_library(
"@angular//packages/forms",
"@angular//packages/platform-browser",
"@angular//packages/platform-browser/animations",
"//src/cdk/bidi",
"//src/cdk/platform",
"//src/cdk/testing",
"//src/lib/core",
Expand Down
30 changes: 30 additions & 0 deletions src/lib/input/input.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ 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 {Directionality, Direction} from '@angular/cdk/bidi';
import {Subject} from 'rxjs';
import {MatInputModule, MatInput, MAT_INPUT_VALUE_ACCESSOR} from './index';
import {MatTextareaAutosize} from './autosize';

Expand Down Expand Up @@ -1391,6 +1393,34 @@ describe('MatInput with appearance', () => {
expect(parseInt(outlineGap.style.width)).toBeGreaterThan(0);
}));

it('should update the outline gap if the direction changes', fakeAsync(() => {
fixture.destroy();
TestBed.resetTestingModule();

const fakeDirectionality = {change: new Subject<Direction>(), value: 'ltr'};
const outlineFixture = createComponent(MatInputWithAppearanceAndLabel, [{
provide: Directionality,
useValue: fakeDirectionality
}]);

outlineFixture.componentInstance.appearance = 'outline';
outlineFixture.detectChanges();
flush();
outlineFixture.detectChanges();

spyOn(outlineFixture.componentInstance.formField, 'updateOutlineGap');

fakeDirectionality.value = 'rtl';
fakeDirectionality.change.next('rtl');
outlineFixture.detectChanges();
flush();
outlineFixture.detectChanges();

expect(outlineFixture.componentInstance.formField.updateOutlineGap).toHaveBeenCalled();
}));



});

describe('MatFormField default options', () => {
Expand Down
6 changes: 3 additions & 3 deletions src/lib/select/select.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ import {
import {MatFormFieldModule} from '@angular/material/form-field';
import {By} from '@angular/platform-browser';
import {NoopAnimationsModule} from '@angular/platform-browser/animations';
import {Subject, Subscription} from 'rxjs';
import {Subject, Subscription, EMPTY, Observable} from 'rxjs';
import {map} from 'rxjs/operators';
import {MatSelectModule} from './index';
import {MatSelect} from './select';
Expand All @@ -76,7 +76,7 @@ const LETTER_KEY_DEBOUNCE_INTERVAL = 200;
describe('MatSelect', () => {
let overlayContainer: OverlayContainer;
let overlayContainerElement: HTMLElement;
let dir: {value: 'ltr'|'rtl'};
let dir: {value: 'ltr'|'rtl', change: Observable<string>};
let scrolledSubject = new Subject();
let viewportRuler: ViewportRuler;
let platform: Platform;
Expand All @@ -98,7 +98,7 @@ describe('MatSelect', () => {
],
declarations: declarations,
providers: [
{provide: Directionality, useFactory: () => dir = {value: 'ltr'}},
{provide: Directionality, useFactory: () => dir = {value: 'ltr', change: EMPTY}},
{
provide: ScrollDispatcher, useFactory: () => ({
scrolled: () => scrolledSubject.asObservable(),
Expand Down

0 comments on commit b78a750

Please sign in to comment.