Skip to content

Commit e57c6aa

Browse files
authored
docs(material/datepicker): display datepicker inside a dialog example (angular#29404)
Adds an example that demonstrates how to display a datepicker inside a matDialog component.
1 parent d3a95ab commit e57c6aa

File tree

5 files changed

+86
-0
lines changed

5 files changed

+86
-0
lines changed

src/components-examples/material/datepicker/BUILD.bazel

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ ng_module(
2121
"//src/material/core",
2222
"//src/material/datepicker",
2323
"//src/material/datepicker/testing",
24+
"//src/material/dialog",
2425
"//src/material/icon",
2526
"//src/material/input",
2627
"@npm//@angular/forms",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<h2 mat-dialog-title>Datepicker in a Dialog</h2>
2+
<mat-dialog-content align="center">
3+
<mat-form-field>
4+
<mat-label>Select a date</mat-label>
5+
<input matInput [matDatepicker]="picker" [formControl]="date">
6+
<mat-datepicker-toggle matIconSuffix [for]="picker"></mat-datepicker-toggle>
7+
<mat-datepicker #picker></mat-datepicker>
8+
</mat-form-field>
9+
</mat-dialog-content>
10+
<mat-dialog-actions>
11+
<button mat-button mat-dialog-close>Clear</button>
12+
<button mat-button [mat-dialog-close]="date.value" cdkFocusInitial>Ok</button>
13+
</mat-dialog-actions>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<p>Selected date: {{selectedDate()}}</p>
2+
<button mat-flat-button color="primary" (click)="openDialog()">Open Dialog</button>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import {ChangeDetectionStrategy, Component, Inject, model} from '@angular/core';
2+
import {FormControl, ReactiveFormsModule} from '@angular/forms';
3+
import {MatButtonModule} from '@angular/material/button';
4+
import {
5+
MAT_DATE_FORMATS,
6+
MAT_NATIVE_DATE_FORMATS,
7+
provideNativeDateAdapter,
8+
} from '@angular/material/core';
9+
import {MatDatepickerModule} from '@angular/material/datepicker';
10+
import {MAT_DIALOG_DATA, MatDialog, MatDialogModule, MatDialogRef} from '@angular/material/dialog';
11+
import {MatFormFieldModule} from '@angular/material/form-field';
12+
import {MatInputModule} from '@angular/material/input';
13+
14+
export interface DialogData {
15+
selectedDate: Date;
16+
}
17+
18+
/** @title Datepicker inside a MatDialog */
19+
@Component({
20+
selector: 'datepicker-dialog-example',
21+
templateUrl: 'datepicker-dialog-example.html',
22+
standalone: true,
23+
imports: [MatButtonModule],
24+
changeDetection: ChangeDetectionStrategy.OnPush,
25+
})
26+
export class DatepickerDialogExample {
27+
selectedDate = model<Date | null>(null);
28+
29+
constructor(public dialog: MatDialog) {}
30+
31+
openDialog() {
32+
const dialogRef = this.dialog.open(DatepickerDialogExampleDialog, {
33+
minWidth: '500px',
34+
data: {selectedDate: this.selectedDate()},
35+
});
36+
37+
dialogRef.afterClosed().subscribe(result => {
38+
this.selectedDate.set(result);
39+
});
40+
}
41+
}
42+
43+
@Component({
44+
selector: 'datepicker-dialog-example',
45+
templateUrl: 'datepicker-dialog-example-dialog.html',
46+
standalone: true,
47+
imports: [
48+
MatDatepickerModule,
49+
MatDialogModule,
50+
MatButtonModule,
51+
MatFormFieldModule,
52+
MatInputModule,
53+
ReactiveFormsModule,
54+
],
55+
providers: [
56+
provideNativeDateAdapter(),
57+
{provide: MAT_DATE_FORMATS, useValue: MAT_NATIVE_DATE_FORMATS},
58+
],
59+
})
60+
export class DatepickerDialogExampleDialog {
61+
readonly date = new FormControl(new Date());
62+
63+
constructor(
64+
public dialogRef: MatDialogRef<DatepickerDialogExampleDialog>,
65+
@Inject(MAT_DIALOG_DATA) public data: DialogData,
66+
) {
67+
this.date.setValue(data.selectedDate);
68+
}
69+
}

src/components-examples/material/datepicker/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@ export {DatepickerStartViewExample} from './datepicker-start-view/datepicker-sta
2525
export {DatepickerTouchExample} from './datepicker-touch/datepicker-touch-example';
2626
export {DatepickerValueExample} from './datepicker-value/datepicker-value-example';
2727
export {DatepickerViewsSelectionExample} from './datepicker-views-selection/datepicker-views-selection-example';
28+
export {DatepickerDialogExample} from './datepicker-dialog/datepicker-dialog-example';

0 commit comments

Comments
 (0)