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 472e728
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
21 changes: 19 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,23 @@ 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.wrap(Cypress.vueWrapper.emitted('sort')).should('have.length', 1)
cy.wrap(Cypress.vueWrapper.emitted('sort')?.[0]?.[0]).should('have.property', 'sortColumnOrder', 'asc')
cy.get('th').eq(1).click().then(() => {
cy.wrap(Cypress.vueWrapper.emitted()).should('have.property', 'sort').and('have.length', 2)
cy.wrap(Cypress.vueWrapper.emitted('sort')?.[1]?.[0]).should('have.property', 'sortColumnOrder', '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 472e728

Please sign in to comment.