Skip to content

feat(datepicker): dispatch events when datepicker is opened and closed #7792

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
Nov 7, 2017
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
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 @@ -171,6 +171,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 @@ -275,6 +281,7 @@ export class MatDatepicker<D> implements OnDestroy {

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

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

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

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