Skip to content

Commit

Permalink
refactor: unsubscribe from RouterLinkActive when stepper is destroyed (
Browse files Browse the repository at this point in the history
  • Loading branch information
splincode authored Sep 11, 2024
1 parent 09db4f2 commit c19426b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 15 deletions.
15 changes: 6 additions & 9 deletions projects/kit/components/stepper/step.component.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {NgIf} from '@angular/common';
import {ChangeDetectionStrategy, Component, inject, Input} from '@angular/core';
import {takeUntilDestroyed} from '@angular/core/rxjs-interop';
import {RouterLinkActive} from '@angular/router';
import {tuiInjectElement} from '@taiga-ui/cdk/utils/dom';
import {TuiIcon} from '@taiga-ui/core/components/icon';
import {TUI_COMMON_ICONS} from '@taiga-ui/core/tokens';
import type {Observable} from 'rxjs';
import {EMPTY, filter} from 'rxjs';

import {TuiStepperComponent} from './stepper.component';
Expand All @@ -29,8 +29,11 @@ import {TuiStepperComponent} from './stepper.component';
export class TuiStep {
private readonly stepper = inject(TuiStepperComponent);
private readonly el = tuiInjectElement();
private readonly routerLinkActive$: Observable<boolean> =
inject(RouterLinkActive, {optional: true})?.isActiveChange || EMPTY;
protected readonly $ = (
inject(RouterLinkActive, {optional: true})?.isActiveChange.asObservable() ?? EMPTY
)
.pipe(filter(Boolean), takeUntilDestroyed())
.subscribe(() => this.activate());

protected focusVisible = false;

Expand All @@ -42,12 +45,6 @@ export class TuiStep {
@Input()
public icon = '';

constructor() {
this.routerLinkActive$.pipe(filter(Boolean)).subscribe(() => {
this.activate();
});
}

protected get isActive(): boolean {
return this.stepper.isActive(this.index);
}
Expand Down
10 changes: 4 additions & 6 deletions projects/kit/components/stepper/stepper.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,16 @@ export class TuiStepperComponent {

protected activeItemIndex = 0;

protected readonly $ = inject(ResizeObserverService, {self: true})
.pipe(takeUntilDestroyed())
.subscribe(() => this.scrollIntoView(this.activeItemIndex));

@Input()
public orientation: TuiOrientation = 'horizontal';

@Output()
public readonly activeItemIndexChange = new EventEmitter<number>();

constructor() {
inject(ResizeObserverService, {self: true})
.pipe(takeUntilDestroyed())
.subscribe(() => this.scrollIntoView(this.activeItemIndex));
}

@Input('activeItemIndex')
public set activeIndex(index: number) {
this.activeItemIndex = index;
Expand Down

0 comments on commit c19426b

Please sign in to comment.