Skip to content

feat(sort): add manual sort #27516

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
50 changes: 50 additions & 0 deletions src/material/sort/sort.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,56 @@ describe('MatSort', () => {
expect(containerA.classList.contains('mat-sort-header-position-before')).toBe(true);
expect(containerB.classList.contains('mat-sort-header-position-before')).toBe(true);
});

it('should correctly sort when passing same direction twice to sort method', () => {
const matSortWithArrowPositionFixture = TestBed.createComponent(MatSortWithArrowPosition);
matSortWithArrowPositionFixture.detectChanges();
const matSortWithArrowPositionComponent = matSortWithArrowPositionFixture.componentInstance;

matSortWithArrowPositionComponent.matSort.sort({
id: 'defaultA',
direction: 'desc',
disableClear: false,
});

matSortWithArrowPositionComponent.matSort.sort({
id: 'defaultA',
direction: 'desc',
disableClear: false,
});

expect(matSortWithArrowPositionComponent.matSort.direction).toBe('desc');
});

it('should correctly sort when passing going desc -> asc -> desc', () => {
const matSortWithArrowPositionFixture = TestBed.createComponent(MatSortWithArrowPosition);
matSortWithArrowPositionFixture.detectChanges();
const matSortWithArrowPositionComponent = matSortWithArrowPositionFixture.componentInstance;

matSortWithArrowPositionComponent.matSort.sort({
id: 'defaultA',
direction: 'desc',
disableClear: false,
});

expect(matSortWithArrowPositionComponent.matSort.direction).toBe('desc');

matSortWithArrowPositionComponent.matSort.sort({
id: 'defaultA',
direction: 'asc',
disableClear: false,
});

expect(matSortWithArrowPositionComponent.matSort.direction).toBe('asc');

matSortWithArrowPositionComponent.matSort.sort({
id: 'defaultA',
direction: 'desc',
disableClear: false,
});

expect(matSortWithArrowPositionComponent.matSort.direction).toBe('desc');
});
});

describe('with default options', () => {
Expand Down
29 changes: 26 additions & 3 deletions src/material/sort/sort.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,17 @@ export interface MatSortable {
/** Whether to disable clearing the sorting state. */
disableClear: boolean;
}
/** Interface for user manual sort. */
export interface MatManualSort {
/** The id of the column being sorted. */
id: string;

/** set direction to exact direction. */
direction: SortDirection;

/** Whether to disable clearing the sorting state. */
disableClear: boolean;
}

/** The current sort state. */
export interface Sort {
Expand Down Expand Up @@ -166,17 +177,29 @@ export class MatSort
}

/** Sets the active sort id and determines the new sort direction. */
sort(sortable: MatSortable): void {
sort(sortable: MatSortable | MatManualSort): void {
if (this.active != sortable.id) {
this.active = sortable.id;
this.direction = sortable.start ? sortable.start : this.start;
if (this._isMatManualSort(sortable)) {
this.direction = sortable.direction;
} else {
this.direction = sortable.start ? sortable.start : this.start;
}
} else {
this.direction = this.getNextSortDirection(sortable);
if (this._isMatManualSort(sortable)) {
this.direction = sortable.direction;
} else {
this.direction = this.getNextSortDirection(sortable);
}
}

this.sortChange.emit({active: this.active, direction: this.direction});
}

private _isMatManualSort(sortable: MatSortable | MatManualSort): sortable is MatManualSort {
return (sortable as MatManualSort).direction !== undefined;
}

/** Returns the next sort direction of the active sortable, checking for potential overrides. */
getNextSortDirection(sortable: MatSortable): SortDirection {
if (!sortable) {
Expand Down