Skip to content

Commit 4cc69d2

Browse files
authored
feat(material-experimental/mdc-checkbox): add default options (#17578)
* feat(material-experimental/mdc-checkbox): add default options * add breaking changes comment
1 parent 05e6d07 commit 4cc69d2

File tree

2 files changed

+109
-6
lines changed

2 files changed

+109
-6
lines changed

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

Lines changed: 84 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
MatCheckboxChange,
1616
MatCheckboxModule
1717
} from './index';
18+
import {MAT_CHECKBOX_DEFAULT_OPTIONS} from '@angular/material/checkbox';
1819

1920

2021
describe('MatCheckbox', () => {
@@ -468,13 +469,48 @@ describe('MatCheckbox', () => {
468469
}));
469470
});
470471

472+
describe(`when MAT_CHECKBOX_CLICK_ACTION is set`, () => {
473+
beforeEach(() => {
474+
TestBed.resetTestingModule();
475+
TestBed.configureTestingModule({
476+
imports: [MatCheckboxModule, FormsModule, ReactiveFormsModule],
477+
declarations: [SingleCheckbox],
478+
providers: [
479+
{provide: MAT_CHECKBOX_CLICK_ACTION, useValue: 'check'},
480+
{provide: MAT_CHECKBOX_DEFAULT_OPTIONS, useValue: {clickAction: 'noop'}}
481+
]
482+
});
483+
484+
fixture = createComponent(SingleCheckbox);
485+
fixture.detectChanges();
486+
487+
checkboxDebugElement = fixture.debugElement.query(By.directive(MatCheckbox))!;
488+
checkboxNativeElement = checkboxDebugElement.nativeElement;
489+
testComponent = fixture.debugElement.componentInstance;
490+
491+
inputElement = checkboxNativeElement.querySelector('input') as HTMLInputElement;
492+
});
493+
494+
it('should override the value set in the default options', fakeAsync(() => {
495+
testComponent.isIndeterminate = true;
496+
inputElement.click();
497+
fixture.detectChanges();
498+
flush();
499+
500+
expect(inputElement.checked).toBe(true);
501+
expect(inputElement.indeterminate).toBe(true);
502+
}));
503+
});
504+
471505
describe(`when MAT_CHECKBOX_CLICK_ACTION is 'check'`, () => {
472506
beforeEach(() => {
473507
TestBed.resetTestingModule();
474508
TestBed.configureTestingModule({
475509
imports: [MatCheckboxModule, FormsModule, ReactiveFormsModule],
476510
declarations: [SingleCheckbox],
477-
providers: [{provide: MAT_CHECKBOX_CLICK_ACTION, useValue: 'check'}]
511+
providers: [
512+
{provide: MAT_CHECKBOX_DEFAULT_OPTIONS, useValue: {clickAction: 'check'}}
513+
]
478514
});
479515

480516
fixture = createComponent(SingleCheckbox);
@@ -506,7 +542,9 @@ describe('MatCheckbox', () => {
506542
TestBed.configureTestingModule({
507543
imports: [MatCheckboxModule, FormsModule, ReactiveFormsModule],
508544
declarations: [SingleCheckbox],
509-
providers: [{provide: MAT_CHECKBOX_CLICK_ACTION, useValue: 'noop'}]
545+
providers: [
546+
{provide: MAT_CHECKBOX_DEFAULT_OPTIONS, useValue: {clickAction: 'noop'}}
547+
]
510548
});
511549

512550
fixture = createComponent(SingleCheckbox);
@@ -933,6 +971,50 @@ describe('MatCheckbox', () => {
933971
});
934972
});
935973

974+
describe('MatCheckboxDefaultOptions', () => {
975+
describe('when MAT_CHECKBOX_DEFAULT_OPTIONS overridden', () => {
976+
beforeEach(() => {
977+
TestBed.configureTestingModule({
978+
imports: [MatCheckboxModule, FormsModule],
979+
declarations: [SingleCheckbox, SingleCheckbox],
980+
providers: [{
981+
provide: MAT_CHECKBOX_DEFAULT_OPTIONS,
982+
useValue: {color: 'primary'},
983+
}],
984+
});
985+
986+
TestBed.compileComponents();
987+
});
988+
989+
it('should override default color in component', () => {
990+
const fixture: ComponentFixture<SingleCheckbox> =
991+
TestBed.createComponent(SingleCheckbox);
992+
fixture.detectChanges();
993+
const checkboxDebugElement: DebugElement =
994+
fixture.debugElement.query(By.directive(MatCheckbox))!;
995+
expect(
996+
checkboxDebugElement.nativeElement.classList
997+
).toContain('mat-primary');
998+
});
999+
1000+
it('should not override explicit input bindings', () => {
1001+
const fixture: ComponentFixture<SingleCheckbox> =
1002+
TestBed.createComponent(SingleCheckbox);
1003+
fixture.componentInstance.checkboxColor = 'warn';
1004+
fixture.detectChanges();
1005+
const checkboxDebugElement: DebugElement =
1006+
fixture.debugElement.query(By.directive(MatCheckbox))!;
1007+
expect(
1008+
checkboxDebugElement.nativeElement.classList
1009+
).not.toContain('mat-primary');
1010+
expect(
1011+
checkboxDebugElement.nativeElement.classList
1012+
).toContain('mat-warn');
1013+
expect(checkboxDebugElement.nativeElement.classList).toContain('mat-warn');
1014+
});
1015+
});
1016+
});
1017+
9361018
/** Simple component for testing a single checkbox. */
9371019
@Component({
9381020
template: `

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

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@ import {
2626
ViewEncapsulation
2727
} from '@angular/core';
2828
import {ControlValueAccessor, NG_VALUE_ACCESSOR} from '@angular/forms';
29-
import {MAT_CHECKBOX_CLICK_ACTION, MatCheckboxClickAction} from '@angular/material/checkbox';
29+
import {
30+
MAT_CHECKBOX_CLICK_ACTION,
31+
MAT_CHECKBOX_DEFAULT_OPTIONS,
32+
MatCheckboxClickAction, MatCheckboxDefaultOptions
33+
} from '@angular/material/checkbox';
3034
import {ThemePalette} from '@angular/material/core';
3135
import {ANIMATION_MODULE_TYPE} from '@angular/platform-browser/animations';
3236
import {MDCCheckboxAdapter, MDCCheckboxFoundation} from '@material/checkbox';
@@ -228,12 +232,29 @@ export class MatCheckbox implements AfterViewInit, OnDestroy, ControlValueAccess
228232
private _changeDetectorRef: ChangeDetectorRef,
229233
private _platform: Platform,
230234
@Attribute('tabindex') tabIndex: string,
235+
/**
236+
* @deprecated `_clickAction` parameter to be removed, use
237+
* `MAT_CHECKBOX_DEFAULT_OPTIONS`
238+
* @breaking-change 10.0.0
239+
*/
231240
@Optional() @Inject(MAT_CHECKBOX_CLICK_ACTION) private _clickAction: MatCheckboxClickAction,
232-
@Optional() @Inject(ANIMATION_MODULE_TYPE) public _animationMode?: string) {
233-
this.tabIndex = parseInt(tabIndex) || 0;
234-
this._checkboxFoundation = new MDCCheckboxFoundation(this._checkboxAdapter);
241+
@Optional() @Inject(ANIMATION_MODULE_TYPE) public _animationMode?: string,
242+
@Optional() @Inject(MAT_CHECKBOX_DEFAULT_OPTIONS)
243+
private _options?: MatCheckboxDefaultOptions) {
235244
// Note: We don't need to set up the MDCFormFieldFoundation. Its only purpose is to manage the
236245
// ripple, which we do ourselves instead.
246+
this.tabIndex = parseInt(tabIndex) || 0;
247+
this._checkboxFoundation = new MDCCheckboxFoundation(this._checkboxAdapter);
248+
249+
this._options = this._options || {};
250+
251+
if (this._options.color) {
252+
this.color = this._options.color;
253+
}
254+
255+
// @breaking-change 10.0.0: Remove this after the `_clickAction` parameter is removed as an
256+
// injection parameter.
257+
this._clickAction = this._clickAction || this._options.clickAction;
237258
}
238259

239260
ngAfterViewInit() {

0 commit comments

Comments
 (0)