Skip to content

Commit 998153a

Browse files
crisbetojosephperrott
authored andcommitted
feat(datepicker): dispatch events when datepicker is opened and closed (#7792)
Adds `opened` and `closed` events, dispatched when a datepicker is opened/closed.
1 parent b997353 commit 998153a

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

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

911942
describe('with missing DateAdapter and MAT_DATE_FORMATS', () => {
@@ -1240,3 +1271,16 @@ class DatepickerWithISOStrings {
12401271
@ViewChild('d') datepicker: MatDatepicker<Date>;
12411272
@ViewChild(MatDatepickerInput) datepickerInput: MatDatepickerInput<Date>;
12421273
}
1274+
1275+
@Component({
1276+
template: `
1277+
<input [(ngModel)]="selected" [matDatepicker]="d">
1278+
<mat-datepicker (opened)="openedSpy()" (closed)="closedSpy()" #d></mat-datepicker>
1279+
`,
1280+
})
1281+
class DatepickerWithEvents {
1282+
selected: Date | null = null;
1283+
openedSpy = jasmine.createSpy('opened spy');
1284+
closedSpy = jasmine.createSpy('closed spy');
1285+
@ViewChild('d') datepicker: MatDatepicker<Date>;
1286+
}

src/lib/datepicker/datepicker.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,12 @@ export class MatDatepicker<D> implements OnDestroy {
172172
/** Classes to be passed to the date picker panel. Supports the same syntax as `ngClass`. */
173173
@Input() panelClass: string | string[];
174174

175+
/** Emits when the datepicker has been opened. */
176+
@Output('opened') openedStream: EventEmitter<void> = new EventEmitter<void>();
177+
178+
/** Emits when the datepicker has been closed. */
179+
@Output('closed') closedStream: EventEmitter<void> = new EventEmitter<void>();
180+
175181
/** Whether the calendar is open. */
176182
opened = false;
177183

@@ -276,6 +282,7 @@ export class MatDatepicker<D> implements OnDestroy {
276282

277283
this.touchUi ? this._openAsDialog() : this._openAsPopup();
278284
this.opened = true;
285+
this.openedStream.emit();
279286
}
280287

281288
/** Close the calendar. */
@@ -301,6 +308,7 @@ export class MatDatepicker<D> implements OnDestroy {
301308
}
302309

303310
this.opened = false;
311+
this.closedStream.emit();
304312
}
305313

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

0 commit comments

Comments
 (0)