Skip to content

Commit d8def8b

Browse files
authored
fix(material-experimental/mdc-form-field): not adding mat-primary theme class (#20605)
The MDC-based form field doesn't add the `mat-primary` class if the color isn't `accent` or `warn`, because it doesn't go through the `color` mixin.
1 parent 8c2838d commit d8def8b

File tree

4 files changed

+70
-3
lines changed

4 files changed

+70
-3
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,9 @@ const FLOATING_LABEL_DEFAULT_DOCKED_TRANSFORM = `translateY(-50%)`;
112112
'[class.mat-form-field-appearance-outline]': 'appearance == "outline"',
113113
'[class.mat-form-field-hide-placeholder]': '_hasFloatingLabel() && !_shouldLabelFloat()',
114114
'[class.mat-focused]': '_control.focused',
115-
'[class.mat-accent]': 'color == "accent"',
116-
'[class.mat-warn]': 'color == "warn"',
115+
'[class.mat-primary]': 'color !== "accent" && color !== "warn"',
116+
'[class.mat-accent]': 'color === "accent"',
117+
'[class.mat-warn]': 'color === "warn"',
117118
'[class.ng-untouched]': '_shouldForward("untouched")',
118119
'[class.ng-touched]': '_shouldForward("touched")',
119120
'[class.ng-pristine]': '_shouldForward("pristine")',

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import {
3232
ErrorStateMatcher,
3333
MAT_LABEL_GLOBAL_OPTIONS,
3434
ShowOnDirtyErrorStateMatcher,
35+
ThemePalette,
3536
} from '@angular/material/core';
3637
import {By} from '@angular/platform-browser';
3738
import {BrowserAnimationsModule, NoopAnimationsModule} from '@angular/platform-browser/animations';
@@ -854,6 +855,27 @@ describe('MatMdcInput without forms', () => {
854855
expect(formField._control.empty).toBe(false);
855856
}));
856857

858+
it('should default the form field color to primary', fakeAsync(() => {
859+
const fixture = createComponent(MatInputWithColor);
860+
fixture.detectChanges();
861+
862+
const formField = fixture.nativeElement.querySelector('.mat-mdc-form-field');
863+
expect(formField.classList).toContain('mat-primary');
864+
}));
865+
866+
it('should be able to change the form field color', fakeAsync(() => {
867+
const fixture = createComponent(MatInputWithColor);
868+
fixture.componentInstance.color = 'accent';
869+
fixture.detectChanges();
870+
const formField = fixture.nativeElement.querySelector('.mat-mdc-form-field');
871+
872+
expect(formField.classList).toContain('mat-accent');
873+
874+
fixture.componentInstance.color = 'warn';
875+
fixture.detectChanges();
876+
expect(formField.classList).toContain('mat-warn');
877+
}));
878+
857879
});
858880

859881
describe('MatMdcInput with forms', () => {
@@ -1717,3 +1739,14 @@ class CustomMatInputAccessor {
17171739
set value(_value: any) {}
17181740
private _value = null;
17191741
}
1742+
1743+
1744+
@Component({
1745+
template: `
1746+
<mat-form-field [color]="color">
1747+
<input matNativeControl>
1748+
</mat-form-field>`
1749+
})
1750+
class MatInputWithColor {
1751+
color: ThemePalette;
1752+
}

src/material-experimental/mdc-select/_select-theme.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848

4949
.mat-mdc-form-field {
5050
&.mat-focused {
51-
.mat-mdc-select-arrow {
51+
&.mat-primary .mat-mdc-select-arrow {
5252
color: _get-mdc-focused-text-color(primary);
5353
}
5454

src/material/input/input.spec.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import {
3131
FloatLabelType,
3232
MAT_LABEL_GLOBAL_OPTIONS,
3333
ShowOnDirtyErrorStateMatcher,
34+
ThemePalette,
3435
} from '@angular/material/core';
3536
import {
3637
getMatFormFieldDuplicatedHintError,
@@ -1036,6 +1037,27 @@ describe('MatInput without forms', () => {
10361037
}).not.toThrow();
10371038
}));
10381039

1040+
it('should default the form field color to primary', fakeAsync(() => {
1041+
const fixture = createComponent(MatInputWithColor);
1042+
fixture.detectChanges();
1043+
1044+
const formField = fixture.nativeElement.querySelector('.mat-form-field');
1045+
expect(formField.classList).toContain('mat-primary');
1046+
}));
1047+
1048+
it('should be able to change the form field color', fakeAsync(() => {
1049+
const fixture = createComponent(MatInputWithColor);
1050+
fixture.componentInstance.color = 'accent';
1051+
fixture.detectChanges();
1052+
const formField = fixture.nativeElement.querySelector('.mat-form-field');
1053+
1054+
expect(formField.classList).toContain('mat-accent');
1055+
1056+
fixture.componentInstance.color = 'warn';
1057+
fixture.detectChanges();
1058+
expect(formField.classList).toContain('mat-warn');
1059+
}));
1060+
10391061
});
10401062

10411063
describe('MatInput with forms', () => {
@@ -2323,3 +2345,14 @@ class MatInputWithDefaultNgIf {}
23232345
class MatInputWithAnotherNgIf {
23242346
inputValue = 'test';
23252347
}
2348+
2349+
2350+
@Component({
2351+
template: `
2352+
<mat-form-field [color]="color">
2353+
<input matNativeControl>
2354+
</mat-form-field>`
2355+
})
2356+
class MatInputWithColor {
2357+
color: ThemePalette;
2358+
}

0 commit comments

Comments
 (0)