Skip to content

Commit 811a3f0

Browse files
committed
fix(material/datepicker): display datepicker inside a dialog example
Adds an example that demonstrates how to display a datepicker inside a matDialog component. Fixes #28186
1 parent 6659d8f commit 811a3f0

File tree

6 files changed

+78
-0
lines changed

6 files changed

+78
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
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",
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<h2 mat-dialog-title>Calendar in a Dialog</h2>
2+
<mat-dialog-content align="center">
3+
<div class="example-calendar-container">
4+
<mat-calendar [(selected)]="data.selectedDate"></mat-calendar>
5+
</div>
6+
</mat-dialog-content>
7+
<mat-dialog-actions>
8+
<button mat-button (click)="onClear()">Clear</button>
9+
<button mat-button [mat-dialog-close]="data.selectedDate" cdkFocusInitial>Ok</button>
10+
</mat-dialog-actions>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.example-calendar-container {
2+
width: 300px;
3+
height: 350px;
4+
}
Lines changed: 2 additions & 0 deletions
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>
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import {ChangeDetectionStrategy, Component, Inject, model} from '@angular/core';
2+
import {MatButtonModule} from '@angular/material/button';
3+
import {
4+
MAT_DATE_FORMATS,
5+
MAT_NATIVE_DATE_FORMATS,
6+
provideNativeDateAdapter,
7+
} from '@angular/material/core';
8+
import {MatDatepickerModule} from '@angular/material/datepicker';
9+
import {MAT_DIALOG_DATA, MatDialog, MatDialogModule, MatDialogRef} from '@angular/material/dialog';
10+
11+
export interface DialogData {
12+
selectedDate: Date;
13+
}
14+
15+
/** @title Datepicker Dialog*/
16+
@Component({
17+
selector: 'datepicker-dialog-example',
18+
templateUrl: 'datepicker-dialog-example.html',
19+
standalone: true,
20+
imports: [MatButtonModule],
21+
changeDetection: ChangeDetectionStrategy.OnPush,
22+
})
23+
export class DatepickerDialogExample {
24+
selectedDate = model<Date | null>(null);
25+
26+
constructor(private dialog: MatDialog) {}
27+
28+
openDialog() {
29+
const dialogRef = this.dialog.open(DatepickerDialogExampleDialog, {
30+
minWidth: 360,
31+
data: {selectedDate: this.selectedDate()},
32+
});
33+
34+
dialogRef.afterClosed().subscribe(result => {
35+
this.selectedDate.set(result);
36+
});
37+
}
38+
}
39+
40+
@Component({
41+
selector: 'datepicker-dialog-example',
42+
standalone: true,
43+
imports: [MatDatepickerModule, MatDialogModule, MatButtonModule],
44+
providers: [
45+
provideNativeDateAdapter(),
46+
{provide: MAT_DATE_FORMATS, useValue: MAT_NATIVE_DATE_FORMATS},
47+
],
48+
templateUrl: 'datepicker-dialog-example-dialog.html',
49+
styleUrl: 'datepicker-dialog-example.css',
50+
})
51+
export class DatepickerDialogExampleDialog {
52+
constructor(
53+
public dialogRef: MatDialogRef<DatepickerDialogExampleDialog>,
54+
@Inject(MAT_DIALOG_DATA) public data: DialogData,
55+
) {}
56+
57+
onClear(): void {
58+
this.dialogRef.close();
59+
}
60+
}

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

Lines changed: 1 addition & 0 deletions
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)