Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(igxInput): #3550 Setting required input value updates valid state. #3563

Merged
merged 8 commits into from
Jan 14, 2019
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,37 @@ describe('IgxInput', () => {
testRequiredValidation(inputElement, fixture);
});

it('Should update style when required input\'s value is set.', () => {
const fixture = TestBed.createComponent(RequiredInputComponent);
fixture.detectChanges();

const igxInput = fixture.componentInstance.igxInput;
const inputElement = fixture.debugElement.query(By.directive(IgxInputDirective)).nativeElement;

dispatchInputEvent('focus', inputElement, fixture);
dispatchInputEvent('blur', inputElement, fixture);

const inputGroupElement = fixture.debugElement.query(By.css('igx-input-group')).nativeElement;
expect(inputGroupElement.classList.contains(INPUT_GROUP_INVALID_CSS_CLASS)).toBe(true);
expect(igxInput.valid).toBe(IgxInputState.INVALID);

igxInput.value = 'test';
fixture.detectChanges();

expect(inputGroupElement.classList.contains(INPUT_GROUP_INVALID_CSS_CLASS)).toBe(false);
expect(igxInput.valid).toBe(IgxInputState.VALID);


igxInput.value = '';
fixture.detectChanges();

dispatchInputEvent('focus', inputElement, fixture);
dispatchInputEvent('blur', inputElement, fixture);

expect(inputGroupElement.classList.contains(INPUT_GROUP_INVALID_CSS_CLASS)).toBe(true);
expect(igxInput.valid).toBe(IgxInputState.INVALID);
});

it('Should style required input with two-way databinding correctly.', () => {
const fixture = TestBed.createComponent(RequiredTwoWayDataBoundInputComponent);
fixture.detectChanges();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export class IgxInputDirective implements AfterViewInit, OnDestroy {
@Input('value')
set value(value: any) {
this.nativeElement.value = value;
this.checkValidity();
}
/**
* Gets the `value` propery.
Expand Down Expand Up @@ -144,9 +145,7 @@ export class IgxInputDirective implements AfterViewInit, OnDestroy {
*/
@HostListener('input')
public onInput() {
if (!this.ngControl && this._hasValidators()) {
this._valid = this.nativeElement.checkValidity() ? IgxInputState.VALID : IgxInputState.INVALID;
}
this.checkValidity();
}
/**
*@hidden
Expand Down Expand Up @@ -294,4 +293,10 @@ export class IgxInputDirective implements AfterViewInit, OnDestroy {
public set valid(value: IgxInputState) {
this._valid = value;
}

private checkValidity() {
if (!this.ngControl && this._hasValidators) {
this._valid = this.nativeElement.checkValidity() ? IgxInputState.VALID : IgxInputState.INVALID;
}
}
}