Skip to content

Commit 11a26e4

Browse files
committed
fix(combo): form control binding w/ empty array and value reset #8098
1 parent 85ce162 commit 11a26e4

File tree

4 files changed

+18
-14
lines changed

4 files changed

+18
-14
lines changed

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

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,10 @@ describe('igxCombo', () => {
9797

9898
// writeValue
9999
expect(combo.value).toBe('');
100-
mockSelection.add_items.and.returnValue(new Set(['test']));
100+
mockSelection.get.and.returnValue(new Set(['test']));
101101
spyOnProperty(combo, 'isRemote').and.returnValue(false);
102102
combo.writeValue(['test']);
103-
// TODO: Uncomment after fix for write value going through entire selection process
104-
// expect(mockNgControl.registerOnChangeCb).not.toHaveBeenCalled();
105-
expect(mockSelection.add_items).toHaveBeenCalledWith(combo.id, ['test'], true);
103+
expect(mockNgControl.registerOnChangeCb).not.toHaveBeenCalled();
106104
expect(mockSelection.select_items).toHaveBeenCalledWith(combo.id, ['test'], true);
107105
expect(combo.value).toBe('test');
108106

@@ -207,17 +205,18 @@ describe('igxCombo', () => {
207205
spyOn(mockIconService, 'addSvgIconFromText').and.returnValue(null);
208206
combo.ngOnInit();
209207
combo.data = data;
210-
spyOn(combo, 'selectItems');
208+
mockSelection.select_items.calls.reset();
209+
spyOnProperty(combo, 'isRemote').and.returnValue(false);
211210
combo.writeValue(['EXAMPLE']);
212-
expect(combo.selectItems).toHaveBeenCalledTimes(1);
211+
expect(mockSelection.select_items).toHaveBeenCalledTimes(1);
213212

214-
// Calling "SelectItems" through the writeValue accessor should clear the previous values;
213+
// Calling "select_items" through the writeValue accessor should clear the previous values;
215214
// Select items is called with the invalid value and it is written in selection, though no item is selected
216215
// Controlling the selection is up to the user
217-
expect(combo.selectItems).toHaveBeenCalledWith(['EXAMPLE'], true);
216+
expect(mockSelection.select_items).toHaveBeenCalledWith(combo.id, ['EXAMPLE'], true);
218217
combo.writeValue(combo.data[0]);
219218
// When value key is specified, the item's value key is stored in the selection
220-
expect(combo.selectItems).toHaveBeenCalledWith(combo.data[0], true);
219+
expect(mockSelection.select_items).toHaveBeenCalledWith(combo.id, [], true);
221220
});
222221
it('should select items through setSelctedItem method', () => {
223222
const selectionService = new IgxSelectionAPIService();

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1258,8 +1258,10 @@ export class IgxComboComponent extends DisplayDensityBase implements IgxComboBas
12581258
* @hidden @internal
12591259
*/
12601260
public writeValue(value: any[]): void {
1261-
this.selectItems(value, true);
1262-
this.cdr.markForCheck();
1261+
const selection = Array.isArray(value) ? value : [];
1262+
const oldSelection = this.selectedItems();
1263+
this.selection.select_items(this.id, selection, true);
1264+
this._value = this.createDisplayText(this.selectedItems(), oldSelection);
12631265
}
12641266

12651267
/**

src/app/combo/combo.sample.html

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,16 @@
1818
<div>
1919
<h5 class="sample-title">Template Form</h5>
2020
<form>
21-
<igx-combo class="input-container" [placeholder]="'Locations'" name="anyName" required
22-
[(ngModel)]="values1" [ngModelOptions]="{ updateOn: 'blur' }" #comboModel="ngModel"
21+
<igx-combo class="input-container" [placeholder]="'Locations'" name="anyName" #comboModel="ngModel"
22+
[(ngModel)]="values1" [ngModelOptions]="{ updateOn: 'blur' }" minlength="2" required
2323
[data]="items" [filterable]="filterableFlag" [displayKey]="valueKeyVar"
2424
[valueKey]="valueKeyVar" [groupKey]="valueKeyVar ? 'region' : ''" [width]="'100%'">
25+
<label igxLabel>States</label>
2526
<igx-hint>Please select the states you've visited</igx-hint>
2627
</igx-combo>
2728
</form>
29+
<button igxButton (click)="values1 = values1.concat(['Missouri'])">Add Missouri</button>
30+
<button igxButton (click)="values1 = []">Clear values </button>
2831
</div>
2932
<div>
3033
<h5 class="sample-title">Reactive Form</h5>

src/app/combo/combo.sample.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export class ComboSampleComponent implements OnInit, AfterViewInit {
4444
public customValuesFlag = true;
4545
public autoFocusSearch = true;
4646
public items: any[] = [];
47-
public values1: Array<any>;
47+
public values1: Array<any> = ['Arizona'];
4848
public values2: Array<any>;
4949

5050
public valueKeyVar = 'field';

0 commit comments

Comments
 (0)