Skip to content

Commit ed1b956

Browse files
committed
update to support disabled
1 parent fb95143 commit ed1b956

File tree

5 files changed

+58
-37
lines changed

5 files changed

+58
-37
lines changed

src/demo-app/table/table-demo.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,8 @@ <h3>MatTable With MatTableDataSource Example</h3>
235235
selected
236236
</div>
237237

238-
<mat-table [dataSource]="matTableDataSource" [trackBy]="userTrackBy" matSort
238+
<mat-table [dataSource]="matTableDataSource" [trackBy]="userTrackBy"
239+
matSort matSortActive="progress" matSortDirection="asc"
239240
#sortForDataSource="matSort">
240241

241242
<!-- Checkbox Column -->

src/demo-app/table/table-demo.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export class TableDemo {
4343
displayedColumns: UserProperties[] = [];
4444
trackByStrategy: TrackByStrategy = 'reference';
4545
changeReferences = false;
46-
progressSortingDisabled = false;
46+
progressSortingDisabled = true;
4747
highlights = new Set<string>();
4848
wasExpanded = new Set<UserData>();
4949

src/lib/sort/sort-header.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@
33
[class.mat-sort-header-position-before]="arrowPosition == 'before'">
44
<button class="mat-sort-header-button" type="button"
55
[attr.disabled]="_isDisabled() || null"
6+
[attr.aria-label]="_intl.sortButtonLabel(id)"
67
(focus)="_setIndicatorHintVisible(true)"
7-
(blur)="_setIndicatorHintVisible(false)"
8-
[attr.aria-label]="_intl.sortButtonLabel(id)">
8+
(blur)="_setIndicatorHintVisible(false)">
99
<ng-content></ng-content>
1010
</button>
1111

1212
<!-- Disable animations while a current animation is running -->
1313
<div class="mat-sort-header-arrow"
1414
[@arrowOpacity]="_getArrowViewState()"
1515
[@arrowPosition]="_getArrowViewState()"
16-
(@arrowPosition.start)="_disableStateAnimation = true"
17-
(@arrowPosition.done)="_disableStateAnimation = false">
16+
(@arrowPosition.start)="_disableViewStateAnimation = true"
17+
(@arrowPosition.done)="_disableViewStateAnimation = false">
1818
<div class="mat-sort-header-stem"></div>
1919
<div class="mat-sort-header-indicator" [@indicator]="_getArrowDirectionState()" >
2020
<div class="mat-sort-header-pointer-left" [@leftPointer]="_getArrowDirectionState()"></div>

src/lib/sort/sort-header.ts

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -202,20 +202,20 @@ export class MatSortHeader extends _MatSortHeaderMixinBase implements MatSortabl
202202
throw getSortHeaderNotContainedWithinSortError();
203203
}
204204

205-
const changes = [_sort.sortChange, _sort._stateChanges, _intl.changes];
206-
this._rerenderSubscription = merge(changes).subscribe(() => {
207-
if (this._isSorted()) {
208-
this._updateArrowDirection();
209-
}
210-
211-
// If this header was recently active and now no longer sorted, animate away the arrow.
212-
if (!this._isSorted() && this.viewState && this._viewState.toState === 'active') {
213-
this._disableViewStateAnimation = false;
214-
this._setAnimationTransitionState({fromState: 'active', toState: this._arrowDirection});
215-
}
216-
217-
changeDetectorRef.markForCheck();
218-
});
205+
this._rerenderSubscription = merge(_sort.sortChange, _sort._stateChanges, _intl.changes)
206+
.subscribe(() => {
207+
if (this._isSorted()) {
208+
this._updateArrowDirection();
209+
}
210+
211+
// If this header was recently active and now no longer sorted, animate away the arrow.
212+
if (!this._isSorted() && this._viewState && this._viewState.toState === 'active') {
213+
this._disableViewStateAnimation = false;
214+
this._setAnimationTransitionState({fromState: 'active', toState: this._arrowDirection});
215+
}
216+
217+
changeDetectorRef.markForCheck();
218+
});
219219
}
220220

221221
ngOnInit() {
@@ -225,7 +225,8 @@ export class MatSortHeader extends _MatSortHeaderMixinBase implements MatSortabl
225225

226226
// Initialize the direction of the arrow and set the view state to be immediately that state.
227227
this._updateArrowDirection();
228-
this._setAnimationTransitionState({toState: this._arrowDirection});
228+
this._setAnimationTransitionState(
229+
{toState: this._isSorted() ? 'active' : this._arrowDirection});
229230

230231
this._sort.register(this);
231232
}
@@ -240,6 +241,9 @@ export class MatSortHeader extends _MatSortHeaderMixinBase implements MatSortabl
240241
* user showing what the active sort will become. If set to false, the arrow will fade away.
241242
*/
242243
_setIndicatorHintVisible(visible: boolean) {
244+
// No-op if the sort header is disabled - should not make the hint visible.
245+
if (this._isDisabled() && visible) { return; }
246+
243247
this._showIndicatorHint = visible;
244248

245249
if (!this._isSorted()) {

src/lib/sort/sort.spec.ts

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,15 @@ describe('MatSort', () => {
180180
expectedStates.set('defaultB', {viewState: 'asc-to-active', arrowDirection: 'active-asc'});
181181
component.expectViewAndDirectionStates(expectedStates);
182182
});
183+
184+
it('should be correct when sort has been disabled', () => {
185+
// Mousing over the first sort should set the view state to hint
186+
component.disabledColumnSort = true;
187+
fixture.detectChanges();
188+
189+
component.dispatchMouseEvent('defaultA', 'mouseenter');
190+
component.expectViewAndDirectionStates(expectedStates);
191+
});
183192
});
184193

185194
it('should be able to cycle from asc -> desc from either start point', () => {
@@ -204,38 +213,38 @@ describe('MatSort', () => {
204213
});
205214

206215
it('should allow for the cycling the sort direction to be disabled per column', () => {
207-
const button = fixture.nativeElement.querySelector('#defaultSortHeaderA button');
216+
const button = fixture.nativeElement.querySelector('#defaultA button');
208217

209-
component.sort('defaultSortHeaderA');
218+
component.sort('defaultA');
210219
expect(component.matSort.direction).toBe('asc');
211220
expect(button.getAttribute('disabled')).toBeFalsy();
212221

213222
component.disabledColumnSort = true;
214223
fixture.detectChanges();
215224

216-
component.sort('defaultSortHeaderA');
225+
component.sort('defaultA');
217226
expect(component.matSort.direction).toBe('asc');
218227
expect(button.getAttribute('disabled')).toBe('true');
219228
});
220229

221230
it('should allow for the cycling the sort direction to be disabled for all columns', () => {
222-
const button = fixture.nativeElement.querySelector('#defaultSortHeaderA button');
231+
const button = fixture.nativeElement.querySelector('#defaultA button');
223232

224-
component.sort('defaultSortHeaderA');
225-
expect(component.matSort.active).toBe('defaultSortHeaderA');
233+
component.sort('defaultA');
234+
expect(component.matSort.active).toBe('defaultA');
226235
expect(component.matSort.direction).toBe('asc');
227236
expect(button.getAttribute('disabled')).toBeFalsy();
228237

229238
component.disableAllSort = true;
230239
fixture.detectChanges();
231240

232-
component.sort('defaultSortHeaderA');
233-
expect(component.matSort.active).toBe('defaultSortHeaderA');
241+
component.sort('defaultA');
242+
expect(component.matSort.active).toBe('defaultA');
234243
expect(component.matSort.direction).toBe('asc');
235244
expect(button.getAttribute('disabled')).toBe('true');
236245

237-
component.sort('defaultSortHeaderB');
238-
expect(component.matSort.active).toBe('defaultSortHeaderA');
246+
component.sort('defaultB');
247+
expect(component.matSort.active).toBe('defaultA');
239248
expect(component.matSort.direction).toBe('asc');
240249
expect(button.getAttribute('disabled')).toBe('true');
241250
});
@@ -359,7 +368,7 @@ describe('MatSort', () => {
359368
*/
360369
function testSingleColumnSortDirectionSequence(fixture: ComponentFixture<SimpleMatSortApp>,
361370
expectedSequence: SortDirection[],
362-
id: string = 'defaultA') {
371+
id: SimpleMatSortAppColumnIds = 'defaultA') {
363372
// Detect any changes that were made in preparation for this sort sequence
364373
fixture.detectChanges();
365374

@@ -387,6 +396,9 @@ function testSingleColumnSortDirectionSequence(fixture: ComponentFixture<SimpleM
387396
expect(component.matSort.direction).toBe(expectedSequence[0]);
388397
}
389398

399+
/** Column IDs of the SimpleMatSortApp for typing of function params in the component (e.g. sort) */
400+
type SimpleMatSortAppColumnIds = 'defaultA' | 'defaultB' | 'overrideStart' | 'overrideDisableClear';
401+
390402
@Component({
391403
template: `
392404
<div matSort
@@ -397,7 +409,9 @@ function testSingleColumnSortDirectionSequence(fixture: ComponentFixture<SimpleM
397409
[matSortDisableClear]="disableClear"
398410
(matSortChange)="latestSortEvent = $event">
399411
<div id="defaultA"
400-
#defaultA mat-sort-header="defaultA"[disabled]="disabledColumnSort">
412+
#defaultA
413+
mat-sort-header="defaultA"
414+
[disabled]="disabledColumnSort">
401415
A
402416
</div>
403417
<div id="defaultB"
@@ -408,12 +422,14 @@ function testSingleColumnSortDirectionSequence(fixture: ComponentFixture<SimpleM
408422
<div id="overrideStart"
409423
#overrideStart
410424
mat-sort-header="overrideStart" start="desc">
411-
D </div>
425+
D
426+
</div>
412427
<div id="overrideDisableClear"
413428
#overrideDisableClear
414429
mat-sort-header="overrideDisableClear"
415430
disableClear>
416-
E </div>
431+
E
432+
</div>
417433
</div>
418434
`
419435
})
@@ -435,11 +451,11 @@ class SimpleMatSortApp {
435451

436452
constructor (public elementRef: ElementRef) { }
437453

438-
sort(id: string) {
454+
sort(id: SimpleMatSortAppColumnIds) {
439455
this.dispatchMouseEvent(id, 'click');
440456
}
441457

442-
dispatchMouseEvent(id: string, event: string) {
458+
dispatchMouseEvent(id: SimpleMatSortAppColumnIds, event: string) {
443459
const sortElement = this.elementRef.nativeElement.querySelector(`#${id}`);
444460
dispatchMouseEvent(sortElement, event);
445461
}

0 commit comments

Comments
 (0)