Skip to content

Commit a256c0f

Browse files
fix(simple-combo): prevent form from getting dirty when tab through an empty combo - master (#14683)
1 parent a1f3fe0 commit a256c0f

File tree

2 files changed

+74
-2
lines changed

2 files changed

+74
-2
lines changed

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

+71-1
Original file line numberDiff line numberDiff line change
@@ -1133,7 +1133,8 @@ describe('IgxSimpleCombo', () => {
11331133
FormsModule,
11341134
IgxSimpleComboSampleComponent,
11351135
IgxComboInContainerTestComponent,
1136-
IgxSimpleComboIconTemplatesComponent
1136+
IgxSimpleComboIconTemplatesComponent,
1137+
IgxSimpleComboDirtyCheckTestComponent
11371138
]
11381139
}).compileComponents();
11391140
}));
@@ -2063,6 +2064,36 @@ describe('IgxSimpleCombo', () => {
20632064

20642065
expect(combo.comboInput.value).toEqual('ariz');
20652066
}));
2067+
2068+
it('should not mark form as dirty when tabbing through an empty combo', fakeAsync(() => {
2069+
fixture = TestBed.createComponent(IgxSimpleComboDirtyCheckTestComponent);
2070+
fixture.detectChanges();
2071+
2072+
combo = fixture.componentInstance.combo;
2073+
input = fixture.debugElement.query(By.css('.igx-input-group__input'));
2074+
reactiveForm = fixture.componentInstance.form;
2075+
fixture.detectChanges();
2076+
2077+
expect(reactiveForm.dirty).toBe(false);
2078+
2079+
input.nativeElement.focus();
2080+
tick();
2081+
fixture.detectChanges();
2082+
2083+
UIInteractions.triggerKeyDownEvtUponElem('ArrowDown', input.nativeElement);
2084+
tick();
2085+
fixture.detectChanges();
2086+
2087+
input.nativeElement.focus();
2088+
tick();
2089+
fixture.detectChanges();
2090+
2091+
UIInteractions.triggerEventHandlerKeyDown('Tab', input);
2092+
tick();
2093+
fixture.detectChanges();
2094+
2095+
expect(reactiveForm.dirty).toBe(false);
2096+
}));
20662097
});
20672098

20682099
describe('Display density', () => {
@@ -3367,3 +3398,42 @@ export class IgxSimpleComboNgModelComponent implements OnInit {
33673398
];
33683399
}
33693400
}
3401+
3402+
@Component({
3403+
template: `
3404+
<form [formGroup]="form">
3405+
<div class="combo-section">
3406+
<igx-simple-combo
3407+
#combo
3408+
[data]="cities"
3409+
[displayKey]="'name'"
3410+
[valueKey]="'id'"
3411+
formControlName="city"
3412+
>
3413+
</igx-simple-combo>
3414+
</div>
3415+
</form>
3416+
`,
3417+
standalone: true,
3418+
imports: [IgxSimpleComboComponent, ReactiveFormsModule]
3419+
})
3420+
export class IgxSimpleComboDirtyCheckTestComponent implements OnInit {
3421+
@ViewChild('combo', { read: IgxSimpleComboComponent, static: true })
3422+
public combo: IgxSimpleComboComponent;
3423+
3424+
public cities: any = [];
3425+
3426+
public form = new FormGroup({
3427+
city: new FormControl<number>({ value: undefined, disabled: false }),
3428+
});
3429+
3430+
public ngOnInit(): void {
3431+
this.cities = [
3432+
{ id: 1, name: 'New York' },
3433+
{ id: 2, name: 'Los Angeles' },
3434+
{ id: 3, name: 'Chicago' },
3435+
{ id: 4, name: 'Houston' },
3436+
{ id: 5, name: 'Phoenix' }
3437+
];
3438+
}
3439+
}

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,9 @@ export class IgxSimpleComboComponent extends IgxComboBaseDirective implements Co
574574
if (this.filteredData.length !== this.data.length && !ignoreFilter) {
575575
newSelection = this.selectionService.delete_items(this.id, this.selectionService.get_all_ids(this.filteredData, this.valueKey));
576576
}
577-
this.setSelection(newSelection);
577+
if (this.selectionService.get(this.id).size > 0 || this.comboInput.value.trim()) {
578+
this.setSelection(newSelection);
579+
}
578580
}
579581

580582
private clearOnBlur(): void {

0 commit comments

Comments
 (0)