Skip to content

Commit

Permalink
fix(addon-table): don't update direction order when updating sorter p…
Browse files Browse the repository at this point in the history
…rogrammatically (#3196)
  • Loading branch information
vladimirpotekhin authored Dec 9, 2022
1 parent 2aca218 commit 0197d56
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,21 +63,28 @@ export class TuiTableDirective<
@tuiDefaultProp()
sorter: TuiComparator<T> = () => 0;

updateSorter(sorter: TuiComparator<T> | null): void {
updateSorterAndDirection(sorter: TuiComparator<T> | null): void {
if (this.sorter === sorter) {
this.direction = this.direction === 1 ? -1 : 1;
this.directionChange.emit(this.direction);
this.updateDirection(this.direction === 1 ? -1 : 1);
} else {
this.sorter = sorter || (() => 0);
this.sorterChange.emit(this.sorter);
this.direction = 1;
this.directionChange.emit(1);
this.updateSorter(sorter);
this.updateDirection(1);
}

this.change$.next();
}

ngAfterViewInit(): void {
this.changeDetectorRef.detectChanges();
}

updateSorter(sorter: TuiComparator<T> | null): void {
this.sorter = sorter || (() => 0);
this.sorterChange.emit(this.sorter);
this.change$.next();
}

private updateDirection(direction: -1 | 1): void {
this.direction = direction;
this.directionChange.emit(this.direction);
this.change$.next();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,11 @@ describe(`TuiDirectionOrder directive`, () => {

expect(testComponent.directionOrderChange).toHaveBeenCalledWith(`desc`);
});

it(`should not emit directionChange when updating sorter programmatically`, () => {
testComponent.table.updateSorter(() => -1);

expect(testComponent.directionOrderChange).not.toHaveBeenCalled();
});
});
});
2 changes: 1 addition & 1 deletion projects/addon-table/components/table/th/th.template.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
type="button"
class="t-sort"
[class.t-sort_sorted]="isCurrent"
(click)="table.updateSorter(sorter)"
(click)="table.updateSorterAndDirection(sorter)"
>
<ng-container [ngTemplateOutlet]="content"></ng-container>
{{ table.change$ | async }}
Expand Down

0 comments on commit 0197d56

Please sign in to comment.