From 14252054a150595d89040a02cb510b9f43f4570b Mon Sep 17 00:00:00 2001 From: shamoon <4887959+shamoon@users.noreply.github.com> Date: Wed, 18 Nov 2026 16:28:02 -0800 Subject: [PATCH] Fix/tweak: update form inputs while typing (#14821) --- .../common/input/input-events.spec.ts | 90 +++++++++++++++++++ .../common/input/number/number.component.html | 2 +- .../common/input/text/text.component.html | 2 +- .../input/textarea/textarea.component.html | 3 +- .../common/input/url/url.component.html | 2 +- .../document-detail.component.spec.ts | 10 +-- 6 files changed, 99 insertions(+), 10 deletions(-) create mode 100644 src-ui/src/app/components/common/input/input-events.spec.ts diff --git a/src-ui/src/app/components/common/input/input-events.spec.ts b/src-ui/src/app/components/common/input/input-events.spec.ts new file mode 100644 index 0000000000..94083a10c8 --- /dev/null +++ b/src-ui/src/app/components/common/input/input-events.spec.ts @@ -0,0 +1,90 @@ +import { provideHttpClient } from '@angular/common/http' +import { provideHttpClientTesting } from '@angular/common/http/testing' +import { Component } from '@angular/core' +import { ComponentFixture, TestBed } from '@angular/core/testing' +import { FormControl, ReactiveFormsModule } from '@angular/forms' +import { NumberComponent } from './number/number.component' +import { TextComponent } from './text/text.component' +import { TextAreaComponent } from './textarea/textarea.component' +import { UrlComponent } from './url/url.component' + +@Component({ + imports: [ + ReactiveFormsModule, + TextComponent, + NumberComponent, + TextAreaComponent, + UrlComponent, + ], + template: ` + + + + + `, +}) +class InputEventsHostComponent { + control = new FormControl(null) +} + +describe.each([ + ['text', 'pngx-input-text input', 'Edited text', 'Edited text'], + ['number', 'pngx-input-number input', '42', 42], + ['textarea', 'pngx-input-textarea textarea', 'Edited\ntext', 'Edited\ntext'], + ['url', 'pngx-input-url input', 'https://example.com', 'https://example.com'], +])( + '%s input events', + (_type, selector: string, text: string, value: string | number) => { + let fixture: ComponentFixture + let control: FormControl + let input: HTMLInputElement | HTMLTextAreaElement + + beforeEach(() => { + TestBed.configureTestingModule({ + imports: [InputEventsHostComponent], + providers: [provideHttpClient(), provideHttpClientTesting()], + }) + fixture = TestBed.createComponent(InputEventsHostComponent) + control = fixture.componentInstance.control + }) + + async function render() { + fixture.detectChanges() + await fixture.whenStable() + input = fixture.nativeElement.querySelector(selector) + } + + it('updates the form while typing and marks it touched only on blur', async () => { + await render() + const changed = jest.fn() + control.valueChanges.subscribe(changed) + input.value = text + input.dispatchEvent(new Event('input')) + expect(control.value).toBe(value) + expect(control.dirty).toBe(true) + expect(control.touched).toBe(false) + + input.dispatchEvent(new Event('change')) + input.dispatchEvent(new Event('blur')) + expect(control.touched).toBe(true) + expect(changed).toHaveBeenCalledTimes(1) + }) + + it('honors an explicit updateOn blur strategy', async () => { + control = new FormControl(null, { + updateOn: 'blur', + }) + fixture.componentInstance.control = control + await render() + input.value = text + input.dispatchEvent(new Event('input')) + expect(control.value).toBeNull() + expect(control.pristine).toBe(true) + + input.dispatchEvent(new Event('blur')) + expect(control.value).toBe(value) + expect(control.dirty).toBe(true) + expect(control.touched).toBe(true) + }) + } +) diff --git a/src-ui/src/app/components/common/input/number/number.component.html b/src-ui/src/app/components/common/input/number/number.component.html index bbbf6fb576..0a1971561e 100644 --- a/src-ui/src/app/components/common/input/number/number.component.html +++ b/src-ui/src/app/components/common/input/number/number.component.html @@ -12,7 +12,7 @@
- + @if (showAdd) { } diff --git a/src-ui/src/app/components/common/input/text/text.component.html b/src-ui/src/app/components/common/input/text/text.component.html index 5c7c5742e4..71a4564a64 100644 --- a/src-ui/src/app/components/common/input/text/text.component.html +++ b/src-ui/src/app/components/common/input/text/text.component.html @@ -11,7 +11,7 @@ }
- + @if (hint) { } diff --git a/src-ui/src/app/components/common/input/textarea/textarea.component.html b/src-ui/src/app/components/common/input/textarea/textarea.component.html index 36e8a01ff9..f16e419a8d 100644 --- a/src-ui/src/app/components/common/input/textarea/textarea.component.html +++ b/src-ui/src/app/components/common/input/textarea/textarea.component.html @@ -18,7 +18,8 @@ [class.font-monospace]="monospace" [(ngModel)]="value" [ngModelOptions]="{standalone: true}" - (change)="onChange(value)" + (ngModelChange)="onChange($event)" + (blur)="onTouched()" [disabled]="disabled" [placeholder]="placeholder" rows="4"> diff --git a/src-ui/src/app/components/common/input/url/url.component.html b/src-ui/src/app/components/common/input/url/url.component.html index 85a9074522..8e455bfe77 100644 --- a/src-ui/src/app/components/common/input/url/url.component.html +++ b/src-ui/src/app/components/common/input/url/url.component.html @@ -10,7 +10,7 @@
- + diff --git a/src-ui/src/app/components/document-detail/document-detail.component.spec.ts b/src-ui/src/app/components/document-detail/document-detail.component.spec.ts index 784a100e1b..b5a64255ee 100644 --- a/src-ui/src/app/components/document-detail/document-detail.component.spec.ts +++ b/src-ui/src/app/components/document-detail/document-detail.component.spec.ts @@ -390,14 +390,12 @@ describe('DocumentDetailComponent', () => { expect(component.documentForm.get('title').value).toEqual('Foo Bar') }) - it('should update title before doc change if was not updated via debounce', () => { + it('should update the title form value without waiting for debounce or blur', () => { jest.useFakeTimers() initNormally() - component.titleInput.value = 'Foo Bar' - component.titleInput.inputField.nativeElement.dispatchEvent( - new Event('change') - ) - jest.advanceTimersByTime(1000) + const input = component.titleInput.inputField.nativeElement + input.value = 'Foo Bar' + input.dispatchEvent(new Event('input')) expect(component.documentForm.get('title').value).toEqual('Foo Bar') })