Skip to content

Commit dcf849c

Browse files
committed
feat(addon-table): add new output event with sort and direction changes
1 parent 290f5ed commit dcf849c

File tree

6 files changed

+47
-11
lines changed

6 files changed

+47
-11
lines changed

projects/addon-table/components/table/directives/sort-by.directive.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type {QueryList} from '@angular/core';
22
import {ContentChildren, Directive, inject, Input, Output} from '@angular/core';
33
import type {TuiComparator} from '@taiga-ui/addon-table/types';
44
import {EMPTY_QUERY} from '@taiga-ui/cdk/constants';
5-
import {delay, filter, map} from 'rxjs';
5+
import {combineLatest, debounceTime, delay, filter, map} from 'rxjs';
66

77
import {TuiTableSortable} from './sortable.directive';
88
import {TuiTableDirective} from './table.directive';
@@ -17,6 +17,9 @@ export class TuiTableSortBy<T extends Partial<Record<keyof T, any>>> {
1717

1818
private readonly table = inject(TuiTableDirective<T>);
1919

20+
/**
21+
* @deprecated: use tuiSortChange
22+
*/
2023
@Output()
2124
public readonly tuiSortByChange = this.table.sorterChange.pipe(
2225
// delay is for getting actual ContentChildren (sortables) https://github.com/angular/angular/issues/38976
@@ -25,6 +28,15 @@ export class TuiTableSortBy<T extends Partial<Record<keyof T, any>>> {
2528
map((sorter) => this.getKey(sorter)),
2629
);
2730

31+
@Output()
32+
public readonly tuiSortChange = combineLatest([
33+
this.tuiSortByChange,
34+
this.table.directionChange,
35+
]).pipe(
36+
debounceTime(0),
37+
map(([sortBy, orderBy]) => ({sortBy, orderBy})),
38+
);
39+
2840
public tuiSortBy: string | keyof T | null = null;
2941

3042
@Input('tuiSortBy')

projects/addon-table/components/table/directives/sortable.directive.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ import {TuiTableDirective} from './table.directive';
1515
export class TuiTableSortable<T extends Partial<Record<keyof T, any>>>
1616
implements OnChanges
1717
{
18-
private readonly table = inject(TuiTableDirective<T>);
19-
private readonly th = inject(TuiTableTh<T>);
18+
private readonly table: TuiTableDirective<T> = inject(TuiTableDirective<T>);
19+
private readonly th: TuiTableTh<T> = inject(TuiTableTh<T>);
2020
private readonly sortBy = inject<TuiTableSortBy<T>>(forwardRef(() => TuiTableSortBy));
2121

2222
@Input({

projects/addon-table/components/table/directives/table.directive.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import type {TuiSizeL, TuiSizeS} from '@taiga-ui/core/types';
2121
import {tuiBadgeOptionsProvider} from '@taiga-ui/kit/components/badge';
2222
import {tuiChipOptionsProvider} from '@taiga-ui/kit/components/chip';
2323
import {tuiProgressOptionsProvider} from '@taiga-ui/kit/components/progress';
24-
import {Subject} from 'rxjs';
24+
import {combineLatest, debounceTime, map, Subject} from 'rxjs';
2525

2626
import {TUI_TABLE_OPTIONS, TuiSortDirection} from '../table.options';
2727
import {TuiStuck} from './stuck.directive';
@@ -71,12 +71,27 @@ export class TuiTableDirective<T extends Partial<Record<keyof T, any>>>
7171
@Input()
7272
public direction = this.options.direction;
7373

74+
/**
75+
* @deprecated: use sortChange
76+
*/
7477
@Output()
7578
public readonly directionChange = new EventEmitter<TuiSortDirection>();
7679

80+
/**
81+
* @deprecated: use sortChange
82+
*/
7783
@Output()
7884
public readonly sorterChange = new EventEmitter<TuiComparator<T> | null>();
7985

86+
@Output()
87+
public readonly sortChange = combineLatest([
88+
this.sorterChange,
89+
this.directionChange,
90+
]).pipe(
91+
debounceTime(0),
92+
map(([sortBy, orderBy]) => ({sortBy, orderBy})),
93+
);
94+
8095
public readonly appearance = signal('table');
8196
public readonly size = signal(this.options.size);
8297
public readonly cleaner = signal(false);

projects/addon-table/components/table/table.options.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ export const TuiSortDirection = {
88
} as const;
99
export type TuiSortDirection = (typeof TuiSortDirection)[keyof typeof TuiSortDirection];
1010

11+
export interface TuiSortAndOrder<T> {
12+
sortBy: keyof T | null;
13+
orderBy: -1 | 1;
14+
}
15+
1116
export interface TuiTableOptions {
1217
readonly direction: TuiSortDirection;
1318
readonly requiredSort: boolean;

projects/demo/src/modules/components/table/examples/4/index.html

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@
5858
[columns]="columns"
5959
[direction]="(direction$ | async) || 1"
6060
[tuiSortBy]="sorter$ | async"
61-
(directionChange)="direction$.next($event)"
62-
(tuiSortByChange)="sorter$.next($event!)"
61+
(tuiSortChange)="change($event)"
6362
>
6463
<thead>
6564
<tr tuiThGroup>

projects/demo/src/modules/components/table/examples/4/index.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ import {Component} from '@angular/core';
33
import {FormControl, FormsModule, ReactiveFormsModule} from '@angular/forms';
44
import {changeDetection} from '@demo/emulate/change-detection';
55
import {encapsulation} from '@demo/emulate/encapsulation';
6-
import type {TuiComparator, TuiTablePaginationEvent} from '@taiga-ui/addon-table';
6+
import type {
7+
TuiComparator,
8+
TuiSortAndOrder,
9+
TuiTablePaginationEvent,
10+
} from '@taiga-ui/addon-table';
711
import {TuiReorder, TuiTable, TuiTablePagination} from '@taiga-ui/addon-table';
812
import {
913
TUI_DEFAULT_MATCHER,
@@ -188,10 +192,6 @@ export default class Example {
188192
.map((column) => KEYS[column] ?? '');
189193
}
190194

191-
protected onDirection(direction: -1 | 1): void {
192-
this.direction$.next(direction);
193-
}
194-
195195
protected onPagination({page, size}: TuiTablePaginationEvent): void {
196196
this.page$.next(page);
197197
this.size$.next(size);
@@ -201,6 +201,11 @@ export default class Example {
201201
return !!this.search && TUI_DEFAULT_MATCHER(value, this.search);
202202
}
203203

204+
protected change<T>({sortBy, orderBy}: TuiSortAndOrder<T>): void {
205+
this.sorter$.next(sortBy as Key);
206+
this.direction$.next(orderBy);
207+
}
208+
204209
private getData(
205210
key: 'age' | 'dob' | 'name',
206211
direction: -1 | 1,

0 commit comments

Comments
 (0)