Skip to content

Commit b78a750

Browse files
crisbetommalerba
authored andcommittedDec 6, 2018
fix(form-field): outline gap not being recalculated on direction changes (#13478)
Since some of the calculations for the outline gap depend on the direction, we need to re-run them of the direction changes.
1 parent b15392d commit b78a750

File tree

5 files changed

+42
-6
lines changed

5 files changed

+42
-6
lines changed
 

‎src/lib/autocomplete/autocomplete.spec.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import {MatOption, MatOptionSelectionChange} from '@angular/material/core';
3636
import {MatFormField, MatFormFieldModule} from '@angular/material/form-field';
3737
import {By} from '@angular/platform-browser';
3838
import {NoopAnimationsModule} from '@angular/platform-browser/animations';
39-
import {Observable, Subject, Subscription} from 'rxjs';
39+
import {Observable, Subject, Subscription, EMPTY} from 'rxjs';
4040
import {map, startWith} from 'rxjs/operators';
4141
import {MatInputModule} from '../input/index';
4242
import {
@@ -485,7 +485,7 @@ describe('MatAutocomplete', () => {
485485

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

491491
rtlFixture.detectChanges();
@@ -498,7 +498,7 @@ describe('MatAutocomplete', () => {
498498
});
499499

500500
it('should update the panel direction if it changes for the trigger', () => {
501-
const dirProvider = {value: 'rtl'};
501+
const dirProvider = {value: 'rtl', change: EMPTY};
502502
const rtlFixture = createComponent(SimpleAutocomplete, [
503503
{provide: Directionality, useFactory: () => dirProvider},
504504
]);

‎src/lib/form-field/form-field.ts

+5
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ export class MatFormField extends _MatFormFieldMixinBase
153153

154154
/** Whether the outline gap needs to be calculated next time the zone has stabilized. */
155155
private _outlineGapCalculationNeededOnStable = false;
156+
156157
private _destroyed = new Subject<void>();
157158

158159
/** The form-field appearance style. */
@@ -325,6 +326,10 @@ export class MatFormField extends _MatFormFieldMixinBase
325326
this._syncDescribedByIds();
326327
this._changeDetectorRef.markForCheck();
327328
});
329+
330+
if (this._dir) {
331+
this._dir.change.pipe(takeUntil(this._destroyed)).subscribe(() => this.updateOutlineGap());
332+
}
328333
}
329334

330335
ngAfterContentChecked() {

‎src/lib/input/BUILD.bazel

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ ng_test_library(
3535
"@angular//packages/forms",
3636
"@angular//packages/platform-browser",
3737
"@angular//packages/platform-browser/animations",
38+
"//src/cdk/bidi",
3839
"//src/cdk/platform",
3940
"//src/cdk/testing",
4041
"//src/lib/core",

‎src/lib/input/input.spec.ts

+30
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ import {By} from '@angular/platform-browser';
4343
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
4444
import {MatStepperModule} from '@angular/material/stepper';
4545
import {MatTabsModule} from '@angular/material/tabs';
46+
import {Directionality, Direction} from '@angular/cdk/bidi';
47+
import {Subject} from 'rxjs';
4648
import {MatInputModule, MatInput, MAT_INPUT_VALUE_ACCESSOR} from './index';
4749
import {MatTextareaAutosize} from './autosize';
4850

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

1396+
it('should update the outline gap if the direction changes', fakeAsync(() => {
1397+
fixture.destroy();
1398+
TestBed.resetTestingModule();
1399+
1400+
const fakeDirectionality = {change: new Subject<Direction>(), value: 'ltr'};
1401+
const outlineFixture = createComponent(MatInputWithAppearanceAndLabel, [{
1402+
provide: Directionality,
1403+
useValue: fakeDirectionality
1404+
}]);
1405+
1406+
outlineFixture.componentInstance.appearance = 'outline';
1407+
outlineFixture.detectChanges();
1408+
flush();
1409+
outlineFixture.detectChanges();
1410+
1411+
spyOn(outlineFixture.componentInstance.formField, 'updateOutlineGap');
1412+
1413+
fakeDirectionality.value = 'rtl';
1414+
fakeDirectionality.change.next('rtl');
1415+
outlineFixture.detectChanges();
1416+
flush();
1417+
outlineFixture.detectChanges();
1418+
1419+
expect(outlineFixture.componentInstance.formField.updateOutlineGap).toHaveBeenCalled();
1420+
}));
1421+
1422+
1423+
13941424
});
13951425

13961426
describe('MatFormField default options', () => {

‎src/lib/select/select.spec.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ import {
5959
import {MatFormFieldModule} from '@angular/material/form-field';
6060
import {By} from '@angular/platform-browser';
6161
import {NoopAnimationsModule} from '@angular/platform-browser/animations';
62-
import {Subject, Subscription} from 'rxjs';
62+
import {Subject, Subscription, EMPTY, Observable} from 'rxjs';
6363
import {map} from 'rxjs/operators';
6464
import {MatSelectModule} from './index';
6565
import {MatSelect} from './select';
@@ -76,7 +76,7 @@ const LETTER_KEY_DEBOUNCE_INTERVAL = 200;
7676
describe('MatSelect', () => {
7777
let overlayContainer: OverlayContainer;
7878
let overlayContainerElement: HTMLElement;
79-
let dir: {value: 'ltr'|'rtl'};
79+
let dir: {value: 'ltr'|'rtl', change: Observable<string>};
8080
let scrolledSubject = new Subject();
8181
let viewportRuler: ViewportRuler;
8282
let platform: Platform;
@@ -98,7 +98,7 @@ describe('MatSelect', () => {
9898
],
9999
declarations: declarations,
100100
providers: [
101-
{provide: Directionality, useFactory: () => dir = {value: 'ltr'}},
101+
{provide: Directionality, useFactory: () => dir = {value: 'ltr', change: EMPTY}},
102102
{
103103
provide: ScrollDispatcher, useFactory: () => ({
104104
scrolled: () => scrolledSubject.asObservable(),

0 commit comments

Comments
 (0)
Please sign in to comment.