Skip to content

Commit a39b7b4

Browse files
Ivan KitanovIvan Kitanov
authored andcommitted
fix(grid): Preventing rangeSelection from element inside cellTemplate
1 parent fe6d9d0 commit a39b7b4

File tree

3 files changed

+43
-5
lines changed

3 files changed

+43
-5
lines changed

projects/igniteui-angular/src/lib/grids/grid/grid-cell-selection.spec.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import {
55
SelectionWithScrollsComponent,
66
SelectionWithTransactionsComponent,
77
CellSelectionNoneComponent,
8-
CellSelectionSingleComponent
8+
CellSelectionSingleComponent,
9+
IgxGridCellTemplateForRangeSelectionComponent
910
} from '../../test-utils/grid-samples.spec';
1011
import { IgxStringFilteringOperand } from '../../data-operations/filtering-condition';
1112
import { UIInteractions, wait } from '../../test-utils/ui-interactions.spec';
@@ -26,7 +27,8 @@ describe('IgxGrid - Cell selection #grid', () => {
2627
SelectionWithScrollsComponent,
2728
SelectionWithTransactionsComponent,
2829
CellSelectionNoneComponent,
29-
CellSelectionSingleComponent
30+
CellSelectionSingleComponent,
31+
IgxGridCellTemplateForRangeSelectionComponent
3032
]
3133
}).compileComponents();
3234
}));
@@ -256,6 +258,23 @@ describe('IgxGrid - Cell selection #grid', () => {
256258
expect(grid.selectedCells.length).toBe(1);
257259
});
258260

261+
it('Should not trigger range selection when CellTemplate is used and the user clicks on element inside it',()=>{
262+
fix = TestBed.createComponent(IgxGridCellTemplateForRangeSelectionComponent);
263+
fix.detectChanges();
264+
grid = fix.componentInstance.grid;
265+
detect = () => grid.cdr.detectChanges();
266+
267+
const selectionChangeSpy = spyOn<any>(grid.rangeSelected, 'emit').and.callThrough();
268+
const cell = grid.gridAPI.get_cell_by_index(1, 'ProductID');
269+
const cellElement = cell.nativeElement;
270+
const span = cellElement.querySelector('span');
271+
272+
expect(span).not.toBeNull();
273+
span.click();
274+
fix.detectChanges();
275+
expect(selectionChangeSpy).not.toHaveBeenCalled();
276+
});
277+
259278
it('Should be able to select range when click on a cell and hold Shift key and click on another Cell', () => {
260279
const firstCell = grid.gridAPI.get_cell_by_index(3, 'HireDate');
261280
const secondCell = grid.gridAPI.get_cell_by_index(1, 'ID');

projects/igniteui-angular/src/lib/grids/selection/selection.service.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,7 @@ export class IgxGridSelectionService {
637637
if (this.areEqualCollections(currSelection, newSelection)) {
638638
return;
639639
}
640-
640+
641641
const args: IRowSelectionEventArgs = {
642642
owner: this.grid,
643643
oldSelection: currSelection,
@@ -857,8 +857,7 @@ export class IgxGridSelectionService {
857857
this.pointerEventInGridBody = false;
858858
this.grid.document.body.removeEventListener('pointerup', this.pointerOriginHandler);
859859

860-
const targetTagName = event.target.tagName.toLowerCase();
861-
if (targetTagName !== 'igx-grid-cell' && targetTagName !== 'igx-tree-grid-cell') {
860+
if (!event.target.closest('igx-grid-cell') && !event.target.closest('igx-tree-grid-cell')) {
862861
this.pointerUp(this._lastSelectedNode, this.grid.rangeSelected, true);
863862
}
864863
};

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1414,6 +1414,26 @@ export class IgxGridRowEditingWithoutEditableColumnsComponent extends BasicGridC
14141414
public override data = SampleTestData.foodProductData();
14151415
}
14161416

1417+
@Component({
1418+
template: `
1419+
<igx-grid #grid [data]="data" [primaryKey]="'ProductID'" width="700px" height="400px" [cellSelection]="'multiple'">
1420+
<igx-column field="ProductID" header="Product ID">
1421+
<ng-template igxCell let-cell="cell" let-val>
1422+
<span style="background-color: red;">val</span>
1423+
<br>
1424+
{{val}}
1425+
</ng-template>
1426+
</igx-column>
1427+
<igx-column field="ReorderLevel" header="Reorder Lever" [dataType]="'number'" [editable]="true" width="100px"></igx-column>
1428+
<igx-column field="ProductName" header="Product Name" [dataType]="'string'" width="150px"></igx-column>
1429+
<igx-column field="OrderDate" header="Order Date" [dataType]="'date'" width="150px" [editable]="false"></igx-column>
1430+
</igx-grid>`,
1431+
imports: [IgxGridComponent, IgxColumnComponent, IgxCellTemplateDirective]
1432+
})
1433+
export class IgxGridCellTemplateForRangeSelectionComponent extends BasicGridComponent {
1434+
public override data = SampleTestData.foodProductData();
1435+
}
1436+
14171437
@Component({
14181438
template: `
14191439
<igx-grid #grid [data]="data" [primaryKey]="'ID'" width="700px" height="400px" [rowEditable]="true" [moving]="true">

0 commit comments

Comments
 (0)