Skip to content

Commit 29a4f17

Browse files
authored
Merge pull request #10 from frankframework/issue/7-search-input-doesnt-update-when-ngmodel-variable-is-updated
Fix search input not being updated on external value changes
2 parents c39ed60 + 2628328 commit 29a4f17

File tree

4 files changed

+41
-6
lines changed

4 files changed

+41
-6
lines changed

Diff for: projects/angular-components/src/lib/search/search.component.html

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
><input
44
type="text"
55
[placeholder]="placeholder"
6-
[attr.value]="value"
76
[disabled]="disabled"
7+
[value]="value"
88
(blur)="_onBlur()"
9-
(input)="_onInteractionEvent($event)"
9+
(input)="_onInputEvent()"
10+
(change)="_onInteractionEvent($event)"
1011
#input
1112
/>
1213
@if (focusKeyEnabled && !disabled) {

Diff for: projects/angular-components/src/lib/search/search.component.ts

+33-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import {
22
booleanAttribute,
33
ChangeDetectionStrategy,
4+
ChangeDetectorRef,
45
Component,
56
ElementRef,
67
forwardRef,
8+
inject,
79
Input,
810
OnDestroy,
911
OnInit,
@@ -30,21 +32,42 @@ export const SEARCH_CONTROL_VALUE_ACCESSOR = {
3032
providers: [SEARCH_CONTROL_VALUE_ACCESSOR],
3133
})
3234
export class SearchComponent implements OnInit, OnDestroy, ControlValueAccessor {
33-
@Input() value: string = '';
3435
@Input() placeholder: string = 'Search...';
3536
@Input() focusKey: string = '/';
3637
@Input({ transform: booleanAttribute }) focusKeyEnabled: boolean = true;
37-
@Input({ transform: booleanAttribute }) disabled: boolean = false;
3838
@Input({ transform: booleanAttribute }) slim: boolean = false;
3939
@ViewChild('input') _inputElement!: ElementRef<HTMLInputElement>;
4040

4141
protected _onChange: (value: string) => void = noop;
4242
protected _onTouched: () => void = noop;
4343

44+
private _value: string = '';
45+
private _disabled: boolean = false;
46+
47+
@Input()
48+
get value(): string {
49+
return this._value;
50+
}
51+
set value(value: string) {
52+
this._value = value;
53+
this._changeDetectorRef.markForCheck();
54+
}
55+
56+
@Input({ transform: booleanAttribute })
57+
get disabled(): boolean {
58+
return this._disabled;
59+
}
60+
set disabled(disabled: boolean) {
61+
this._disabled = disabled;
62+
this._changeDetectorRef.markForCheck();
63+
}
64+
65+
private _changeDetectorRef = inject(ChangeDetectorRef);
4466
private searchSubject: Subject<string> = new Subject();
4567

4668
ngOnInit(): void {
4769
this.searchSubject.pipe(debounceTime(200)).subscribe((value) => {
70+
this.value = value;
4871
this._onChange(value);
4972
});
5073

@@ -78,12 +101,18 @@ export class SearchComponent implements OnInit, OnDestroy, ControlValueAccessor
78101
}
79102

80103
protected _onBlur(): void {
81-
Promise.resolve().then(() => this._onTouched());
104+
Promise.resolve().then(() => {
105+
this._onTouched();
106+
this._changeDetectorRef.markForCheck();
107+
});
108+
}
109+
110+
protected _onInputEvent(): void {
111+
this.searchSubject.next(this._inputElement.nativeElement.value);
82112
}
83113

84114
protected _onInteractionEvent(event: Event): void {
85115
event.stopPropagation();
86-
this.searchSubject.next(this._inputElement.nativeElement.value);
87116
}
88117

89118
protected _onKeyEvent(event: KeyboardEvent): void {

Diff for: src/app/app.component.html

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ <h2>Search</h2>
3232
</div>
3333
</div>
3434
<p>Search query: {{ searchText }}</p>
35+
<ff-button (click)="changeSearchQuery()">Update search query</ff-button>
3536
</li>
3637
<li>
3738
<h2>Alert</h2>

Diff for: src/app/app.component.ts

+4
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,8 @@ export class AppComponent implements OnInit {
5757
protected toggleTheme(): void {
5858
document.body.classList.toggle('ff-dark-theme');
5959
}
60+
61+
protected changeSearchQuery(): void {
62+
this.searchText = 'Eekum Bokum';
63+
}
6064
}

0 commit comments

Comments
 (0)