Skip to content

Commit 0410756

Browse files
committed
fix(checkbox): not handling unknown color palette
Fixes the checkbox component not falling back to the default color, if the color is `undefined`. We need to handle this, because `ThemePalette` allows `undefined`. Fixes #18465.
1 parent 3d35180 commit 0410756

File tree

4 files changed

+19
-5
lines changed

4 files changed

+19
-5
lines changed

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
flushMicrotasks,
88
TestBed,
99
} from '@angular/core/testing';
10+
import {ThemePalette} from '@angular/material/core';
1011
import {FormControl, FormsModule, NgModel, ReactiveFormsModule} from '@angular/forms';
1112
import {By} from '@angular/platform-browser';
1213
import {
@@ -467,6 +468,13 @@ describe('MDC-based MatCheckbox', () => {
467468
expect(checkboxNativeElement.classList.contains('mat-accent')).toBe(true);
468469
expect(checkboxNativeElement.classList.contains('custom-class')).toBe(true);
469470
}));
471+
472+
it('should default to accent if no color is passed in', fakeAsync(() => {
473+
testComponent.checkboxColor = undefined;
474+
fixture.detectChanges();
475+
expect(checkboxNativeElement.classList).toContain('mat-accent');
476+
}));
477+
470478
});
471479

472480
describe(`when MAT_CHECKBOX_CLICK_ACTION is set`, () => {
@@ -1052,7 +1060,7 @@ class SingleCheckbox {
10521060
parentElementClicked: boolean = false;
10531061
parentElementKeyedUp: boolean = false;
10541062
checkboxId: string|null = 'simple-check';
1055-
checkboxColor: string = 'primary';
1063+
checkboxColor: ThemePalette = 'primary';
10561064
checkboxValue: string = 'single_checkbox';
10571065

10581066
onCheckboxClick: (event?: Event) => void = () => {};

src/material-experimental/mdc-checkbox/checkbox.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ export class MatCheckboxChange {
5959
host: {
6060
'class': 'mat-mdc-checkbox',
6161
'[attr.tabindex]': 'null',
62-
'[class.mat-primary]': 'color == "primary"',
63-
'[class.mat-accent]': 'color == "accent"',
64-
'[class.mat-warn]': 'color == "warn"',
62+
'[class.mat-primary]': 'color === "primary"',
63+
'[class.mat-accent]': 'color !== "primary" && color !== "warn"',
64+
'[class.mat-warn]': 'color === "warn"',
6565
'[class._mat-animation-noopable]': `_animationMode === 'NoopAnimations'`,
6666
'[class.mdc-checkbox--disabled]': 'disabled',
6767
'[id]': 'id',

src/material/checkbox/checkbox.spec.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,8 +465,14 @@ describe('MatCheckbox', () => {
465465
expect(checkboxNativeElement.classList.contains('mat-primary')).toBe(false);
466466
expect(checkboxNativeElement.classList.contains('mat-accent')).toBe(true);
467467
expect(checkboxNativeElement.classList.contains('custom-class')).toBe(true);
468+
});
468469

470+
it('should default to accent if no color is passed in', () => {
471+
testComponent.checkboxColor = undefined;
472+
fixture.detectChanges();
473+
expect(checkboxNativeElement.classList).toContain('mat-accent');
469474
});
475+
470476
});
471477

472478
describe('state transition css classes', () => {

src/material/checkbox/checkbox.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ const _MatCheckboxMixinBase:
100100
CanDisableRippleCtor &
101101
CanDisableCtor &
102102
typeof MatCheckboxBase =
103-
mixinTabIndex(mixinColor(mixinDisableRipple(mixinDisabled(MatCheckboxBase))));
103+
mixinTabIndex(mixinColor(mixinDisableRipple(mixinDisabled(MatCheckboxBase)), 'accent'));
104104

105105

106106
/**

0 commit comments

Comments
 (0)