Skip to content

Commit 3f557aa

Browse files
Merge pull request #14482 from IgniteUI/mkirkova/fix-14086-17.2.x
Allow negative values in filter input - 17.2.x
2 parents abe9131 + 54ef3da commit 3f557aa

File tree

4 files changed

+68
-8
lines changed

4 files changed

+68
-8
lines changed

projects/igniteui-angular/src/lib/grids/filtering/base/grid-filtering-row.component.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,13 @@ export class IgxGridFilteringRowComponent implements AfterViewInit, OnDestroy {
8080

8181
@Input()
8282
public get value(): any {
83-
return this.expression ? this.expression.searchVal : null;
83+
return this._value;
8484
}
8585

8686
public set value(val) {
87-
if (!val && val !== 0 && this.expression.searchVal) {
87+
if (!val && val !== 0 && (this.expression.searchVal || this.expression.searchVal === 0)) {
8888
this.expression.searchVal = null;
89+
this._value = null;
8990
const index = this.expressionsList.findIndex(item => item.expression === this.expression);
9091
if (index === 0 && this.expressionsList.length === 1) {
9192
this.filteringService.clearFilter(this.column.field);
@@ -97,11 +98,15 @@ export class IgxGridFilteringRowComponent implements AfterViewInit, OnDestroy {
9798
return;
9899
}
99100
} else {
101+
if (val === '') {
102+
return;
103+
}
100104
const oldValue = this.expression.searchVal;
101105
if (isEqual(oldValue, val)) {
102106
return;
103107
}
104108

109+
this._value = val;
105110
this.expression.searchVal = DataUtil.parseValue(this.column.dataType, val);
106111
if (this.expressionsList.find(item => item.expression === this.expression) === undefined) {
107112
this.addExpression(true);
@@ -206,6 +211,7 @@ export class IgxGridFilteringRowComponent implements AfterViewInit, OnDestroy {
206211
private isKeyPressed = false;
207212
private isComposing = false;
208213
private _cancelChipClick = false;
214+
private _value = null;
209215

210216
/** switch to icon buttons when width is below 432px */
211217
private readonly NARROW_WIDTH_THRESHOLD = 432;
@@ -235,6 +241,7 @@ export class IgxGridFilteringRowComponent implements AfterViewInit, OnDestroy {
235241
const selectedItem = this.expressionsList.find(expr => expr.isSelected === true);
236242
if (selectedItem) {
237243
this.expression = selectedItem.expression;
244+
this._value = this.expression.searchVal;
238245
}
239246

240247
this.filteringService.grid.localeChange
@@ -458,6 +465,7 @@ export class IgxGridFilteringRowComponent implements AfterViewInit, OnDestroy {
458465
this.removeExpression(indexToDeselect, this.expression);
459466
}
460467
this.resetExpression();
468+
this._value = this.expression.searchVal;
461469
this.scrollChipsWhenAddingExpression();
462470
}
463471

@@ -631,7 +639,7 @@ export class IgxGridFilteringRowComponent implements AfterViewInit, OnDestroy {
631639
item.isSelected = !item.isSelected;
632640
if (item.isSelected) {
633641
this.expression = item.expression;
634-
642+
this._value = this.expression.searchVal;
635643
this.focusEditElement();
636644
}
637645
}

projects/igniteui-angular/src/lib/grids/grid/grid-filtering-ui.spec.ts

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ import {
4343
IgxGridExternalESFComponent,
4444
IgxGridExternalESFTemplateComponent,
4545
IgxGridDatesFilteringComponent,
46-
LoadOnDemandFilterStrategy
46+
LoadOnDemandFilterStrategy,
47+
IgxGridFilteringNumericComponent
4748
} from '../../test-utils/grid-samples.spec';
4849
import { GridSelectionMode, FilterMode } from '../common/enums';
4950
import { ControlsFunction } from '../../test-utils/controls-functions.spec';
@@ -71,7 +72,8 @@ describe('IgxGrid - Filtering Row UI actions #grid', () => {
7172
IgxGridFilteringScrollComponent,
7273
IgxGridFilteringMCHComponent,
7374
IgxGridFilteringTemplateComponent,
74-
IgxGridDatesFilteringComponent
75+
IgxGridDatesFilteringComponent,
76+
IgxGridFilteringNumericComponent
7577
]
7678
});
7779
}));
@@ -904,6 +906,38 @@ describe('IgxGrid - Filtering Row UI actions #grid', () => {
904906
expect(input.properties.readOnly).toBeTruthy();
905907
}));
906908

909+
it('should correctly filter negative values', fakeAsync(() => {
910+
fix = TestBed.createComponent(IgxGridFilteringNumericComponent);
911+
fix.detectChanges();
912+
grid = fix.componentInstance.grid;
913+
914+
GridFunctions.clickFilterCellChip(fix, 'Number');
915+
916+
const filteringRow = fix.debugElement.query(By.directive(IgxGridFilteringRowComponent));
917+
const input = filteringRow.query(By.directive(IgxInputDirective));
918+
919+
// Set input and confirm
920+
GridFunctions.typeValueInFilterRowInput('-1', fix);
921+
922+
expect(input.componentInstance.value).toEqual('-1');
923+
expect(grid.rowList.length).toEqual(1);
924+
925+
GridFunctions.typeValueInFilterRowInput('0', fix);
926+
927+
expect(input.componentInstance.value).toEqual('0');
928+
expect(grid.rowList.length).toEqual(0);
929+
930+
GridFunctions.typeValueInFilterRowInput('-0.5', fix);
931+
932+
expect(input.componentInstance.value).toEqual('-0.5');
933+
expect(grid.rowList.length).toEqual(1);
934+
935+
GridFunctions.typeValueInFilterRowInput('', fix);
936+
937+
expect(input.componentInstance.value).toEqual(null);
938+
expect(grid.rowList.length).toEqual(3);
939+
}));
940+
907941
it('Should focus input .', fakeAsync(() => {
908942
GridFunctions.clickFilterCellChip(fix, 'ProductName');
909943

@@ -2751,12 +2785,12 @@ describe('IgxGrid - Filtering Row UI actions #grid', () => {
27512785
it('Should not prevent mousedown event when target is within the filter cell template', fakeAsync(() => {
27522786
const filterCell = GridFunctions.getFilterCell(fix, 'ProductName');
27532787
const input = filterCell.query(By.css('input')).nativeElement;
2754-
2788+
27552789
const mousedownEvent = new MouseEvent('mousedown', { bubbles: true });
27562790
const preventDefaultSpy = spyOn(mousedownEvent, 'preventDefault');
27572791
input.dispatchEvent(mousedownEvent, { bubbles: true });
27582792
fix.detectChanges();
2759-
2793+
27602794
expect(preventDefaultSpy).not.toHaveBeenCalled();
27612795
}));
27622796

@@ -2768,7 +2802,7 @@ describe('IgxGrid - Filtering Row UI actions #grid', () => {
27682802
const preventDefaultSpy = spyOn(mousedownEvent, 'preventDefault');
27692803
firstCell.dispatchEvent(mousedownEvent);
27702804
fix.detectChanges();
2771-
2805+
27722806
expect(preventDefaultSpy).toHaveBeenCalled();
27732807
}));
27742808
});

projects/igniteui-angular/src/lib/test-utils/grid-samples.spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,17 @@ export class IgxGridFilteringComponent extends BasicGridComponent {
764764
}
765765
}
766766

767+
@Component({
768+
template: `<igx-grid [data]="data" height="500px" [allowFiltering]="true">
769+
<igx-column width="100px" [field]="'Number'" [header]="'Number'" [filterable]="true" dataType="number"></igx-column>
770+
</igx-grid>`,
771+
standalone: true,
772+
imports: [IgxGridComponent, IgxColumnComponent]
773+
})
774+
export class IgxGridFilteringNumericComponent extends BasicGridComponent {
775+
public override data = SampleTestData.numericData();
776+
}
777+
767778
@Component({
768779
template: `<igx-grid [data]="data" height="500px" [allowFiltering]="true">
769780
<igx-column width="100px" [field]="'ID'" [header]="'ID'" [hasSummary]="true"

projects/igniteui-angular/src/lib/test-utils/sample-test-data.spec.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,13 @@ export class SampleTestData {
6868
{ Number: 3, String: '3', Boolean: true, Date: new Date(2018, 9, 22) }
6969
]);
7070

71+
/* Fields: Number: number; 3 items. */
72+
public static numericData = () => ([
73+
{ Number: -1 },
74+
{ Number: 2.5 },
75+
{ Number: -0.5 }
76+
]);
77+
7178
/* Fields: Name: string, Avatar: string; 3 items. */
7279
public static personAvatarData = () => ([
7380
{

0 commit comments

Comments
 (0)