Skip to content
Open
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
4 changes: 2 additions & 2 deletions src/material/sort/sort-header.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
<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-descending]="_sort.direction === 'desc'"
[class.mat-sort-header-ascending]="_sort.direction === 'asc'"
[class.mat-sort-header-descending]="_getArrowDirection() === 'desc'"
[class.mat-sort-header-ascending]="_getArrowDirection() === 'asc'"
[class.mat-sort-header-recently-cleared-ascending]="_recentlyCleared() === 'asc'"
[class.mat-sort-header-recently-cleared-descending]="_recentlyCleared() === 'desc'"
[class.mat-sort-header-animations-disabled]="_animationsDisabled"
Expand Down
8 changes: 8 additions & 0 deletions src/material/sort/sort-header.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,14 @@ export class MatSortHeader implements MatSortable, OnDestroy, OnInit, AfterViewI
);
}

/**
* Gets the arrow direction for this sort header. Uses the active sort direction
* when this header is sorted, otherwise falls back to its initial sort direction.
*/
_getArrowDirection(): SortDirection {
return this._isSorted() ? this._sort.direction : this.start || this._sort.start;
}

_isDisabled() {
return this._sort.disabled || this.disabled;
}
Expand Down
36 changes: 36 additions & 0 deletions src/material/sort/sort.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,42 @@ describe('MatSort', () => {
expect(component.matSort.direction).toBe('asc');
});

it('should use initial sort start direction on unsorted headers when another column is sorted', () => {
const containerA = fixture.nativeElement.querySelector(
'#defaultA .mat-sort-header-container',
);
const containerB = fixture.nativeElement.querySelector(
'#defaultB .mat-sort-header-container',
);
const containerOverride = fixture.nativeElement.querySelector(
'#overrideStart .mat-sort-header-container',
);

expect(containerA.classList.contains('mat-sort-header-ascending')).toBe(true);
expect(containerA.classList.contains('mat-sort-header-descending')).toBe(false);
expect(containerOverride.classList.contains('mat-sort-header-descending')).toBe(true);
expect(containerOverride.classList.contains('mat-sort-header-ascending')).toBe(false);

// Sort defaultA to desc (asc -> desc)
component.sort('defaultA');
component.sort('defaultA');
fixture.detectChanges();

expect(containerA.classList.contains('mat-sort-header-descending')).toBe(true);
expect(containerA.classList.contains('mat-sort-header-ascending')).toBe(false);
expect(containerB.classList.contains('mat-sort-header-ascending')).toBe(true);
expect(containerB.classList.contains('mat-sort-header-descending')).toBe(false);
expect(containerOverride.classList.contains('mat-sort-header-descending')).toBe(true);

// Unsorted headers should also reflect parent matSortStart changes
component.start = 'desc';
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();

expect(containerB.classList.contains('mat-sort-header-descending')).toBe(true);
expect(containerB.classList.contains('mat-sort-header-ascending')).toBe(false);
});

it(
'should throw an error if an MatSortable is not contained within an MatSort ' + 'directive',
() => {
Expand Down
Loading