Skip to content

Commit 6275339

Browse files
authored
fix(form-field): incorrectly calculating start gap in RTL in the presence 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.
1 parent 32aec93 commit 6275339

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/material/form-field/form-field.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,7 @@ export class MatFormField extends _MatFormFieldMixinBase
579579
for (const child of labelEl.children) {
580580
labelWidth += child.offsetWidth;
581581
}
582-
startWidth = labelStart - containerStart - outlineGapPadding;
582+
startWidth = Math.abs(labelStart - containerStart) - outlineGapPadding;
583583
gapWidth = labelWidth > 0 ? labelWidth * floatingLabelScale + outlineGapPadding * 2 : 0;
584584
}
585585

src/material/input/input.spec.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1346,6 +1346,28 @@ describe('MatInput with appearance', () => {
13461346
expect(parseInt(outlineGap.style.width)).toBeGreaterThan(0);
13471347
}));
13481348

1349+
it('should calculate the gap when starting off in RTL', fakeAsync(() => {
1350+
fixture.destroy();
1351+
TestBed.resetTestingModule();
1352+
1353+
const outlineFixture = createComponent(MatInputWithAppearanceAndLabel, [{
1354+
provide: Directionality,
1355+
useValue: {change: new Subject<Direction>(), value: 'rtl'}
1356+
}]);
1357+
1358+
outlineFixture.componentInstance.appearance = 'outline';
1359+
outlineFixture.detectChanges();
1360+
flush();
1361+
outlineFixture.detectChanges();
1362+
1363+
const wrapperElement = outlineFixture.nativeElement;
1364+
const outlineStart = wrapperElement.querySelector('.mat-form-field-outline-start');
1365+
const outlineGap = wrapperElement.querySelector('.mat-form-field-outline-gap');
1366+
1367+
expect(parseInt(outlineStart.style.width)).toBeGreaterThan(0);
1368+
expect(parseInt(outlineGap.style.width)).toBeGreaterThan(0);
1369+
}));
1370+
13491371
it('should not set an outline gap if the label is empty', fakeAsync(() => {
13501372
fixture.destroy();
13511373
TestBed.resetTestingModule();

0 commit comments

Comments
 (0)