Skip to content

Commit 82ba377

Browse files
authored
feat: add option to cancel Row Detail opening (#1125)
- user can now prevent event and return false on `onBeforeRowDetailToggle` event to cancel opening certain Row Detail, for example the code below would block opening Row Detail when its id is `1`
1 parent fdc79a8 commit 82ba377

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

packages/row-detail-view-plugin/src/slickRowDetailView.spec.ts

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,10 +306,40 @@ describe('SlickRowDetailView plugin', () => {
306306
expect(stopPropagationSpy).not.toHaveBeenCalled();
307307
});
308308

309-
it('should trigger onClick and ', () => {
309+
it('should trigger onClick and NOT expect Row Detail to be toggled when onBeforeRowDetailToggle returns false', () => {
310+
const mockProcess = jest.fn();
311+
const expandDetailViewSpy = jest.spyOn(plugin, 'expandDetailView');
312+
const onBeforeSlickEventData = new Slick.EventData();
313+
jest.spyOn(onBeforeSlickEventData, 'getReturnValue').mockReturnValue(false);
314+
const beforeRowDetailToggleSpy = jest.spyOn(plugin.onBeforeRowDetailToggle, 'notify').mockReturnValueOnce(onBeforeSlickEventData);
315+
const afterRowDetailToggleSpy = jest.spyOn(plugin.onAfterRowDetailToggle, 'notify');
316+
const itemMock = { id: 123, firstName: 'John', lastName: 'Doe', _collapsed: true };
317+
const detailView = `<span>loading...</span>`;
318+
jest.spyOn(gridStub.getEditorLock(), 'isActive').mockReturnValue(false);
319+
jest.spyOn(gridStub.getEditorLock(), 'commitCurrentEdit').mockReturnValue(true);
320+
jest.spyOn(gridStub, 'getDataItem').mockReturnValue(itemMock);
321+
jest.spyOn(gridStub, 'getColumns').mockReturnValue(mockColumns);
322+
jest.spyOn(gridStub, 'getOptions').mockReturnValue({ ...gridOptionsMock, rowDetailView: { process: mockProcess, columnIndexPosition: 0, useRowClick: true, maxRows: 2, panelRows: 2 } as any });
323+
324+
plugin.init(gridStub);
325+
plugin.onAsyncResponse.notify({ item: itemMock, itemDetail: itemMock, detailView, }, new Slick.EventData());
326+
327+
const clickEvent = new Event('click');
328+
Object.defineProperty(clickEvent, 'target', { writable: true, configurable: true, value: document.createElement('div') });
329+
Object.defineProperty(clickEvent, 'isPropagationStopped', { writable: true, configurable: true, value: jest.fn() });
330+
Object.defineProperty(clickEvent, 'isImmediatePropagationStopped', { writable: true, configurable: true, value: jest.fn() });
331+
gridStub.onClick.notify({ row: 0, cell: 1, grid: gridStub }, clickEvent);
332+
333+
expect(beforeRowDetailToggleSpy).toHaveBeenCalled();
334+
expect(afterRowDetailToggleSpy).not.toHaveBeenCalled();
335+
expect(expandDetailViewSpy).not.toHaveBeenCalled();
336+
});
337+
338+
it('should trigger onClick and expect Row Detail to be toggled', () => {
310339
const mockProcess = jest.fn();
311340
const expandDetailViewSpy = jest.spyOn(plugin, 'expandDetailView');
312341
const beforeRowDetailToggleSpy = jest.spyOn(plugin.onBeforeRowDetailToggle, 'notify');
342+
const afterRowDetailToggleSpy = jest.spyOn(plugin.onAfterRowDetailToggle, 'notify');
313343
const itemMock = { id: 123, firstName: 'John', lastName: 'Doe', _collapsed: true };
314344
const detailView = `<span>loading...</span>`;
315345
jest.spyOn(gridStub.getEditorLock(), 'isActive').mockReturnValue(false);
@@ -328,6 +358,7 @@ describe('SlickRowDetailView plugin', () => {
328358
gridStub.onClick.notify({ row: 0, cell: 1, grid: gridStub }, clickEvent);
329359

330360
expect(beforeRowDetailToggleSpy).toHaveBeenCalled();
361+
expect(afterRowDetailToggleSpy).toHaveBeenCalled();
331362
expect(expandDetailViewSpy).toHaveBeenCalledWith({
332363
_collapsed: false, _detailContent: undefined, _detailViewLoaded: true,
333364
_height: 75, _sizePadding: 3, firstName: 'John', id: 123, lastName: 'Doe'
@@ -410,7 +441,7 @@ describe('SlickRowDetailView plugin', () => {
410441
_collapsed: true, _detailViewLoaded: true, _sizePadding: 0, _height: 150, _detailContent: '<span>loading...</span>',
411442
id: 123, firstName: 'John', lastName: 'Doe',
412443
}
413-
});
444+
}, expect.anything(), expect.anything());
414445
expect(afterRowDetailToggleSpy).toHaveBeenCalledWith({
415446
grid: gridStub,
416447
item: {

packages/row-detail-view-plugin/src/slickRowDetailView.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -682,10 +682,10 @@ export class SlickRowDetailView implements ExternalResource, UniversalRowDetailV
682682
}
683683

684684
// trigger an event before toggling
685-
this.onBeforeRowDetailToggle.notify({
686-
grid: this._grid,
687-
item: dataContext
688-
});
685+
// user could cancel the Row Detail opening when event is returning false
686+
if (this.onBeforeRowDetailToggle.notify({ grid: this._grid, item: dataContext }, e, this).getReturnValue() === false) {
687+
return;
688+
}
689689

690690
this.toggleRowSelection(args.row, dataContext);
691691

0 commit comments

Comments
 (0)