-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6536cc7
commit 0a0b6c7
Showing
6 changed files
with
161 additions
and
64 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import React from 'react'; | ||
import Table from '@mui/material/Table'; | ||
import TableBody from '@mui/material/TableBody'; | ||
import TableCell from '@mui/material/TableCell'; | ||
import TableContainer from '@mui/material/TableContainer'; | ||
import TableHead from '@mui/material/TableHead'; | ||
import TableRow from '@mui/material/TableRow'; | ||
import TableSortLabel from '@mui/material/TableSortLabel'; | ||
import Paper from '@mui/material/Paper'; | ||
|
||
const SortableTable = ({ data, columns, handleSort, getSortIndicator }) => { | ||
return ( | ||
<TableContainer component={Paper}> | ||
<Table> | ||
<TableHead> | ||
<TableRow> | ||
{columns.map((column) => ( | ||
<TableCell | ||
key={column.key} | ||
align={column.align || 'left'} | ||
style={{ | ||
cursor: 'pointer', | ||
fontWeight: 'bold', | ||
backgroundColor: '#e3f2fd', | ||
}} | ||
> | ||
{column.sortable ? ( | ||
<TableSortLabel | ||
active={getSortIndicator(column.key)} | ||
direction={getSortIndicator(column.key) || 'asc'} | ||
onClick={() => handleSort(column.key)} | ||
> | ||
{column.label} | ||
</TableSortLabel> | ||
) : ( | ||
column.label | ||
)} | ||
</TableCell> | ||
))} | ||
</TableRow> | ||
</TableHead> | ||
<TableBody> | ||
{data.map((row, index) => ( | ||
<TableRow key={index}> | ||
{columns.map((column) => ( | ||
<TableCell key={column.key} align={column.align || 'left'}> | ||
{column.render ? column.render(row[column.key], row) : row[column.key]} | ||
</TableCell> | ||
))} | ||
</TableRow> | ||
))} | ||
</TableBody> | ||
</Table> | ||
</TableContainer> | ||
); | ||
}; | ||
|
||
export default SortableTable; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { useState } from 'react'; | ||
|
||
const useSortableTable = (data, columns) => { | ||
const [sortConfig, setSortConfig] = useState({ key: null, direction: 'asc' }); | ||
|
||
const handleSort = (key) => { | ||
const direction = sortConfig.key === key && sortConfig.direction === 'asc' ? 'desc' : 'asc'; | ||
setSortConfig({ key, direction }); | ||
}; | ||
|
||
// Function to get the value from a nested object | ||
const getValue = (item, key) => { | ||
const keys = key.split('.'); // Split key into parts for nested access | ||
return keys.reduce((obj, keyPart) => obj && obj[keyPart], item); | ||
}; | ||
|
||
const sortedData = sortConfig.key | ||
? [...data].sort((a, b) => { | ||
const aValue = getValue(a, sortConfig.key); | ||
const bValue = getValue(b, sortConfig.key); | ||
|
||
if (aValue < bValue) return sortConfig.direction === 'asc' ? -1 : 1; | ||
if (aValue > bValue) return sortConfig.direction === 'asc' ? 1 : -1; | ||
return 0; | ||
}) | ||
: data; | ||
|
||
const getSortIndicator = (key) => { | ||
if (sortConfig.key !== key) return ''; | ||
return sortConfig.direction === 'asc' ? '↑' : '↓'; | ||
}; | ||
|
||
return { | ||
sortedData, | ||
handleSort, | ||
getSortIndicator, | ||
}; | ||
}; | ||
|
||
export default useSortableTable; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/** | ||
* Sorts an array of objects by a given key with an optional nested field. | ||
* @param {Array} data - The array of objects to sort. | ||
* @param {string} key - The key to sort by. | ||
* @param {string} direction - The sort direction ('asc' or 'desc'). | ||
* @param {boolean} isNested - Whether the key is nested in an object. | ||
* @returns {Array} - The sorted array. | ||
*/ | ||
export function sortData(data, key, direction, isNested = false) { | ||
return [...data].sort((a, b) => { | ||
const aValue = isNested ? a.jobApplication[key]?.toLowerCase() : a[key]?.toLowerCase(); | ||
const bValue = isNested ? b.jobApplication[key]?.toLowerCase() : b[key]?.toLowerCase(); | ||
|
||
if (aValue < bValue) return direction === 'asc' ? -1 : 1; | ||
if (aValue > bValue) return direction === 'asc' ? 1 : -1; | ||
return 0; | ||
}); | ||
} |