Skip to content

Commit b9f5eb1

Browse files
authored
Merge branch 'master' into ddincheva/fixIssue5551-master
2 parents 2aa9713 + 2822fe3 commit b9f5eb1

File tree

4 files changed

+63
-17
lines changed

4 files changed

+63
-17
lines changed

projects/igniteui-angular/src/lib/core/grid-selection.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -272,11 +272,11 @@ export class IgxGridSelectionService {
272272
* Adds a single node.
273273
* Single clicks | Ctrl + single clicks on cells is the usual case.
274274
*/
275-
add(node: ISelectionNode): void {
275+
add(node: ISelectionNode, addToRange = true): void {
276276
this.selection.has(node.row) ? this.selection.get(node.row).add(node.column) :
277277
this.selection.set(node.row, new Set<number>()).get(node.row).add(node.column);
278278

279-
this._ranges.add(JSON.stringify(this.generateRange(node)));
279+
if (addToRange) { this._ranges.add(JSON.stringify(this.generateRange(node))); }
280280
}
281281

282282
/**
@@ -307,10 +307,10 @@ export class IgxGridSelectionService {
307307
return (this.isActiveNode(node) && this.grid.isCellSelectable) || this.isInMap(node);
308308
}
309309

310-
isActiveNode(node: ISelectionNode, mrl = false): boolean {
310+
isActiveNode(node: ISelectionNode): boolean {
311311
if (this.activeElement) {
312312
const isActive = this.activeElement.column === node.column && this.activeElement.row === node.row;
313-
if (mrl) {
313+
if (this.grid.hasColumnLayouts) {
314314
const layout = this.activeElement.layout;
315315
return isActive && this.isActiveLayout(layout, node.layout);
316316
}

projects/igniteui-angular/src/lib/grids/cell.component.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -802,11 +802,7 @@ export class IgxGridCellComponent implements OnInit, OnChanges, OnDestroy {
802802
this.focused = true;
803803
this.row.focused = true;
804804
const node = this.selectionNode;
805-
const mrl = this.grid.hasColumnLayouts;
806-
807-
if (this.grid.isCellSelectable && !this.selectionService.isActiveNode(node, mrl)) {
808-
this.grid.onSelection.emit({ cell: this, event });
809-
}
805+
const shouldEmitSelection = !this.selectionService.isActiveNode(node);
810806

811807
if (this.selectionService.primaryButton) {
812808
this._updateCRUDStatus();
@@ -819,9 +815,13 @@ export class IgxGridCellComponent implements OnInit, OnChanges, OnDestroy {
819815
}
820816

821817
this.selectionService.primaryButton = true;
822-
if (this.cellSelectionMode === GridSelectionMode.multiple) {
818+
if (this.cellSelectionMode === GridSelectionMode.multiple && this.selectionService.activeElement) {
819+
this.selectionService.add(this.selectionService.activeElement, false); // pointer events handle range generation
823820
this.selectionService.keyboardStateOnFocus(node, this.grid.onRangeSelection, this.nativeElement);
824821
}
822+
if (this.grid.isCellSelectable && shouldEmitSelection) {
823+
this.grid.onSelection.emit({ cell: this, event });
824+
}
825825
}
826826

827827
/**

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -831,6 +831,28 @@ describe('IgxGrid - Cell selection #grid', () => {
831831
expect(grid.getSelectedData()).toEqual([]);
832832
expect(selectionChangeSpy).toHaveBeenCalledTimes(0);
833833
});
834+
835+
it('Should return correct selected data when onSelection event is emitted', () => {
836+
let selectedData = [];
837+
grid.onSelection.subscribe((e) => {
838+
selectedData = grid.getSelectedData();
839+
});
840+
841+
const cell = grid.getCellByColumn(2, 'Name');
842+
UIInteractions.simulateClickAndSelectCellEvent(cell);
843+
fix.detectChanges();
844+
845+
expect(selectedData.length).toBe(1);
846+
expect(selectedData[0]).toEqual({'Name': 'Monica Reyes'});
847+
848+
const idCell = grid.getCellByColumn(1, 'ID');
849+
UIInteractions.simulateClickAndSelectCellEvent(idCell, false, true);
850+
fix.detectChanges();
851+
852+
expect(selectedData.length).toBe(2);
853+
expect(selectedData[0]).toEqual({'Name': 'Monica Reyes'});
854+
expect(selectedData[1]).toEqual({'ID': 957});
855+
});
834856
});
835857

836858
describe('Keyboard navigation', () => {
@@ -3183,6 +3205,25 @@ describe('IgxGrid - Cell selection #grid', () => {
31833205
expect(grid.getSelectedData()).toEqual([]);
31843206
expect(grid.getSelectedRanges()).toEqual([]);
31853207
});
3208+
3209+
it('Should return correct selected data when onSelection event is emitted using mouse click and kb navigation', () => {
3210+
let selectedData = [];
3211+
grid.onSelection.subscribe((e) => {
3212+
selectedData = grid.getSelectedData();
3213+
});
3214+
3215+
const cell = grid.getCellByColumn(2, 'Name');
3216+
UIInteractions.simulateClickAndSelectCellEvent(cell);
3217+
fix.detectChanges();
3218+
expect(selectedData.length).toBe(1);
3219+
expect(selectedData[0]).toEqual({'Name': 'Monica Reyes'});
3220+
3221+
UIInteractions.triggerKeyDownWithBlur('arrowdown', cell.nativeElement, true, false, true);
3222+
fix.detectChanges();
3223+
3224+
expect(selectedData.length).toBe(1);
3225+
expect(selectedData[0]).toEqual({'Name': 'Laurence Johnson'});
3226+
});
31863227
});
31873228

31883229
});

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

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -690,22 +690,24 @@ describe('IgxTreeGrid - Selection #tGrid', () => {
690690
it('should return the correct type of cell when clicking on a cells', () => {
691691
const rows = TreeGridFunctions.getAllRows(fix);
692692
const normalCells = TreeGridFunctions.getNormalCells(rows[0]);
693-
normalCells[0].triggerEventHandler('focus', new Event('focus'));
693+
UIInteractions.simulateClickAndSelectCellEvent(normalCells[0]);
694+
fix.detectChanges();
694695

695696
expect(treeGrid.selectedCells.length).toBe(1);
696697
expect(treeGrid.selectedCells[0] instanceof IgxGridCellComponent).toBe(true);
697698

698699
let treeGridCell = TreeGridFunctions.getTreeCell(rows[0]);
699-
treeGridCell.triggerEventHandler('focus', new Event('focus'));
700+
UIInteractions.simulateClickAndSelectCellEvent(treeGridCell);
701+
fix.detectChanges();
700702

701703
expect(treeGrid.selectedCells.length).toBe(1);
702704
expect(treeGrid.selectedCells[0] instanceof IgxTreeGridCellComponent).toBe(true);
703705

704706
// perform 2 clicks and check selection again
705707
treeGridCell = TreeGridFunctions.getTreeCell(rows[0]);
706-
treeGridCell.triggerEventHandler('focus', new Event('focus'));
708+
UIInteractions.simulateClickAndSelectCellEvent(treeGridCell);
707709
treeGridCell = TreeGridFunctions.getTreeCell(rows[0]);
708-
treeGridCell.triggerEventHandler('focus', new Event('focus'));
710+
UIInteractions.simulateClickAndSelectCellEvent(treeGridCell);
709711
fix.detectChanges();
710712

711713
expect(treeGrid.selectedCells.length).toBe(1);
@@ -717,21 +719,24 @@ describe('IgxTreeGrid - Selection #tGrid', () => {
717719

718720
// level 1
719721
let treeGridCell = TreeGridFunctions.getTreeCell(rows[0]);
720-
treeGridCell.triggerEventHandler('focus', new Event('focus'));
722+
UIInteractions.simulateClickAndSelectCellEvent(treeGridCell);
723+
fix.detectChanges();
721724
expect(treeGrid.selectedCells.length).toBe(1);
722725
expect(treeGrid.selectedCells[0] instanceof IgxGridCellComponent).toBe(true);
723726
expect(treeGrid.selectedCells[0].value).toBe(147);
724727

725728
// level 2
726729
treeGridCell = TreeGridFunctions.getTreeCell(rows[1]);
727-
treeGridCell.triggerEventHandler('focus', new Event('focus'));
730+
UIInteractions.simulateClickAndSelectCellEvent(treeGridCell);
731+
fix.detectChanges();
728732
expect(treeGrid.selectedCells.length).toBe(1);
729733
expect(treeGrid.selectedCells[0] instanceof IgxGridCellComponent).toBe(true);
730734
expect(treeGrid.selectedCells[0].value).toBe(475);
731735

732736
// level 3
733737
treeGridCell = TreeGridFunctions.getTreeCell(rows[2]);
734-
treeGridCell.triggerEventHandler('focus', new Event('focus'));
738+
UIInteractions.simulateClickAndSelectCellEvent(treeGridCell);
739+
fix.detectChanges();
735740
expect(treeGrid.selectedCells.length).toBe(1);
736741
expect(treeGrid.selectedCells[0] instanceof IgxGridCellComponent).toBe(true);
737742
expect(treeGrid.selectedCells[0].value).toBe(957);

0 commit comments

Comments
 (0)