Skip to content
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

feat(datagrid): add ability to unsort datagrid colums #1617

Draft
wants to merge 13 commits into
base: main
Choose a base branch
from
Draft
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
2 changes: 2 additions & 0 deletions .storybook/stories/datagrid/datagrid-column.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export default {
expandable: false,
compact: false,
hidableColumns: false,
clrDgSortDisableUnsort: false,
height: 0,
ClrDatagridSortOrder: ClrDatagridSortOrder,
},
Expand Down Expand Up @@ -95,6 +96,7 @@ const ColumnFilterTemplate: StoryFn = args => ({
[clrFilterNumberMinPlaceholder]="clrFilterNumberMinPlaceholder"
[clrFilterStringPlaceholder]="clrFilterStringPlaceholder"
[clrFilterValue]="clrFilterValue"
[clrDgSortDisableUnsort]="clrDgSortDisableUnsort"
(clrDgColumnResize)="clrDgColumnResize($event)"
(clrDgSortOrderChange)="clrDgSortOrderChange($event)"
(clrFilterValueChange)="clrFilterValueChange($event)"
Expand Down
4 changes: 3 additions & 1 deletion projects/angular/clarity.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1311,6 +1311,8 @@ export class ClrDatagridColumn<T = any> extends DatagridFilterRegistrar<T, ClrDa
// (undocumented)
get ariaSort(): "none" | "ascending" | "descending";
// (undocumented)
clrDgSortDisableUnsort: boolean;
// (undocumented)
get colType(): 'string' | 'number';
set colType(value: 'string' | 'number');
customFilter: boolean;
Expand Down Expand Up @@ -1357,7 +1359,7 @@ export class ClrDatagridColumn<T = any> extends DatagridFilterRegistrar<T, ClrDa
// (undocumented)
get _view(): any;
// (undocumented)
static ɵcmp: i0.ɵɵComponentDeclaration<ClrDatagridColumn<any>, "clr-dg-column", never, { "filterStringPlaceholder": "clrFilterStringPlaceholder"; "filterNumberMaxPlaceholder": "clrFilterNumberMaxPlaceholder"; "filterNumberMinPlaceholder": "clrFilterNumberMinPlaceholder"; "colType": "clrDgColType"; "field": "clrDgField"; "sortBy": "clrDgSortBy"; "sortOrder": "clrDgSortOrder"; "updateFilterValue": "clrFilterValue"; }, { "sortOrderChange": "clrDgSortOrderChange"; "filterValueChange": "clrFilterValueChange"; }, ["projectedFilter"], ["clr-dg-filter, clr-dg-string-filter, clr-dg-numeric-filter", "*"], false, [{ directive: typeof i1_6.ClrPopoverHostDirective; inputs: {}; outputs: {}; }]>;
static ɵcmp: i0.ɵɵComponentDeclaration<ClrDatagridColumn<any>, "clr-dg-column", never, { "filterStringPlaceholder": "clrFilterStringPlaceholder"; "filterNumberMaxPlaceholder": "clrFilterNumberMaxPlaceholder"; "filterNumberMinPlaceholder": "clrFilterNumberMinPlaceholder"; "clrDgSortDisableUnsort": "clrDgSortDisableUnsort"; "colType": "clrDgColType"; "field": "clrDgField"; "sortBy": "clrDgSortBy"; "sortOrder": "clrDgSortOrder"; "updateFilterValue": "clrFilterValue"; }, { "sortOrderChange": "clrDgSortOrderChange"; "filterValueChange": "clrFilterValueChange"; }, ["projectedFilter"], ["clr-dg-filter, clr-dg-string-filter, clr-dg-numeric-filter", "*"], false, [{ directive: typeof i1_6.ClrPopoverHostDirective; inputs: {}; outputs: {}; }]>;
// (undocumented)
static ɵfac: i0.ɵɵFactoryDeclaration<ClrDatagridColumn<any>, never>;
}
Expand Down
16 changes: 15 additions & 1 deletion projects/angular/src/data/datagrid/datagrid-column.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ export default function (): void {
expect(component.sortOrder).toBe(ClrDatagridSortOrder.ASC);
component.sort();
expect(component.sortOrder).toBe(ClrDatagridSortOrder.DESC);
component.sort();
expect(component.sortOrder).toBe(ClrDatagridSortOrder.UNSORTED);
});

it('knows when the column has an ascending sortDirection', function () {
Expand All @@ -114,12 +116,21 @@ export default function (): void {

it('sets the column sortDirection to null when sort is cleared', function () {
component.sortBy = comparator;
expect(component.sortDirection).toBe(undefined);
expect(component.sortDirection).toBeUndefined();
component.sort();
sortService.clear();
expect(component.sortDirection).toBeNull();
});

it('sets the column sortDirection to null when sort is rotated to initial state', function () {
component.sortBy = comparator;
expect(component.sortDirection).toBeUndefined();
component.sort();
component.sort();
component.sort();
expect(component.sortDirection).toBeNull();
});

it('offers a shortcut to sort based on a property name', function () {
component.field = 'test';
expect(sortService.comparator).toBeUndefined();
Expand Down Expand Up @@ -312,6 +323,9 @@ export default function (): void {
title.click();
context.detectChanges();
expect(context.clarityDirective.sortOrder).toBe(ClrDatagridSortOrder.DESC);
title.click();
context.detectChanges();
expect(context.clarityDirective.sortOrder).toBe(ClrDatagridSortOrder.UNSORTED);
});

it('adds and removes the correct direction when sorting', function () {
Expand Down
34 changes: 20 additions & 14 deletions projects/angular/src/data/datagrid/datagrid-column.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ export class ClrDatagridColumn<T = any>
@Input('clrFilterStringPlaceholder') filterStringPlaceholder: string;
@Input('clrFilterNumberMaxPlaceholder') filterNumberMaxPlaceholder: string;
@Input('clrFilterNumberMinPlaceholder') filterNumberMinPlaceholder: string;
@Input('clrDgSortDisableUnsort') clrDgSortDisableUnsort = false;

@Output('clrDgSortOrderChange') sortOrderChange = new EventEmitter<ClrDatagridSortOrder>();
@Output('clrFilterValueChange') filterValueChange = new EventEmitter();
Expand Down Expand Up @@ -202,16 +203,12 @@ export class ClrDatagridColumn<T = any>
set sortBy(comparator: ClrDatagridComparatorInterface<T> | string) {
if (typeof comparator === 'string') {
this._sortBy = new DatagridPropertyComparator(comparator);
} else if (comparator) {
this._sortBy = comparator;
} else if (this.field) {
this._sortBy = new DatagridPropertyComparator(this.field);
} else {
if (comparator) {
this._sortBy = comparator;
} else {
if (this.field) {
this._sortBy = new DatagridPropertyComparator(this.field);
} else {
delete this._sortBy;
}
}
delete this._sortBy;
}
}

Expand All @@ -230,17 +227,18 @@ export class ClrDatagridColumn<T = any>
}

switch (value) {
// the Unsorted case happens when the current state is either Asc or Desc
default:
case ClrDatagridSortOrder.UNSORTED:
this._sort.clear();
break;
case ClrDatagridSortOrder.ASC:
this.sort(false);
break;
case ClrDatagridSortOrder.DESC:
this.sort(true);
break;
// the Unsorted case happens when the current state is neither Asc nor Desc
case ClrDatagridSortOrder.UNSORTED:
default:
this._sort.clear();
this._sortDirection = null;
break;
}
}

Expand Down Expand Up @@ -357,6 +355,14 @@ export class ClrDatagridColumn<T = any>
return;
}

if (!this.clrDgSortDisableUnsort && reverse === undefined && this.sortOrder === ClrDatagridSortOrder.DESC) {
this._sortOrder = ClrDatagridSortOrder.UNSORTED;
this._sort.clear();
this._sortDirection = null;
this.sortOrderChange.emit(this._sortOrder);
return;
}

this._sort.toggle(this._sortBy, reverse);

// setting the private variable to not retrigger the setter logic
Expand Down
9 changes: 7 additions & 2 deletions projects/demo/src/app/datagrid/sorting/sorting.html
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,13 @@ <h2>Custom sort</h2>
<clr-datagrid>
<clr-dg-column>User ID</clr-dg-column>
<clr-dg-column>Name</clr-dg-column>
<clr-dg-column>Creation date</clr-dg-column>
<clr-dg-column [clrDgField]="'pokemon.name'" [clrDgSortBy]="pokemonComparator" [(clrDgSortOrder)]="sortOrder">
<clr-dg-column [clrDgField]="'creation'" [clrDgSortBy]="userCreationComparator">Creation date</clr-dg-column>
<clr-dg-column
[clrDgField]="'pokemon.name'"
[clrDgSortBy]="pokemonComparator"
[(clrDgSortOrder)]="sortOrder"
[clrDgSortDisableUnsort]="true"
>
Pokemon
</clr-dg-column>
<clr-dg-column>Favorite color</clr-dg-column>
Expand Down
9 changes: 8 additions & 1 deletion projects/demo/src/app/datagrid/sorting/sorting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,18 @@
*/

import { Component } from '@angular/core';
import { ClrDatagridSortOrder } from '@clr/angular';
import { ClrDatagridComparatorInterface, ClrDatagridSortOrder } from '@clr/angular';

import { Inventory } from '../inventory/inventory';
import { User } from '../inventory/user';
import { PokemonComparator } from '../utils/pokemon-comparator';

export class UserCreationComparator implements ClrDatagridComparatorInterface<User> {
compare(a: User, b: User) {
return a.creation.getTime() - b.creation.getTime();
}
}

@Component({
selector: 'clr-datagrid-sorting-demo',
providers: [Inventory],
Expand All @@ -23,6 +29,7 @@ export class DatagridSortingDemo {
sortOrder: ClrDatagridSortOrder = ClrDatagridSortOrder.UNSORTED;

pokemonComparator = new PokemonComparator();
userCreationComparator = new UserCreationComparator();

constructor(inventory: Inventory) {
inventory.size = 10;
Expand Down
Loading