Skip to content

Commit

Permalink
chore: comments
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimirpotekhin committed Nov 29, 2023
1 parent 587f557 commit f621ced
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {
ChangeDetectorRef,
ContentChildren,
Directive,
Inject,
Expand All @@ -9,7 +8,7 @@ import {
} from '@angular/core';
import {TuiComparator} from '@taiga-ui/addon-table/types';
import {EMPTY_QUERY} from '@taiga-ui/cdk';
import {filter, map, tap} from 'rxjs/operators';
import {delay, filter, map} from 'rxjs/operators';

import {TuiSortableDirective} from './sortable.directive';
import {TuiTableDirective} from './table.directive';
Expand All @@ -21,22 +20,30 @@ export class TuiSortByDirective<T extends Partial<Record<keyof T, any>>> {
@ContentChildren(TuiSortableDirective, {descendants: true})
private readonly sortables: QueryList<TuiSortableDirective<T>> = EMPTY_QUERY;

@Input()
tuiSortBy: string | keyof T | null = null;
@Input('tuiSortBy')
set sortBy(sortBy: string | keyof T | null) {
this.tuiSortBy = sortBy;
this.checkSortables();
}

@Output()
readonly tuiSortByChange = this.table.sorterChange.pipe(
// recalculate ContentChildren (sortables) https://github.com/angular/angular/issues/38976
tap(() => this.cdr.detectChanges()),
// delay is for getting actual ContentChildren (sortables) https://github.com/angular/angular/issues/38976
delay(0),
filter(() => !!this.sortables.length),
map(sorter => this.getKey(sorter)),
);

tuiSortBy: string | keyof T | null = null;

constructor(
@Inject(ChangeDetectorRef) private readonly cdr: ChangeDetectorRef,
@Inject(TuiTableDirective) private readonly table: TuiTableDirective<T>,
) {}

checkSortables(): void {
this.sortables.forEach(s => s.check());
}

private getKey(sorter: TuiComparator<T> | null): keyof T | null {
return this.sortables.find(s => s.sorter === sorter)?.key || null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Directive, DoCheck, forwardRef, Inject, OnInit} from '@angular/core';
import {Directive, forwardRef, Inject, OnInit} from '@angular/core';
import {TuiComparator} from '@taiga-ui/addon-table/types';

import {TuiThComponent} from '../th/th.component';
Expand All @@ -9,7 +9,7 @@ import {TuiTableDirective} from './table.directive';
selector: 'th[tuiTh][tuiSortable]',
})
export class TuiSortableDirective<T extends Partial<Record<keyof T, any>>>
implements DoCheck, OnInit
implements OnInit
{
constructor(
@Inject(forwardRef(() => TuiSortByDirective))
Expand All @@ -29,7 +29,7 @@ export class TuiSortableDirective<T extends Partial<Record<keyof T, any>>>
this.th.sorter = this.sorter;
}

ngDoCheck(): void {
check(): void {
if (this.match && this.table.sorter !== this.sorter) {
this.table.updateSorter(this.sorter);
}
Expand Down
8 changes: 0 additions & 8 deletions projects/addon-table/components/table/th/th.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {tuiDefaultSort, TuiTableSortKeyException} from '@taiga-ui/cdk';
import {TUI_ELEMENT_REF} from '@taiga-ui/core';

import {TuiHeadDirective} from '../directives/head.directive';
import {TuiSortByDirective} from '../directives/sort-by.directive';
import {TuiTableDirective} from '../directives/table.directive';
import {TUI_TABLE_OPTIONS, TuiTableOptions} from '../table.options';

Expand Down Expand Up @@ -53,9 +52,6 @@ export class TuiThComponent<T extends Partial<Record<keyof T, any>>> {
@Optional()
@Inject(forwardRef(() => TuiTableDirective))
readonly table: TuiTableDirective<T> | null,
@Optional()
@Inject(forwardRef(() => TuiSortByDirective))
private readonly sortBy: TuiSortByDirective<T> | null,
) {}

get key(): keyof T {
Expand All @@ -81,10 +77,6 @@ export class TuiThComponent<T extends Partial<Record<keyof T, any>>> {
}

updateSorterAndDirection(): void {
if (this.sortBy) {
this.sortBy.tuiSortBy = this.key;
}

this.table?.updateSorterAndDirection(
this.isCurrentAndAscDirection ? null : this.sorter,
);
Expand Down

0 comments on commit f621ced

Please sign in to comment.