Skip to content

Commit 3c6ad58

Browse files
Merge pull request #8 from RandomSoftwareSL/bugfix/list-does-not-filter-when-typing
fix: 🐛 List does not filter when typing
2 parents f4eddd4 + ae3f3c8 commit 3c6ad58

9 files changed

+75
-14
lines changed

README.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ export class YourModule {}
5959

6060
example : -
6161

62+
```html
6263
<form [formGroup]="consumerForm">
6364
<typeHeadInput
6465
labelText="Year"
@@ -70,12 +71,15 @@ example : -
7071
[placeholder]="'Year'"
7172
[name]="'value'"
7273
[isNumberInput]="true"
73-
[serverSideSearchCallback]="yearCallBackFunction"
7474
[enableServerSideData]="true"
75+
[serverSideSearchCallback]="yearCallBackFunction"
7576
[customFieldText]="'Year'"
76-
(newValue)="changeSelectedText($event)"
77+
requiredErrorMessage="Year filed cannot be empty"
78+
[autoDisplayFirst]="true"
79+
placeholder="Year"
7780
></typeHeadInput>
7881
</form>
82+
```
7983

8084
# Development
8185

projects/consumer/src/app/app.component.html

+3-2
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@
99
[placeholder]="'Year'"
1010
[name]="'value'"
1111
[isNumberInput]="true"
12-
[serverSideSearchCallback]="yearCallBackFunction"
1312
[enableServerSideData]="true"
14-
(newValue)="changeSelectedText($event)"
13+
[serverSideSearchCallback]="yearCallBackFunction"
1514
[customFieldText]="'Year'"
1615
requiredErrorMessage="Year filed cannot be empty"
16+
[autoDisplayFirst]="true"
17+
placeholder="Year"
1718
></typeHeadInput>
1819
</form>

projects/type-head-input/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@randomsoftwareltd/type-head-input",
3-
"version": "2.0.6",
3+
"version": "2.0.7",
44
"peerDependencies": {
55
"@angular/common": "^17.2.3",
66
"@angular/core": "^17.2.3",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/* tslint:disable:no-unused-variable */
2+
3+
import { TestBed, async } from '@angular/core/testing';
4+
import { ValueFilterPipe } from './value-filter.pipe';
5+
6+
describe('Pipe: ValueFiltere', () => {
7+
it('create an instance', () => {
8+
let pipe = new ValueFilterPipe();
9+
expect(pipe).toBeTruthy();
10+
});
11+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { Pipe, PipeTransform } from "@angular/core";
2+
3+
@Pipe({
4+
name: "valueFilter",
5+
})
6+
export class ValueFilterPipe implements PipeTransform {
7+
transform(
8+
items: any[] | null,
9+
filterValue: string,
10+
fieldName: string
11+
): any[] {
12+
if (!items) {
13+
return [];
14+
}
15+
16+
if (!filterValue) {
17+
return items;
18+
}
19+
20+
if (typeof filterValue !== "string") {
21+
return items;
22+
}
23+
24+
filterValue = filterValue.toLowerCase();
25+
26+
return items.filter((item) => {
27+
const fieldValue = item[fieldName].toString();
28+
if (typeof fieldValue === "string") {
29+
return fieldValue.toLowerCase().includes(filterValue);
30+
}
31+
return false;
32+
});
33+
}
34+
}

projects/type-head-input/src/lib/type-head-input.component.html

+5-4
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
>
55
<p-dropdown
66
#select
7-
[options]="enableServerSideData ? (list$ | async)! : items"
8-
(onChange)="
9-
select.filterValue ? (select.filterValue = $event?.value) : null;
10-
onFilterEmitter($event)
7+
[options]="
8+
enableServerSideData
9+
? ((list$ | async)! | valueFilter : searchValue : 'value')
10+
: (items | valueFilter : searchValue : 'value')
1111
"
12+
(onChange)="onFilterEmitter($event)"
1213
[formControl]="control | formControl"
1314
[editable]="true"
1415
[filter]="select?.value?.length > 0 ? true : false"

projects/type-head-input/src/lib/type-head-input.component.ts

+11-5
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ export class TypeHeadInputComponent {
7777
newValueSource = new BehaviorSubject<any>(null);
7878
newValueObs$ = this.newValueSource.asObservable();
7979
isAvailability: boolean = false;
80+
searchValue!: string;
8081

8182
constructor() {}
8283

@@ -184,31 +185,36 @@ export class TypeHeadInputComponent {
184185
}
185186

186187
onFilterEmitter(event: any) {
188+
this.searchValue =
189+
typeof this.select?.value === "object" &&
190+
this.select?.value.hasOwnProperty("value")
191+
? this.select?.value?.value
192+
: this.select?.value;
187193
if (this.enableServerSideData) {
188194
if (this.select?.options === null) {
189195
this.selectedTypeHeadSource.next(event);
190196
return;
191197
}
192198
this.isAvailability = this.select?.options!.some((item) => {
193199
return this.isCaseInsensitive
194-
? item.value.toLowerCase() === this.select?.filterValue?.toLowerCase()
195-
: item.value === this.select.filterValue;
200+
? item.value?.toLowerCase() === this.searchValue?.toLowerCase()
201+
: item?.value === this.searchValue;
196202
});
197203
} else {
198204
this.isAvailability = this.items.some((item) => {
199205
return this.isCaseInsensitive
200-
? item.value.toLowerCase() === this.select?.filterValue?.toLowerCase()
201-
: item.value === this.select.filterValue;
206+
? item?.value?.toLowerCase() === this.searchValue?.toLowerCase()
207+
: item?.value === this.searchValue;
202208
});
203209
}
204210
}
205211

206-
207212
onClick() {
208213
if (this.select.overlayVisible) {
209214
this.select.show();
210215
} else {
211216
this.select.hide();
217+
this.searchValue = "";
212218
}
213219
}
214220
}

projects/type-head-input/src/lib/type-head-input.module.ts

+3
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@ import { CommonModule } from "@angular/common";
1010
import { FormsModule, ReactiveFormsModule } from "@angular/forms";
1111
import { FormControlPipe } from "./pipe/form-Control.pipe";
1212
import { NumbersOnlyDirective } from "./directive/numbers-only.directive";
13+
import { ValueFilterPipe } from "./pipe/value-filter.pipe";
1314
@NgModule({
1415
declarations: [
1516
TypeHeadInputComponent,
1617
NgTypeHeadInputItemTemplateDirective,
1718
NgTypeHeadInputFilterTemplateDirective,
1819
NumbersOnlyDirective,
1920
FormControlPipe,
21+
ValueFilterPipe,
2022
],
2123
imports: [
2224
CommonModule,
@@ -33,6 +35,7 @@ import { NumbersOnlyDirective } from "./directive/numbers-only.directive";
3335
DropdownModule,
3436
SkeletonModule,
3537
FormControlPipe,
38+
ValueFilterPipe,
3639
],
3740
})
3841
export class TypeHeadInputModule {}

projects/type-head-input/src/public-api.ts

+1
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ export * from "./lib/type-head-input.module";
88
export * from "./lib/directive/form-template.directive";
99
export * from "./lib/pipe/form-Control.pipe";
1010
export * from "./lib/directive/numbers-only.directive";
11+
export * from "./lib/pipe/value-filter.pipe";

0 commit comments

Comments
 (0)