Skip to content
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

fix(form-field): incorrect assumptions about page direction #17395

Merged
merged 1 commit into from
Dec 3, 2019
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
22 changes: 9 additions & 13 deletions src/material/form-field/form-field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/

import {Direction, Directionality} from '@angular/cdk/bidi';
import {Directionality} from '@angular/cdk/bidi';
import {coerceBooleanProperty} from '@angular/cdk/coercion';
import {
AfterContentChecked,
Expand Down Expand Up @@ -232,15 +232,6 @@ export class MatFormField extends _MatFormFieldMixinBase
/** Whether the Angular animations are enabled. */
_animationsEnabled: boolean;

/* Holds the previous direction emitted by directionality service change emitter.
This is used in updateOutlineGap() method to update the width and position of the gap in the
outline. Only relevant for the outline appearance. The direction is getting updated in the
UI after directionality service change emission. So the outlines gaps are getting
updated in updateOutlineGap() method before connectionContainer child direction change
in UI. We may get wrong calculations. So we are storing the previous direction to get the
correct outline calculations*/
private _previousDirection: Direction = 'ltr';

/**
* @deprecated
* @breaking-change 8.0.0
Expand Down Expand Up @@ -356,8 +347,13 @@ export class MatFormField extends _MatFormFieldMixinBase

if (this._dir) {
this._dir.change.pipe(takeUntil(this._destroyed)).subscribe(() => {
this.updateOutlineGap();
this._previousDirection = this._dir.value;
if (typeof requestAnimationFrame === 'function') {
this._ngZone.runOutsideAngular(() => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this whole if-else be wrapped in runOutsideAngular? Currently it looks like it runs in the zone if the browser doesn't support requestAnimationFrame and outside if it does

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updateOutlineGap doesn't do any zone-ey things, we only need to run requestAnimationFrame on the outside because it can trigger change detection otherwise.

requestAnimationFrame(() => this.updateOutlineGap());
});
} else {
this.updateOutlineGap();
}
});
}
}
Expand Down Expand Up @@ -580,7 +576,7 @@ export class MatFormField extends _MatFormFieldMixinBase

/** Gets the start end of the rect considering the current directionality. */
private _getStartEnd(rect: ClientRect): number {
return this._previousDirection === 'rtl' ? rect.right : rect.left;
return (this._dir && this._dir.value === 'rtl') ? rect.right : rect.left;
}

/** Checks whether the form field is attached to the DOM. */
Expand Down
8 changes: 4 additions & 4 deletions src/material/input/input.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
ViewEncapsulation,
ElementRef,
} from '@angular/core';
import {ComponentFixture, fakeAsync, flush, TestBed} from '@angular/core/testing';
import {ComponentFixture, fakeAsync, flush, TestBed, tick} from '@angular/core/testing';
import {
FormControl,
FormGroup,
Expand Down Expand Up @@ -1444,7 +1444,7 @@ describe('MatInput with appearance', () => {
fakeDirectionality.value = 'rtl';
fakeDirectionality.change.next('rtl');
outlineFixture.detectChanges();
flush();
tick(16.6); // Angular replaces requestAnimationFrame calls with 16.6ms timeouts in tests.
outlineFixture.detectChanges();

expect(outlineFixture.componentInstance.formField.updateOutlineGap).toHaveBeenCalled();
Expand Down Expand Up @@ -1479,7 +1479,7 @@ describe('MatInput with appearance', () => {
fakeDirectionality.value = 'rtl';
fakeDirectionality.change.next('rtl');
outlineFixture.detectChanges();
flush();
tick(16.6); // Angular replaces requestAnimationFrame calls with 16.6ms timeouts in tests.
outlineFixture.detectChanges();

let wrapperElement = outlineFixture.nativeElement;
Expand All @@ -1493,7 +1493,7 @@ describe('MatInput with appearance', () => {
fakeDirectionality.value = 'ltr';
fakeDirectionality.change.next('ltr');
outlineFixture.detectChanges();
flush();
tick(16.6);
outlineFixture.detectChanges();

wrapperElement = outlineFixture.nativeElement;
Expand Down