Skip to content

Commit 1be91fa

Browse files
committed
feat(datepicker): dispatch events when datepicker is opened and closed
Adds the `opened` and `closed` events that are dispatched when a datepicker is opened/closed. Fixes #7626.
1 parent 9673f63 commit 1be91fa

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

src/lib/datepicker/datepicker.spec.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ describe('MatDatepicker', () => {
6060
MultiInputDatepicker,
6161
NoInputDatepicker,
6262
StandardDatepicker,
63+
DatepickerWithEvents,
6364
],
6465
});
6566

@@ -910,6 +911,36 @@ describe('MatDatepicker', () => {
910911
});
911912
}));
912913
});
914+
915+
describe('with events', () => {
916+
let fixture: ComponentFixture<DatepickerWithEvents>;
917+
let testComponent: DatepickerWithEvents;
918+
919+
beforeEach(async(() => {
920+
fixture = TestBed.createComponent(DatepickerWithEvents);
921+
fixture.detectChanges();
922+
testComponent = fixture.componentInstance;
923+
}));
924+
925+
it('should dispatch an event when a datepicker is opened', () => {
926+
testComponent.datepicker.open();
927+
fixture.detectChanges();
928+
929+
expect(testComponent.openedSpy).toHaveBeenCalled();
930+
});
931+
932+
it('should dispatch an event when a datepicker is closed', () => {
933+
testComponent.datepicker.open();
934+
fixture.detectChanges();
935+
936+
testComponent.datepicker.close();
937+
fixture.detectChanges();
938+
939+
expect(testComponent.closedSpy).toHaveBeenCalled();
940+
});
941+
942+
});
943+
913944
});
914945

915946
describe('with missing DateAdapter and MAT_DATE_FORMATS', () => {
@@ -1244,3 +1275,16 @@ class DatepickerWithISOStrings {
12441275
@ViewChild('d') datepicker: MatDatepicker<Date>;
12451276
@ViewChild(MatDatepickerInput) datepickerInput: MatDatepickerInput<Date>;
12461277
}
1278+
1279+
@Component({
1280+
template: `
1281+
<input [(ngModel)]="selected" [matDatepicker]="d">
1282+
<mat-datepicker (opened)="openedSpy()" (closed)="closedSpy()" #d></mat-datepicker>
1283+
`,
1284+
})
1285+
class DatepickerWithEvents {
1286+
selected: Date | null = null;
1287+
openedSpy = jasmine.createSpy('opened spy');
1288+
closedSpy = jasmine.createSpy('closed spy');
1289+
@ViewChild('d') datepicker: MatDatepicker<Date>;
1290+
}

src/lib/datepicker/datepicker.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,12 @@ export class MatDatepicker<D> implements OnDestroy {
167167
*/
168168
@Output() selectedChanged = new EventEmitter<D>();
169169

170+
/** Emits when the datepicker has been opened. */
171+
@Output('opened') openedStream: EventEmitter<void> = new EventEmitter<void>();
172+
173+
/** Emits when the datepicker has been closed. */
174+
@Output() closed: EventEmitter<void> = new EventEmitter<void>();
175+
170176
/** Whether the calendar is open. */
171177
opened = false;
172178

@@ -271,6 +277,7 @@ export class MatDatepicker<D> implements OnDestroy {
271277

272278
this.touchUi ? this._openAsDialog() : this._openAsPopup();
273279
this.opened = true;
280+
this.openedStream.emit();
274281
}
275282

276283
/** Close the calendar. */
@@ -296,6 +303,7 @@ export class MatDatepicker<D> implements OnDestroy {
296303
}
297304

298305
this.opened = false;
306+
this.closed.emit();
299307
}
300308

301309
/** Open the calendar as a dialog. */

0 commit comments

Comments
 (0)