fix: align useTableSort with PHP version — userClicked ref, nullable activeSort
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
interface SortIconProps {
|
||||
column: string
|
||||
sort: string
|
||||
sort: string | null
|
||||
order: string
|
||||
}
|
||||
|
||||
|
||||
@@ -1,19 +1,23 @@
|
||||
import { useState, useCallback } from 'react'
|
||||
import { useState, useCallback, useRef } from 'react'
|
||||
|
||||
export default function useTableSort(defaultSort = 'id') {
|
||||
export default function useTableSort(defaultSort = 'id', defaultOrder: 'asc' | 'desc' = 'desc') {
|
||||
const [sort, setSort] = useState(defaultSort)
|
||||
const [order, setOrder] = useState<'asc' | 'desc'>('desc')
|
||||
const [order, setOrder] = useState<'asc' | 'desc'>(defaultOrder)
|
||||
const userClicked = useRef(false)
|
||||
|
||||
const handleSort = useCallback((column: string) => {
|
||||
userClicked.current = true
|
||||
setSort(prev => {
|
||||
if (prev === column) {
|
||||
setOrder(o => (o === 'asc' ? 'desc' : 'asc'))
|
||||
return column
|
||||
return prev
|
||||
}
|
||||
setOrder('desc')
|
||||
return column
|
||||
})
|
||||
}, [])
|
||||
|
||||
return { sort, order, handleSort, activeSort: sort }
|
||||
const activeSort = userClicked.current ? sort : null
|
||||
|
||||
return { sort, order, handleSort, activeSort }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user