From 4923175403a1ca5193191d9bff71db1fbbbf8b62 Mon Sep 17 00:00:00 2001 From: Abdul karim mia <97598943+abdul-karim-mia@users.noreply.github.com> Date: Thu, 12 Nov 2026 17:49:32 +0530 Subject: [PATCH] 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> --- src/material/sort/sort-header.html | 4 ++-- src/material/sort/sort-header.ts | 8 +++++++ src/material/sort/sort.spec.ts | 36 ++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 2 deletions(-) diff --git a/src/material/sort/sort-header.html b/src/material/sort/sort-header.html index 9cc1c26493a7..090dd526326a 100644 --- a/src/material/sort/sort-header.html +++ b/src/material/sort/sort-header.html @@ -11,8 +11,8 @@
{ 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', () => {