@@ -6,11 +6,10 @@ import { GridType } from './grid.interface';
66import { Subject } from 'rxjs' ;
77import { isEqual } from '../../core/utils' ;
88
9- export class IgxRow {
9+ export class IgxEditRow {
1010 public transactionState : any ;
1111 public state : any ;
1212 public newData : any ;
13- public isAddRow : boolean ;
1413
1514 constructor ( public id : any , public index : number , public data : any , public grid : IgxGridBaseDirective & GridType ) { }
1615
@@ -21,31 +20,57 @@ export class IgxRow {
2120 oldValue : this . data ,
2221 cancel : false ,
2322 owner : this . grid ,
24- isAddRow : this . isAddRow || false ,
2523 event
2624 } ;
2725 if ( includeNewValue ) {
28- args . newValue = this . newData ;
26+ args . newValue = this . newData ?? this . data ;
2927 }
3028 return args ;
3129 }
3230
3331 public createDoneEditEventArgs ( cachedRowData : any , event ?: Event ) : IGridEditDoneEventArgs {
3432 const updatedData = this . grid . transactions . enabled ?
3533 this . grid . transactions . getAggregatedValue ( this . id , true ) : this . grid . gridAPI . getRowData ( this . id ) ;
36- const rowData = updatedData === null ? this . grid . gridAPI . getRowData ( this . id ) : updatedData ;
34+ const rowData = updatedData ?? this . grid . gridAPI . getRowData ( this . id ) ;
3735 const args : IGridEditDoneEventArgs = {
3836 rowID : this . id ,
3937 rowData,
4038 oldValue : cachedRowData ,
4139 newValue : updatedData ,
4240 owner : this . grid ,
43- isAddRow : this . isAddRow || false ,
4441 event
4542 } ;
4643
4744 return args ;
4845 }
46+
47+ public getClassName ( ) {
48+ return this . constructor . name ;
49+ }
50+ }
51+
52+ export class IgxAddRow extends IgxEditRow {
53+ public isAddRow : boolean = true ;
54+
55+ public createEditEventArgs ( includeNewValue = true , event ?: Event ) : IGridEditEventArgs {
56+ const args = super . createEditEventArgs ( includeNewValue , event ) ;
57+ args . oldValue = null ;
58+ args . isAddRow = true ;
59+ return args ;
60+ }
61+
62+ public createDoneEditEventArgs ( cachedRowData : any , event ?: Event ) : IGridEditDoneEventArgs {
63+ const args = super . createDoneEditEventArgs ( null , event ) ;
64+ args . isAddRow = true ;
65+ return args ;
66+ }
67+ }
68+
69+ export interface IgxAddRowParent {
70+ rowID : string ;
71+ index : number ;
72+ asChild : boolean ;
73+ isPinned : boolean ;
4974}
5075
5176export class IgxCell {
@@ -106,16 +131,10 @@ export class IgxCell {
106131 }
107132}
108133
109- export interface IgxRowParent {
110- rowID : string ;
111- index : number ;
112- asChild : boolean ;
113- isPinned : boolean ;
114- }
115134export class IgxCellCrudState {
116135 public grid : IgxGridBaseDirective & GridType ;
117136 public cell : IgxCell | null = null ;
118- public row : IgxRow | null = null ;
137+ public row : IgxEditRow | null = null ;
119138 public isInCompositionMode = false ;
120139 public cancelAddMode = false ;
121140
@@ -129,8 +148,8 @@ export class IgxCellCrudState {
129148 cell . rowData ?? cell . row . data , cell . grid ) ;
130149 }
131150
132- public createRow ( cell : IgxCell ) : IgxRow {
133- return this . row = new IgxRow ( cell . id . rowID , cell . rowIndex , cell . rowData , cell . grid ) ;
151+ public createRow ( cell : IgxCell ) : IgxEditRow {
152+ return this . row = new IgxEditRow ( cell . id . rowID , cell . rowIndex , cell . rowData , cell . grid ) ;
134153 }
135154
136155 public sameRow ( rowID ) : boolean {
@@ -229,7 +248,7 @@ export class IgxCellCrudState {
229248 }
230249}
231250export class IgxRowCrudState extends IgxCellCrudState {
232- public row : IgxRow | null = null ;
251+ public row : IgxEditRow | null = null ;
233252 public closeRowEditingOverlay = new Subject ( ) ;
234253
235254 private _rowEditingBlocked = false ;
@@ -261,8 +280,10 @@ export class IgxRowCrudState extends IgxCellCrudState {
261280 console . warn ( 'The grid must have a `primaryKey` specified when using `rowEditable`!' ) ;
262281 }
263282
264- if ( ! this . row ) {
265- this . createRow ( this . cell ) ;
283+ if ( ! this . row || ! ( this . row . getClassName ( ) === IgxEditRow . name ) ) {
284+ if ( ! this . row ) {
285+ this . createRow ( this . cell ) ;
286+ }
266287 const rowArgs = this . row . createEditEventArgs ( false , event ) ;
267288
268289 this . grid . rowEditEnter . emit ( rowArgs ) ;
@@ -317,9 +338,12 @@ export class IgxRowCrudState extends IgxCellCrudState {
317338 let nonCancelableArgs ;
318339 if ( ! commit ) {
319340 this . grid . transactions . endPending ( false ) ;
320- } else {
341+ } else if ( this . row . getClassName ( ) === IgxEditRow . name ) {
321342 rowEditArgs = this . grid . gridAPI . update_row ( this . row , this . row . newData , event ) ;
322343 nonCancelableArgs = this . rowEditDone ( rowEditArgs . oldValue , event ) ;
344+ } else {
345+ this . grid . transactions . endPending ( false ) ;
346+ this . grid . gridAPI . addRowToData ( this . row . newData ?? this . row . data ) ;
323347 }
324348
325349 nonCancelableArgs = this . exitRowEdit ( rowEditArgs . oldValue , event ) ;
@@ -359,7 +383,7 @@ export class IgxRowCrudState extends IgxCellCrudState {
359383 }
360384 }
361385
362- public updateRowEditData ( row : IgxRow , value ?: any ) {
386+ public updateRowEditData ( row : IgxEditRow , value ?: any ) {
363387 const grid = this . grid ;
364388
365389 const rowInEditMode = grid . gridAPI . crudService . row ;
@@ -378,17 +402,7 @@ export class IgxRowCrudState extends IgxCellCrudState {
378402}
379403
380404export class IgxRowAddCrudState extends IgxRowCrudState {
381- public addRowParent : IgxRowParent = null ;
382- public addRow : IgxRow | null = null ;
383-
384- /**
385- * @hidden @internal
386- */
387- public createRow ( cell : IgxCell ) : IgxRow {
388- this . row = super . createRow ( cell ) ;
389- this . row . isAddRow = this . addRow ? this . addRow . id === this . row . id : false ;
390- return this . row ;
391- }
405+ public addRowParent : IgxAddRowParent = null ;
392406
393407 /**
394408 * @hidden @internal
@@ -398,7 +412,7 @@ export class IgxRowAddCrudState extends IgxRowCrudState {
398412
399413 const newRec = this . grid . getEmptyRecordObjectFor ( parentRow ? parentRow . rowData : null ) ;
400414 const addRowIndex = this . addRowParent . index + 1 ;
401- return this . addRow = new IgxRow ( newRec [ this . primaryKey ] , addRowIndex , newRec , this . grid ) ;
415+ return this . row = new IgxAddRow ( newRec [ this . primaryKey ] , addRowIndex , newRec , this . grid ) ;
402416 }
403417
404418 /**
@@ -423,7 +437,7 @@ export class IgxRowAddCrudState extends IgxRowCrudState {
423437 * @hidden @internal
424438 */
425439 public endRowTransaction ( commit : boolean , event ?: Event ) : IGridEditEventArgs {
426- if ( this . addRow ) {
440+ if ( this . row && this . row . getClassName ( ) === IgxAddRow . name ) {
427441 this . grid . rowAdded . pipe ( first ( ) ) . subscribe ( ( addRowArgs : IRowDataEventArgs ) => {
428442 const rowData = addRowArgs . data ;
429443 const pinnedIndex = this . grid . pinnedRecords . findIndex ( x => x [ this . primaryKey ] === rowData [ this . primaryKey ] ) ;
@@ -434,29 +448,13 @@ export class IgxRowAddCrudState extends IgxRowCrudState {
434448 const showIndex = isInView ? - 1 : dataIndex ;
435449 this . grid . showSnackbarFor ( showIndex ) ;
436450 } ) ;
437-
438- this . addRow . newData = this . row . newData ;
439451 }
440452
441453 const args = super . endRowTransaction ( commit , event ) ;
442454
443- if ( this . addRow ) {
444- this . grid . transactions . endPending ( true ) ;
445-
446- if ( commit ) {
447- args . isAddRow = true ;
448-
449- if ( ! this . grid . transactions . enabled ) {
450- this . grid . gridAPI . addRowToData ( this . addRow . newData || this . addRow . data ) ;
451- }
452-
453- this . grid . rowAddedNotifier . next ( this . addRow . newData || this . addRow . data ) ;
454- this . grid . rowAdded . emit ( { data : this . addRow . newData || this . addRow . data } ) ;
455- } else if ( this . grid . transactions . enabled ) {
456- this . grid . transactions . clear ( this . addRow . id ) ;
457- }
458-
459- this . endAddRow ( ) ;
455+ this . endAddRow ( ) ;
456+ if ( commit ) {
457+ this . grid . rowAdded . emit ( { data : args . newValue } ) ;
460458 }
461459
462460 return args ;
@@ -466,7 +464,6 @@ export class IgxRowAddCrudState extends IgxRowCrudState {
466464 * @hidden @internal
467465 */
468466 public endAddRow ( ) {
469- this . addRow = null ;
470467 this . addRowParent = null ;
471468 this . grid . triggerPipes ( ) ;
472469 }
@@ -555,17 +552,16 @@ export class IgxGridCRUDService extends IgxRowAddCrudState {
555552 this . createAddRow ( parentRow , asChild ) ;
556553
557554 this . grid . transactions . startPending ( ) ;
558- this . grid . gridAPI . addRowToData ( this . addRow . data , parentRow ? parentRow . rowID : null , this . addRow . index ) ;
559555 if ( this . addRowParent . isPinned ) {
560556 // If parent is pinned, add the new row to pinned records
561- ( this . grid as any ) . _pinnedRecordIDs . splice ( this . addRow . index , 0 , this . addRow . id ) ;
557+ ( this . grid as any ) . _pinnedRecordIDs . splice ( this . row . index , 0 , this . row . id ) ;
562558 }
563559
564560 this . grid . triggerPipes ( ) ;
565561 this . grid . notifyChanges ( true ) ;
566562
567- this . grid . navigateTo ( this . addRow . index , - 1 ) ;
568- const dummyRow = this . grid . gridAPI . get_row_by_index ( this . addRow . index ) ;
563+ this . grid . navigateTo ( this . row . index , - 1 ) ;
564+ const dummyRow = this . grid . gridAPI . get_row_by_index ( this . row . index ) ;
569565 dummyRow . triggerAddAnimation ( ) ;
570566 dummyRow . addAnimationEnd . pipe ( first ( ) ) . subscribe ( ( ) => {
571567 const cell = dummyRow . cells . find ( c => c . editable ) ;
0 commit comments