From 22bc90c73d3b6c0012806369c5da3ebe0dfc3ffc Mon Sep 17 00:00:00 2001 From: Hsuan Lee Date: Thu, 19 Jul 2018 17:29:26 +0800 Subject: [PATCH] fix(module:tabs): prevent focus cause scroll offset close #1821 --- components/tabs/nz-tabs.spec.ts | 16 ++++++++++++++-- components/tabs/nz-tabset.component.ts | 21 ++++++++++++++++++++- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/components/tabs/nz-tabs.spec.ts b/components/tabs/nz-tabs.spec.ts index cda9f9c0fe..97d6d9c457 100644 --- a/components/tabs/nz-tabs.spec.ts +++ b/components/tabs/nz-tabs.spec.ts @@ -1,6 +1,7 @@ import { Component, TemplateRef, ViewChild, ViewEncapsulation } from '@angular/core'; -import { fakeAsync, tick, TestBed } from '@angular/core/testing'; +import { fakeAsync, flush, tick, TestBed } from '@angular/core/testing'; import { By } from '@angular/platform-browser'; +import { dispatchFakeEvent } from '../core/testing'; import { NzTabsModule } from './nz-tabs.module'; import { NzTabSetComponent } from './nz-tabset.component'; @@ -448,6 +449,17 @@ describe('tabs', () => { expect(testComponent.select02).toHaveBeenCalledTimes(0); expect(testComponent.deselect02).toHaveBeenCalledTimes(0); })); + it('should prevent focus scroll', fakeAsync(() => { + fixture.detectChanges(); + expect(tabs.nativeElement.scrollLeft).toBe(0); + const input: HTMLInputElement = tabs.nativeElement.querySelector('input'); + input.focus(); + expect(tabs.nativeElement.scrollLeft > 0).toBe(true); + dispatchFakeEvent(tabs.nativeElement, 'scroll'); + flush(); + fixture.detectChanges(); + expect(tabs.nativeElement.scrollLeft).toBe(0); + })); }); }); @@ -484,7 +496,7 @@ describe('tabs', () => { (nzDeselect)="deselect01()" (nzSelect)="select01()" (nzClick)="click01()" - [nzDisabled]="disabled">Content 2 + [nzDisabled]="disabled">Content 2 0) { + target.scrollLeft = 0; + if (this.document && this.document.activeElement) { + (this.document.activeElement as HTMLElement).blur(); + } + } + } + + // tslint:disable-next-line:no-any + constructor(private renderer: Renderer2, private nzUpdateHostClassService: NzUpdateHostClassService, private elementRef: ElementRef, @Optional() @Inject(DOCUMENT) private document: any) { this.el = this.elementRef.nativeElement; }