Skip to content

Commit

Permalink
feat(datepicker): dispatch events when datepicker is opened and closed (
Browse files Browse the repository at this point in the history
#7792)

Adds `opened` and `closed` events, dispatched when a datepicker is opened/closed.
  • Loading branch information
crisbeto authored and josephperrott committed Nov 7, 2017
1 parent b997353 commit 998153a
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/lib/datepicker/datepicker.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ describe('MatDatepicker', () => {
MultiInputDatepicker,
NoInputDatepicker,
StandardDatepicker,
DatepickerWithEvents,
],
});

Expand Down Expand Up @@ -906,6 +907,36 @@ describe('MatDatepicker', () => {
});
}));
});

describe('with events', () => {
let fixture: ComponentFixture<DatepickerWithEvents>;
let testComponent: DatepickerWithEvents;

beforeEach(async(() => {
fixture = TestBed.createComponent(DatepickerWithEvents);
fixture.detectChanges();
testComponent = fixture.componentInstance;
}));

it('should dispatch an event when a datepicker is opened', () => {
testComponent.datepicker.open();
fixture.detectChanges();

expect(testComponent.openedSpy).toHaveBeenCalled();
});

it('should dispatch an event when a datepicker is closed', () => {
testComponent.datepicker.open();
fixture.detectChanges();

testComponent.datepicker.close();
fixture.detectChanges();

expect(testComponent.closedSpy).toHaveBeenCalled();
});

});

});

describe('with missing DateAdapter and MAT_DATE_FORMATS', () => {
Expand Down Expand Up @@ -1240,3 +1271,16 @@ class DatepickerWithISOStrings {
@ViewChild('d') datepicker: MatDatepicker<Date>;
@ViewChild(MatDatepickerInput) datepickerInput: MatDatepickerInput<Date>;
}

@Component({
template: `
<input [(ngModel)]="selected" [matDatepicker]="d">
<mat-datepicker (opened)="openedSpy()" (closed)="closedSpy()" #d></mat-datepicker>
`,
})
class DatepickerWithEvents {
selected: Date | null = null;
openedSpy = jasmine.createSpy('opened spy');
closedSpy = jasmine.createSpy('closed spy');
@ViewChild('d') datepicker: MatDatepicker<Date>;
}
8 changes: 8 additions & 0 deletions src/lib/datepicker/datepicker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,12 @@ export class MatDatepicker<D> implements OnDestroy {
/** Classes to be passed to the date picker panel. Supports the same syntax as `ngClass`. */
@Input() panelClass: string | string[];

/** Emits when the datepicker has been opened. */
@Output('opened') openedStream: EventEmitter<void> = new EventEmitter<void>();

/** Emits when the datepicker has been closed. */
@Output('closed') closedStream: EventEmitter<void> = new EventEmitter<void>();

/** Whether the calendar is open. */
opened = false;

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

this.touchUi ? this._openAsDialog() : this._openAsPopup();
this.opened = true;
this.openedStream.emit();
}

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

this.opened = false;
this.closedStream.emit();
}

/** Open the calendar as a dialog. */
Expand Down

0 comments on commit 998153a

Please sign in to comment.