Skip to content

Commit 4d3309c

Browse files
authored
feat(grid): enable the developer to cancel pinning/unpinning row (#10350)
* feat(grid): enable the developer to cancel pinning/unpinning row * feat(grid): add test for cancel pin/unpin
1 parent bcb9483 commit 4d3309c

File tree

3 files changed

+75
-7
lines changed

3 files changed

+75
-7
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ export interface IRowToggleEventArgs extends IBaseEventArgs {
163163
/**
164164
* Event emitted when a row's pin state changes.
165165
*/
166-
export interface IPinRowEventArgs extends IBaseEventArgs {
166+
export interface IPinRowEventArgs extends IBaseEventArgs, CancelableEventArgs {
167167
/**
168168
* The ID of the row, that was pinned/unpinned.
169169
* ID is either the primaryKey value or the data record instance.
@@ -172,7 +172,7 @@ export interface IPinRowEventArgs extends IBaseEventArgs {
172172
row?: RowType;
173173
/** The index at which to pin the row in the pinned rows collection. */
174174
insertAtIndex?: number;
175-
/** Whether or noy the row is pinned or unpinned. */
175+
/** Whether or not the row is pinned or unpinned. */
176176
readonly isPinned: boolean;
177177
}
178178

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4652,10 +4652,15 @@ export abstract class IgxGridBaseDirective extends DisplayDensityBase implements
46524652
insertAtIndex: index,
46534653
isPinned: true,
46544654
rowID,
4655-
row
4655+
row,
4656+
cancel: false
46564657
};
46574658
this.rowPinning.emit(eventArgs);
46584659

4660+
if (eventArgs.cancel) {
4661+
return;
4662+
}
4663+
46594664
this.crudService.endEdit(false);
46604665

46614666
const insertIndex = typeof eventArgs.insertAtIndex === 'number' ? eventArgs.insertAtIndex : this._pinnedRecordIDs.length;
@@ -4688,10 +4693,15 @@ export abstract class IgxGridBaseDirective extends DisplayDensityBase implements
46884693
const eventArgs: IPinRowEventArgs = {
46894694
isPinned: false,
46904695
rowID,
4691-
row
4696+
row,
4697+
cancel: false
46924698
};
46934699
this.rowPinning.emit(eventArgs);
46944700

4701+
if (eventArgs.cancel) {
4702+
return;
4703+
}
4704+
46954705
this.crudService.endEdit(false);
46964706
this._pinnedRecordIDs.splice(index, 1);
46974707
this.pipeTrigger++;

projects/igniteui-angular/src/lib/grids/grid/grid-row-pinning.spec.ts

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { TestBed, fakeAsync, tick } from '@angular/core/testing';
33
import { By } from '@angular/platform-browser';
44
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
55
import { IgxGridComponent } from './grid.component';
6-
import { IgxGridModule } from './public_api';
6+
import { IgxGridModule, IPinRowEventArgs } from './public_api';
77
import { configureTestSuite } from '../../test-utils/configure-suite';
88
import { ColumnPinningPosition, RowPinningPosition } from '../common/enums';
99
import { IPinningConfig } from '../grid.common';
@@ -164,7 +164,8 @@ describe('Row Pinning #grid', () => {
164164
rowID,
165165
insertAtIndex: 0,
166166
isPinned: true,
167-
row
167+
row,
168+
cancel: false
168169
});
169170

170171
row = grid.getRowByIndex(0);
@@ -190,7 +191,8 @@ describe('Row Pinning #grid', () => {
190191
rowID,
191192
insertAtIndex: 0,
192193
isPinned: true,
193-
row
194+
row,
195+
cancel: false
194196
});
195197

196198
row.unpin();
@@ -200,6 +202,62 @@ describe('Row Pinning #grid', () => {
200202
expect(grid.rowPinned.emit).toHaveBeenCalledTimes(2);
201203
});
202204

205+
it(`Should be able to cancel rowPinning on pin/unpin event.`, () => {
206+
spyOn(grid.rowPinning, 'emit').and.callThrough();
207+
let sub = grid.rowPinning.subscribe((e: IPinRowEventArgs) => {
208+
e.cancel = true;
209+
});
210+
211+
const row = grid.getRowByIndex(0);
212+
const rowID = row.key;
213+
expect(row.pinned).toBeFalsy();
214+
215+
row.pin();
216+
fix.detectChanges();
217+
218+
expect(grid.rowPinning.emit).toHaveBeenCalledTimes(1);
219+
expect(grid.rowPinning.emit).toHaveBeenCalledWith({
220+
insertAtIndex: 0,
221+
isPinned: true,
222+
rowID,
223+
row,
224+
cancel: true
225+
});
226+
expect(row.pinned).toBeFalsy();
227+
228+
sub.unsubscribe();
229+
230+
row.pin();
231+
fix.detectChanges();
232+
233+
expect(grid.rowPinning.emit).toHaveBeenCalledTimes(2);
234+
expect(grid.rowPinning.emit).toHaveBeenCalledWith({
235+
insertAtIndex: 0,
236+
isPinned: true,
237+
rowID,
238+
row,
239+
cancel: false
240+
});
241+
expect(row.pinned).toBe(true);
242+
243+
sub = grid.rowPinning.subscribe((e: IPinRowEventArgs) => {
244+
e.cancel = true;
245+
});
246+
247+
row.unpin();
248+
fix.detectChanges();
249+
250+
expect(grid.rowPinning.emit).toHaveBeenCalledTimes(3);
251+
expect(grid.rowPinning.emit).toHaveBeenCalledWith({
252+
isPinned: false,
253+
rowID,
254+
row,
255+
cancel: true
256+
});
257+
expect(row.pinned).toBe(true);
258+
sub.unsubscribe();
259+
});
260+
203261
it('should pin/unpin via grid API methods.', () => {
204262
// pin 2nd row
205263
grid.pinRow(fix.componentInstance.data[1]);

0 commit comments

Comments
 (0)