Skip to content

Commit

Permalink
fix(kit): add overload for TuiFilterByInputPipe (#8912)
Browse files Browse the repository at this point in the history
  • Loading branch information
splincode authored Sep 12, 2024
1 parent 1dd5049 commit 6883ea6
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {Component, inject} from '@angular/core';
import {FormControl, FormGroup, ReactiveFormsModule} from '@angular/forms';
import {changeDetection} from '@demo/emulate/change-detection';
import {encapsulation} from '@demo/emulate/encapsulation';
import type {TuiStringMatcher} from '@taiga-ui/cdk';
import {
TuiDataListWrapper,
TuiFilterByInputPipe,
Expand All @@ -27,7 +28,7 @@ interface User {
encapsulation,
changeDetection,
})
export default class Example {
export default class Example<T extends User = User> {
protected readonly items = inject<readonly string[]>('Pythons' as any);

protected readonly users = [
Expand All @@ -36,18 +37,18 @@ export default class Example {
{id: 3, name: 'Graham Chapman'},
{id: 4, name: 'Michael Palin'},
{id: 5, name: 'Terry Gilliam'},
];
] as unknown as readonly T[];

protected readonly form = new FormGroup({
user: new FormControl<User | null>(null),
user2: new FormControl<User | null>(null),
user: new FormControl<T | null>(null),
user2: new FormControl<T | null>(null),
});

protected readonly stringify = ({name}: User): string => name;
protected readonly stringify = ({name}: T): string => name;

protected readonly matcherString = (name: string, search: string): boolean =>
name.split(' ').pop()!.toLowerCase().startsWith(search.toLowerCase());
name.split(' ').pop()?.toLowerCase().startsWith(search.toLowerCase()) ?? false;

protected readonly matcherUser = (user: User, search: string): boolean =>
protected readonly matcherUser: TuiStringMatcher<T> = (user, search): boolean =>
user.name.toLowerCase().startsWith(search.toLowerCase());
}
9 changes: 1 addition & 8 deletions projects/kit/pipes/filter-by-input/filter-by-input.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,6 @@ import {TUI_DATA_LIST_HOST} from '@taiga-ui/core/components/data-list';
import {TuiTextfieldComponent} from '@taiga-ui/core/components/textfield';
import {tuiIsFlat} from '@taiga-ui/kit/utils';

type TuiArrayElement<A> =
A extends ReadonlyArray<infer T>
? A extends ReadonlyArray<ReadonlyArray<infer G>>
? G
: T
: never;

// TODO: Consider replacing TuiTextfieldComponent with proper token once we refactor textfields
@Pipe({
standalone: true,
Expand All @@ -25,7 +18,7 @@ export class TuiFilterByInputPipe implements PipeTransform {
private readonly textfield = inject(TuiTextfieldComponent, {optional: true});
private readonly host = inject(TUI_DATA_LIST_HOST);

public transform<T>(items: T, matcher?: TuiStringMatcher<TuiArrayElement<T>>): T;
public transform<T>(items: readonly T[], matcher?: TuiStringMatcher<T>): readonly T[];
public transform<T>(
items: ReadonlyArray<readonly T[]> | readonly T[] | null,
matcher: TuiStringMatcher<T> = TUI_DEFAULT_MATCHER,
Expand Down

0 comments on commit 6883ea6

Please sign in to comment.