1
- import { BehaviorSubject } from 'rxjs' ;
2
- import { Component , ElementRef , Type , ViewChild } from '@angular/core' ;
3
- import { CommonModule } from '@angular/common' ;
4
- import { FormsModule , NgForm } from '@angular/forms' ;
5
- import { ComponentFixture , fakeAsync , flush , TestBed , tick } from '@angular/core/testing' ;
6
1
import { DataSource } from '@angular/cdk/collections' ;
7
2
import { CdkTableModule } from '@angular/cdk/table' ;
8
- import { CdkPopoverEditModule , PopoverEditClickOutBehavior } from './index' ;
3
+ import { CommonModule } from '@angular/common' ;
4
+ import { Component , ElementRef , Type , ViewChild } from '@angular/core' ;
5
+ import { ComponentFixture , fakeAsync , flush , TestBed , tick } from '@angular/core/testing' ;
6
+ import { FormsModule , NgForm } from '@angular/forms' ;
7
+ import { BehaviorSubject } from 'rxjs' ;
8
+
9
+ import { CdkPopoverEditColspan , CdkPopoverEditModule , PopoverEditClickOutBehavior } from './index' ;
9
10
10
11
const EDIT_TEMPLATE = `
11
12
<div style="background-color: white;">
@@ -33,6 +34,8 @@ const CELL_TEMPLATE = `
33
34
</span>
34
35
` ;
35
36
37
+ const POPOVER_EDIT_DIRECTIVE = `[cdkPopoverEdit]="nameEdit" [cdkPopoverEditColspan]="colspan"` ;
38
+
36
39
interface PeriodicElement {
37
40
name : string ;
38
41
weight : number ;
@@ -45,6 +48,7 @@ abstract class BaseTestComponent {
45
48
46
49
ignoreSubmitUnlessValid = true ;
47
50
clickOutBehavior : PopoverEditClickOutBehavior = 'close' ;
51
+ colspan : CdkPopoverEditColspan = { } ;
48
52
49
53
onSubmit ( element : PeriodicElement , form : NgForm ) {
50
54
if ( ! form . valid ) { return ; }
@@ -62,7 +66,7 @@ abstract class BaseTestComponent {
62
66
63
67
getEditCell ( rowIndex = 0 ) {
64
68
const row = getRows ( this . table . nativeElement ) [ rowIndex ] ;
65
- return getCells ( row ) [ 0 ] ;
69
+ return getCells ( row ) [ 1 ] ;
66
70
}
67
71
68
72
focusEditCell ( rowIndex = 0 ) {
@@ -84,6 +88,10 @@ abstract class BaseTestComponent {
84
88
flush ( ) ;
85
89
}
86
90
91
+ getEditPane ( ) {
92
+ return document . querySelector ( '.cdk-edit-pane' ) ;
93
+ }
94
+
87
95
getInput ( ) {
88
96
return document . querySelector ( 'input' ) as HTMLInputElement | null ;
89
97
}
@@ -125,7 +133,10 @@ abstract class BaseTestComponent {
125
133
</ng-template>
126
134
127
135
<tr *ngFor="let element of elements">
128
- <td [cdkPopoverEdit]="nameEdit" [cdkPopoverEditContext]="element">
136
+ <td> just a cell </td>
137
+
138
+ <td ${ POPOVER_EDIT_DIRECTIVE }
139
+ [cdkPopoverEditContext]="element">
129
140
${ CELL_TEMPLATE }
130
141
</td>
131
142
@@ -142,7 +153,9 @@ class VanillaTableOutOfCell extends BaseTestComponent {
142
153
template : `
143
154
<table #table editable>
144
155
<tr *ngFor="let element of elements">
145
- <td [cdkPopoverEdit]="nameEdit">
156
+ <td> just a cell </td>
157
+
158
+ <td ${ POPOVER_EDIT_DIRECTIVE } >
146
159
${ CELL_TEMPLATE }
147
160
148
161
<ng-template #nameEdit>
@@ -175,9 +188,15 @@ class ElementDataSource extends DataSource<PeriodicElement> {
175
188
template : `
176
189
<div #table>
177
190
<cdk-table cdk-table editable [dataSource]="dataSource">
191
+ <ng-container cdkColumnDef="before">
192
+ <cdk-cell *cdkCellDef="let element">
193
+ just a cell
194
+ </cdk-cell>
195
+ </ng-container>
196
+
178
197
<ng-container cdkColumnDef="name">
179
198
<cdk-cell *cdkCellDef="let element"
180
- [cdkPopoverEdit]="nameEdit" >
199
+ ${ POPOVER_EDIT_DIRECTIVE } >
181
200
${ CELL_TEMPLATE }
182
201
183
202
<ng-template #nameEdit>
@@ -202,17 +221,23 @@ class ElementDataSource extends DataSource<PeriodicElement> {
202
221
`
203
222
} )
204
223
class CdkFlexTableInCell extends BaseTestComponent {
205
- displayedColumns = [ 'name' , 'weight' ] ;
224
+ displayedColumns = [ 'before' , ' name', 'weight' ] ;
206
225
dataSource = new ElementDataSource ( ) ;
207
226
}
208
227
209
228
@Component ( {
210
229
template : `
211
230
<div #table>
212
231
<table cdk-table editable [dataSource]="dataSource">
232
+ <ng-container cdkColumnDef="before">
233
+ <td cdk-cell *cdkCellDef="let element">
234
+ just a cell
235
+ </td>
236
+ </ng-container>
237
+
213
238
<ng-container cdkColumnDef="name">
214
239
<td cdk-cell *cdkCellDef="let element"
215
- [cdkPopoverEdit]="nameEdit" >
240
+ ${ POPOVER_EDIT_DIRECTIVE } >
216
241
${ CELL_TEMPLATE }
217
242
218
243
<ng-template #nameEdit>
@@ -237,7 +262,7 @@ class CdkFlexTableInCell extends BaseTestComponent {
237
262
`
238
263
} )
239
264
class CdkTableInCell extends BaseTestComponent {
240
- displayedColumns = [ 'name' , 'weight' ] ;
265
+ displayedColumns = [ 'before' , ' name', 'weight' ] ;
241
266
dataSource = new ElementDataSource ( ) ;
242
267
}
243
268
@@ -316,6 +341,49 @@ describe('CDK Popover Edit', () => {
316
341
expect ( component . getInput ( ) ! . value ) . toBe ( 'Hydrogen' ) ;
317
342
} ) ) ;
318
343
344
+ it ( 'positions the lens at the top left corner and spans the full width of the cell' ,
345
+ fakeAsync ( ( ) => {
346
+ component . openLens ( ) ;
347
+
348
+ const paneRect = component . getEditPane ( ) ! . getBoundingClientRect ( ) ;
349
+ const cellRect = component . getEditCell ( ) . getBoundingClientRect ( ) ;
350
+
351
+ expect ( paneRect . width ) . toEqual ( cellRect . width ) ;
352
+ expect ( paneRect . left ) . toEqual ( cellRect . left ) ;
353
+ expect ( paneRect . top ) . toEqual ( cellRect . top ) ;
354
+ } ) ) ;
355
+
356
+ it ( 'adjusts the positioning of the lens based on colspan' , fakeAsync ( ( ) => {
357
+ const cellRects = getCells ( getRows ( component . table . nativeElement ) [ 0 ] )
358
+ . map ( cell => cell . getBoundingClientRect ( ) ) ;
359
+
360
+ component . colspan = { before : 1 } ;
361
+ fixture . detectChanges ( ) ;
362
+
363
+ component . openLens ( ) ;
364
+
365
+ let paneRect = component . getEditPane ( ) ! . getBoundingClientRect ( ) ;
366
+ expect ( paneRect . top ) . toEqual ( cellRects [ 0 ] . top ) ;
367
+ expect ( paneRect . left ) . toEqual ( cellRects [ 0 ] . left ) ;
368
+ expect ( paneRect . right ) . toEqual ( cellRects [ 1 ] . right ) ;
369
+
370
+ component . colspan = { after : 1 } ;
371
+ fixture . detectChanges ( ) ;
372
+
373
+ paneRect = component . getEditPane ( ) ! . getBoundingClientRect ( ) ;
374
+ expect ( paneRect . top ) . toEqual ( cellRects [ 1 ] . top ) ;
375
+ expect ( paneRect . left ) . toEqual ( cellRects [ 1 ] . left ) ;
376
+ expect ( paneRect . right ) . toEqual ( cellRects [ 2 ] . right ) ;
377
+
378
+ component . colspan = { before : 1 , after : 1 } ;
379
+ fixture . detectChanges ( ) ;
380
+
381
+ paneRect = component . getEditPane ( ) ! . getBoundingClientRect ( ) ;
382
+ expect ( paneRect . top ) . toEqual ( cellRects [ 0 ] . top ) ;
383
+ expect ( paneRect . left ) . toEqual ( cellRects [ 0 ] . left ) ;
384
+ expect ( paneRect . right ) . toEqual ( cellRects [ 2 ] . right ) ;
385
+ } ) ) ;
386
+
319
387
it ( 'updates the form and submits, closing the lens' , fakeAsync ( ( ) => {
320
388
component . openLens ( ) ;
321
389
0 commit comments