Skip to content

fix(material/datepicker): display datepicker inside a dialog example #29404

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/components-examples/material/datepicker/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ ng_module(
"//src/material/core",
"//src/material/datepicker",
"//src/material/datepicker/testing",
"//src/material/dialog",
"//src/material/icon",
"//src/material/input",
"@npm//@angular/forms",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<h2 mat-dialog-title>Datepicker in a Dialog</h2>
<mat-dialog-content align="center">
<mat-form-field>
<mat-label>Select a date</mat-label>
<input matInput [matDatepicker]="picker" [formControl]="date">
<mat-datepicker-toggle matIconSuffix [for]="picker"></mat-datepicker-toggle>
<mat-datepicker #picker></mat-datepicker>
</mat-form-field>
</mat-dialog-content>
<mat-dialog-actions>
<button mat-button mat-dialog-close>Clear</button>
<button mat-button [mat-dialog-close]="date.value" cdkFocusInitial>Ok</button>
</mat-dialog-actions>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<p>Selected date: {{selectedDate()}}</p>
<button mat-flat-button color="primary" (click)="openDialog()">Open Dialog</button>
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import {ChangeDetectionStrategy, Component, Inject, model} from '@angular/core';
import {FormControl, ReactiveFormsModule} from '@angular/forms';
import {MatButtonModule} from '@angular/material/button';
import {
MAT_DATE_FORMATS,
MAT_NATIVE_DATE_FORMATS,
provideNativeDateAdapter,
} from '@angular/material/core';
import {MatDatepickerModule} from '@angular/material/datepicker';
import {MAT_DIALOG_DATA, MatDialog, MatDialogModule, MatDialogRef} from '@angular/material/dialog';
import {MatFormFieldModule} from '@angular/material/form-field';
import {MatInputModule} from '@angular/material/input';

export interface DialogData {
selectedDate: Date;
}

/** @title Datepicker inside a MatDialog */
@Component({
selector: 'datepicker-dialog-example',
templateUrl: 'datepicker-dialog-example.html',
standalone: true,
imports: [MatButtonModule],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class DatepickerDialogExample {
selectedDate = model<Date | null>(null);

constructor(public dialog: MatDialog) {}

openDialog() {
const dialogRef = this.dialog.open(DatepickerDialogExampleDialog, {
minWidth: '500px',
data: {selectedDate: this.selectedDate()},
});

dialogRef.afterClosed().subscribe(result => {
this.selectedDate.set(result);
});
}
}

@Component({
selector: 'datepicker-dialog-example',
templateUrl: 'datepicker-dialog-example-dialog.html',
standalone: true,
imports: [
MatDatepickerModule,
MatDialogModule,
MatButtonModule,
MatFormFieldModule,
MatInputModule,
ReactiveFormsModule,
],
providers: [
provideNativeDateAdapter(),
{provide: MAT_DATE_FORMATS, useValue: MAT_NATIVE_DATE_FORMATS},
],
})
export class DatepickerDialogExampleDialog {
readonly date = new FormControl(new Date());

constructor(
public dialogRef: MatDialogRef<DatepickerDialogExampleDialog>,
@Inject(MAT_DIALOG_DATA) public data: DialogData,
) {
this.date.setValue(data.selectedDate);
}
}
1 change: 1 addition & 0 deletions src/components-examples/material/datepicker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ export {DatepickerStartViewExample} from './datepicker-start-view/datepicker-sta
export {DatepickerTouchExample} from './datepicker-touch/datepicker-touch-example';
export {DatepickerValueExample} from './datepicker-value/datepicker-value-example';
export {DatepickerViewsSelectionExample} from './datepicker-views-selection/datepicker-views-selection-example';
export {DatepickerDialogExample} from './datepicker-dialog/datepicker-dialog-example';
Loading