-
Notifications
You must be signed in to change notification settings - Fork 1
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
f516eee
commit ac92b9c
Showing
30 changed files
with
465 additions
and
159 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,40 @@ | ||
import useSortableData from '@ui/components/data-table/useSortableData'; | ||
|
||
type DataTableProps = { | ||
columns: string[]; | ||
data: string[]; | ||
}; | ||
|
||
function DataTable({ columns, data }: DataTableProps) { | ||
if (!data.length) throw new Error('No data provided'); | ||
|
||
const { items, sort, sortConfig } = useSortableData(data); | ||
return ( | ||
<table> | ||
<thead> | ||
<tr> | ||
{columns.map(column => ( | ||
<th | ||
key={column} | ||
onClick={() => sort(column)} | ||
className={sortConfig && sortConfig.key === column ? sortConfig.direction : undefined} | ||
> | ||
{column} | ||
</th> | ||
))} | ||
</tr> | ||
</thead> | ||
<tbody> | ||
{items.map((item, index) => ( | ||
<tr key={index}> | ||
{columns.map(column => ( | ||
<td key={column}>{item}</td> | ||
))} | ||
</tr> | ||
))} | ||
</tbody> | ||
</table> | ||
); | ||
} | ||
|
||
export default DataTable; |
35 changes: 35 additions & 0 deletions
35
code/client/src/ui/components/data-table/useSortableData.tsx
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,35 @@ | ||
import { useState, useMemo } from 'react'; | ||
|
||
type SortConfig = { | ||
key: string; | ||
direction: 'asc' | 'desc'; | ||
}; | ||
|
||
function useSortableData(items: any[], config: SortConfig | null = null) { | ||
const [sortConfig, setSortConfig] = useState<SortConfig | null>(config); | ||
|
||
const sortedItems = useMemo(() => { | ||
const sortableItems = [...items]; | ||
if (sortConfig !== null) { | ||
sortableItems.sort((a, b) => { | ||
if (a[sortConfig.key] < b[sortConfig.key]) { | ||
return sortConfig.direction === 'asc' ? -1 : 1; | ||
} | ||
if (a[sortConfig.key] > b[sortConfig.key]) { | ||
return sortConfig.direction === 'asc' ? 1 : -1; | ||
} | ||
return 0; | ||
}); | ||
} | ||
return sortableItems; | ||
}, [items, sortConfig]); | ||
|
||
const sort = (key: string) => { | ||
const direction = sortConfig && sortConfig.key === key && sortConfig.direction === 'asc' ? 'desc' : 'asc'; | ||
setSortConfig({ key, direction }); | ||
}; | ||
|
||
return { items: sortedItems, sort, sortConfig }; | ||
} | ||
|
||
export default useSortableData; |
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 |
---|---|---|
@@ -1,6 +1,2 @@ | ||
.dialog { | ||
.button { | ||
margin: 0 !important; | ||
padding: 0 !important; | ||
} | ||
} |
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
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
Oops, something went wrong.