Skip to content

Commit

Permalink
feat(kit): Tabs update scroll position on active index change (#268)
Browse files Browse the repository at this point in the history
  • Loading branch information
makkaronis4e committed Mar 8, 2021
1 parent 6f89b82 commit a753a5e
Showing 1 changed file with 22 additions and 12 deletions.
34 changes: 22 additions & 12 deletions projects/kit/components/tabs/tabs/tabs.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,11 @@ export class TuiTabsComponent implements AfterViewChecked {
@tuiDefaultProp()
underline = true;

@Input()
@tuiDefaultProp()
activeItemIndex = 0;
@Input('activeItemIndex')
set activeItemIndexSetter(index: number) {
this.activeItemIndex = index;
this.updateScrollPosition(this.tabs[index]);
}

@Output()
readonly activeItemIndexChange = new EventEmitter<number>();
Expand All @@ -66,6 +68,8 @@ export class TuiTabsComponent implements AfterViewChecked {
@ContentChildren(forwardRef(() => TuiTabComponent))
private readonly children: QueryList<unknown> = EMPTY_QUERY;

activeItemIndex = 0;

constructor(
@Inject(ElementRef) private readonly elementRef: ElementRef<HTMLElement>,
@Inject(Renderer2) private readonly renderer: Renderer2,
Expand Down Expand Up @@ -122,8 +126,22 @@ export class TuiTabsComponent implements AfterViewChecked {
return;
}

this.activeItemIndex = index;
this.activeItemIndexSetter = index;
this.activeItemIndexChange.emit(index);
}

@HostListener('keydown.arrowRight.prevent', ['$event.target', '1'])
@HostListener('keydown.arrowLeft.prevent', ['$event.target', '-1'])
onKeyDownArrow(current: HTMLElement, step: number) {
const {tabs} = this;

moveFocus(tabs.indexOf(current), tabs, step);
}

updateScrollPosition(element?: HTMLElement) {
if (!element) {
return;
}

const {offsetLeft, offsetWidth} = element;
const {nativeElement} = this.elementRef;
Expand All @@ -140,12 +158,4 @@ export class TuiTabsComponent implements AfterViewChecked {
offsetLeft + offsetWidth - nativeElement.offsetWidth;
}
}

@HostListener('keydown.arrowRight.prevent', ['$event.target', '1'])
@HostListener('keydown.arrowLeft.prevent', ['$event.target', '-1'])
onKeyDownArrow(current: HTMLElement, step: number) {
const {tabs} = this;

moveFocus(tabs.indexOf(current), tabs, step);
}
}

0 comments on commit a753a5e

Please sign in to comment.