Skip to content

Commit 9de7c18

Browse files
committed
feat(grid): add deselect for single selection
1 parent adc5e24 commit 9de7c18

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1036,7 +1036,12 @@ export class IgxGridCellComponent implements OnInit, OnChanges, OnDestroy, CellT
10361036

10371037
if (this.selectionService.primaryButton) {
10381038
const currentActive = this.selectionService.activeElement;
1039-
this.selectionService.activeElement = node;
1039+
if (this.cellSelectionMode === GridSelectionMode.single && (event as any).ctrlKey && this.selected) {
1040+
this.selectionService.activeElement = null;
1041+
shouldEmitSelection = true;
1042+
} else {
1043+
this.selectionService.activeElement = node;
1044+
}
10401045
const cancel = this._updateCRUDStatus(event);
10411046
if (cancel) {
10421047
this.selectionService.activeElement = currentActive;

projects/igniteui-angular/src/lib/grids/grid-navigation.service.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,11 @@ export class IgxGridNavigationService {
136136
this.activeNode = null;
137137
return;
138138
}
139-
if ((!this.activeNode || !Object.keys(this.activeNode).length || this.activeNode.row < 0 || this.activeNode.row > gridRows - 1) &&
140-
this.grid.selectionService.isInMap(this.lastActiveNode)) {
139+
if (!this.activeNode || !Object.keys(this.activeNode).length || this.activeNode.row < 0 || this.activeNode.row > gridRows - 1) {
141140
const hasLastActiveNode = Object.keys(this.lastActiveNode).length;
141+
if (hasLastActiveNode && this.grid.cellSelection !== GridSelectionMode.none && !this.grid.selectionService.isInMap(this.lastActiveNode)) {
142+
return;
143+
}
142144
const shouldClearSelection = hasLastActiveNode && (this.lastActiveNode.row < 0 || this.lastActiveNode.row > gridRows - 1);
143145
this.setActiveNode(this.lastActiveNode.row >= 0 && this.lastActiveNode.row < gridRows ?
144146
this.firstVisibleNode(this.lastActiveNode.row) : this.firstVisibleNode());

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3154,6 +3154,25 @@ describe('IgxGrid - Cell selection #grid', () => {
31543154
GridSelectionFunctions.verifySelectedRange(grid, 0, 0, 0, 0);
31553155
});
31563156

3157+
it('Should deselect a selected cell with Ctrl + click', () => {
3158+
const selectionChangeSpy = spyOn<any>(grid.selected, 'emit').and.callThrough();
3159+
const firstCell = grid.gridAPI.get_cell_by_index(1, 'ParentID');
3160+
3161+
// Click on a cell
3162+
UIInteractions.simulateClickAndSelectEvent(firstCell);
3163+
fix.detectChanges();
3164+
GridSelectionFunctions.verifyCellSelected(firstCell);
3165+
expect(grid.selectedCells.length).toBe(1);
3166+
expect(selectionChangeSpy).toHaveBeenCalledTimes(1);
3167+
3168+
// Click on same cell holding Ctrl
3169+
UIInteractions.simulateClickAndSelectEvent(firstCell, false, true);
3170+
fix.detectChanges();
3171+
GridSelectionFunctions.verifyCellSelected(firstCell, false);
3172+
expect(grid.selectedCells.length).toBe(0);
3173+
expect(selectionChangeSpy).toHaveBeenCalledTimes(2);
3174+
});
3175+
31573176
it('When when navigate with arrow keys cell selection should be changed', () => {
31583177
const selectionChangeSpy = spyOn<any>(grid.selected, 'emit').and.callThrough();
31593178
let cell = grid.gridAPI.get_cell_by_index(1, 'Name');

0 commit comments

Comments
 (0)