Skip to content

Commit 4923175

Browse files
fix(material/sort): use initial start direction for unsorted header arrows
Compute the header arrow direction in _getArrowDirection() so unsorted headers use their own start direction instead of inheriting another active header's sort direction. Fixes #33933 Signed-off-by: Abdul karim mia <97598943+abdul-karim-mia@users.noreply.github.com>
1 parent 32cf962 commit 4923175

3 files changed

Lines changed: 46 additions & 2 deletions

File tree

‎src/material/sort/sort-header.html‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
<div class="mat-sort-header-container mat-focus-indicator"
1212
[class.mat-sort-header-sorted]="_isSorted()"
1313
[class.mat-sort-header-position-before]="arrowPosition === 'before'"
14-
[class.mat-sort-header-descending]="_sort.direction === 'desc'"
15-
[class.mat-sort-header-ascending]="_sort.direction === 'asc'"
14+
[class.mat-sort-header-descending]="_getArrowDirection() === 'desc'"
15+
[class.mat-sort-header-ascending]="_getArrowDirection() === 'asc'"
1616
[class.mat-sort-header-recently-cleared-ascending]="_recentlyCleared() === 'asc'"
1717
[class.mat-sort-header-recently-cleared-descending]="_recentlyCleared() === 'desc'"
1818
[class.mat-sort-header-animations-disabled]="_animationsDisabled"

‎src/material/sort/sort-header.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,14 @@ export class MatSortHeader implements MatSortable, OnDestroy, OnInit, AfterViewI
194194
);
195195
}
196196

197+
/**
198+
* Gets the arrow direction for this sort header. Uses the active sort direction
199+
* when this header is sorted, otherwise falls back to its initial sort direction.
200+
*/
201+
_getArrowDirection(): SortDirection {
202+
return this._isSorted() ? this._sort.direction : this.start || this._sort.start;
203+
}
204+
197205
_isDisabled() {
198206
return this._sort.disabled || this.disabled;
199207
}

‎src/material/sort/sort.spec.ts‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,42 @@ describe('MatSort', () => {
157157
expect(component.matSort.direction).toBe('asc');
158158
});
159159

160+
it('should use initial sort start direction on unsorted headers when another column is sorted', () => {
161+
const containerA = fixture.nativeElement.querySelector(
162+
'#defaultA .mat-sort-header-container',
163+
);
164+
const containerB = fixture.nativeElement.querySelector(
165+
'#defaultB .mat-sort-header-container',
166+
);
167+
const containerOverride = fixture.nativeElement.querySelector(
168+
'#overrideStart .mat-sort-header-container',
169+
);
170+
171+
expect(containerA.classList.contains('mat-sort-header-ascending')).toBe(true);
172+
expect(containerA.classList.contains('mat-sort-header-descending')).toBe(false);
173+
expect(containerOverride.classList.contains('mat-sort-header-descending')).toBe(true);
174+
expect(containerOverride.classList.contains('mat-sort-header-ascending')).toBe(false);
175+
176+
// Sort defaultA to desc (asc -> desc)
177+
component.sort('defaultA');
178+
component.sort('defaultA');
179+
fixture.detectChanges();
180+
181+
expect(containerA.classList.contains('mat-sort-header-descending')).toBe(true);
182+
expect(containerA.classList.contains('mat-sort-header-ascending')).toBe(false);
183+
expect(containerB.classList.contains('mat-sort-header-ascending')).toBe(true);
184+
expect(containerB.classList.contains('mat-sort-header-descending')).toBe(false);
185+
expect(containerOverride.classList.contains('mat-sort-header-descending')).toBe(true);
186+
187+
// Unsorted headers should also reflect parent matSortStart changes
188+
component.start = 'desc';
189+
fixture.changeDetectorRef.markForCheck();
190+
fixture.detectChanges();
191+
192+
expect(containerB.classList.contains('mat-sort-header-descending')).toBe(true);
193+
expect(containerB.classList.contains('mat-sort-header-ascending')).toBe(false);
194+
});
195+
160196
it(
161197
'should throw an error if an MatSortable is not contained within an MatSort ' + 'directive',
162198
() => {

0 commit comments

Comments
 (0)