Skip to content

Commit

Permalink
fix(form-field): incorrectly calculating start gap in RTL in the pres…
Browse files Browse the repository at this point in the history
…ence of a prefix (#18867)

The logic that figures out the outline gap in the presence of a prefix was producing a negative width which is invalid. It seems like this has been there since the beginning, but we haven't noticed it, because our dev app doesn't have many examples with a prefix and it always starts off from LTR and we switch it to RTL manually which doesn't have this problem since it already got a valid width while it was in LTR.

Fixes #18857.
  • Loading branch information
crisbeto committed Mar 21, 2020
1 parent 32aec93 commit 6275339
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/material/form-field/form-field.ts
Expand Up @@ -579,7 +579,7 @@ export class MatFormField extends _MatFormFieldMixinBase
for (const child of labelEl.children) {
labelWidth += child.offsetWidth;
}
startWidth = labelStart - containerStart - outlineGapPadding;
startWidth = Math.abs(labelStart - containerStart) - outlineGapPadding;
gapWidth = labelWidth > 0 ? labelWidth * floatingLabelScale + outlineGapPadding * 2 : 0;
}

Expand Down
22 changes: 22 additions & 0 deletions src/material/input/input.spec.ts
Expand Up @@ -1346,6 +1346,28 @@ describe('MatInput with appearance', () => {
expect(parseInt(outlineGap.style.width)).toBeGreaterThan(0);
}));

it('should calculate the gap when starting off in RTL', fakeAsync(() => {
fixture.destroy();
TestBed.resetTestingModule();

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

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

const wrapperElement = outlineFixture.nativeElement;
const outlineStart = wrapperElement.querySelector('.mat-form-field-outline-start');
const outlineGap = wrapperElement.querySelector('.mat-form-field-outline-gap');

expect(parseInt(outlineStart.style.width)).toBeGreaterThan(0);
expect(parseInt(outlineGap.style.width)).toBeGreaterThan(0);
}));

it('should not set an outline gap if the label is empty', fakeAsync(() => {
fixture.destroy();
TestBed.resetTestingModule();
Expand Down

0 comments on commit 6275339

Please sign in to comment.