Skip to content

Commit aa60a84

Browse files
authored
docs: document button-toggle-group usage with forms (#21879)
Fixes #21774
1 parent 678c651 commit aa60a84

File tree

4 files changed

+44
-0
lines changed

4 files changed

+44
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<section>
2+
<h4>Button Toggle inside of a Template-driven form</h4>
3+
<mat-button-toggle-group [(ngModel)]="fontStyle" aria-label="Font Style">
4+
<mat-button-toggle value="bold">Bold</mat-button-toggle>
5+
<mat-button-toggle value="italic">Italic</mat-button-toggle>
6+
<mat-button-toggle value="underline">Underline</mat-button-toggle>
7+
</mat-button-toggle-group>
8+
<p>Chosen value is {{fontStyle}}</p>
9+
</section>
10+
11+
<section>
12+
<h4>Button Toggle inside of a Reactive form</h4>
13+
<mat-button-toggle-group [formControl]="fontStyleControl" aria-label="Font Style">
14+
<mat-button-toggle value="bold">Bold</mat-button-toggle>
15+
<mat-button-toggle value="italic">Italic</mat-button-toggle>
16+
<mat-button-toggle value="underline">Underline</mat-button-toggle>
17+
</mat-button-toggle-group>
18+
<p>Chosen value is {{fontStyleControl.value}}</p>
19+
</section>
20+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import {Component} from '@angular/core';
2+
import {FormControl} from '@angular/forms';
3+
4+
/**
5+
* @title Button-toggles with forms
6+
*/
7+
@Component({
8+
selector: 'button-toggle-forms-example',
9+
templateUrl: 'button-toggle-forms-example.html',
10+
})
11+
export class ButtonToggleFormsExample {
12+
fontStyleControl = new FormControl();
13+
fontStyle?: string;
14+
}

src/components-examples/material/button-toggle/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {NgModule} from '@angular/core';
2+
import {FormsModule, ReactiveFormsModule} from '@angular/forms';
23
import {MatButtonToggleModule} from '@angular/material/button-toggle';
34
import {MatIconModule} from '@angular/material/icon';
45
import {
@@ -9,25 +10,30 @@ import {
910
} from './button-toggle-exclusive/button-toggle-exclusive-example';
1011
import {ButtonToggleOverviewExample} from './button-toggle-overview/button-toggle-overview-example';
1112
import {ButtonToggleHarnessExample} from './button-toggle-harness/button-toggle-harness-example';
13+
import {ButtonToggleFormsExample} from './button-toggle-forms/button-toggle-forms-example';
1214

1315
export {
1416
ButtonToggleAppearanceExample,
1517
ButtonToggleExclusiveExample,
1618
ButtonToggleOverviewExample,
1719
ButtonToggleHarnessExample,
20+
ButtonToggleFormsExample,
1821
};
1922

2023
const EXAMPLES = [
2124
ButtonToggleAppearanceExample,
2225
ButtonToggleExclusiveExample,
2326
ButtonToggleOverviewExample,
2427
ButtonToggleHarnessExample,
28+
ButtonToggleFormsExample,
2529
];
2630

2731
@NgModule({
2832
imports: [
33+
FormsModule,
2934
MatButtonToggleModule,
3035
MatIconModule,
36+
ReactiveFormsModule,
3137
],
3238
declarations: EXAMPLES,
3339
exports: EXAMPLES,

src/material/button-toggle/button-toggle.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ be configured globally using the `MAT_BUTTON_TOGGLE_DEFAULT_OPTIONS` injection t
2222

2323
<!-- example(button-toggle-appearance) -->
2424

25+
### Use with `@angular/forms`
26+
`<mat-button-toggle-group>` is compatible with `@angular/forms` and supports both `FormsModule`
27+
and `ReactiveFormsModule`.
28+
2529
### Accessibility
2630
The button-toggles internally use native `button` elements with `aria-pressed` to convey
2731
their toggled state. The button-toggle-group surrounding the individual buttons applies

0 commit comments

Comments
 (0)