Skip to content

Commit 82ecfbf

Browse files
fix(simple-combo): prevent form from getting dirty when tab through an empty combo - 17.2.x (#14685)
1 parent 20cce1c commit 82ecfbf

File tree

2 files changed

+74
-2
lines changed

2 files changed

+74
-2
lines changed

projects/igniteui-angular/src/lib/simple-combo/simple-combo.component.spec.ts

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1017,7 +1017,8 @@ describe('IgxSimpleCombo', () => {
10171017
FormsModule,
10181018
IgxSimpleComboSampleComponent,
10191019
IgxComboInContainerTestComponent,
1020-
IgxSimpleComboIconTemplatesComponent
1020+
IgxSimpleComboIconTemplatesComponent,
1021+
IgxSimpleComboDirtyCheckTestComponent
10211022
]
10221023
}).compileComponents();
10231024
}));
@@ -1947,6 +1948,36 @@ describe('IgxSimpleCombo', () => {
19471948

19481949
expect(combo.comboInput.value).toEqual('ariz');
19491950
}));
1951+
1952+
it('should not mark form as dirty when tabbing through an empty combo', fakeAsync(() => {
1953+
fixture = TestBed.createComponent(IgxSimpleComboDirtyCheckTestComponent);
1954+
fixture.detectChanges();
1955+
1956+
combo = fixture.componentInstance.combo;
1957+
input = fixture.debugElement.query(By.css('.igx-input-group__input'));
1958+
reactiveForm = fixture.componentInstance.form;
1959+
fixture.detectChanges();
1960+
1961+
expect(reactiveForm.dirty).toBe(false);
1962+
1963+
input.nativeElement.focus();
1964+
tick();
1965+
fixture.detectChanges();
1966+
1967+
UIInteractions.triggerKeyDownEvtUponElem('ArrowDown', input.nativeElement);
1968+
tick();
1969+
fixture.detectChanges();
1970+
1971+
input.nativeElement.focus();
1972+
tick();
1973+
fixture.detectChanges();
1974+
1975+
UIInteractions.triggerEventHandlerKeyDown('Tab', input);
1976+
tick();
1977+
fixture.detectChanges();
1978+
1979+
expect(reactiveForm.dirty).toBe(false);
1980+
}));
19501981
});
19511982

19521983
describe('Display density', () => {
@@ -3198,3 +3229,42 @@ export class IgxSimpleComboNgModelComponent implements OnInit {
31983229
];
31993230
}
32003231
}
3232+
3233+
@Component({
3234+
template: `
3235+
<form [formGroup]="form">
3236+
<div class="combo-section">
3237+
<igx-simple-combo
3238+
#combo
3239+
[data]="cities"
3240+
[displayKey]="'name'"
3241+
[valueKey]="'id'"
3242+
formControlName="city"
3243+
>
3244+
</igx-simple-combo>
3245+
</div>
3246+
</form>
3247+
`,
3248+
standalone: true,
3249+
imports: [IgxSimpleComboComponent, ReactiveFormsModule]
3250+
})
3251+
export class IgxSimpleComboDirtyCheckTestComponent implements OnInit {
3252+
@ViewChild('combo', { read: IgxSimpleComboComponent, static: true })
3253+
public combo: IgxSimpleComboComponent;
3254+
3255+
public cities: any = [];
3256+
3257+
public form = new FormGroup({
3258+
city: new FormControl<number>({ value: undefined, disabled: false }),
3259+
});
3260+
3261+
public ngOnInit(): void {
3262+
this.cities = [
3263+
{ id: 1, name: 'New York' },
3264+
{ id: 2, name: 'Los Angeles' },
3265+
{ id: 3, name: 'Chicago' },
3266+
{ id: 4, name: 'Houston' },
3267+
{ id: 5, name: 'Phoenix' }
3268+
];
3269+
}
3270+
}

projects/igniteui-angular/src/lib/simple-combo/simple-combo.component.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,9 @@ export class IgxSimpleComboComponent extends IgxComboBaseDirective implements Co
567567
if (this.filteredData.length !== this.data.length && !ignoreFilter) {
568568
newSelection = this.selectionService.delete_items(this.id, this.selectionService.get_all_ids(this.filteredData, this.valueKey));
569569
}
570-
this.setSelection(newSelection);
570+
if (this.selectionService.get(this.id).size > 0 || this.comboInput.value.trim()) {
571+
this.setSelection(newSelection);
572+
}
571573
}
572574

573575
private clearOnBlur(): void {

0 commit comments

Comments
 (0)