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

fix(useHandleSortState): fix correct direction when switching from other active column #1876

Merged
merged 1 commit into from
Jan 7, 2023
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -522,4 +522,100 @@ describe('useHandleSortState', () => {
},
})
})

it('should set correct direction/active state when switching from active column', () => {
const { result } = renderHook(useHandleSortState, {
initialProps: {
one: {
active: true,
direction: 'asc',
},
two: {
direction: 'desc',
},
},
})

const sortHandler = {
one: expect.any(Function),
two: expect.any(Function),
}

const simulateOne = () => act(() => result.current.sortHandler.one())
const simulateTwo = () => act(() => result.current.sortHandler.two())

expect(result.current).toEqual({
activeSortName: 'one',
sortHandler,
sortState: {
one: {
active: true,
direction: 'asc',
reversed: false,
},
two: {
active: false,
direction: 'desc',
reversed: true,
},
},
})

simulateTwo()

expect(result.current).toEqual({
activeSortName: 'two',
sortHandler,
sortState: {
one: {
active: false,
direction: 'asc',
reversed: false,
},
two: {
active: true,
direction: 'desc',
reversed: true,
},
},
})

simulateTwo()

expect(result.current).toEqual({
activeSortName: null,
sortHandler,
sortState: {
one: {
active: false,
direction: 'asc',
reversed: false,
},
two: {
active: false,
direction: 'off',
reversed: undefined,
},
},
})

simulateOne()

expect(result.current).toEqual({
activeSortName: 'one',
sortHandler,
sortState: {
one: {
active: true,
direction: 'asc',
reversed: false,
},
two: {
active: false,
direction: 'off',
reversed: undefined,
},
},
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,10 @@ export function TableSort() {
const { sortState, sortHandler } = useHandleSortState({
column1: {
active: true,
direction: 'off',
direction: 'asc',
},
column2: {
direction: 'desc',
},
})

Expand Down Expand Up @@ -681,10 +684,18 @@ export function TableSort() {
active={sortState.column1.active}
reversed={sortState.column1.reversed}
>
<Th.SortButton text={'Name'} onClick={sortHandler.column1} />
<Th.SortButton text="Name" onClick={sortHandler.column1} />
</Th>
<Th scope="col" sortable>
<Th.SortButton text={'Min amount'} />
<Th
scope="col"
sortable
active={sortState.column2.active}
reversed={sortState.column2.reversed}
>
<Th.SortButton
text="Min amount"
onClick={sortHandler.column2}
/>
</Th>
</Tr>
</thead>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ export function useHandleSortState(
state.direction = state.lastDirection
state.active = true
state.lastDirection = null
} else if (!state.active && state.direction !== 'off') {
state.active = true
} else {
state.direction = getNextMode({
direction: state.direction,
Expand Down