Skip to content

Commit eb58644

Browse files
authored
fix(query-builder): prevent fields collection change (#15607)
* fix(query-builder): add test for mutated fields collection * fix(query-builder): prevent changing fields collection * fix(query-builder): fix Advanced filtering failing tests
1 parent b248b4c commit eb58644

File tree

2 files changed

+31
-36
lines changed

2 files changed

+31
-36
lines changed

projects/igniteui-angular/src/lib/query-builder/query-builder-tree.component.ts

+19-27
Original file line numberDiff line numberDiff line change
@@ -172,17 +172,12 @@ export class IgxQueryBuilderTreeComponent implements AfterViewInit, OnDestroy {
172172
@Input()
173173
public set fields(fields: FieldType[]) {
174174
this._fields = fields;
175-
175+
176+
this._fields = this._fields?.map(f => ({...f, filters: this.getFilters(f), pipeArgs: this.getPipeArgs(f) }));
177+
176178
if (!this._fields && this.isAdvancedFiltering()) {
177179
this._fields = this.entities[0].fields;
178180
}
179-
180-
if (this._fields) {
181-
this._fields.forEach(field => {
182-
this.setFilters(field);
183-
this.setFormat(field);
184-
});
185-
}
186181
}
187182

188183
/**
@@ -1182,7 +1177,7 @@ export class IgxQueryBuilderTreeComponent implements AfterViewInit, OnDestroy {
11821177
if (!this.selectedField) {
11831178
this.fieldSelect.input.nativeElement.focus();
11841179
} else if (this.selectedField.filters.condition(this.selectedCondition)?.isUnary) {
1185-
this.conditionSelect.input.nativeElement.focus();
1180+
this.conditionSelect?.input.nativeElement.focus();
11861181
} else {
11871182
const input = this.searchValueInput?.nativeElement || this.picker?.getEditElement();
11881183
input?.focus();
@@ -1485,16 +1480,19 @@ export class IgxQueryBuilderTreeComponent implements AfterViewInit, OnDestroy {
14851480
return ctx;
14861481
}
14871482

1488-
private setFormat(field: FieldType) {
1489-
if (!field.pipeArgs) {
1490-
field.pipeArgs = { digitsInfo: DEFAULT_PIPE_DIGITS_INFO };
1483+
private getPipeArgs(field: FieldType) {
1484+
let pipeArgs = {...field.pipeArgs};
1485+
if (!pipeArgs) {
1486+
pipeArgs = { digitsInfo: DEFAULT_PIPE_DIGITS_INFO };
14911487
}
14921488

1493-
if (!field.pipeArgs.format) {
1494-
field.pipeArgs.format = field.dataType === DataType.Time ?
1489+
if (!pipeArgs.format) {
1490+
pipeArgs.format = field.dataType === DataType.Time ?
14951491
DEFAULT_PIPE_TIME_FORMAT : field.dataType === DataType.DateTime ?
14961492
DEFAULT_PIPE_DATE_TIME_FORMAT : DEFAULT_PIPE_DATE_FORMAT;
14971493
}
1494+
1495+
return pipeArgs;
14981496
}
14991497

15001498
private selectDefaultCondition() {
@@ -1503,30 +1501,24 @@ export class IgxQueryBuilderTreeComponent implements AfterViewInit, OnDestroy {
15031501
}
15041502
}
15051503

1506-
private setFilters(field: FieldType) {
1504+
private getFilters(field: FieldType) {
15071505
if (!field.filters) {
15081506
switch (field.dataType) {
15091507
case DataType.Boolean:
1510-
field.filters = IgxBooleanFilteringOperand.instance();
1511-
break;
1508+
return IgxBooleanFilteringOperand.instance();
15121509
case DataType.Number:
15131510
case DataType.Currency:
15141511
case DataType.Percent:
1515-
field.filters = IgxNumberFilteringOperand.instance();
1516-
break;
1512+
return IgxNumberFilteringOperand.instance();
15171513
case DataType.Date:
1518-
field.filters = IgxDateFilteringOperand.instance();
1519-
break;
1514+
return IgxDateFilteringOperand.instance();
15201515
case DataType.Time:
1521-
field.filters = IgxTimeFilteringOperand.instance();
1522-
break;
1516+
return IgxTimeFilteringOperand.instance();
15231517
case DataType.DateTime:
1524-
field.filters = IgxDateTimeFilteringOperand.instance();
1525-
break;
1518+
return IgxDateTimeFilteringOperand.instance();
15261519
case DataType.String:
15271520
default:
1528-
field.filters = IgxStringFilteringOperand.instance();
1529-
break;
1521+
return IgxStringFilteringOperand.instance();
15301522
}
15311523
}
15321524
}

projects/igniteui-angular/src/lib/query-builder/query-builder.component.spec.ts

+12-9
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,16 @@ describe('IgxQueryBuilder', () => {
119119
expect(mainEntityContainer.children[1].children[1].tagName).toBe('IGX-COMBO');
120120
expect(nestedEntityContainer.children[1].children[1].tagName).toBe('IGX-SELECT');
121121
}));
122+
123+
it('Should return proper fields collection without additional props.', fakeAsync(() => {
124+
queryBuilder.expressionTree = QueryBuilderFunctions.generateExpressionTree();
125+
fix.detectChanges();
126+
127+
queryBuilder.entities[0].fields.forEach(field => {
128+
expect(field.filters).toBeUndefined();
129+
expect(field.pipeArgs).toBeUndefined();
130+
});
131+
}));
122132
});
123133

124134
describe('Interactions', () => {
@@ -3243,8 +3253,7 @@ export class IgxQueryBuilderSampleTestComponent implements OnInit {
32433253
<p class="selectedField">{{selectedField.field}}</p>
32443254
<p class="selectedCondition">{{selectedCondition}}</p>
32453255
} @else if (selectedField?.field === 'OrderId' && selectedCondition === 'equals') {
3246-
<igx-combo [data]="comboData" [(ngModel)]="searchValue.value"
3247-
(selectionChanging)="handleChange($event, selectedField, searchValue)" [displayKey]="'field'">
3256+
<igx-combo [data]="comboData" [(ngModel)]="searchValue.value" [displayKey]="'field'">
32483257
</igx-combo>
32493258
} @else {
32503259
<ng-container #defaultTemplate *ngTemplateOutlet="defaultSearchValueTemplate"></ng-container>
@@ -3273,6 +3282,7 @@ export class IgxQueryBuilderCustomTemplateSampleTestComponent implements OnInit
32733282

32743283
public ngOnInit(): void {
32753284
this.entities = SampleEntities.map(a => ({ ...a }));
3285+
this.entities[1].fields[0].formatter = (value: any, rowData: any) => rowData === 'equals' ? (Array.from(value)[0] as any).id : value;
32763286

32773287
const tree = new FilteringExpressionsTree(FilteringLogic.And, null, 'Orders', ['*']);
32783288
tree.filteringOperands.push({
@@ -3290,11 +3300,4 @@ export class IgxQueryBuilderCustomTemplateSampleTestComponent implements OnInit
32903300
{ id: 1, field: 'B' }
32913301
];
32923302
}
3293-
3294-
public handleChange(ev, selectedField, searchVal) {
3295-
if (selectedField.field === 'OrderId') {
3296-
searchVal.value = ev.newValue[0];
3297-
selectedField.formatter = (value: any, rowData: any) => rowData === 'equals' ? (Array.from(value)[0] as any).id : value;
3298-
}
3299-
}
33003303
}

0 commit comments

Comments
 (0)