Skip to content

Commit 37578ef

Browse files
ChronosSFIMinchev64kdinev
authored
feat(grid): make contextMenu fire on row area without cells (#13487) (#14087)
* feat(grid): make contextMenu fire on row area without cells * docs(changelog): changing the changelog to better explain the new change --------- Co-authored-by: IMinchev <[email protected]> Co-authored-by: Konstantin Dinev <[email protected]>
1 parent 9d0b490 commit 37578ef

File tree

9 files changed

+64
-26
lines changed

9 files changed

+64
-26
lines changed

Diff for: CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ All notable changes for each version of this project will be documented in this
3030
- `IgxGrid`, `IgxTreeGrid`, `IgxHierarchicalGrid`
3131
- Enhanced the advanced filtering to emit the `filtering` event when filters are applied.
3232

33+
### General
34+
- `IgxGrid`, `IgxTreeGrid`, `IgxHierarchicalGrid`
35+
- The `contextMenu` event now fires when the end-user clicks to the right of the right-most cell in the grid in case the grid's columns don't span its full width. For this reason the event argument of the event is now of type `IGridContextMenuEventArgs` which contains the row object as well as the cell one. The latter will be `null` if the event didn't originate from a cell. **This is not a breaking change** as the new type extends the old.
36+
3337
## 17.1.0
3438
### New Features
3539
- `IgxGrid`, `IgxTreeGrid`, `IgxHierarchicalGrid`

Diff for: projects/igniteui-angular/src/lib/grids/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ A list of the events emitted by the **igx-grid**:
238238
|`dataPreLoad`| Emitted when a new chunk of data is loaded from virtualization. |
239239
|`columnPin`|Emitted when a column is pinned or unpinned through the grid API. The index that the column is inserted at may be changed through the `insertAtIndex` property. Use `isPinned` to check whether the column is pinned or unpinned.|
240240
|`columnResized`|Emitted when a column is resized. Returns the column object, previous and new column width.|
241-
|`contextMenu`|Emitted when a cell is right clicked. Returns the cell object.|
241+
|`contextMenu`|Emitted when a cell or row is right clicked. Returns the cell or row object.|
242242
|`doubleClick`|Emitted when a cell is double clicked. Returns the cell object.|
243243
|`columnVisibilityChanged`| Emitted when `IgxColumnComponent` visibility is changed. Args: { column: any, newValue: boolean } |
244244
|`groupingDone`|Emitted when the grouping state changes as a result of grouping columns, ungrouping columns or a combination of both. Provides an array of `ISortingExpression`, an array of the **newly** grouped columns as `IgxColumnComponent` references and an array of the **newly** ungrouped columns as `IgxColumnComponent` references.|

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

-12
Original file line numberDiff line numberDiff line change
@@ -834,18 +834,6 @@ export class IgxGridCellComponent implements OnInit, OnChanges, OnDestroy, CellT
834834
});
835835
}
836836

837-
/**
838-
* @hidden
839-
* @internal
840-
*/
841-
@HostListener('contextmenu', ['$event'])
842-
public onContextMenu(event: MouseEvent) {
843-
this.grid.contextMenu.emit({
844-
cell: this.getCellType(),
845-
event
846-
});
847-
}
848-
849837
/**
850838
* @hidden
851839
* @internal

Diff for: projects/igniteui-angular/src/lib/grids/common/events.ts

+3
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ export interface IGridRowEventArgs extends IBaseEventArgs {
4141
event: Event;
4242
}
4343

44+
/** Represents an event argument for the grid contextMenu output */
45+
export interface IGridContextMenuEventArgs extends IGridCellEventArgs, IGridRowEventArgs {}
46+
4447
/** Represents event arguments related to grid editing completion. */
4548
export interface IGridEditDoneEventArgs extends IBaseEventArgs {
4649
/**

Diff for: projects/igniteui-angular/src/lib/grids/common/grid.interface.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import {
77
IColumnMovingEventArgs, IPinColumnEventArgs,
88
IActiveNodeChangeEventArgs,
99
ICellPosition, IFilteringEventArgs, IColumnResizeEventArgs, IRowToggleEventArgs, IGridToolbarExportEventArgs, IPinRowEventArgs,
10-
IGridRowEventArgs, IGridEditEventArgs, IRowDataCancelableEventArgs, IGridEditDoneEventArgs
10+
IGridRowEventArgs, IGridEditEventArgs, IRowDataCancelableEventArgs, IGridEditDoneEventArgs,
11+
IGridContextMenuEventArgs
1112
} from '../common/events';
1213
import { DisplayDensity, IDensityChangedEventArgs } from '../../core/density';
1314
import { ChangeDetectorRef, ElementRef, EventEmitter, InjectionToken, QueryList, TemplateRef, ViewContainerRef } from '@angular/core';
@@ -1028,7 +1029,7 @@ export interface GridType extends IGridDataBindable {
10281029
cellClick: EventEmitter<IGridCellEventArgs>;
10291030
rowClick: EventEmitter<IGridRowEventArgs>;
10301031
doubleClick: EventEmitter<IGridCellEventArgs>;
1031-
contextMenu: EventEmitter<IGridCellEventArgs>;
1032+
contextMenu: EventEmitter<IGridContextMenuEventArgs>;
10321033
selected: EventEmitter<IGridCellEventArgs>;
10331034
rangeSelected: EventEmitter<GridSelectionRange>;
10341035
rowSelectionChanging: EventEmitter<IRowSelectionEventArgs>;

Diff for: projects/igniteui-angular/src/lib/grids/grid-base.directive.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,8 @@ import {
121121
IGridEditEventArgs,
122122
IRowDataCancelableEventArgs,
123123
IGridEditDoneEventArgs,
124-
IGridRowEventArgs
124+
IGridRowEventArgs,
125+
IGridContextMenuEventArgs
125126
} from './common/events';
126127
import { IgxAdvancedFilteringDialogComponent } from './filtering/advanced-filtering/advanced-filtering-dialog.component';
127128
import {
@@ -836,16 +837,16 @@ export abstract class IgxGridBaseDirective extends DisplayDensityBase implements
836837
public columnResized = new EventEmitter<IColumnResizeEventArgs>();
837838

838839
/**
839-
* Emitted when a cell is right clicked.
840+
* Emitted when a cell or row is right clicked.
840841
*
841842
* @remarks
842-
* Returns the `IgxGridCell` object.
843+
* Returns the `IgxGridCell` object if the immediate context menu target is a cell or an `IgxGridRow` otherwise.
843844
* ```html
844845
* <igx-grid #grid [data]="localData" (contextMenu)="contextMenu($event)" [autoGenerate]="true"></igx-grid>
845846
* ```
846847
*/
847848
@Output()
848-
public contextMenu = new EventEmitter<IGridCellEventArgs>();
849+
public contextMenu = new EventEmitter<IGridContextMenuEventArgs>();
849850

850851
/**
851852
* Emitted when a cell is double clicked.

Diff for: projects/igniteui-angular/src/lib/grids/grid/cell.spec.ts

+5-6
Original file line numberDiff line numberDiff line change
@@ -124,16 +124,15 @@ describe('IgxGrid - Cell component #grid', () => {
124124

125125
it('Should trigger contextMenu event when right click into cell', () => {
126126
spyOn(grid.contextMenu, 'emit').and.callThrough();
127-
const event = new Event('contextmenu');
127+
const event = new Event('contextmenu', { bubbles: true });
128128
cellElem.nativeElement.dispatchEvent(event);
129-
const args: IGridCellEventArgs = {
130-
cell: grid.getCellByColumn(0, 'ID'),
131-
event
132-
};
133129

134130
fix.detectChanges();
135131
expect(grid.contextMenu.emit).toHaveBeenCalledTimes(1);
136-
expect(grid.contextMenu.emit).toHaveBeenCalledWith(args);
132+
expect(grid.contextMenu.emit).toHaveBeenCalledWith(jasmine.objectContaining({
133+
cell: jasmine.anything(),
134+
row: jasmine.anything()
135+
}));
137136
});
138137

139138
it('Should trigger doubleClick event when double click into cell', () => {

Diff for: projects/igniteui-angular/src/lib/grids/grid/grid.component.spec.ts

+29-1
Original file line numberDiff line numberDiff line change
@@ -2083,7 +2083,35 @@ describe('IgxGrid Component Tests #grid', () => {
20832083
fix.detectChanges();
20842084
expect(grid.rowClick.emit).toHaveBeenCalledTimes(2);
20852085
expect(grid.rowClick.emit).toHaveBeenCalledWith(args);
2086-
})
2086+
});
2087+
2088+
it('Should emit contextMenu when clicking outside of the columns area', () => {
2089+
const fix = TestBed.createComponent(IgxGridDefaultRenderingComponent);
2090+
fix.componentInstance.initColumnsRows(5, 5);
2091+
//fix.componentInstance.columns.forEach(c => c.width = '100px');
2092+
fix.componentInstance.grid.width = '900px';
2093+
fix.detectChanges();
2094+
const grid = fix.componentInstance.grid;
2095+
grid.columnList.forEach(c => c.width = '100px');
2096+
fix.detectChanges();
2097+
const spy = spyOn(grid.contextMenu, 'emit').and.callThrough();
2098+
const event = new Event('contextmenu', { bubbles: true });
2099+
const row = grid.rowList.get(0);
2100+
const cell = row.cells.get(0);
2101+
cell.nativeElement.dispatchEvent(event);
2102+
fix.detectChanges();
2103+
expect(grid.contextMenu.emit).toHaveBeenCalledTimes(1);
2104+
expect(grid.contextMenu.emit).toHaveBeenCalledWith(jasmine.objectContaining({
2105+
cell: jasmine.anything()
2106+
}));
2107+
spy.calls.reset();
2108+
row.nativeElement.dispatchEvent(event);
2109+
fix.detectChanges();
2110+
expect(grid.contextMenu.emit).toHaveBeenCalledTimes(1);
2111+
expect(grid.contextMenu.emit).toHaveBeenCalledWith(jasmine.objectContaining({
2112+
row: jasmine.anything()
2113+
}));
2114+
});
20872115

20882116
it(`Verify that getRowData returns correct data`, () => {
20892117
const fix = TestBed.createComponent(IgxGridDefaultRenderingComponent);

Diff for: projects/igniteui-angular/src/lib/grids/row.directive.ts

+14
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,20 @@ export class IgxRowDirective implements DoCheck, AfterViewInit, OnDestroy {
411411
}
412412
}
413413

414+
/**
415+
* @hidden
416+
* @internal
417+
*/
418+
@HostListener('contextmenu', ['$event'])
419+
public onContextMenu(event: MouseEvent) {
420+
const cell = (event.target as HTMLElement).closest('.igx-grid__td');
421+
this.grid.contextMenu.emit({
422+
row: this,
423+
cell: this.cells.find(c => c.nativeElement === cell),
424+
event
425+
});
426+
}
427+
414428
/**
415429
* @hidden
416430
* @internal

0 commit comments

Comments
 (0)