From 0197d5617acf55f2c80ea536b8e3eed060c0ac5c Mon Sep 17 00:00:00 2001 From: Vladimir Potekhin <46284632+vladimirpotekhin@users.noreply.github.com> Date: Fri, 9 Dec 2022 14:43:40 +0300 Subject: [PATCH] fix(addon-table): don't update direction order when updating sorter programmatically (#3196) --- .../table/directives/table.directive.ts | 25 ++++++++++++------- .../test/direction-order.directive.spec.ts | 6 +++++ .../components/table/th/th.template.html | 2 +- 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/projects/addon-table/components/table/directives/table.directive.ts b/projects/addon-table/components/table/directives/table.directive.ts index 7139820a4db6..ac8e5c56380e 100644 --- a/projects/addon-table/components/table/directives/table.directive.ts +++ b/projects/addon-table/components/table/directives/table.directive.ts @@ -63,21 +63,28 @@ export class TuiTableDirective< @tuiDefaultProp() sorter: TuiComparator = () => 0; - updateSorter(sorter: TuiComparator | null): void { + updateSorterAndDirection(sorter: TuiComparator | 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 | 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(); + } } diff --git a/projects/addon-table/components/table/directives/test/direction-order.directive.spec.ts b/projects/addon-table/components/table/directives/test/direction-order.directive.spec.ts index 6eae02183dbb..cf5b816c87e1 100644 --- a/projects/addon-table/components/table/directives/test/direction-order.directive.spec.ts +++ b/projects/addon-table/components/table/directives/test/direction-order.directive.spec.ts @@ -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(); + }); }); }); diff --git a/projects/addon-table/components/table/th/th.template.html b/projects/addon-table/components/table/th/th.template.html index 5592294b8ada..f13ecfc6a6b6 100644 --- a/projects/addon-table/components/table/th/th.template.html +++ b/projects/addon-table/components/table/th/th.template.html @@ -3,7 +3,7 @@ type="button" class="t-sort" [class.t-sort_sorted]="isCurrent" - (click)="table.updateSorter(sorter)" + (click)="table.updateSorterAndDirection(sorter)" > {{ table.change$ | async }}