Skip to content
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

fix(module:tabs): prevent focus cause scroll offset #1845

Merged
merged 1 commit into from
Jul 22, 2018
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
16 changes: 14 additions & 2 deletions components/tabs/nz-tabs.spec.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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);
}));
});
});

Expand Down Expand Up @@ -484,7 +496,7 @@ describe('tabs', () => {
(nzDeselect)="deselect01()"
(nzSelect)="select01()"
(nzClick)="click01()"
[nzDisabled]="disabled">Content 2<!----></nz-tab>
[nzDisabled]="disabled">Content 2<!----><input></nz-tab>
<nz-tab
nzTitle="add"
*ngIf="add"
Expand Down
21 changes: 20 additions & 1 deletion components/tabs/nz-tabset.component.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
/** get some code from https://github.com/angular/material2 */

import { DOCUMENT } from '@angular/common';
import {
AfterContentChecked,
AfterViewInit,
Component,
ElementRef,
EventEmitter,
Inject,
Input,
OnInit,
Optional,
Output,
Renderer2,
TemplateRef,
Expand Down Expand Up @@ -42,6 +45,9 @@ export type NzTabType = 'line' | 'card';
preserveWhitespaces: false,
providers : [ NzUpdateHostClassService ],
templateUrl : './nz-tabset.component.html',
host : {
'(scroll)': 'onScroll($event)'
},
styles : [ `
:host {
display: block;
Expand Down Expand Up @@ -227,7 +233,20 @@ export class NzTabSetComponent implements AfterContentChecked, OnInit, AfterView
this.listOfNzTabComponent.splice(this.listOfNzTabComponent.indexOf(value), 1);
}

constructor(private renderer: Renderer2, private nzUpdateHostClassService: NzUpdateHostClassService, private elementRef: ElementRef) {
// From https://github.com/react-component/tabs/blob/master/src/Tabs.js
// Prevent focus to make the Tabs scroll offset
onScroll($event: Event): void {
const target: Element = $event.target as Element;
if (target.scrollLeft > 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;
}

Expand Down