Skip to content

Commit e679094

Browse files
committed
docs(material/checkbox): Update checkbox docs & examples
1 parent 3b32d0e commit e679094

File tree

8 files changed

+53
-36
lines changed

8 files changed

+53
-36
lines changed

src/components-examples/material/checkbox/checkbox-configurable/checkbox-configurable-example.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import {Component} from '@angular/core';
2-
import {MatRadioModule} from '@angular/material/radio';
1+
import {ChangeDetectionStrategy, Component} from '@angular/core';
32
import {FormsModule} from '@angular/forms';
4-
import {MatCheckboxModule} from '@angular/material/checkbox';
53
import {MatCardModule} from '@angular/material/card';
4+
import {MatCheckboxModule} from '@angular/material/checkbox';
5+
import {MatRadioModule} from '@angular/material/radio';
66

77
/**
88
* @title Configurable checkbox
@@ -13,6 +13,7 @@ import {MatCardModule} from '@angular/material/card';
1313
styleUrl: 'checkbox-configurable-example.css',
1414
standalone: true,
1515
imports: [MatCardModule, MatCheckboxModule, FormsModule, MatRadioModule],
16+
changeDetection: ChangeDetectionStrategy.OnPush,
1617
})
1718
export class CheckboxConfigurableExample {
1819
checked = false;

src/components-examples/material/checkbox/checkbox-harness/checkbox-harness-example.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {Component} from '@angular/core';
1+
import {ChangeDetectionStrategy, Component} from '@angular/core';
22
import {MatCheckboxModule} from '@angular/material/checkbox';
33

44
/**
@@ -9,6 +9,7 @@ import {MatCheckboxModule} from '@angular/material/checkbox';
99
templateUrl: 'checkbox-harness-example.html',
1010
standalone: true,
1111
imports: [MatCheckboxModule],
12+
changeDetection: ChangeDetectionStrategy.OnPush,
1213
})
1314
export class CheckboxHarnessExample {
1415
disabled = true;

src/components-examples/material/checkbox/checkbox-overview/checkbox-overview-example.html

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,20 @@
55

66
<section class="example-section">
77
<span class="example-list-section">
8-
<mat-checkbox class="example-margin"
9-
[checked]="allComplete"
10-
[color]="task.color"
11-
[indeterminate]="someComplete()"
12-
(change)="setAll($event.checked)">
8+
<mat-checkbox
9+
class="example-margin"
10+
[checked]="allComplete"
11+
[indeterminate]="someComplete()"
12+
(change)="setAll($event.checked)"
13+
>
1314
{{task.name}}
1415
</mat-checkbox>
1516
</span>
1617
<span class="example-list-section">
1718
<ul>
1819
@for (subtask of task.subtasks; track subtask) {
1920
<li>
20-
<mat-checkbox [(ngModel)]="subtask.completed"
21-
[color]="subtask.color"
22-
(ngModelChange)="updateAllComplete()">
21+
<mat-checkbox [(ngModel)]="subtask.completed" (ngModelChange)="updateAllComplete()">
2322
{{subtask.name}}
2423
</mat-checkbox>
2524
</li>

src/components-examples/material/checkbox/checkbox-overview/checkbox-overview-example.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
import {Component} from '@angular/core';
2-
import {ThemePalette} from '@angular/material/core';
1+
import {ChangeDetectionStrategy, Component} from '@angular/core';
32
import {FormsModule} from '@angular/forms';
43
import {MatCheckboxModule} from '@angular/material/checkbox';
54

65
export interface Task {
76
name: string;
87
completed: boolean;
9-
color: ThemePalette;
108
subtasks?: Task[];
119
}
1210

@@ -19,16 +17,16 @@ export interface Task {
1917
styleUrl: 'checkbox-overview-example.css',
2018
standalone: true,
2119
imports: [MatCheckboxModule, FormsModule],
20+
changeDetection: ChangeDetectionStrategy.OnPush,
2221
})
2322
export class CheckboxOverviewExample {
24-
task: Task = {
25-
name: 'Indeterminate',
23+
readonly task: Task = {
24+
name: 'Parent task',
2625
completed: false,
27-
color: 'primary',
2826
subtasks: [
29-
{name: 'Primary', completed: false, color: 'primary'},
30-
{name: 'Accent', completed: false, color: 'accent'},
31-
{name: 'Warn', completed: false, color: 'warn'},
27+
{name: 'Child task 1', completed: false},
28+
{name: 'Child task 2', completed: false},
29+
{name: 'Child task 3', completed: false},
3230
],
3331
};
3432

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import {Component} from '@angular/core';
2-
import {FormBuilder, FormsModule, ReactiveFormsModule} from '@angular/forms';
31
import {JsonPipe} from '@angular/common';
2+
import {ChangeDetectionStrategy, Component, inject} from '@angular/core';
3+
import {FormBuilder, FormsModule, ReactiveFormsModule} from '@angular/forms';
44
import {MatCheckboxModule} from '@angular/material/checkbox';
55

66
/** @title Checkboxes with reactive forms */
@@ -10,13 +10,14 @@ import {MatCheckboxModule} from '@angular/material/checkbox';
1010
styleUrl: 'checkbox-reactive-forms-example.css',
1111
standalone: true,
1212
imports: [FormsModule, ReactiveFormsModule, MatCheckboxModule, JsonPipe],
13+
changeDetection: ChangeDetectionStrategy.OnPush,
1314
})
1415
export class CheckboxReactiveFormsExample {
15-
toppings = this._formBuilder.group({
16+
private readonly _formBuilder = inject(FormBuilder);
17+
18+
readonly toppings = this._formBuilder.group({
1619
pepperoni: false,
1720
extracheese: false,
1821
mushroom: false,
1922
});
20-
21-
constructor(private _formBuilder: FormBuilder) {}
2223
}

src/material/checkbox/checkbox-config.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ import {ThemePalette} from '@angular/material/core';
1010

1111
/** Default `mat-checkbox` options that can be overridden. */
1212
export interface MatCheckboxDefaultOptions {
13-
/** Default theme color palette to be used for checkboxes. */
13+
/**
14+
* Default theme color palette to be used for checkboxes. This API is supported in M2 themes
15+
* only, it has no effect in M3 themes. For information on applying color variants in M3, see
16+
* https://material.angular.io/guide/theming#using-component-color-variants
17+
*/
1418
color?: ThemePalette;
1519
/** Default checkbox click action for checkboxes. */
1620
clickAction?: MatCheckboxClickAction;

src/material/checkbox/checkbox.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ enhanced with Material Design styling and animations.
44
<!-- example(checkbox-overview) -->
55

66
### Checkbox label
7+
78
The checkbox label is provided as the content to the `<mat-checkbox>` element. The label can be
89
positioned before or after the checkbox by setting the `labelPosition` property to `'before'` or
910
`'after'`.
@@ -14,16 +15,19 @@ If you don't want the label to appear next to the checkbox, you can use
1415
specify an appropriate label.
1516

1617
### Use with `@angular/forms`
18+
1719
`<mat-checkbox>` is compatible with `@angular/forms` and supports both `FormsModule`
1820
and `ReactiveFormsModule`.
1921

2022
### Indeterminate state
23+
2124
`<mat-checkbox>` supports an `indeterminate` state, similar to the native `<input type="checkbox">`.
2225
While the `indeterminate` property of the checkbox is true, it will render as indeterminate
2326
regardless of the `checked` value. Any interaction with the checkbox by a user (i.e., clicking) will
2427
remove the indeterminate state.
2528

2629
### Click action config
30+
2731
When user clicks on the `mat-checkbox`, the default behavior is toggle `checked` value and set
2832
`indeterminate` to `false`. This behavior can be customized by
2933
[providing a new value](https://angular.io/guide/dependency-injection)
@@ -38,22 +42,27 @@ providers: [
3842
The possible values are:
3943

4044
#### `noop`
45+
4146
Do not change the `checked` value or `indeterminate` value. Developers have the power to
4247
implement customized click actions.
4348

4449
#### `check`
50+
4551
Toggle `checked` value of the checkbox, ignore `indeterminate` value. If the
4652
checkbox is in `indeterminate` state, the checkbox will display as an `indeterminate` checkbox
4753
regardless the `checked` value.
4854

4955
#### `check-indeterminate`
56+
5057
Default behavior of `mat-checkbox`. Always set `indeterminate` to `false`
5158
when user click on the `mat-checkbox`.
5259
This matches the behavior of native `<input type="checkbox">`.
5360

5461
### Theming
55-
The color of a `<mat-checkbox>` can be changed by using the `color` property. By default, checkboxes
56-
use the theme's accent color. This can be changed to `'primary'` or `'warn'`.
62+
63+
The color of a `<mat-checkbox>` can be changed by specifying a `$color-variant` when applying the
64+
`mat.checkbox-theme` or `mat.checkbox-color` mixins. By default, checkboxes use the theme's primary
65+
palette. This can be changed to `'secondary'`, `'tertiary'`, or `'error'`.
5766

5867
### Accessibility
5968

src/material/checkbox/checkbox.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,28 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9+
import {FocusableOption} from '@angular/cdk/a11y';
910
import {
11+
ANIMATION_MODULE_TYPE,
1012
AfterViewInit,
1113
Attribute,
12-
booleanAttribute,
1314
ChangeDetectionStrategy,
1415
ChangeDetectorRef,
1516
Component,
1617
ElementRef,
1718
EventEmitter,
18-
forwardRef,
1919
Inject,
2020
Input,
2121
NgZone,
22-
numberAttribute,
2322
OnChanges,
2423
Optional,
2524
Output,
2625
SimpleChanges,
2726
ViewChild,
2827
ViewEncapsulation,
29-
ANIMATION_MODULE_TYPE,
28+
booleanAttribute,
29+
forwardRef,
30+
numberAttribute,
3031
} from '@angular/core';
3132
import {
3233
AbstractControl,
@@ -36,8 +37,7 @@ import {
3637
ValidationErrors,
3738
Validator,
3839
} from '@angular/forms';
39-
import {_MatInternalFormField, MatRipple} from '@angular/material/core';
40-
import {FocusableOption} from '@angular/cdk/a11y';
40+
import {MatRipple, _MatInternalFormField} from '@angular/material/core';
4141
import {
4242
MAT_CHECKBOX_DEFAULT_OPTIONS,
4343
MAT_CHECKBOX_DEFAULT_OPTIONS_FACTORY,
@@ -202,7 +202,11 @@ export class MatCheckbox
202202

203203
// TODO(crisbeto): this should be a ThemePalette, but some internal apps were abusing
204204
// the lack of type checking previously and assigning random strings.
205-
/** Palette color of the checkbox. */
205+
/**
206+
* Palette color of the checkbox. This API is supported in M2 themes only, it has no effect in M3
207+
* themes. For information on applying color variants in M3, see
208+
* https://material.angular.io/guide/theming#using-component-color-variants
209+
*/
206210
@Input() color: string | undefined;
207211

208212
/**

0 commit comments

Comments
 (0)