Skip to content

refactor(sort): remove sort header dependency on table #13507

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/cdk/table/cell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ export const _CdkColumnDefBase: CanStickCtor & typeof CdkColumnDefBase =
*/
@Directive({
selector: '[cdkColumnDef]',
inputs: ['sticky']
inputs: ['sticky'],
providers: [{
provide: 'MAT_SORT_HEADER_COLUMN_DEF',
useExisting: CdkColumnDef
}],
})
export class CdkColumnDef extends _CdkColumnDefBase implements CanStick {
/** Unique name for this column. */
Expand Down
1 change: 0 additions & 1 deletion src/lib/sort/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ ng_module(
"@angular//packages/core",
"@rxjs",
"//src/cdk/coercion",
"//src/cdk/table",
"//src/lib/core",
],
)
Expand Down
19 changes: 14 additions & 5 deletions src/lib/sort/sort-header.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
*/

import {coerceBooleanProperty} from '@angular/cdk/coercion';
import {CdkColumnDef} from '@angular/cdk/table';
import {
ChangeDetectionStrategy,
ChangeDetectorRef,
Expand All @@ -17,6 +16,7 @@ import {
OnInit,
Optional,
ViewEncapsulation,
Inject,
} from '@angular/core';
import {CanDisable, CanDisableCtor, mixinDisabled} from '@angular/material/core';
import {merge, Subscription} from 'rxjs';
Expand Down Expand Up @@ -53,6 +53,11 @@ export interface ArrowViewStateTransition {
toState: ArrowViewState;
}

/** Column definition associated with a `MatSortHeader`. */
interface MatSortHeaderColumnDef {
name: string;
}

/**
* Applies sorting behavior (click to change sort) and styles to an element, including an
* arrow to display the current sort direction.
Expand Down Expand Up @@ -134,8 +139,12 @@ export class MatSortHeader extends _MatSortHeaderMixinBase
constructor(public _intl: MatSortHeaderIntl,
changeDetectorRef: ChangeDetectorRef,
@Optional() public _sort: MatSort,
@Optional() public _cdkColumnDef: CdkColumnDef) {

@Inject('MAT_SORT_HEADER_COLUMN_DEF') @Optional()
public _columnDef: MatSortHeaderColumnDef) {
// Note that we use a string token for the `_columnDef`, because the value is provided both by
// `material/table` and `cdk/table` and we can't have the CDK depending on Material,
// and we want to avoid having the sort header depending on the CDK table because
// of this single reference.
super();

if (!_sort) {
Expand All @@ -159,8 +168,8 @@ export class MatSortHeader extends _MatSortHeaderMixinBase
}

ngOnInit() {
if (!this.id && this._cdkColumnDef) {
this.id = this._cdkColumnDef.name;
if (!this.id && this._columnDef) {
this.id = this._columnDef.name;
}

// Initialize the direction of the arrow and set the view state to be immediately that state.
Expand Down
5 changes: 4 additions & 1 deletion src/lib/table/cell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ export class MatFooterCellDef extends _CdkFooterCellDef {}
*/
@Directive({
selector: '[matColumnDef]',
providers: [{provide: CdkColumnDef, useExisting: MatColumnDef}],
providers: [
{provide: CdkColumnDef, useExisting: MatColumnDef},
{provide: 'MAT_SORT_HEADER_COLUMN_DEF', useExisting: MatColumnDef}
],
})
export class MatColumnDef extends CdkColumnDef {
/** Unique name for this column. */
Expand Down