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' ;
61import { DataSource } from '@angular/cdk/collections' ;
72import { 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' ;
910
1011const EDIT_TEMPLATE = `
1112 <div style="background-color: white;">
@@ -33,6 +34,8 @@ const CELL_TEMPLATE = `
3334 </span>
3435 ` ;
3536
37+ const POPOVER_EDIT_DIRECTIVE = `[cdkPopoverEdit]="nameEdit" [cdkPopoverEditColspan]="colspan"` ;
38+
3639interface PeriodicElement {
3740 name : string ;
3841 weight : number ;
@@ -45,6 +48,7 @@ abstract class BaseTestComponent {
4548
4649 ignoreSubmitUnlessValid = true ;
4750 clickOutBehavior : PopoverEditClickOutBehavior = 'close' ;
51+ colspan : CdkPopoverEditColspan = { } ;
4852
4953 onSubmit ( element : PeriodicElement , form : NgForm ) {
5054 if ( ! form . valid ) { return ; }
@@ -62,7 +66,7 @@ abstract class BaseTestComponent {
6266
6367 getEditCell ( rowIndex = 0 ) {
6468 const row = getRows ( this . table . nativeElement ) [ rowIndex ] ;
65- return getCells ( row ) [ 0 ] ;
69+ return getCells ( row ) [ 1 ] ;
6670 }
6771
6872 focusEditCell ( rowIndex = 0 ) {
@@ -84,6 +88,10 @@ abstract class BaseTestComponent {
8488 flush ( ) ;
8589 }
8690
91+ getEditPane ( ) {
92+ return document . querySelector ( '.cdk-edit-pane' ) ;
93+ }
94+
8795 getInput ( ) {
8896 return document . querySelector ( 'input' ) as HTMLInputElement | null ;
8997 }
@@ -125,7 +133,10 @@ abstract class BaseTestComponent {
125133 </ng-template>
126134
127135 <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">
129140 ${ CELL_TEMPLATE }
130141 </td>
131142
@@ -142,7 +153,9 @@ class VanillaTableOutOfCell extends BaseTestComponent {
142153 template : `
143154 <table #table editable>
144155 <tr *ngFor="let element of elements">
145- <td [cdkPopoverEdit]="nameEdit">
156+ <td> just a cell </td>
157+
158+ <td ${ POPOVER_EDIT_DIRECTIVE } >
146159 ${ CELL_TEMPLATE }
147160
148161 <ng-template #nameEdit>
@@ -175,9 +188,15 @@ class ElementDataSource extends DataSource<PeriodicElement> {
175188 template : `
176189 <div #table>
177190 <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+
178197 <ng-container cdkColumnDef="name">
179198 <cdk-cell *cdkCellDef="let element"
180- [cdkPopoverEdit]="nameEdit" >
199+ ${ POPOVER_EDIT_DIRECTIVE } >
181200 ${ CELL_TEMPLATE }
182201
183202 <ng-template #nameEdit>
@@ -202,17 +221,23 @@ class ElementDataSource extends DataSource<PeriodicElement> {
202221 `
203222} )
204223class CdkFlexTableInCell extends BaseTestComponent {
205- displayedColumns = [ 'name' , 'weight' ] ;
224+ displayedColumns = [ 'before' , ' name', 'weight' ] ;
206225 dataSource = new ElementDataSource ( ) ;
207226}
208227
209228@Component ( {
210229 template : `
211230 <div #table>
212231 <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+
213238 <ng-container cdkColumnDef="name">
214239 <td cdk-cell *cdkCellDef="let element"
215- [cdkPopoverEdit]="nameEdit" >
240+ ${ POPOVER_EDIT_DIRECTIVE } >
216241 ${ CELL_TEMPLATE }
217242
218243 <ng-template #nameEdit>
@@ -237,7 +262,7 @@ class CdkFlexTableInCell extends BaseTestComponent {
237262 `
238263} )
239264class CdkTableInCell extends BaseTestComponent {
240- displayedColumns = [ 'name' , 'weight' ] ;
265+ displayedColumns = [ 'before' , ' name', 'weight' ] ;
241266 dataSource = new ElementDataSource ( ) ;
242267}
243268
@@ -316,6 +341,49 @@ describe('CDK Popover Edit', () => {
316341 expect ( component . getInput ( ) ! . value ) . toBe ( 'Hydrogen' ) ;
317342 } ) ) ;
318343
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 ) . toBe ( cellRect . width ) ;
352+ expect ( paneRect . left ) . toBe ( cellRect . left ) ;
353+ expect ( paneRect . top ) . toBe ( 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 ) . toBe ( cellRects [ 0 ] . top ) ;
367+ expect ( paneRect . left ) . toBe ( cellRects [ 0 ] . left ) ;
368+ expect ( paneRect . right ) . toBe ( cellRects [ 1 ] . right ) ;
369+
370+ component . colspan = { after : 1 } ;
371+ fixture . detectChanges ( ) ;
372+
373+ paneRect = component . getEditPane ( ) ! . getBoundingClientRect ( ) ;
374+ expect ( paneRect . top ) . toBe ( cellRects [ 1 ] . top ) ;
375+ expect ( paneRect . left ) . toBe ( cellRects [ 1 ] . left ) ;
376+ expect ( paneRect . right ) . toBe ( cellRects [ 2 ] . right ) ;
377+
378+ component . colspan = { before : 1 , after : 1 } ;
379+ fixture . detectChanges ( ) ;
380+
381+ paneRect = component . getEditPane ( ) ! . getBoundingClientRect ( ) ;
382+ expect ( paneRect . top ) . toBe ( cellRects [ 0 ] . top ) ;
383+ expect ( paneRect . left ) . toBe ( cellRects [ 0 ] . left ) ;
384+ expect ( paneRect . right ) . toBe ( cellRects [ 2 ] . right ) ;
385+ } ) ) ;
386+
319387 it ( 'updates the form and submits, closing the lens' , fakeAsync ( ( ) => {
320388 component . openLens ( ) ;
321389
0 commit comments