-
-
Save JBassx/b571bc81b22cf45045f8fcacbdd04a67 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' | |
import styles from './SortableTable.module.css' | |
type TableComponentProps = { | |
data: TableDataItem[] | |
columns: string[] | |
onItemSelectChange?: (itemId: string, isChecked: boolean) => void | |
} | |
export default function SortableTable({ | |
data, | |
columns, | |
onItemSelectChange, | |
}: TableComponentProps) { | |
const { sortedData, sortKey, sortDirection, sortByKey, toggleSelect } = | |
useSortableTable(data, columns[0]) | |
// Event handlers should be kept in the component's scope | |
const handleToggleSelect = (itemId: string, isChecked: boolean) => { | |
toggleSelect(itemId) | |
if (typeof onItemSelectChange === 'function') { | |
onItemSelectChange(itemId, isChecked) | |
} | |
} | |
return ( | |
<table className={styles.sortableTable}> | |
<thead> | |
<tr> | |
<th>Select</th> | |
{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} className={item.selected ? styles.selected : ''}> | |
<td> | |
<input | |
type="checkbox" | |
checked={item.selected} | |
onChange={(e) => handleToggleSelect(item.id, e.target.checked)} | |
/> | |
</td> | |
{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