From e7be078d89ccb4ba04db64c57df43e3ebc4b2a8c Mon Sep 17 00:00:00 2001 From: ViktorSlavov Date: Wed, 12 Jun 2019 13:02:23 +0300 Subject: [PATCH 1/3] fix(grid): fix row editing warning to show when !primaryKey, #5010 # Conflicts: # projects/igniteui-angular/src/lib/grids/grid-base.component.ts --- .../src/lib/grids/grid-base.component.ts | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/projects/igniteui-angular/src/lib/grids/grid-base.component.ts b/projects/igniteui-angular/src/lib/grids/grid-base.component.ts index bfa46be5e41..5a89026b164 100644 --- a/projects/igniteui-angular/src/lib/grids/grid-base.component.ts +++ b/projects/igniteui-angular/src/lib/grids/grid-base.component.ts @@ -243,6 +243,7 @@ export abstract class IgxGridBaseComponent extends DisplayDensityBase implements private _locale = null; private _observer: MutationObserver; private _destroyed = false; + private _primaryKey = null; private overlayIDs = []; /** * An accessor that sets the resource strings. @@ -596,11 +597,9 @@ export abstract class IgxGridBaseComponent extends DisplayDensityBase implements * @memberof IgxGridBaseComponent */ set rowEditable(val: boolean) { - if (val && (this.primaryKey === undefined || this.primaryKey === null)) { - console.warn('The grid must have a `primaryKey` specified when using `rowEditable`!'); - } this._rowEditable = val; if (this.gridAPI.grid) { + this.checkEditKey(val); this.refreshGridState(); } } @@ -754,8 +753,16 @@ export abstract class IgxGridBaseComponent extends DisplayDensityBase implements */ @WatchChanges() @Input() - public primaryKey; + public set primaryKey(val: any) { + this._primaryKey = val; + if (this.gridAPI.grid) { + this.checkEditKey(this._rowEditable); + } + } + public get primaryKey(): any { + return this._primaryKey; + } /** * An @Input property that sets the message displayed when there are no records. * ```html @@ -2582,6 +2589,12 @@ export abstract class IgxGridBaseComponent extends DisplayDensityBase implements this.hideOverlays(); } + private checkEditKey(val: boolean): void { + if (val && (this.primaryKey === undefined || this.primaryKey === null)) { + console.warn('The grid must have a `primaryKey` specified when using `rowEditable`!'); + } + } + /** * @hidden * @internal @@ -2697,6 +2710,7 @@ export abstract class IgxGridBaseComponent extends DisplayDensityBase implements this.calcWidth = this._width && this._width.indexOf('%') === -1 ? parseInt(this._width, 10) : 0; this.shouldGenerate = this.autoGenerate; this._scrollWidth = this.getScrollWidth(); + this.checkEditKey(this._rowEditable); } protected setupColumns() { From 265fa9701da16f79697922904c7773f997484bec Mon Sep 17 00:00:00 2001 From: ViktorSlavov Date: Thu, 13 Jun 2019 14:46:15 +0300 Subject: [PATCH 2/3] fix(grid): move row edit warning to crudService.beginRowEdit, #5010 --- .../src/lib/core/grid-selection.ts | 3 +++ .../src/lib/grids/grid-base.component.ts | 19 +------------------ 2 files changed, 4 insertions(+), 18 deletions(-) diff --git a/projects/igniteui-angular/src/lib/core/grid-selection.ts b/projects/igniteui-angular/src/lib/core/grid-selection.ts index 0986efd12bd..6aee9472269 100644 --- a/projects/igniteui-angular/src/lib/core/grid-selection.ts +++ b/projects/igniteui-angular/src/lib/core/grid-selection.ts @@ -126,6 +126,9 @@ export class IgxGridCRUDService { } beginRowEdit() { + if (this.grid.rowEditable && (this.grid.primaryKey === undefined || this.grid.primaryKey === null)) { + console.warn('The grid must have a `primaryKey` specified when using `rowEditable`!'); + } this.row = this.createRow(this.cell); const args = { rowID: this.row.id, diff --git a/projects/igniteui-angular/src/lib/grids/grid-base.component.ts b/projects/igniteui-angular/src/lib/grids/grid-base.component.ts index 24de0f3a69a..6a224cff404 100644 --- a/projects/igniteui-angular/src/lib/grids/grid-base.component.ts +++ b/projects/igniteui-angular/src/lib/grids/grid-base.component.ts @@ -243,7 +243,6 @@ export abstract class IgxGridBaseComponent extends DisplayDensityBase implements private _locale = null; private _observer: MutationObserver; private _destroyed = false; - private _primaryKey = null; private overlayIDs = []; /** * An accessor that sets the resource strings. @@ -599,7 +598,6 @@ export abstract class IgxGridBaseComponent extends DisplayDensityBase implements set rowEditable(val: boolean) { this._rowEditable = val; if (this.gridAPI.grid) { - this.checkEditKey(val); this.refreshGridState(); } } @@ -753,16 +751,8 @@ export abstract class IgxGridBaseComponent extends DisplayDensityBase implements */ @WatchChanges() @Input() - public set primaryKey(val: any) { - this._primaryKey = val; - if (this.gridAPI.grid) { - this.checkEditKey(this._rowEditable); - } - } + public primaryKey; - public get primaryKey(): any { - return this._primaryKey; - } /** * An @Input property that sets the message displayed when there are no records. * ```html @@ -2596,12 +2586,6 @@ export abstract class IgxGridBaseComponent extends DisplayDensityBase implements this.hideOverlays(); } - private checkEditKey(val: boolean): void { - if (val && (this.primaryKey === undefined || this.primaryKey === null)) { - console.warn('The grid must have a `primaryKey` specified when using `rowEditable`!'); - } - } - /** * @hidden * @internal @@ -2717,7 +2701,6 @@ export abstract class IgxGridBaseComponent extends DisplayDensityBase implements this.calcWidth = this._width && this._width.indexOf('%') === -1 ? parseInt(this._width, 10) : 0; this.shouldGenerate = this.autoGenerate; this._scrollWidth = this.getScrollWidth(); - this.checkEditKey(this._rowEditable); } protected setupColumns() { From 460732a3a939f221a475f9f798823aec11fc441c Mon Sep 17 00:00:00 2001 From: ViktorSlavov Date: Thu, 13 Jun 2019 15:52:14 +0300 Subject: [PATCH 3/3] test(grid): rework rowEdit/primaryKey warning test, #5010 --- .../src/lib/grids/grid/grid.component.spec.ts | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/projects/igniteui-angular/src/lib/grids/grid/grid.component.spec.ts b/projects/igniteui-angular/src/lib/grids/grid/grid.component.spec.ts index 0e2aa401eba..73c04e998f0 100644 --- a/projects/igniteui-angular/src/lib/grids/grid/grid.component.spec.ts +++ b/projects/igniteui-angular/src/lib/grids/grid/grid.component.spec.ts @@ -1391,22 +1391,21 @@ describe('IgxGrid Component Tests', () => { grid.rowEditable = true; fix.detectChanges(); tick(16); - expect(console.warn).toHaveBeenCalledWith('The grid must have a `primaryKey` specified when using `rowEditable`!'); - expect(console.warn).toHaveBeenCalledTimes(1); // Throws warinig but still sets the property correctly expect(grid.rowEditable).toBeTruthy(); + const cell = grid.getRowByIndex(2).cells.toArray()[1].nativeElement; + spyOn(grid, 'openRowOverlay'); + cell.dispatchEvent(new Event('focus')); tick(16); fix.detectChanges(); - grid.primaryKey = 'ProductID'; - grid.rowEditable = false; - fix.detectChanges(); + cell.dispatchEvent(new Event('dblclick')); tick(16); - grid.rowEditable = true; fix.detectChanges(); - tick(16); + expect(console.warn).toHaveBeenCalledWith('The grid must have a `primaryKey` specified when using `rowEditable`!'); expect(console.warn).toHaveBeenCalledTimes(1); - expect(grid.rowEditable).toBeTruthy(); + // Still calls openRowOverlay, just logs the warning + expect(grid.openRowOverlay).toHaveBeenCalled(); })); it('Should be able to enter edit mode on dblclick, enter and f2', fakeAsync(() => { const fix = TestBed.createComponent(IgxGridRowEditingComponent);