Skip to content

refactor(checkbox): remove deprecated APIs for version 10 #19381

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/dev-app/checkbox/checkbox-demo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

import {Component, Directive} from '@angular/core';
import {MAT_CHECKBOX_CLICK_ACTION} from '@angular/material/checkbox';
import {MAT_CHECKBOX_DEFAULT_OPTIONS} from '@angular/material/checkbox';
import {ANIMATION_MODULE_TYPE} from '@angular/platform-browser/animations';
import {ThemePalette} from '@angular/material/core';

Expand All @@ -20,14 +20,14 @@ export interface Task {

@Directive({
selector: '[clickActionNoop]',
providers: [{provide: MAT_CHECKBOX_CLICK_ACTION, useValue: 'noop'}],
providers: [{provide: MAT_CHECKBOX_DEFAULT_OPTIONS, useValue: {clickAction: 'noop'}}],
})
export class ClickActionNoop {
}

@Directive({
selector: '[clickActionCheck]',
providers: [{provide: MAT_CHECKBOX_CLICK_ACTION, useValue: 'check'}],
providers: [{provide: MAT_CHECKBOX_DEFAULT_OPTIONS, useValue: {clickAction: 'check'}}],
})
export class ClickActionCheck {
}
Expand Down
6 changes: 3 additions & 3 deletions src/dev-app/mdc-checkbox/mdc-checkbox-demo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

import {Component, Directive} from '@angular/core';
import {MAT_CHECKBOX_CLICK_ACTION} from '@angular/material/checkbox';
import {MAT_CHECKBOX_DEFAULT_OPTIONS} from '@angular/material/checkbox';
import {ANIMATION_MODULE_TYPE} from '@angular/platform-browser/animations';
import {ThemePalette} from '@angular/material/core';

Expand All @@ -20,14 +20,14 @@ export interface Task {

@Directive({
selector: '[clickActionNoop]',
providers: [{provide: MAT_CHECKBOX_CLICK_ACTION, useValue: 'noop'}],
providers: [{provide: MAT_CHECKBOX_DEFAULT_OPTIONS, useValue: {clickAction: 'noop'}}],
})
export class ClickActionNoop {
}

@Directive({
selector: '[clickActionCheck]',
providers: [{provide: MAT_CHECKBOX_CLICK_ACTION, useValue: 'check'}],
providers: [{provide: MAT_CHECKBOX_DEFAULT_OPTIONS, useValue: {clickAction: 'check'}}],
})
export class ClickActionCheck {
}
Expand Down
34 changes: 0 additions & 34 deletions src/material-experimental/mdc-checkbox/checkbox.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
import {FormControl, FormsModule, NgModel, ReactiveFormsModule} from '@angular/forms';
import {By} from '@angular/platform-browser';
import {
MAT_CHECKBOX_CLICK_ACTION,
MatCheckbox,
MatCheckboxChange,
MatCheckboxModule
Expand Down Expand Up @@ -469,39 +468,6 @@ describe('MDC-based MatCheckbox', () => {
}));
});

describe(`when MAT_CHECKBOX_CLICK_ACTION is set`, () => {
beforeEach(() => {
TestBed.resetTestingModule();
TestBed.configureTestingModule({
imports: [MatCheckboxModule, FormsModule, ReactiveFormsModule],
declarations: [SingleCheckbox],
providers: [
{provide: MAT_CHECKBOX_CLICK_ACTION, useValue: 'check'},
{provide: MAT_CHECKBOX_DEFAULT_OPTIONS, useValue: {clickAction: 'noop'}}
]
});

fixture = createComponent(SingleCheckbox);
fixture.detectChanges();

checkboxDebugElement = fixture.debugElement.query(By.directive(MatCheckbox))!;
checkboxNativeElement = checkboxDebugElement.nativeElement;
testComponent = fixture.debugElement.componentInstance;

inputElement = checkboxNativeElement.querySelector('input') as HTMLInputElement;
});

it('should override the value set in the default options', fakeAsync(() => {
testComponent.isIndeterminate = true;
inputElement.click();
fixture.detectChanges();
flush();

expect(inputElement.checked).toBe(true);
expect(inputElement.indeterminate).toBe(true);
}));
});

describe(`when MAT_CHECKBOX_CLICK_ACTION is 'check'`, () => {
beforeEach(() => {
TestBed.resetTestingModule();
Expand Down
22 changes: 5 additions & 17 deletions src/material-experimental/mdc-checkbox/checkbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,7 @@ import {
ViewEncapsulation
} from '@angular/core';
import {ControlValueAccessor, NG_VALUE_ACCESSOR} from '@angular/forms';
import {
MAT_CHECKBOX_CLICK_ACTION,
MAT_CHECKBOX_DEFAULT_OPTIONS,
MatCheckboxClickAction, MatCheckboxDefaultOptions
} from '@angular/material/checkbox';
import {MAT_CHECKBOX_DEFAULT_OPTIONS, MatCheckboxDefaultOptions} from '@angular/material/checkbox';
import {ThemePalette, RippleAnimationConfig} from '@angular/material/core';
import {ANIMATION_MODULE_TYPE} from '@angular/platform-browser/animations';
import {MDCCheckboxAdapter, MDCCheckboxFoundation} from '@material/checkbox';
Expand Down Expand Up @@ -233,12 +229,6 @@ export class MatCheckbox implements AfterViewInit, OnDestroy, ControlValueAccess
constructor(
private _changeDetectorRef: ChangeDetectorRef,
@Attribute('tabindex') tabIndex: string,
/**
* @deprecated `_clickAction` parameter to be removed, use
* `MAT_CHECKBOX_DEFAULT_OPTIONS`
* @breaking-change 10.0.0
*/
@Optional() @Inject(MAT_CHECKBOX_CLICK_ACTION) private _clickAction: MatCheckboxClickAction,
@Optional() @Inject(ANIMATION_MODULE_TYPE) public _animationMode?: string,
@Optional() @Inject(MAT_CHECKBOX_DEFAULT_OPTIONS)
private _options?: MatCheckboxDefaultOptions) {
Expand All @@ -252,10 +242,6 @@ export class MatCheckbox implements AfterViewInit, OnDestroy, ControlValueAccess
if (this._options.color) {
this.color = this._options.color;
}

// @breaking-change 10.0.0: Remove this after the `_clickAction` parameter is removed as an
// injection parameter.
this._clickAction = this._clickAction || this._options.clickAction;
}

ngAfterViewInit() {
Expand Down Expand Up @@ -334,13 +320,15 @@ export class MatCheckbox implements AfterViewInit, OnDestroy, ControlValueAccess
* state like other browsers do.
*/
_onClick() {
if (this._clickAction === 'noop') {
const clickAction = this._options?.clickAction;

if (clickAction === 'noop') {
this._nativeCheckbox.nativeElement.checked = this.checked;
this._nativeCheckbox.nativeElement.indeterminate = this.indeterminate;
return;
}

if (this.indeterminate && this._clickAction !== 'check') {
if (this.indeterminate && clickAction !== 'check') {
this.indeterminate = false;
// tslint:disable:max-line-length
// We use `Promise.resolve().then` to ensure the same timing as the original `MatCheckbox`:
Expand Down
5 changes: 0 additions & 5 deletions src/material-experimental/mdc-checkbox/public-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,9 @@ export * from './checkbox';
export * from './module';

export {
MAT_CHECKBOX_CLICK_ACTION,
MAT_CHECKBOX_REQUIRED_VALIDATOR,
MatCheckboxClickAction,
MatCheckboxRequiredValidator,
_MatCheckboxRequiredValidatorModule,
/**
* @deprecated
* @breaking-change 9.0.0
*/
TransitionCheckState,
} from '@angular/material/checkbox';
8 changes: 0 additions & 8 deletions src/material/checkbox/checkbox-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,3 @@ export function MAT_CHECKBOX_DEFAULT_OPTIONS_FACTORY(): MatCheckboxDefaultOption
* undefined: Same as `check-indeterminate`.
*/
export type MatCheckboxClickAction = 'noop' | 'check' | 'check-indeterminate' | undefined;

/**
* Injection token that can be used to specify the checkbox click behavior.
* @deprecated Injection token will be removed, use `MAT_CHECKBOX_DEFAULT_OPTIONS` instead.
* @breaking-change 10.0.0
*/
export const MAT_CHECKBOX_CLICK_ACTION =
new InjectionToken<MatCheckboxClickAction>('mat-checkbox-click-action');
34 changes: 0 additions & 34 deletions src/material/checkbox/checkbox.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
MatCheckboxChange,
MatCheckboxModule
} from './index';
import {MAT_CHECKBOX_CLICK_ACTION} from './checkbox-config';
import {MutationObserverFactory} from '@angular/cdk/observers';
import {ThemePalette} from '@angular/material/core';

Expand Down Expand Up @@ -539,39 +538,6 @@ describe('MatCheckbox', () => {
}));
});

describe(`when MAT_CHECKBOX_CLICK_ACTION is set`, () => {
beforeEach(() => {
TestBed.resetTestingModule();
TestBed.configureTestingModule({
imports: [MatCheckboxModule, FormsModule, ReactiveFormsModule],
declarations: [SingleCheckbox],
providers: [
{provide: MAT_CHECKBOX_CLICK_ACTION, useValue: 'check'},
{provide: MAT_CHECKBOX_DEFAULT_OPTIONS, useValue: {clickAction: 'noop'}}
]
});

fixture = createComponent(SingleCheckbox);
fixture.detectChanges();

checkboxDebugElement = fixture.debugElement.query(By.directive(MatCheckbox))!;
checkboxNativeElement = checkboxDebugElement.nativeElement;
testComponent = fixture.debugElement.componentInstance;

inputElement = checkboxNativeElement.querySelector('input') as HTMLInputElement;
});

it('should override the value set in the default options', fakeAsync(() => {
testComponent.isIndeterminate = true;
inputElement.click();
fixture.detectChanges();
flush();

expect(inputElement.checked).toBe(true);
expect(inputElement.indeterminate).toBe(true);
}));
});

describe(`when MAT_CHECKBOX_CLICK_ACTION is 'check'`, () => {
beforeEach(() => {
TestBed.resetTestingModule();
Expand Down
25 changes: 6 additions & 19 deletions src/material/checkbox/checkbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,7 @@ import {
mixinTabIndex,
} from '@angular/material/core';
import {ANIMATION_MODULE_TYPE} from '@angular/platform-browser/animations';
import {
MAT_CHECKBOX_CLICK_ACTION,
MAT_CHECKBOX_DEFAULT_OPTIONS,
MatCheckboxClickAction,
MatCheckboxDefaultOptions
} from './checkbox-config';
import {MAT_CHECKBOX_DEFAULT_OPTIONS, MatCheckboxDefaultOptions} from './checkbox-config';


// Increasing integer for generating unique ids for checkbox components.
Expand Down Expand Up @@ -199,13 +194,6 @@ export class MatCheckbox extends _MatCheckboxMixinBase implements ControlValueAc
private _focusMonitor: FocusMonitor,
private _ngZone: NgZone,
@Attribute('tabindex') tabIndex: string,
/**
* @deprecated `_clickAction` parameter to be removed, use
* `MAT_CHECKBOX_DEFAULT_OPTIONS`
* @breaking-change 10.0.0
*/
@Optional() @Inject(MAT_CHECKBOX_CLICK_ACTION)
private _clickAction: MatCheckboxClickAction,
@Optional() @Inject(ANIMATION_MODULE_TYPE) public _animationMode?: string,
@Optional() @Inject(MAT_CHECKBOX_DEFAULT_OPTIONS)
private _options?: MatCheckboxDefaultOptions) {
Expand All @@ -231,9 +219,6 @@ export class MatCheckbox extends _MatCheckboxMixinBase implements ControlValueAc
});
}
});

// TODO: Remove this after the `_clickAction` parameter is removed as an injection parameter.
this._clickAction = this._clickAction || this._options.clickAction;
}

ngAfterViewInit() {
Expand Down Expand Up @@ -395,6 +380,8 @@ export class MatCheckbox extends _MatCheckboxMixinBase implements ControlValueAc
* @param event
*/
_onInputClick(event: Event) {
const clickAction = this._options?.clickAction;

// We have to stop propagation for click events on the visual hidden input element.
// By default, when a user clicks on a label element, a generated click event will be
// dispatched on the associated input element. Since we are using a label element as our
Expand All @@ -405,9 +392,9 @@ export class MatCheckbox extends _MatCheckboxMixinBase implements ControlValueAc
event.stopPropagation();

// If resetIndeterminate is false, and the current state is indeterminate, do nothing on click
if (!this.disabled && this._clickAction !== 'noop') {
if (!this.disabled && clickAction !== 'noop') {
// When user manually click on the checkbox, `indeterminate` is set to false.
if (this.indeterminate && this._clickAction !== 'check') {
if (this.indeterminate && clickAction !== 'check') {

Promise.resolve().then(() => {
this._indeterminate = false;
Expand All @@ -423,7 +410,7 @@ export class MatCheckbox extends _MatCheckboxMixinBase implements ControlValueAc
// It is important to only emit it, if the native input triggered one, because
// we don't want to trigger a change event, when the `checked` variable changes for example.
this._emitChangeEvent();
} else if (!this.disabled && this._clickAction === 'noop') {
} else if (!this.disabled && clickAction === 'noop') {
// Reset native input when clicked with noop. The native checkbox becomes checked after
// click, reset it to be align with `checked` value of `mat-checkbox`.
this._inputElement.nativeElement.checked = this.checked;
Expand Down
4 changes: 4 additions & 0 deletions src/material/schematics/ng-update/data/constructor-checks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ export const constructorChecks: VersionChanges<ConstructorChecksUpgradeData> = {
{
pr: 'https://github.com/angular/components/pull/19379',
changes: ['MatSlider']
},
{
pr: 'https://github.com/angular/components/pull/19381',
changes: ['MatCheckbox']
}
],
[TargetVersion.V9]: [
Expand Down
7 changes: 2 additions & 5 deletions tools/public_api_guard/material/checkbox.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ export declare class _MatCheckboxRequiredValidatorModule {
static ɵmod: i0.ɵɵNgModuleDefWithMeta<_MatCheckboxRequiredValidatorModule, [typeof i1.MatCheckboxRequiredValidator], never, [typeof i1.MatCheckboxRequiredValidator]>;
}

export declare const MAT_CHECKBOX_CLICK_ACTION: InjectionToken<MatCheckboxClickAction>;

export declare const MAT_CHECKBOX_CONTROL_VALUE_ACCESSOR: any;

export declare const MAT_CHECKBOX_DEFAULT_OPTIONS: InjectionToken<MatCheckboxDefaultOptions>;
Expand Down Expand Up @@ -35,8 +33,7 @@ export declare class MatCheckbox extends _MatCheckboxMixinBase implements Contro
set required(value: boolean);
ripple: MatRipple;
value: string;
constructor(elementRef: ElementRef<HTMLElement>, _changeDetectorRef: ChangeDetectorRef, _focusMonitor: FocusMonitor, _ngZone: NgZone, tabIndex: string,
_clickAction: MatCheckboxClickAction, _animationMode?: string | undefined, _options?: MatCheckboxDefaultOptions | undefined);
constructor(elementRef: ElementRef<HTMLElement>, _changeDetectorRef: ChangeDetectorRef, _focusMonitor: FocusMonitor, _ngZone: NgZone, tabIndex: string, _animationMode?: string | undefined, _options?: MatCheckboxDefaultOptions | undefined);
_getAriaChecked(): 'true' | 'false' | 'mixed';
_isRippleDisabled(): any;
_onInputClick(event: Event): void;
Expand All @@ -56,7 +53,7 @@ export declare class MatCheckbox extends _MatCheckboxMixinBase implements Contro
static ngAcceptInputType_indeterminate: BooleanInput;
static ngAcceptInputType_required: BooleanInput;
static ɵcmp: i0.ɵɵComponentDefWithMeta<MatCheckbox, "mat-checkbox", ["matCheckbox"], { "disableRipple": "disableRipple"; "color": "color"; "tabIndex": "tabIndex"; "ariaLabel": "aria-label"; "ariaLabelledby": "aria-labelledby"; "id": "id"; "required": "required"; "labelPosition": "labelPosition"; "name": "name"; "value": "value"; "checked": "checked"; "disabled": "disabled"; "indeterminate": "indeterminate"; }, { "change": "change"; "indeterminateChange": "indeterminateChange"; }, never, ["*"]>;
static ɵfac: i0.ɵɵFactoryDef<MatCheckbox, [null, null, null, null, { attribute: "tabindex"; }, { optional: true; }, { optional: true; }, { optional: true; }]>;
static ɵfac: i0.ɵɵFactoryDef<MatCheckbox, [null, null, null, null, { attribute: "tabindex"; }, { optional: true; }, { optional: true; }]>;
}

export declare class MatCheckboxChange {
Expand Down