Skip to content

feat(material/sort): default arrow position in MatSortDefaultOptions #23609

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 1 commit into from
Feb 18, 2022
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
2 changes: 1 addition & 1 deletion src/material/sort/sort-header.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
-->
<div class="mat-sort-header-container mat-focus-indicator"
[class.mat-sort-header-sorted]="_isSorted()"
[class.mat-sort-header-position-before]="arrowPosition == 'before'"
[class.mat-sort-header-position-before]="arrowPosition === 'before'"
[attr.tabindex]="_isDisabled() ? null : 0"
role="button">

Expand Down
17 changes: 15 additions & 2 deletions src/material/sort/sort-header.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,13 @@ import {
} from '@angular/core';
import {CanDisable, mixinDisabled} from '@angular/material/core';
import {merge, Subscription} from 'rxjs';
import {MatSort, MatSortable} from './sort';
import {
MAT_SORT_DEFAULT_OPTIONS,
MatSort,
MatSortable,
MatSortDefaultOptions,
SortHeaderArrowPosition,
} from './sort';
import {matSortAnimations} from './sort-animations';
import {SortDirection} from './sort-direction';
import {getSortHeaderNotContainedWithinSortError} from './sort-errors';
Expand Down Expand Up @@ -134,7 +140,7 @@ export class MatSortHeader
@Input('mat-sort-header') id: string;

/** Sets the position of the arrow that displays when sorted. */
@Input() arrowPosition: 'before' | 'after' = 'after';
@Input() arrowPosition: SortHeaderArrowPosition = 'after';

/** Overrides the sort start value of the containing MatSort for this MatSortable. */
@Input() start: 'asc' | 'desc';
Expand Down Expand Up @@ -182,6 +188,9 @@ export class MatSortHeader
private _elementRef: ElementRef<HTMLElement>,
/** @breaking-change 14.0.0 _ariaDescriber will be required. */
@Optional() private _ariaDescriber?: AriaDescriber | null,
@Optional()
@Inject(MAT_SORT_DEFAULT_OPTIONS)
defaultOptions?: MatSortDefaultOptions,
) {
// 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,
Expand All @@ -193,6 +202,10 @@ export class MatSortHeader
throw getSortHeaderNotContainedWithinSortError();
}

if (defaultOptions?.arrowPosition) {
this.arrowPosition = defaultOptions?.arrowPosition;
}

this._handleStateChanges();
}

Expand Down
138 changes: 136 additions & 2 deletions src/material/sort/sort.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ describe('MatSort', () => {
MatSortDuplicateMatSortableIdsApp,
MatSortableMissingIdApp,
MatSortableInvalidDirection,
MatSortableInvalidDirection,
MatSortWithArrowPosition,
],
}).compileComponents();
}),
Expand Down Expand Up @@ -78,7 +80,6 @@ describe('MatSort', () => {
const cdkTableMatSortAppFixture = TestBed.createComponent(CdkTableMatSortApp);
const cdkTableMatSortAppComponent = cdkTableMatSortAppFixture.componentInstance;

cdkTableMatSortAppFixture.detectChanges();
cdkTableMatSortAppFixture.detectChanges();

const sortables = cdkTableMatSortAppComponent.matSort.sortables;
Expand All @@ -92,7 +93,6 @@ describe('MatSort', () => {
const matTableMatSortAppFixture = TestBed.createComponent(MatTableMatSortApp);
const matTableMatSortAppComponent = matTableMatSortAppFixture.componentInstance;

matTableMatSortAppFixture.detectChanges();
matTableMatSortAppFixture.detectChanges();

const sortables = matTableMatSortAppComponent.matSort.sortables;
Expand Down Expand Up @@ -439,6 +439,64 @@ describe('MatSort', () => {
descriptionElement = document.getElementById(descriptionId);
expect(descriptionElement?.textContent).toBe('Sort 2nd column');
});

it('should render arrows after sort header by default', () => {
const matSortWithArrowPositionFixture = TestBed.createComponent(MatSortWithArrowPosition);

matSortWithArrowPositionFixture.detectChanges();

const containerA = matSortWithArrowPositionFixture.nativeElement.querySelector(
'#defaultA .mat-sort-header-container',
);
const containerB = matSortWithArrowPositionFixture.nativeElement.querySelector(
'#defaultB .mat-sort-header-container',
);

expect(containerA.classList.contains('mat-sort-header-position-before')).toBe(false);
expect(containerB.classList.contains('mat-sort-header-position-before')).toBe(false);
});

it('should render arrows before if appropriate parameter passed', () => {
const matSortWithArrowPositionFixture = TestBed.createComponent(MatSortWithArrowPosition);
const matSortWithArrowPositionComponent = matSortWithArrowPositionFixture.componentInstance;
matSortWithArrowPositionComponent.arrowPosition = 'before';

matSortWithArrowPositionFixture.detectChanges();

const containerA = matSortWithArrowPositionFixture.nativeElement.querySelector(
'#defaultA .mat-sort-header-container',
);
const containerB = matSortWithArrowPositionFixture.nativeElement.querySelector(
'#defaultB .mat-sort-header-container',
);

expect(containerA.classList.contains('mat-sort-header-position-before')).toBe(true);
expect(containerB.classList.contains('mat-sort-header-position-before')).toBe(true);
});

it('should render arrows in proper position based on arrowPosition parameter', () => {
const matSortWithArrowPositionFixture = TestBed.createComponent(MatSortWithArrowPosition);
const matSortWithArrowPositionComponent = matSortWithArrowPositionFixture.componentInstance;

matSortWithArrowPositionFixture.detectChanges();

const containerA = matSortWithArrowPositionFixture.nativeElement.querySelector(
'#defaultA .mat-sort-header-container',
);
const containerB = matSortWithArrowPositionFixture.nativeElement.querySelector(
'#defaultB .mat-sort-header-container',
);

expect(containerA.classList.contains('mat-sort-header-position-before')).toBe(false);
expect(containerB.classList.contains('mat-sort-header-position-before')).toBe(false);

matSortWithArrowPositionComponent.arrowPosition = 'before';

matSortWithArrowPositionFixture.detectChanges();

expect(containerA.classList.contains('mat-sort-header-position-before')).toBe(true);
expect(containerB.classList.contains('mat-sort-header-position-before')).toBe(true);
});
});

describe('with default options', () => {
Expand Down Expand Up @@ -477,6 +535,45 @@ describe('MatSort', () => {
testSingleColumnSortDirectionSequence(fixture, ['desc', 'asc']);
});
});

describe('with default arrowPosition', () => {
let fixture: ComponentFixture<MatSortWithoutInputs>;

beforeEach(
waitForAsync(() => {
TestBed.configureTestingModule({
imports: [MatSortModule, MatTableModule, CdkTableModule, NoopAnimationsModule],
declarations: [MatSortWithoutInputs],
providers: [
{
provide: MAT_SORT_DEFAULT_OPTIONS,
useValue: {
disableClear: true,
arrowPosition: 'before',
},
},
],
}).compileComponents();
}),
);

beforeEach(() => {
fixture = TestBed.createComponent(MatSortWithoutInputs);
fixture.detectChanges();
});

it('should render arrows in proper position', () => {
const containerA = fixture.nativeElement.querySelector(
'#defaultA .mat-sort-header-container',
);
const containerB = fixture.nativeElement.querySelector(
'#defaultB .mat-sort-header-container',
);

expect(containerA.classList.contains('mat-sort-header-position-before')).toBe(true);
expect(containerB.classList.contains('mat-sort-header-position-before')).toBe(true);
});
});
});

/**
Expand Down Expand Up @@ -736,3 +833,40 @@ class MatSortWithoutExplicitInputs {
dispatchMouseEvent(sortElement, event);
}
}

@Component({
template: `
<div matSort>
<div id="defaultA" #defaultA mat-sort-header="defaultA" [arrowPosition]="arrowPosition">
A
</div>
<div id="defaultB" #defaultB mat-sort-header="defaultB" [arrowPosition]="arrowPosition">
B
</div>
</div>
`,
})
class MatSortWithArrowPosition {
arrowPosition?: 'before' | 'after';
@ViewChild(MatSort) matSort: MatSort;
@ViewChild('defaultA') defaultA: MatSortHeader;
@ViewChild('defaultB') defaultB: MatSortHeader;
}

@Component({
template: `
<div matSort>
<div id="defaultA" #defaultA mat-sort-header="defaultA">
A
</div>
<div id="defaultB" #defaultB mat-sort-header="defaultB">
B
</div>
</div>
`,
})
class MatSortWithoutInputs {
@ViewChild(MatSort) matSort: MatSort;
@ViewChild('defaultA') defaultA: MatSortHeader;
@ViewChild('defaultB') defaultB: MatSortHeader;
}
5 changes: 5 additions & 0 deletions src/material/sort/sort.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ import {
getSortInvalidDirectionError,
} from './sort-errors';

/** Position of the arrow that displays when sorted. */
export type SortHeaderArrowPosition = 'before' | 'after';

/** Interface for a directive that holds sorting state consumed by `MatSortHeader`. */
export interface MatSortable {
/** The id of the column being sorted. */
Expand All @@ -53,6 +56,8 @@ export interface Sort {
export interface MatSortDefaultOptions {
/** Whether to disable clearing the sorting state. */
disableClear?: boolean;
/** Position of the arrow that displays when sorted. */
arrowPosition?: SortHeaderArrowPosition;
}

/** Injection token to be used to override the default options for `mat-sort`. */
Expand Down
10 changes: 7 additions & 3 deletions tools/public_api_guard/material/sort.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,17 @@ export const matSortAnimations: {

// @public
export interface MatSortDefaultOptions {
arrowPosition?: SortHeaderArrowPosition;
disableClear?: boolean;
}

// @public
export class MatSortHeader extends _MatSortHeaderBase implements CanDisable, MatSortable, OnDestroy, OnInit, AfterViewInit {
constructor(
_intl: MatSortHeaderIntl, _changeDetectorRef: ChangeDetectorRef, _sort: MatSort, _columnDef: MatSortHeaderColumnDef, _focusMonitor: FocusMonitor, _elementRef: ElementRef<HTMLElement>,
_ariaDescriber?: AriaDescriber | null | undefined);
_ariaDescriber?: AriaDescriber | null | undefined, defaultOptions?: MatSortDefaultOptions);
_arrowDirection: SortDirection;
arrowPosition: 'before' | 'after';
arrowPosition: SortHeaderArrowPosition;
// (undocumented)
_columnDef: MatSortHeaderColumnDef;
get disableClear(): boolean;
Expand Down Expand Up @@ -146,7 +147,7 @@ export class MatSortHeader extends _MatSortHeaderBase implements CanDisable, Mat
// (undocumented)
static ɵcmp: i0.ɵɵComponentDeclaration<MatSortHeader, "[mat-sort-header]", ["matSortHeader"], { "disabled": "disabled"; "id": "mat-sort-header"; "arrowPosition": "arrowPosition"; "start": "start"; "sortActionDescription": "sortActionDescription"; "disableClear": "disableClear"; }, {}, never, ["*"]>;
// (undocumented)
static ɵfac: i0.ɵɵFactoryDeclaration<MatSortHeader, [null, null, { optional: true; }, { optional: true; }, null, null, { optional: true; }]>;
static ɵfac: i0.ɵɵFactoryDeclaration<MatSortHeader, [null, null, { optional: true; }, { optional: true; }, null, null, { optional: true; }, { optional: true; }]>;
}

// @public
Expand Down Expand Up @@ -177,6 +178,9 @@ export interface Sort {
// @public
export type SortDirection = 'asc' | 'desc' | '';

// @public
export type SortHeaderArrowPosition = 'before' | 'after';

// (No @packageDocumentation comment for this package)

```