Skip to content

Commit 46ebbad

Browse files
authored
refactor(cdk/table): change deprecated APIs for v12 (#21877)
Changes the APIs that were marked as deprecated for v12. BREAKING CHANGES: * `_viewRepeater`, `_coalescedStyleScheduler` and `_viewportRuler` parameters of the `CdkTable` constructor are now required. * `_coalescedStyleScheduler` parameter of the `StickyStyler` constructor is now required.
1 parent 07eb0f7 commit 46ebbad

File tree

6 files changed

+47
-84
lines changed

6 files changed

+47
-84
lines changed

src/cdk/schematics/ng-update/data/constructor-checks.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ export type ConstructorChecksUpgradeData = string;
1717
* automatically through type checking.
1818
*/
1919
export const constructorChecks: VersionChanges<ConstructorChecksUpgradeData> = {
20+
[TargetVersion.V12]: [
21+
{
22+
pr: 'https://github.com/angular/components/pull/21876',
23+
changes: ['CdkTable', 'StickyStyler']
24+
}
25+
],
2026
[TargetVersion.V11]: [
2127
{
2228
pr: 'https://github.com/angular/components/pull/20454',

src/cdk/table/sticky-styler.ts

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,7 @@ export class StickyStyler {
4848
constructor(private _isNativeHtmlTable: boolean,
4949
private _stickCellCss: string,
5050
public direction: Direction,
51-
/**
52-
* @deprecated `_coalescedStyleScheduler` parameter to become required.
53-
* @breaking-change 11.0.0
54-
*/
55-
private _coalescedStyleScheduler?: _CoalescedStyleScheduler,
51+
private _coalescedStyleScheduler: _CoalescedStyleScheduler,
5652
private _isBrowser = true,
5753
private readonly _needsPositionStickyOnElement = true,
5854
private readonly _positionListener?: StickyPositioningListener) {
@@ -86,7 +82,7 @@ export class StickyStyler {
8682
}
8783

8884
// Coalesce with sticky row/column updates (and potentially other changes like column resize).
89-
this._scheduleStyleChanges(() => {
85+
this._coalescedStyleScheduler.schedule(() => {
9086
for (const element of elementsToClear) {
9187
this._removeStickyStyle(element, stickyDirections);
9288
}
@@ -128,7 +124,7 @@ export class StickyStyler {
128124
const firstStickyEnd = stickyEndStates.indexOf(true);
129125

130126
// Coalesce with sticky row updates (and potentially other changes like column resize).
131-
this._scheduleStyleChanges(() => {
127+
this._coalescedStyleScheduler.schedule(() => {
132128
const isRtl = this.direction === 'rtl';
133129
const start = isRtl ? 'right' : 'left';
134130
const end = isRtl ? 'left' : 'right';
@@ -213,7 +209,7 @@ export class StickyStyler {
213209

214210
// Coalesce with other sticky row updates (top/bottom), sticky columns updates
215211
// (and potentially other changes like column resize).
216-
this._scheduleStyleChanges(() => {
212+
this._coalescedStyleScheduler.schedule(() => {
217213
for (let rowIndex = 0; rowIndex < rows.length; rowIndex++) {
218214
if (!states[rowIndex]) {
219215
continue;
@@ -250,7 +246,7 @@ export class StickyStyler {
250246
const tfoot = tableElement.querySelector('tfoot')!;
251247

252248
// Coalesce with other sticky updates (and potentially other changes like column resize).
253-
this._scheduleStyleChanges(() => {
249+
this._coalescedStyleScheduler.schedule(() => {
254250
if (stickyStates.some(state => !state)) {
255251
this._removeStickyStyle(tfoot, ['bottom']);
256252
} else {
@@ -392,17 +388,4 @@ export class StickyStyler {
392388

393389
return positions;
394390
}
395-
396-
/**
397-
* Schedules styles to be applied when the style scheduler deems appropriate.
398-
* @breaking-change 11.0.0 This method can be removed in favor of calling
399-
* `CoalescedStyleScheduler.schedule` directly once the scheduler is a required parameter.
400-
*/
401-
private _scheduleStyleChanges(changes: () => void) {
402-
if (this._coalescedStyleScheduler) {
403-
this._coalescedStyleScheduler.schedule(changes);
404-
} else {
405-
changes();
406-
}
407-
}
408391
}

src/cdk/table/table.ts

Lines changed: 21 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -500,22 +500,17 @@ export class CdkTable<T> implements AfterContentChecked, CollectionViewer, OnDes
500500
protected readonly _elementRef: ElementRef, @Attribute('role') role: string,
501501
@Optional() protected readonly _dir: Directionality, @Inject(DOCUMENT) _document: any,
502502
private _platform: Platform,
503-
503+
@Inject(_VIEW_REPEATER_STRATEGY)
504+
protected readonly _viewRepeater: _ViewRepeater<T, RenderRow<T>, RowContext<T>>,
505+
@Inject(_COALESCED_STYLE_SCHEDULER)
506+
protected readonly _coalescedStyleScheduler: _CoalescedStyleScheduler,
507+
private readonly _viewportRuler: ViewportRuler,
504508
/**
505-
* @deprecated `_coalescedStyleScheduler`, `_viewRepeater` and `_viewportRuler`
506-
* parameters to become required.
507-
* @breaking-change 11.0.0
509+
* @deprecated `_stickyPositioningListener` parameter to become required.
510+
* @breaking-change 13.0.0
508511
*/
509-
@Optional() @Inject(_VIEW_REPEATER_STRATEGY)
510-
protected readonly _viewRepeater?: _ViewRepeater<T, RenderRow<T>, RowContext<T>>,
511-
@Optional() @Inject(_COALESCED_STYLE_SCHEDULER)
512-
protected readonly _coalescedStyleScheduler?: _CoalescedStyleScheduler,
513512
@Optional() @SkipSelf() @Inject(STICKY_POSITIONING_LISTENER)
514-
protected readonly _stickyPositioningListener?: StickyPositioningListener,
515-
// Optional for backwards compatibility. The viewport ruler is provided in root. Therefore,
516-
// this property will never be null.
517-
// tslint:disable-next-line: lightweight-tokens
518-
@Optional() private readonly _viewportRuler?: ViewportRuler) {
513+
protected readonly _stickyPositioningListener: StickyPositioningListener) {
519514
if (!role) {
520515
this._elementRef.nativeElement.setAttribute('role', 'grid');
521516
}
@@ -538,14 +533,9 @@ export class CdkTable<T> implements AfterContentChecked, CollectionViewer, OnDes
538533
return this.trackBy ? this.trackBy(dataRow.dataIndex, dataRow.data) : dataRow;
539534
});
540535

541-
// Table cell dimensions may change after resizing the window. Signal the sticky styler to
542-
// refresh its cache of cell widths the next time sticky styles are updated.
543-
// @breaking-change 11.0.0 Remove null check for _viewportRuler once it's a required parameter.
544-
if (this._viewportRuler) {
545-
this._viewportRuler.change().pipe(takeUntil(this._onDestroy)).subscribe(() => {
546-
this._forceRecalculateCellWidths = true;
547-
});
548-
}
536+
this._viewportRuler.change().pipe(takeUntil(this._onDestroy)).subscribe(() => {
537+
this._forceRecalculateCellWidths = true;
538+
});
549539
}
550540

551541
ngAfterContentChecked() {
@@ -626,38 +616,16 @@ export class CdkTable<T> implements AfterContentChecked, CollectionViewer, OnDes
626616
}
627617
const viewContainer = this._rowOutlet.viewContainer;
628618

629-
// @breaking-change 11.0.0 Remove null check for `_viewRepeater` and the
630-
// `else` clause once `_viewRepeater` is turned into a required parameter.
631-
if (this._viewRepeater) {
632-
this._viewRepeater.applyChanges(
633-
changes,
634-
viewContainer,
635-
(record: IterableChangeRecord<RenderRow<T>>,
636-
_adjustedPreviousIndex: number|null,
637-
currentIndex: number|null) => this._getEmbeddedViewArgs(record.item, currentIndex!),
638-
(record) => record.item.data,
639-
(change: _ViewRepeaterItemChange<RenderRow<T>, RowContext<T>>) => {
640-
if (change.operation === _ViewRepeaterOperation.INSERTED && change.context) {
641-
this._renderCellTemplateForItem(change.record.item.rowDef, change.context);
642-
}
643-
});
644-
} else {
645-
changes.forEachOperation(
646-
(record: IterableChangeRecord<RenderRow<T>>, prevIndex: number|null,
647-
currentIndex: number|null) => {
648-
if (record.previousIndex == null) {
649-
const renderRow = record.item;
650-
const rowDef = renderRow.rowDef;
651-
const context: RowContext<T> = {$implicit: renderRow.data};
652-
this._renderRow(this._rowOutlet, rowDef, currentIndex!, context);
653-
} else if (currentIndex == null) {
654-
viewContainer.remove(prevIndex!);
655-
} else {
656-
const view = <RowViewRef<T>>viewContainer.get(prevIndex!);
657-
viewContainer.move(view!, currentIndex);
658-
}
659-
});
660-
}
619+
this._viewRepeater.applyChanges(changes, viewContainer,
620+
(record: IterableChangeRecord<RenderRow<T>>,
621+
_adjustedPreviousIndex: number|null,
622+
currentIndex: number|null) => this._getEmbeddedViewArgs(record.item, currentIndex!),
623+
(record) => record.item.data,
624+
(change: _ViewRepeaterItemChange<RenderRow<T>, RowContext<T>>) => {
625+
if (change.operation === _ViewRepeaterOperation.INSERTED && change.context) {
626+
this._renderCellTemplateForItem(change.record.item.rowDef, change.context);
627+
}
628+
});
661629

662630
// Update the meta context of a row's context data (index, count, first, last, ...)
663631
this._updateRowIndexContext();

src/material-experimental/mdc-table/table.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import {
1818
CdkTable,
1919
_CoalescedStyleScheduler,
2020
_COALESCED_STYLE_SCHEDULER,
21+
CDK_TABLE,
22+
STICKY_POSITIONING_LISTENER,
2123
} from '@angular/cdk/table';
2224
import {
2325
_DisposeViewRepeaterStrategy,
@@ -48,10 +50,13 @@ export class MatRecycleRows {}
4850
},
4951
providers: [
5052
{provide: CdkTable, useExisting: MatTable},
53+
{provide: CDK_TABLE, useExisting: MatTable},
5154
{provide: _COALESCED_STYLE_SCHEDULER, useClass: _CoalescedStyleScheduler},
5255
// TODO(michaeljamesparsons) Abstract the view repeater strategy to a directive API so this code
5356
// is only included in the build if used.
5457
{provide: _VIEW_REPEATER_STRATEGY, useClass: _DisposeViewRepeaterStrategy},
58+
// Prevent nested tables from seeing this table's StickyPositioningListener.
59+
{provide: STICKY_POSITIONING_LISTENER, useValue: null},
5560
],
5661
encapsulation: ViewEncapsulation.None,
5762
// See note on CdkTable for explanation on why this uses the default change detection strategy.

src/material/table/table.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
CDK_TABLE_TEMPLATE,
1111
CdkTable,
1212
CDK_TABLE,
13-
_CoalescedStyleScheduler, _COALESCED_STYLE_SCHEDULER
13+
_CoalescedStyleScheduler, _COALESCED_STYLE_SCHEDULER, STICKY_POSITIONING_LISTENER
1414
} from '@angular/cdk/table';
1515
import {ChangeDetectionStrategy, Component, Directive, ViewEncapsulation} from '@angular/core';
1616
import {
@@ -50,6 +50,8 @@ export class MatRecycleRows {}
5050
{provide: CdkTable, useExisting: MatTable},
5151
{provide: CDK_TABLE, useExisting: MatTable},
5252
{provide: _COALESCED_STYLE_SCHEDULER, useClass: _CoalescedStyleScheduler},
53+
// Prevent nested tables from seeing this table's StickyPositioningListener.
54+
{provide: STICKY_POSITIONING_LISTENER, useValue: null},
5355
],
5456
encapsulation: ViewEncapsulation.None,
5557
// See note on CdkTable for explanation on why this uses the default change detection strategy.

tools/public_api_guard/cdk/table.d.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ export declare class CdkRowDef<T> extends BaseRowDef {
193193

194194
export declare class CdkTable<T> implements AfterContentChecked, CollectionViewer, OnDestroy, OnInit {
195195
protected readonly _changeDetectorRef: ChangeDetectorRef;
196-
protected readonly _coalescedStyleScheduler?: _CoalescedStyleScheduler | undefined;
196+
protected readonly _coalescedStyleScheduler: _CoalescedStyleScheduler;
197197
_contentColumnDefs: QueryList<CdkColumnDef>;
198198
_contentFooterRowDefs: QueryList<CdkFooterRowDef>;
199199
_contentHeaderRowDefs: QueryList<CdkHeaderRowDef>;
@@ -209,8 +209,8 @@ export declare class CdkTable<T> implements AfterContentChecked, CollectionViewe
209209
_noDataRow: CdkNoDataRow;
210210
_noDataRowOutlet: NoDataRowOutlet;
211211
_rowOutlet: DataRowOutlet;
212-
protected readonly _stickyPositioningListener?: StickyPositioningListener | undefined;
213-
protected readonly _viewRepeater?: _ViewRepeater<T, RenderRow<T>, RowContext<T>> | undefined;
212+
protected readonly _stickyPositioningListener: StickyPositioningListener;
213+
protected readonly _viewRepeater: _ViewRepeater<T, RenderRow<T>, RowContext<T>>;
214214
get dataSource(): CdkTableDataSourceInput<T>;
215215
set dataSource(dataSource: CdkTableDataSourceInput<T>);
216216
get fixedLayout(): boolean;
@@ -225,8 +225,8 @@ export declare class CdkTable<T> implements AfterContentChecked, CollectionViewe
225225
start: number;
226226
end: number;
227227
}>;
228-
constructor(_differs: IterableDiffers, _changeDetectorRef: ChangeDetectorRef, _elementRef: ElementRef, role: string, _dir: Directionality, _document: any, _platform: Platform,
229-
_viewRepeater?: _ViewRepeater<T, RenderRow<T>, RowContext<T>> | undefined, _coalescedStyleScheduler?: _CoalescedStyleScheduler | undefined, _stickyPositioningListener?: StickyPositioningListener | undefined, _viewportRuler?: ViewportRuler | undefined);
228+
constructor(_differs: IterableDiffers, _changeDetectorRef: ChangeDetectorRef, _elementRef: ElementRef, role: string, _dir: Directionality, _document: any, _platform: Platform, _viewRepeater: _ViewRepeater<T, RenderRow<T>, RowContext<T>>, _coalescedStyleScheduler: _CoalescedStyleScheduler, _viewportRuler: ViewportRuler,
229+
_stickyPositioningListener: StickyPositioningListener);
230230
_getRenderedRows(rowOutlet: RowOutlet): HTMLElement[];
231231
_getRowDefs(data: T, dataIndex: number): CdkRowDef<T>[];
232232
addColumnDef(columnDef: CdkColumnDef): void;
@@ -248,7 +248,7 @@ export declare class CdkTable<T> implements AfterContentChecked, CollectionViewe
248248
static ngAcceptInputType_fixedLayout: BooleanInput;
249249
static ngAcceptInputType_multiTemplateDataRows: BooleanInput;
250250
static ɵcmp: i0.ɵɵComponentDefWithMeta<CdkTable<any>, "cdk-table, table[cdk-table]", ["cdkTable"], { "trackBy": "trackBy"; "dataSource": "dataSource"; "multiTemplateDataRows": "multiTemplateDataRows"; "fixedLayout": "fixedLayout"; }, {}, ["_noDataRow", "_contentColumnDefs", "_contentRowDefs", "_contentHeaderRowDefs", "_contentFooterRowDefs"], ["caption", "colgroup, col"]>;
251-
static ɵfac: i0.ɵɵFactoryDef<CdkTable<any>, [null, null, null, { attribute: "role"; }, { optional: true; }, null, null, { optional: true; }, { optional: true; }, { optional: true; skipSelf: true; }, { optional: true; }]>;
251+
static ɵfac: i0.ɵɵFactoryDef<CdkTable<any>, [null, null, null, { attribute: "role"; }, { optional: true; }, null, null, null, null, null, { optional: true; skipSelf: true; }]>;
252252
}
253253

254254
export declare class CdkTableModule {
@@ -344,8 +344,7 @@ export declare type StickySize = number | null | undefined;
344344

345345
export declare class StickyStyler {
346346
direction: Direction;
347-
constructor(_isNativeHtmlTable: boolean, _stickCellCss: string, direction: Direction,
348-
_coalescedStyleScheduler?: _CoalescedStyleScheduler | undefined, _isBrowser?: boolean, _needsPositionStickyOnElement?: boolean, _positionListener?: StickyPositioningListener | undefined);
347+
constructor(_isNativeHtmlTable: boolean, _stickCellCss: string, direction: Direction, _coalescedStyleScheduler: _CoalescedStyleScheduler, _isBrowser?: boolean, _needsPositionStickyOnElement?: boolean, _positionListener?: StickyPositioningListener | undefined);
349348
_addStickyStyle(element: HTMLElement, dir: StickyDirection, dirValue: number, isBorderElement: boolean): void;
350349
_getCalculatedZIndex(element: HTMLElement): string;
351350
_getCellWidths(row: HTMLElement, recalculateCellWidths?: boolean): number[];

0 commit comments

Comments
 (0)