Skip to content

Commit f525f47

Browse files
committed
feat(checkbox): Support checkbox click action config
1 parent 541a95e commit f525f47

File tree

5 files changed

+140
-4
lines changed

5 files changed

+140
-4
lines changed

src/lib/checkbox/checkbox-config.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
import {InjectionToken} from '@angular/core';
9+
10+
11+
/**
12+
* Checkbox click action when user click on input element.
13+
* noop: Do not toggle checked or indeterminate.
14+
* check: Only toggle checked status, ignore indeterminate.
15+
* check-indeterminate: Toggle checked status, set indeterminate to false. Default behavior.
16+
* undefined: Same as `check-indeterminate`.
17+
*/
18+
export type MatCheckboxClickAction = 'noop' | 'check' | 'check-indeterminate' | undefined;
19+
20+
/**
21+
* Injection token that can be used to specify the checkbox click behavior.
22+
*/
23+
export const MAT_CHECKBOX_CLICK_ACTION =
24+
new InjectionToken<MatCheckboxClickAction>('mat-checkbox-click-action');

src/lib/checkbox/checkbox.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,24 @@ While the `indeterminate` property of the checkbox is true, it will render as in
2323
regardless of the `checked` value. Any interaction with the checkbox by a user (i.e., clicking) will
2424
remove the indeterminate state.
2525

26+
### Click action config
27+
When user clicks on the `mat-checkbox`, the default behavior is toggle `checked` value and set
28+
`indeterminate` to `false`. Developers now are able to change the behavior by providing a new value
29+
of `MatCheckboxClickAction` to the checkbox. The possible values are:
30+
31+
#### `noop`
32+
Do not change the `checked` value or `indeterminate` value. Developers have the power to
33+
implement customized click actions.
34+
35+
#### `check`
36+
Toggle `checked` value of the checkbox, ignore `indeterminate` value. If the
37+
checkbox is in `indeterminate` state, the checkbox will display as an `indeterminate` checkbox
38+
regardless the `checked` value.
39+
40+
####`check-indeterminate`
41+
Default behavior of `mat-checkbox`. Always set `indeterminate` to `false`
42+
when user click on the `mat-checkbox`.
43+
2644
### Theming
2745
The color of a `<mat-checkbox>` can be changed by using the `color` property. By default, checkboxes
2846
use the theme's accent color. This can be changed to `'primary'` or `'warn'`.

src/lib/checkbox/checkbox.spec.ts

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {ViewportRuler} from '@angular/cdk/scrolling';
1313
import {dispatchFakeEvent, FakeViewportRuler} from '@angular/cdk/testing';
1414
import {MatCheckbox, MatCheckboxChange, MatCheckboxModule} from './index';
1515
import {RIPPLE_FADE_IN_DURATION, RIPPLE_FADE_OUT_DURATION} from '@angular/material/core';
16+
import {MAT_CHECKBOX_CLICK_ACTION} from './checkbox-config';
1617

1718

1819
describe('MatCheckbox', () => {
@@ -542,6 +543,88 @@ describe('MatCheckbox', () => {
542543
expect(checkboxNativeElement).not.toMatch(/^mat\-checkbox\-anim/g);
543544
});
544545
});
546+
547+
describe('click action checked only setting', () => {
548+
beforeEach(() => {
549+
TestBed.resetTestingModule();
550+
TestBed.configureTestingModule({
551+
imports: [MatCheckboxModule, FormsModule, ReactiveFormsModule],
552+
declarations: [
553+
SingleCheckbox,
554+
],
555+
providers: [
556+
{provide: MAT_CHECKBOX_CLICK_ACTION, useValue: 'check'},
557+
{provide: ViewportRuler, useClass: FakeViewportRuler}
558+
]
559+
});
560+
561+
fixture = TestBed.createComponent(SingleCheckbox);
562+
fixture.detectChanges();
563+
564+
checkboxDebugElement = fixture.debugElement.query(By.directive(MatCheckbox));
565+
checkboxNativeElement = checkboxDebugElement.nativeElement;
566+
checkboxInstance = checkboxDebugElement.componentInstance;
567+
testComponent = fixture.debugElement.componentInstance;
568+
569+
inputElement = <HTMLInputElement>checkboxNativeElement.querySelector('input');
570+
labelElement = <HTMLLabelElement>checkboxNativeElement.querySelector('label');
571+
});
572+
573+
it('should not set indeterminate to false on click if noop is set', fakeAsync(() => {
574+
575+
testComponent.isIndeterminate = true;
576+
inputElement.click();
577+
578+
fixture.detectChanges();
579+
flushMicrotasks();
580+
fixture.detectChanges();
581+
expect(inputElement.checked).toBe(true);
582+
expect(checkboxNativeElement.classList).toContain('mat-checkbox-checked');
583+
expect(inputElement.indeterminate).toBe(true);
584+
expect(checkboxNativeElement.classList).toContain('mat-checkbox-indeterminate');
585+
}));
586+
});
587+
588+
describe('click action noop setting', () => {
589+
beforeEach(() => {
590+
TestBed.resetTestingModule();
591+
TestBed.configureTestingModule({
592+
imports: [MatCheckboxModule, FormsModule, ReactiveFormsModule],
593+
declarations: [
594+
SingleCheckbox,
595+
],
596+
providers: [
597+
{provide: MAT_CHECKBOX_CLICK_ACTION, useValue: 'noop'},
598+
{provide: ViewportRuler, useClass: FakeViewportRuler}
599+
]
600+
});
601+
602+
fixture = TestBed.createComponent(SingleCheckbox);
603+
fixture.detectChanges();
604+
605+
checkboxDebugElement = fixture.debugElement.query(By.directive(MatCheckbox));
606+
checkboxNativeElement = checkboxDebugElement.nativeElement;
607+
checkboxInstance = checkboxDebugElement.componentInstance;
608+
testComponent = fixture.debugElement.componentInstance;
609+
610+
inputElement = <HTMLInputElement>checkboxNativeElement.querySelector('input');
611+
labelElement = <HTMLLabelElement>checkboxNativeElement.querySelector('label');
612+
});
613+
614+
it('should not set indeterminate to false on click if noop is set', fakeAsync(() => {
615+
testComponent.isIndeterminate = true;
616+
inputElement.click();
617+
618+
fixture.detectChanges();
619+
flushMicrotasks();
620+
fixture.detectChanges();
621+
622+
expect(inputElement.checked).toBe(false);
623+
expect(checkboxNativeElement.classList).not.toContain('mat-checkbox-checked');
624+
expect(inputElement.indeterminate).toBe(true);
625+
expect(checkboxNativeElement.classList).toContain('mat-checkbox-indeterminate');
626+
}));
627+
});
545628
});
546629

547630
describe('with change event and no initial value', () => {

src/lib/checkbox/checkbox.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@ import {
1717
ElementRef,
1818
EventEmitter,
1919
forwardRef,
20+
Inject,
21+
InjectionToken,
2022
Input,
2123
OnDestroy,
24+
Optional,
2225
Output,
2326
Renderer2,
2427
ViewChild,
@@ -37,6 +40,7 @@ import {
3740
mixinTabIndex,
3841
RippleRef,
3942
} from '@angular/material/core';
43+
import {MAT_CHECKBOX_CLICK_ACTION, MatCheckboxClickAction} from './checkbox-config';
4044

4145

4246
// Increasing integer for generating unique ids for checkbox components.
@@ -201,7 +205,9 @@ export class MatCheckbox extends _MatCheckboxMixinBase implements ControlValueAc
201205
elementRef: ElementRef,
202206
private _changeDetectorRef: ChangeDetectorRef,
203207
private _focusMonitor: FocusMonitor,
204-
@Attribute('tabindex') tabIndex: string) {
208+
@Attribute('tabindex') tabIndex: string,
209+
@Optional() @Inject(MAT_CHECKBOX_CLICK_ACTION)
210+
private _clickAction: MatCheckboxClickAction) {
205211
super(renderer, elementRef);
206212

207213
this.tabIndex = parseInt(tabIndex) || 0;
@@ -367,9 +373,10 @@ export class MatCheckbox extends _MatCheckboxMixinBase implements ControlValueAc
367373

368374
this._removeFocusRipple();
369375

370-
if (!this.disabled) {
376+
// If resetIndeterminate is false, and the current state is indeterminate, do nothing on click
377+
if (!this.disabled && this._clickAction !== 'noop') {
371378
// When user manually click on the checkbox, `indeterminate` is set to false.
372-
if (this._indeterminate) {
379+
if (this.indeterminate && this._clickAction !== 'check') {
373380
Promise.resolve().then(() => {
374381
this._indeterminate = false;
375382
this.indeterminateChange.emit(this._indeterminate);
@@ -378,12 +385,15 @@ export class MatCheckbox extends _MatCheckboxMixinBase implements ControlValueAc
378385

379386
this.toggle();
380387
this._transitionCheckState(
381-
this._checked ? TransitionCheckState.Checked : TransitionCheckState.Unchecked);
388+
this._checked ? TransitionCheckState.Checked : TransitionCheckState.Unchecked);
382389

383390
// Emit our custom change event if the native input emitted one.
384391
// It is important to only emit it, if the native input triggered one, because
385392
// we don't want to trigger a change event, when the `checked` variable changes for example.
386393
this._emitChangeEvent();
394+
} else if (!this.disabled && this._clickAction === 'noop') {
395+
// Reset native input when clicked with noop.
396+
this._inputElement.nativeElement.checked = this.checked;
387397
}
388398
}
389399

src/lib/checkbox/public-api.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88

99
export * from './checkbox';
10+
export * from './checkbox-config';
1011
export * from './checkbox-module';
1112
export * from './checkbox-required-validator';
1213

0 commit comments

Comments
 (0)