Skip to content

Commit

Permalink
fix(input): input emits unexpected valueChanges (#UIM-366) (#415)
Browse files Browse the repository at this point in the history
8x
  • Loading branch information
lskramarov authored Feb 20, 2020
1 parent ea01300 commit b749bec
Show file tree
Hide file tree
Showing 6 changed files with 213 additions and 50 deletions.
16 changes: 10 additions & 6 deletions packages/mosaic-dev/validation/module.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* tslint:disable:no-console no-reserved-keywords */
import { Component, NgModule, ViewEncapsulation } from '@angular/core';
import { ChangeDetectorRef, Component, NgModule, ViewChild, ViewEncapsulation } from '@angular/core';
import {
AbstractControl, FormBuilder,
FormControl,
Expand Down Expand Up @@ -119,10 +119,7 @@ export function ldapLoginValidator(loginRegex: RegExp): ValidatorFn {
selector: 'app',
template: require('./template.html'),
encapsulation: ViewEncapsulation.None,
styleUrls: ['./styles.scss'],
providers: [
// { provide: MC_VALIDATION, useValue: { useValidation: false } }
]
styleUrls: ['./styles.scss']
})
export class DemoComponent {
reactiveTypeaheadItems: string[] = [];
Expand All @@ -131,6 +128,10 @@ export class DemoComponent {
treeSelectValue: string = '';
typeaheadItems: string[] = [];

control = new FormControl('', [Validators.pattern('[a-zA-Z]*')]);

value = '';

reactiveForm: FormGroup;

formWithCustomValidator = new FormGroup(
Expand All @@ -152,7 +153,7 @@ export class DemoComponent {

dataSource: McTreeFlatDataSource<FileNode, FileFlatNode>;

constructor(private formBuilder: FormBuilder) {
constructor(private formBuilder: FormBuilder, public changeDetectorRef: ChangeDetectorRef) {
this.treeFlattener = new McTreeFlattener(
this.transformer, this.getLevel, this.isExpandable, this.getChildren
);
Expand All @@ -173,6 +174,9 @@ export class DemoComponent {
reactiveTypeaheadValue: new FormControl([], [Validators.required])
});

this.control.valueChanges.subscribe((value) => {
console.log('valueChanges: ', value); // tslint:disable-line:no-console
});
}

onSubmitReactiveForm(form: FormGroup) {
Expand Down
8 changes: 4 additions & 4 deletions packages/mosaic-dev/validation/styles.scss
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
display: flex;
flex-direction: row;

& form {
margin-left: 50px;
}

& .mc-form-field {
width: 200px;
}
}

.margin {
margin-left: 50px;
}

header {
$config: mc-typography-config();

Expand Down
48 changes: 26 additions & 22 deletions packages/mosaic-dev/validation/template.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div class="container">
<form [formGroup]="reactiveForm" (ngSubmit)="onSubmitReactiveForm(reactiveForm)" novalidate>
<form class="margin" [formGroup]="reactiveForm" (ngSubmit)="onSubmitReactiveForm(reactiveForm)" novalidate>
<div>Reactive form</div>
<br>

Expand All @@ -17,15 +17,6 @@
<mc-option value="Focused">Focused</mc-option>
<mc-option value="Selected">Selected</mc-option>
<mc-option value="Selected1">Selected1</mc-option>
<mc-option value="Selected2">Selected2</mc-option>
<mc-option value="Selected3">Selected3</mc-option>
<mc-option value="Selected4">Selected4</mc-option>
<mc-option value="Selected5">Selected5</mc-option>
<mc-option value="Selected6">Selected6</mc-option>
<mc-option value="Selected7">Selected7</mc-option>
<mc-option value="Selected8">Selected8</mc-option>
<mc-option value="Selected9">Selected9</mc-option>
<mc-option value="Selected10">Selected10</mc-option>
</mc-select>
</mc-form-field>

Expand Down Expand Up @@ -75,7 +66,7 @@

<br><br>

<form #form="ngForm" (ngSubmit)="onSubmitTemplateForm(form)" novalidate>
<form class="margin" #form="ngForm" (ngSubmit)="onSubmitTemplateForm(form)" novalidate>
<div>Template form</div>
<br>

Expand All @@ -84,7 +75,7 @@
name="input"
mcInput
[(ngModel)]="inputValue"
placeholder="required pattern=[a-zA-Z]*" required pattern="[a-zA-Z]*">
placeholder="required pattern=[a-zA-Z]*" required>
</mc-form-field>

<br><br>
Expand All @@ -97,15 +88,6 @@
<mc-option value="Focused">Focused</mc-option>
<mc-option value="Selected">Selected</mc-option>
<mc-option value="Selected1">Selected1</mc-option>
<mc-option value="Selected2">Selected2</mc-option>
<mc-option value="Selected3">Selected3</mc-option>
<mc-option value="Selected4">Selected4</mc-option>
<mc-option value="Selected5">Selected5</mc-option>
<mc-option value="Selected6">Selected6</mc-option>
<mc-option value="Selected7">Selected7</mc-option>
<mc-option value="Selected8">Selected8</mc-option>
<mc-option value="Selected9">Selected9</mc-option>
<mc-option value="Selected10">Selected10</mc-option>
</mc-select>
</mc-form-field>

Expand Down Expand Up @@ -158,7 +140,7 @@

<br><br>

<form [formGroup]="formWithCustomValidator"
<form class="margin" [formGroup]="formWithCustomValidator"
(ngSubmit)="onSubmitFormWithCustomValidator(formWithCustomValidator)"
novalidate>
<div>formGroup with custom validator</div>
Expand All @@ -178,4 +160,26 @@

<button mc-button>Submit</button>
</form>

<div class="margin">
<div>formControl</div>

<br>

<mc-form-field>
<input mcInput [formControl]="control" type="text" placeholder="Number MaxMin Step">
</mc-form-field>

<br><br>

<form #form="ngForm">
<mc-form-field>
<input mcInput [(ngModel)]="value" type="text" name="qwe" required>
</mc-form-field>

<br><br>

<button mc-button type="submit">submit</button>
</form>
</div>
</div>
6 changes: 3 additions & 3 deletions packages/mosaic/core/validation/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ export function setMosaicValidation(component) {
if (parentForm) {
parentForm.ngSubmit.subscribe(() => {
// tslint:disable-next-line: no-unnecessary-type-assertion
ngControl.control!.updateValueAndValidity();
ngControl.control!.updateValueAndValidity({ emitEvent: false });
});
}

if (component.ngModel) {
setMosaicValidationForModelControl(component, component.rawValidators, parentForm);
} else if (component.formControlName) {
} else if (component.formControlName || component.ngControl) {
setMosaicValidationForFormControl(component, parentForm, ngControl);
}
}
Expand Down Expand Up @@ -92,7 +92,7 @@ export function setMosaicValidationForFormControl(component, parentForm: NgForm,
ngControl.statusChanges!
.subscribe(() => {
// changed required validation logic
if (ngControl.invalid && !parentForm.submitted && ngControl.errors!.required) {
if (ngControl.invalid && (parentForm && !parentForm.submitted) && ngControl.errors!.required) {
setValidState(ngControl.control!, originalValidator!);
}

Expand Down
Loading

0 comments on commit b749bec

Please sign in to comment.