Skip to content

Commit 95cc020

Browse files
crisbetowagnermaciel
authored andcommitted
fix(material/form-field): dynamic hint end alignment not working (#20657)
Binding to the `align` property of `mat-hint` didn't work, because while we were adding a class, the class wasn't being used. Using a static value works by accident. Fixes #20629. (cherry picked from commit a087edd)
1 parent d10a20f commit 95cc020

File tree

6 files changed

+44
-7
lines changed

6 files changed

+44
-7
lines changed

src/material-experimental/mdc-form-field/directives/hint.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ let nextUniqueId = 0;
1515
selector: 'mat-hint',
1616
host: {
1717
'class': 'mat-mdc-form-field-hint',
18-
'[class.mat-form-field-hint-end]': 'align == "end"',
18+
'[class.mat-mdc-form-field-hint-end]': 'align === "end"',
1919
'[id]': 'id',
2020
// Remove align attribute to prevent it from interfering with layout.
2121
'[attr.align]': 'null',

src/material-experimental/mdc-form-field/form-field.scss

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@
5858
box-sizing: border-box;
5959
}
6060

61+
.mat-mdc-form-field-hint-end {
62+
order: 1;
63+
}
64+
6165
// In order to make it possible for developers to disable animations for form-fields,
6266
// we only activate the animation styles if animations are not explicitly disabled.
6367
.mat-mdc-form-field:not(.mat-form-field-no-animations) {

src/material-experimental/mdc-input/input.spec.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -538,16 +538,30 @@ describe('MatMdcInput without forms', () => {
538538
expect(input.getAttribute('aria-describedby')).toBe('start end');
539539
}));
540540

541+
it('should set a class on the hint element based on its alignment', fakeAsync(() => {
542+
const fixture = createComponent(MatInputMultipleHintTestController);
543+
544+
fixture.componentInstance.startId = 'start';
545+
fixture.componentInstance.endId = 'end';
546+
fixture.detectChanges();
547+
548+
const start = fixture.nativeElement.querySelector('#start');
549+
const end = fixture.nativeElement.querySelector('#end');
550+
551+
expect(start.classList).not.toContain('mat-mdc-form-field-hint-end');
552+
expect(end.classList).toContain('mat-mdc-form-field-hint-end');
553+
}));
554+
541555
it('sets the aria-describedby when a hintLabel is set, in addition to a mat-hint',
542556
fakeAsync(() => {
543557
let fixture = createComponent(MatInputMultipleHintMixedTestController);
544558

545559
fixture.detectChanges();
546560

547561
let hintLabel = fixture.debugElement.query(
548-
By.css('.mat-mdc-form-field-hint:not(.mat-form-field-hint-end)'))!.nativeElement;
562+
By.css('.mat-mdc-form-field-hint:not(.mat-mdc-form-field-hint-end)'))!.nativeElement;
549563
let endLabel = fixture.debugElement
550-
.query(By.css('.mat-mdc-form-field-hint.mat-form-field-hint-end'))!.nativeElement;
564+
.query(By.css('.mat-mdc-form-field-hint.mat-mdc-form-field-hint-end'))!.nativeElement;
551565
let input = fixture.debugElement.query(By.css('input'))!.nativeElement;
552566
let ariaValue = input.getAttribute('aria-describedby');
553567

src/material/form-field/form-field.scss

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,10 @@ $mat-form-field-default-infix-width: 180px !default;
230230
position: relative;
231231
}
232232

233+
.mat-form-field-hint-end {
234+
order: 1;
235+
}
236+
233237
.mat-form-field._mat-animation-noopable {
234238
.mat-form-field-label,
235239
.mat-form-field-ripple {

src/material/form-field/hint.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export const _MAT_HINT = new InjectionToken<MatHint>('MatHint');
2525
selector: 'mat-hint',
2626
host: {
2727
'class': 'mat-hint',
28-
'[class.mat-right]': 'align == "end"',
28+
'[class.mat-form-field-hint-end]': 'align === "end"',
2929
'[attr.id]': 'id',
3030
// Remove align attribute to prevent it from interfering with layout.
3131
'[attr.align]': 'null',

src/material/input/input.spec.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -628,15 +628,30 @@ describe('MatInput without forms', () => {
628628
expect(input.getAttribute('aria-describedby')).toBe('start end');
629629
}));
630630

631+
it('should set a class on the hint element based on its alignment', fakeAsync(() => {
632+
const fixture = createComponent(MatInputMultipleHintTestController);
633+
634+
fixture.componentInstance.startId = 'start';
635+
fixture.componentInstance.endId = 'end';
636+
fixture.detectChanges();
637+
638+
const start = fixture.nativeElement.querySelector('#start');
639+
const end = fixture.nativeElement.querySelector('#end');
640+
641+
expect(start.classList).not.toContain('mat-form-field-hint-end');
642+
expect(end.classList).toContain('mat-form-field-hint-end');
643+
}));
644+
631645
it('sets the aria-describedby when a hintLabel is set, in addition to a mat-hint',
632646
fakeAsync(() => {
633647
let fixture = createComponent(MatInputMultipleHintMixedTestController);
634648

635649
fixture.detectChanges();
636650

637-
let hintLabel =
638-
fixture.debugElement.query(By.css('.mat-hint:not(.mat-right)'))!.nativeElement;
639-
let endLabel = fixture.debugElement.query(By.css('.mat-hint.mat-right'))!.nativeElement;
651+
let hintLabel = fixture.debugElement
652+
.query(By.css('.mat-hint:not(.mat-form-field-hint-end)'))!.nativeElement;
653+
let endLabel = fixture.debugElement
654+
.query(By.css('.mat-hint.mat-form-field-hint-end'))!.nativeElement;
640655
let input = fixture.debugElement.query(By.css('input'))!.nativeElement;
641656
let ariaValue = input.getAttribute('aria-describedby');
642657

0 commit comments

Comments
 (0)