Skip to content

Commit 76496f1

Browse files
committed
refactor(cdk-experimental/popover-edit): switch to standalone
Reworks `cdk-experimental/popover-edit` to support standalone. Includes removing one provider whose default was provided in an `NgModule`.
1 parent 3418952 commit 76496f1

File tree

7 files changed

+53
-103
lines changed

7 files changed

+53
-103
lines changed

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import {ScrollDispatcher, ViewportRuler} from '@angular/cdk/scrolling';
1414

1515
import {EditEventDispatcher} from './edit-event-dispatcher';
1616
import {FocusDispatcher} from './focus-dispatcher';
17-
import {PopoverEditPositionStrategyFactory} from './popover-edit-position-strategy-factory';
1817
import {EditRef} from './edit-ref';
1918

2019
/**
@@ -32,7 +31,6 @@ export class EditServices {
3231
readonly focusTrapFactory: FocusTrapFactory,
3332
readonly ngZone: NgZone,
3433
readonly overlay: Overlay,
35-
readonly positionFactory: PopoverEditPositionStrategyFactory,
3634
readonly scrollDispatcher: ScrollDispatcher,
3735
readonly viewportRuler: ViewportRuler,
3836
) {}

src/cdk-experimental/popover-edit/lens-directives.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export type PopoverEditClickOutBehavior = 'close' | 'submit' | 'noop';
3636
'(document:click)': 'handlePossibleClickOut($event)',
3737
'(keydown)': '_handleKeydown($event)',
3838
},
39+
standalone: true,
3940
})
4041
export class CdkEditControl<FormValue> implements OnDestroy, OnInit {
4142
protected readonly destroyed = new Subject<void>();
@@ -60,7 +61,10 @@ export class CdkEditControl<FormValue> implements OnDestroy, OnInit {
6061
*/
6162
ignoreSubmitUnlessValid = true;
6263

63-
constructor(protected readonly elementRef: ElementRef, readonly editRef: EditRef<FormValue>) {}
64+
constructor(
65+
protected readonly elementRef: ElementRef,
66+
readonly editRef: EditRef<FormValue>,
67+
) {}
6468

6569
ngOnInit(): void {
6670
this.editRef.init(this.preservedFormValue);
@@ -140,9 +144,10 @@ export class CdkEditControl<FormValue> implements OnDestroy, OnInit {
140144
@Directive({
141145
selector: 'button[cdkEditRevert]',
142146
host: {
143-
'type': 'button', // Prevents accidental form submits.
147+
'type': 'button',
144148
'(click)': 'revertEdit()',
145149
},
150+
standalone: true,
146151
})
147152
export class CdkEditRevert<FormValue> {
148153
/** Type of the button. Defaults to `button` to avoid accident form submits. */
@@ -163,6 +168,7 @@ export class CdkEditRevert<FormValue> {
163168
'(keydown.enter)': 'closeEdit()',
164169
'(keydown.space)': 'closeEdit()',
165170
},
171+
standalone: true,
166172
})
167173
export class CdkEditClose<FormValue> {
168174
constructor(

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

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@ import {
1616
CdkEditOpen,
1717
} from './table-directives';
1818
import {CdkEditControl, CdkEditRevert, CdkEditClose} from './lens-directives';
19-
import {
20-
DefaultPopoverEditPositionStrategyFactory,
21-
PopoverEditPositionStrategyFactory,
22-
} from './popover-edit-position-strategy-factory';
2319

2420
const EXPORTED_DECLARATIONS = [
2521
CdkPopoverEdit,
@@ -33,14 +29,7 @@ const EXPORTED_DECLARATIONS = [
3329
];
3430

3531
@NgModule({
36-
imports: [OverlayModule],
32+
imports: [OverlayModule, ...EXPORTED_DECLARATIONS],
3733
exports: EXPORTED_DECLARATIONS,
38-
declarations: EXPORTED_DECLARATIONS,
39-
providers: [
40-
{
41-
provide: PopoverEditPositionStrategyFactory,
42-
useClass: DefaultPopoverEditPositionStrategyFactory,
43-
},
44-
],
4534
})
4635
export class CdkPopoverEditModule {}

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

Lines changed: 0 additions & 80 deletions
This file was deleted.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {DataSource} from '@angular/cdk/collections';
22
import {LEFT_ARROW, UP_ARROW, RIGHT_ARROW, DOWN_ARROW, TAB} from '@angular/cdk/keycodes';
33
import {CdkTableModule} from '@angular/cdk/table';
4-
import {dispatchKeyboardEvent} from '../../cdk/testing/private';
4+
import {dispatchKeyboardEvent} from '@angular/cdk/testing/private';
55
import {CommonModule} from '@angular/common';
66
import {Component, Directive, ElementRef, ViewChild} from '@angular/core';
77
import {ComponentFixture, fakeAsync, flush, TestBed, tick} from '@angular/core/testing';

src/cdk-experimental/popover-edit/public-api.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ export * from './focus-dispatcher';
1212
export * from './form-value-container';
1313
export * from './lens-directives';
1414
export * from './popover-edit-module';
15-
export * from './popover-edit-position-strategy-factory';
1615
export * from './table-directives';
1716

1817
// Private to Angular Components

src/cdk-experimental/popover-edit/table-directives.ts

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88
import {FocusTrap} from '@angular/cdk/a11y';
9-
import {OverlayRef, PositionStrategy} from '@angular/cdk/overlay';
9+
import {OverlayRef, OverlaySizeConfig, PositionStrategy} from '@angular/cdk/overlay';
1010
import {TemplatePortal} from '@angular/cdk/portal';
1111
import {
1212
AfterViewInit,
@@ -63,6 +63,7 @@ const MOUSE_MOVE_THROTTLE_TIME_MS = 10;
6363
@Directive({
6464
selector: 'table[editable], cdk-table[editable], mat-table[editable]',
6565
providers: [EditEventDispatcher, EditServices],
66+
standalone: true,
6667
})
6768
export class CdkEditable implements AfterViewInit, OnDestroy {
6869
protected readonly destroyed = new Subject<void>();
@@ -175,6 +176,7 @@ const POPOVER_EDIT_INPUTS = [
175176
selector: '[cdkPopoverEdit]:not([cdkPopoverEditTabOut])',
176177
host: POPOVER_EDIT_HOST_BINDINGS,
177178
inputs: POPOVER_EDIT_INPUTS,
179+
standalone: true,
178180
})
179181
export class CdkPopoverEdit<C> implements AfterViewInit, OnDestroy {
180182
/** The edit lens template shown over the cell on edit. */
@@ -337,20 +339,53 @@ export class CdkPopoverEdit<C> implements AfterViewInit, OnDestroy {
337339
}
338340

339341
private _getPositionStrategy(): PositionStrategy {
340-
return this.services.positionFactory.positionStrategyForCells(this._getOverlayCells());
342+
const cells = this._getOverlayCells();
343+
return this.services.overlay
344+
.position()
345+
.flexibleConnectedTo(cells[0])
346+
.withGrowAfterOpen()
347+
.withPush()
348+
.withViewportMargin(16)
349+
.withPositions([
350+
{
351+
originX: 'start',
352+
originY: 'top',
353+
overlayX: 'start',
354+
overlayY: 'top',
355+
},
356+
]);
341357
}
342358

343359
private _updateOverlaySize(): void {
344-
this.overlayRef!.updateSize(
345-
this.services.positionFactory.sizeConfigForCells(this._getOverlayCells()),
346-
);
360+
this.overlayRef!.updateSize(this._sizeConfigForCells(this._getOverlayCells()));
347361
}
348362

349363
private _maybeReturnFocusToCell(): void {
350364
if (closest(document.activeElement, EDIT_PANE_SELECTOR) === this.overlayRef!.overlayElement) {
351365
this.elementRef.nativeElement!.focus();
352366
}
353367
}
368+
369+
private _sizeConfigForCells(cells: HTMLElement[]): OverlaySizeConfig {
370+
if (cells.length === 0) {
371+
return {};
372+
}
373+
374+
if (cells.length === 1) {
375+
return {width: cells[0].getBoundingClientRect().width};
376+
}
377+
378+
let firstCell, lastCell;
379+
if (this.services.directionality.value === 'ltr') {
380+
firstCell = cells[0];
381+
lastCell = cells[cells.length - 1];
382+
} else {
383+
lastCell = cells[0];
384+
firstCell = cells[cells.length - 1];
385+
}
386+
387+
return {width: lastCell.getBoundingClientRect().right - firstCell.getBoundingClientRect().left};
388+
}
354389
}
355390

356391
/**
@@ -362,6 +397,7 @@ export class CdkPopoverEdit<C> implements AfterViewInit, OnDestroy {
362397
selector: '[cdkPopoverEdit][cdkPopoverEditTabOut]',
363398
host: POPOVER_EDIT_HOST_BINDINGS,
364399
inputs: POPOVER_EDIT_INPUTS,
400+
standalone: true,
365401
})
366402
export class CdkPopoverEditTabOut<C> extends CdkPopoverEdit<C> {
367403
protected override focusTrap?: FocusEscapeNotifier = undefined;
@@ -399,6 +435,7 @@ export class CdkPopoverEditTabOut<C> extends CdkPopoverEdit<C> {
399435
*/
400436
@Directive({
401437
selector: '[cdkRowHoverContent]',
438+
standalone: true,
402439
})
403440
export class CdkRowHoverContent implements AfterViewInit, OnDestroy {
404441
protected readonly destroyed = new Subject<void>();
@@ -495,6 +532,7 @@ export class CdkRowHoverContent implements AfterViewInit, OnDestroy {
495532
host: {
496533
'(click)': 'openEdit($event)',
497534
},
535+
standalone: true,
498536
})
499537
export class CdkEditOpen {
500538
constructor(

0 commit comments

Comments
 (0)