Skip to content

Commit 008b011

Browse files
spliffonespike-rabbit
authored andcommitted
fix: ensure custom sort icons are applied
Ensure consumers can provide multiple classes for a single sort direction.
1 parent 32cbf11 commit 008b011

3 files changed

Lines changed: 93 additions & 12 deletions

File tree

‎projects/ngx-datatable/src/lib/components/header/header-cell.component.spec.ts‎

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
11
import { TestbedHarnessEnvironment } from '@angular/cdk/testing/testbed';
2-
import { AfterViewInit, Component, signal, TemplateRef, viewChild } from '@angular/core';
2+
import {
3+
AfterViewInit,
4+
Component,
5+
inputBinding,
6+
outputBinding,
7+
signal,
8+
TemplateRef,
9+
viewChild,
10+
WritableSignal
11+
} from '@angular/core';
312
import { ComponentFixture, TestBed } from '@angular/core/testing';
13+
import { userEvent } from '@vitest/browser/context';
414

515
import {
616
InnerSortEvent,
717
SortableTableColumnInternal,
818
TableColumnInternal
919
} from '../../types/internal.types';
20+
import { SortPropDir } from '../../types/public.types';
1021
import { toInternalColumn } from '../../utils/column-helper';
1122
import { DataTableHeaderCellComponent } from './header-cell.component';
1223
import { HeaderCellHarness } from './testing/header-cell.harnes';
@@ -160,3 +171,69 @@ describe('DataTableHeaderCellComponent with template', () => {
160171
});
161172
});
162173
});
174+
175+
describe('DataTableHeaderCellComponent - custom sort icons', () => {
176+
let fixture: ComponentFixture<DataTableHeaderCellComponent>;
177+
let sorts: WritableSignal<SortPropDir[]>;
178+
const column = signal({
179+
name: 'test',
180+
prop: 'test',
181+
sortable: true,
182+
resizeable: false,
183+
width: signal(20)
184+
});
185+
const sortAscendingIcon = signal('icon up');
186+
const sortDescendingIcon = signal('icon down');
187+
188+
beforeEach(async () => {
189+
sorts = signal<SortPropDir[]>([]);
190+
fixture = TestBed.createComponent(DataTableHeaderCellComponent, {
191+
bindings: [
192+
inputBinding('sortType', () => 'single'),
193+
inputBinding('ariaHeaderCheckboxMessage', () => 'Select All'),
194+
inputBinding('sortAscendingIcon', sortAscendingIcon),
195+
inputBinding('sortDescendingIcon', sortDescendingIcon),
196+
inputBinding('column', column),
197+
inputBinding('sorts', sorts),
198+
inputBinding('showResizeHandle', () => false),
199+
outputBinding('sort', (event: InnerSortEvent) => {
200+
sorts.set([{ prop: event.column.prop!, dir: event.newValue! }]);
201+
})
202+
]
203+
});
204+
await fixture.whenStable();
205+
});
206+
207+
it('should apply custom sortAscendingIcon class when toggling to ascending sort', async () => {
208+
const label = fixture.nativeElement.querySelector('.datatable-header-cell-label');
209+
// eslint-disable-next-line @angular-eslint/no-experimental
210+
await userEvent.click(label);
211+
await fixture.whenStable();
212+
213+
const sortBtn = fixture.nativeElement.querySelector('.sort-btn');
214+
215+
expect(sortBtn).toHaveClass('sort-btn');
216+
expect(sortBtn).toHaveClass('sort-asc');
217+
expect(sortBtn).toHaveClass('icon');
218+
expect(sortBtn).toHaveClass('up');
219+
expect(sortBtn).not.toHaveClass('datatable-icon-up');
220+
});
221+
222+
it('should apply custom sortDescendingIcon class when toggling to descending sort', async () => {
223+
const label = fixture.nativeElement.querySelector('.datatable-header-cell-label');
224+
// eslint-disable-next-line @angular-eslint/no-experimental
225+
await userEvent.click(label);
226+
await fixture.whenStable();
227+
228+
// eslint-disable-next-line @angular-eslint/no-experimental
229+
await userEvent.click(label);
230+
await fixture.whenStable();
231+
232+
const sortButton = fixture.nativeElement.querySelector('.sort-btn');
233+
expect(sortButton).toHaveClass('sort-btn');
234+
expect(sortButton).toHaveClass('sort-desc');
235+
expect(sortButton).toHaveClass('icon');
236+
expect(sortButton).toHaveClass('down');
237+
expect(sortButton).not.toHaveClass('datatable-icon-down');
238+
});
239+
});

‎projects/ngx-datatable/src/lib/components/header/header-cell.component.ts‎

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,9 @@ export class DataTableHeaderCellComponent implements OnInit, OnDestroy {
142142

143143
protected readonly isCheckboxable = computed(() => this.column().headerCheckboxable);
144144

145-
protected readonly sortClass = computed<string[] | undefined>(() => {
146-
return this.calcSortClass(this.sortDir());
147-
});
145+
protected readonly sortClass = computed<string | undefined>(() =>
146+
this.calcSortClass(this.sortDir())
147+
);
148148
protected readonly sortDir = computed<SortDirection | undefined>(() => {
149149
return this.calcSortDir(this.sorts());
150150
});
@@ -217,16 +217,20 @@ export class DataTableHeaderCellComponent implements OnInit, OnDestroy {
217217
});
218218
}
219219

220-
calcSortClass(sortDir: SortDirection | undefined): string[] | undefined {
220+
calcSortClass(sortDir: SortDirection | undefined): string | undefined {
221221
if (!this.cellContext().column.sortable) {
222222
return undefined;
223223
}
224-
if (sortDir === 'asc') {
225-
return ['sort-btn', 'sort-asc', this.sortAscendingIcon() ?? 'datatable-icon-up'];
226-
} else if (sortDir === 'desc') {
227-
return ['sort-btn', 'sort-desc', this.sortDescendingIcon() ?? 'datatable-icon-down'];
228-
} else {
229-
return ['sort-btn', this.sortUnsetIcon() ?? 'datatable-icon-sort-unset'];
224+
225+
const base = 'sort-btn';
226+
227+
switch (sortDir) {
228+
case 'asc':
229+
return `${base} sort-asc ${this.sortAscendingIcon() ?? 'datatable-icon-up'}`;
230+
case 'desc':
231+
return `${base} sort-desc ${this.sortDescendingIcon() ?? 'datatable-icon-down'}`;
232+
default:
233+
return `${base} ${this.sortUnsetIcon() ?? 'datatable-icon-sort-unset'}`;
230234
}
231235
}
232236

‎projects/ngx-datatable/tsconfig.spec.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"extends": "../../tsconfig.json",
33
"compilerOptions": {
44
"outDir": "../../out-tsc/spec",
5-
"types": ["vitest/globals"]
5+
"types": ["vitest/globals", "vitest/browser"]
66
},
77
"include": ["src/**/*.spec.ts", "src/**/*.d.ts"]
88
}

0 commit comments

Comments
 (0)