Skip to content

fix(datepicker): no input focus after close #124

Merged
merged 4 commits into from
May 30, 2019
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
18 changes: 12 additions & 6 deletions packages/mosaic/datepicker/datepicker.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,18 @@ describe('McDatepicker', () => {
flush();
}));

it('should focus input after close', fakeAsync(() => {
testComponent.datepicker.open();
fixture.detectChanges();

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

flush();

expect(document.activeElement).toBe(testComponent.datepicker.datepickerInput.elementRef.nativeElement);
}));

it('open non-touch should open popup', () => {
expect(document.querySelector('.cdk-overlay-pane.mc-datepicker__popup')).toBeNull();

Expand Down Expand Up @@ -1228,12 +1240,6 @@ describe('McDatepicker', () => {
}));

it('should not reopen if the browser fires the focus event asynchronously', fakeAsync(() => {
// Stub out the real focus method so we can call it reliably.
spyOn(input, 'focus').and.callFake(() => {
// Dispatch the event handler async to simulate the IE11 behavior.
Promise.resolve().then(() => dispatchFakeEvent(input, 'focus'));
});

// Open initially by focusing.
input.focus();
fixture.detectChanges();
Expand Down
2 changes: 2 additions & 0 deletions packages/mosaic/datepicker/datepicker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,8 @@ export class McDatepicker<D> implements OnDestroy, CanColor {
this._opened = false;
this.closedStream.emit();
this.focusedElementBeforeOpen = null;

this.datepickerInput.elementRef.nativeElement.focus();
}
};

Expand Down