Skip to content

Instantly share code, notes, and snippets.

@JBassx
Created May 15, 2024 13:11
Show Gist options
  • Save JBassx/362085917faf70dba10a82974354de7c to your computer and use it in GitHub Desktop.
Save JBassx/362085917faf70dba10a82974354de7c to your computer and use it in GitHub Desktop.
// SortableTable.tsx
import useSortableTable, { TableDataItem } from './useSortableTable'
interface TableComponentProps {
data: TableDataItem[]
columns: string[]
}
export default function SortableTable({ data, columns }: TableComponentProps) {
const { sortedData, sortKey, sortDirection, sortByKey } = useSortableTable(
data,
columns[0]
)
return (
<table>
<thead>
<tr>
{columns.map((column) => (
<th key={column} onClick={() => sortByKey(column)}>
{column}
{sortKey === column && (
<span>{sortDirection === 'asc' ? '▲' : '▼'}</span>
)}
</th>
))}
</tr>
</thead>
<tbody>
{sortedData.map((item) => (
<tr key={item.id}>
{columns.map((column) => (
<td key={column}>{item[column]}</td>
))}
</tr>
))}
</tbody>
</table>
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment