Skip to content

Commit e24dbef

Browse files
authored
Merge branch '20.1.x' into sivanova/input-refinement
2 parents 9be011c + 8ef93bd commit e24dbef

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

projects/igniteui-angular/src/lib/directives/input/input.directive.spec.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -908,6 +908,22 @@ describe('IgxInput', () => {
908908

909909
expect(formControl.touched).toBe(true);
910910
}));
911+
912+
it('should update validity when control is marked as touched', fakeAsync(() => {
913+
const fixture = TestBed.createComponent(ReactiveFormComponent);
914+
fixture.detectChanges();
915+
916+
const component = fixture.componentInstance;
917+
const igxInput = component.strIgxInput;
918+
919+
expect(igxInput.valid).toBe(IgxInputState.INITIAL);
920+
921+
component.markAllAsTouched();
922+
tick();
923+
fixture.detectChanges();
924+
925+
expect(igxInput.valid).toBe(IgxInputState.INVALID);
926+
}));
911927
});
912928

913929
@Component({
@@ -1201,6 +1217,12 @@ class ReactiveFormComponent {
12011217
this.textareaControl.markAsTouched();
12021218
this.textareaControl.updateValueAndValidity();
12031219
}
1220+
1221+
public markAllAsTouched() {
1222+
if (!this.form.valid) {
1223+
this.form.markAllAsTouched();
1224+
}
1225+
}
12041226
}
12051227

12061228
@Component({

projects/igniteui-angular/src/lib/directives/input/input.directive.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ import {
1616
import {
1717
AbstractControl,
1818
NgControl,
19-
NgModel
19+
NgModel,
20+
TouchedChangeEvent
2021
} from '@angular/forms';
21-
import { Subscription } from 'rxjs';
22+
import { filter, Subscription } from 'rxjs';
2223
import { IgxInputGroupBase } from '../../input-group/input-group.common';
2324

2425
const nativeValidationAttributes = [
@@ -100,6 +101,7 @@ export class IgxInputDirective implements AfterViewInit, OnDestroy {
100101
private _valid = IgxInputState.INITIAL;
101102
private _statusChanges$: Subscription;
102103
private _valueChanges$: Subscription;
104+
private _touchedChanges$: Subscription;
103105
private _fileNames: string;
104106
private _disabled = false;
105107

@@ -317,6 +319,14 @@ export class IgxInputDirective implements AfterViewInit, OnDestroy {
317319
this._valueChanges$ = this.ngControl.valueChanges.subscribe(
318320
this.onValueChanged.bind(this)
319321
);
322+
323+
if (this.ngControl.control) {
324+
this._touchedChanges$ = this.ngControl.control.events
325+
.pipe(filter(e => e instanceof TouchedChangeEvent))
326+
.subscribe(
327+
this.updateValidityState.bind(this)
328+
);
329+
}
320330
}
321331

322332
this.cdr.detectChanges();
@@ -330,6 +340,10 @@ export class IgxInputDirective implements AfterViewInit, OnDestroy {
330340
if (this._valueChanges$) {
331341
this._valueChanges$.unsubscribe();
332342
}
343+
344+
if (this._touchedChanges$) {
345+
this._touchedChanges$.unsubscribe();
346+
}
333347
}
334348
/**
335349
* Sets a focus on the igxInput.

0 commit comments

Comments
 (0)