Skip to content

Commit

Permalink
fix(ktableview): sort order when changing sort column [KHCP-14449]
Browse files Browse the repository at this point in the history
  • Loading branch information
Leopoldthecoder committed Dec 18, 2024
1 parent 04151e5 commit e92d5a8
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
Binary file added cypress/downloads/downloads.html
Binary file not shown.
19 changes: 17 additions & 2 deletions src/components/KTableView/KTableView.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ const largeDataSet = [
const options = {
headers: [
{ label: 'Name', key: 'name', sortable: true },
{ label: 'ID', key: 'id', sortable: false },
{ label: 'ID', key: 'id', sortable: true },
{ label: 'Enabled', key: 'enabled', sortable: false },
{ label: '', key: 'actions', sortable: false },
] as TableHeader[],
Expand Down Expand Up @@ -445,7 +445,7 @@ describe('KTableView', () => {
})

cy.get('th').each(($el, index) => {
if (index === 0) {
if (index <= 1) {
cy.wrap($el).should('have.class', 'sortable')
}
})
Expand All @@ -464,6 +464,21 @@ describe('KTableView', () => {
cy.wrap(Cypress.vueWrapper.emitted()).should('have.property', 'sort').and('have.length', 1)
})
})

it('should have correct sort order class when changing sort column', () => {
cy.mount(KTableView, {
props: {
headers: options.headers,
data: options.data,
},
})
cy.get('th').eq(0).click().then(() => {
cy.get('th').eq(0).should('have.class', 'asc')
cy.get('th').eq(1).click().then(() => {
cy.get('th').eq(1).should('have.class', 'asc')
})
})
})
})

describe('pagination', () => {
Expand Down
6 changes: 5 additions & 1 deletion src/components/KTableView/KTableView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -701,10 +701,14 @@ const getHeaderClasses = (column: TableViewHeader, index: number): Record<string
const onHeaderClick = (column: TableViewHeader) => {
if (column.sortable && column.key !== TableViewHeaderKeys.BULK_ACTIONS && column.key !== TableViewHeaderKeys.ACTIONS) {
let newSortColumnOrder = 'asc'
if (column.key === sortColumnKey.value && sortColumnOrder.value === 'asc') {
newSortColumnOrder = 'desc'
}
emit('sort', {
prevKey: sortColumnKey.value,
sortColumnKey: column.key,
sortColumnOrder: sortColumnOrder.value === 'asc' ? 'desc' : 'asc', // display opposite because sortColumnOrder outdated
sortColumnOrder: newSortColumnOrder,
})
sortClickHandler(column)
}
Expand Down

0 comments on commit e92d5a8

Please sign in to comment.