Skip to content

Commit 9c8580c

Browse files
committed
fix(grid): allow negative values in filter input
1 parent a59767a commit 9c8580c

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
@@ -81,12 +81,13 @@ export class IgxGridFilteringRowComponent implements AfterViewInit, OnDestroy {
8181

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

8787
public set value(val) {
88-
if (!val && val !== 0 && this.expression.searchVal) {
88+
if (!val && val !== 0 && (this.expression.searchVal || this.expression.searchVal === 0)) {
8989
this.expression.searchVal = null;
90+
this._value = null;
9091
const index = this.expressionsList.findIndex(item => item.expression === this.expression);
9192
if (index === 0 && this.expressionsList.length === 1) {
9293
this.filteringService.clearFilter(this.column.field);
@@ -98,11 +99,15 @@ export class IgxGridFilteringRowComponent implements AfterViewInit, OnDestroy {
9899
return;
99100
}
100101
} else {
102+
if (val === '') {
103+
return;
104+
}
101105
const oldValue = this.expression.searchVal;
102106
if (isEqual(oldValue, val)) {
103107
return;
104108
}
105109

110+
this._value = val;
106111
this.expression.searchVal = DataUtil.parseValue(this.column.dataType, val);
107112
if (this.expressionsList.find(item => item.expression === this.expression) === undefined) {
108113
this.addExpression(true);
@@ -194,6 +199,7 @@ export class IgxGridFilteringRowComponent implements AfterViewInit, OnDestroy {
194199
private isKeyPressed = false;
195200
private isComposing = false;
196201
private _cancelChipClick = false;
202+
private _value = null;
197203
private _icons = [
198204
{
199205
name: 'clear',
@@ -289,6 +295,7 @@ export class IgxGridFilteringRowComponent implements AfterViewInit, OnDestroy {
289295
const selectedItem = this.expressionsList.find(expr => expr.isSelected === true);
290296
if (selectedItem) {
291297
this.expression = selectedItem.expression;
298+
this._value = this.expression.searchVal;
292299
}
293300

294301
this.filteringService.grid.localeChange
@@ -512,6 +519,7 @@ export class IgxGridFilteringRowComponent implements AfterViewInit, OnDestroy {
512519
this.removeExpression(indexToDeselect, this.expression);
513520
}
514521
this.resetExpression();
522+
this._value = this.expression.searchVal;
515523
this.scrollChipsWhenAddingExpression();
516524
}
517525

@@ -685,7 +693,7 @@ export class IgxGridFilteringRowComponent implements AfterViewInit, OnDestroy {
685693
item.isSelected = !item.isSelected;
686694
if (item.isSelected) {
687695
this.expression = item.expression;
688-
696+
this._value = this.expression.searchVal;
689697
this.focusEditElement();
690698
}
691699
}

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
@@ -42,7 +42,8 @@ import {
4242
IgxGridExternalESFComponent,
4343
IgxGridExternalESFTemplateComponent,
4444
IgxGridDatesFilteringComponent,
45-
LoadOnDemandFilterStrategy
45+
LoadOnDemandFilterStrategy,
46+
IgxGridFilteringNumericComponent
4647
} from '../../test-utils/grid-samples.spec';
4748
import { GridSelectionMode, FilterMode, Size } from '../common/enums';
4849
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

@@ -2755,12 +2789,12 @@ describe('IgxGrid - Filtering Row UI actions #grid', () => {
27552789
it('Should not prevent mousedown event when target is within the filter cell template', fakeAsync(() => {
27562790
const filterCell = GridFunctions.getFilterCell(fix, 'ProductName');
27572791
const input = filterCell.query(By.css('input')).nativeElement;
2758-
2792+
27592793
const mousedownEvent = new MouseEvent('mousedown', { bubbles: true });
27602794
const preventDefaultSpy = spyOn(mousedownEvent, 'preventDefault');
27612795
input.dispatchEvent(mousedownEvent, { bubbles: true });
27622796
fix.detectChanges();
2763-
2797+
27642798
expect(preventDefaultSpy).not.toHaveBeenCalled();
27652799
}));
27662800

@@ -2772,7 +2806,7 @@ describe('IgxGrid - Filtering Row UI actions #grid', () => {
27722806
const preventDefaultSpy = spyOn(mousedownEvent, 'preventDefault');
27732807
firstCell.dispatchEvent(mousedownEvent);
27742808
fix.detectChanges();
2775-
2809+
27762810
expect(preventDefaultSpy).toHaveBeenCalled();
27772811
}));
27782812
});

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)