Skip to content

Commit f3080b4

Browse files
committed
feat(popover-edit): Adds support for spanning multiple columns and setting width of the popup based on the size of the cell(s)
1 parent 2f009d0 commit f3080b4

9 files changed

+342
-30
lines changed

src/cdk-experimental/popover-edit/edit-ref.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import {Injectable, OnDestroy, Self} from '@angular/core';
1010
import {ControlContainer} from '@angular/forms';
11-
import {Subject} from 'rxjs';
11+
import {Observable, Subject} from 'rxjs';
1212
import {take} from 'rxjs/operators';
1313

1414
import {EditEventDispatcher} from './edit-event-dispatcher';
@@ -21,7 +21,7 @@ import {EditEventDispatcher} from './edit-event-dispatcher';
2121
export class EditRef<FormValue> implements OnDestroy {
2222
/** Emits the final value of this edit instance before closing. */
2323
private readonly _finalValueSubject = new Subject<FormValue>();
24-
readonly finalValue = this._finalValueSubject.asObservable();
24+
readonly finalValue: Observable<FormValue> = this._finalValueSubject.asObservable();
2525

2626
/** The value to set the form back to on revert. */
2727
private _revertFormValue: FormValue;

src/cdk-experimental/popover-edit/popover-edit-position-strategy-factory.ts

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,27 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {ElementRef, Injectable} from '@angular/core';
10-
import {Overlay, PositionStrategy} from '@angular/cdk/overlay';
9+
import {Directionality} from '@angular/cdk/bidi';
10+
import {Overlay, OverlaySizeConfig, PositionStrategy} from '@angular/cdk/overlay';
11+
import {Injectable} from '@angular/core';
1112

1213
/**
13-
* Overridable factory responsible for configuring how cdkPopoverEdit popovers are positioned.
14+
* Overridable factory responsible for configuring how cdkPopoverEdit popovers are positioned
15+
* and sized.
1416
*/
1517
@Injectable()
1618
export abstract class PopoverEditPositionStrategyFactory {
17-
abstract forElementRef(elementRef: ElementRef): PositionStrategy;
19+
/**
20+
* Creates a PositionStrategy based on the specified table cells.
21+
* The cells will be provided in DOM order.
22+
*/
23+
abstract positionStrategyForCells(cells: HTMLElement[]): PositionStrategy;
24+
25+
/**
26+
* Creates an OverlaySizeConfig based on the specified table cells.
27+
* The cells will be provided in DOM order.
28+
*/
29+
abstract sizeConfigForCells(cells: HTMLElement[]): OverlaySizeConfig;
1830
}
1931

2032
/**
@@ -24,19 +36,42 @@ export abstract class PopoverEditPositionStrategyFactory {
2436
*/
2537
@Injectable()
2638
export class DefaultPopoverEditPositionStrategyFactory extends PopoverEditPositionStrategyFactory {
27-
constructor(protected readonly overlay: Overlay) {
39+
constructor(protected readonly direction: Directionality, protected readonly overlay: Overlay) {
2840
super();
2941
}
3042

31-
forElementRef(elementRef: ElementRef): PositionStrategy {
32-
return this.overlay.position().flexibleConnectedTo(elementRef)
43+
positionStrategyForCells(cells: HTMLElement[]): PositionStrategy {
44+
return this.overlay.position()
45+
.flexibleConnectedTo(cells[0])
3346
.withGrowAfterOpen()
3447
.withPush()
48+
.withViewportMargin(16)
3549
.withPositions([{
3650
originX: 'start',
3751
originY: 'top',
3852
overlayX: 'start',
3953
overlayY: 'top',
4054
}]);
4155
}
56+
57+
sizeConfigForCells(cells: HTMLElement[]): OverlaySizeConfig {
58+
if (cells.length === 0) {
59+
return {};
60+
}
61+
62+
if (cells.length === 1) {
63+
return {width: cells[0].getBoundingClientRect().width};
64+
}
65+
66+
let firstCell, lastCell;
67+
if (this.direction.value === 'ltr') {
68+
firstCell = cells[0];
69+
lastCell = cells[cells.length - 1];
70+
} else {
71+
lastCell = cells[0];
72+
firstCell = cells[cells.length - 1];
73+
}
74+
75+
return {width: lastCell.getBoundingClientRect().right - firstCell.getBoundingClientRect().left};
76+
}
4277
}

src/cdk-experimental/popover-edit/popover-edit.spec.ts

Lines changed: 81 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
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';
61
import {DataSource} from '@angular/cdk/collections';
72
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';
910

1011
const 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+
3639
interface 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
})
204223
class 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
})
239264
class 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

Comments
 (0)