-
-
Save JBassx/362085917faf70dba10a82974354de7c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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