-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add useTable hook, custom table component and refactor manage user page
- Loading branch information
1 parent
a2a5a65
commit 06dd34e
Showing
3 changed files
with
219 additions
and
83 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// takes in data and displays them in the component | ||
import React from "react"; | ||
import { | ||
Table, | ||
TableContainer, | ||
TableBody, | ||
TableFooter, | ||
TableHead, | ||
Paper, | ||
} from "@mui/material"; | ||
|
||
interface ICustomTable<T> { | ||
data: T[]; | ||
renderRow: (row: T) => React.ReactNode; | ||
renderHead: () => React.ReactNode; | ||
renderFooter: () => React.ReactNode; | ||
getKey: (row: T) => string | number; | ||
} | ||
|
||
export default function CustomTable<T>(props: ICustomTable<T>) { | ||
const { data, renderRow, renderHead, renderFooter, getKey } = props; | ||
return ( | ||
<TableContainer component={Paper}> | ||
<Table> | ||
<TableHead>{renderHead()}</TableHead> | ||
<TableBody> | ||
{data.map((row: T) => ( | ||
<React.Fragment key={getKey(row)}>{renderRow(row)}</React.Fragment> | ||
))} | ||
</TableBody> | ||
<TableFooter>{renderFooter()}</TableFooter> | ||
</Table> | ||
</TableContainer> | ||
); | ||
} |
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,68 @@ | ||
import { useState, useMemo } from "react"; | ||
|
||
// want to take in data and be able to paginate, sort and filter the data | ||
|
||
interface UseTableOptions<T> { | ||
data: T[]; | ||
initialPageSize?: number; | ||
initialSortField?: keyof T; | ||
initialAscendingOrder?: boolean; | ||
filterFunction?: (item: T) => boolean; | ||
} | ||
|
||
export default function useTable<T>({ | ||
data, | ||
initialPageSize = 10, | ||
initialSortField, | ||
initialAscendingOrder = true, | ||
filterFunction = () => true, | ||
}: UseTableOptions<T>) { | ||
const [page, setPage] = useState(0); | ||
const [pageSize, setPageSize] = useState(initialPageSize); | ||
const [sortField, setSortField] = useState<keyof T | undefined>( | ||
initialSortField, | ||
); | ||
const [sortAscending, setSortAscending] = useState(initialAscendingOrder); | ||
|
||
// first we filter the data, then we sort it, then paginate it | ||
const filteredData = useMemo(() => { | ||
return data.filter((item: T) => filterFunction(item)); | ||
}, [data, filterFunction]); | ||
|
||
const sortedData = useMemo(() => { | ||
if (!sortField) return filteredData; | ||
return filteredData.sort((a, b) => { | ||
const aValue = a[sortField]; | ||
const bValue = b[sortField]; | ||
|
||
if (initialAscendingOrder) return aValue > bValue ? 1 : -1; | ||
return aValue < bValue ? 1 : -1; | ||
}); | ||
}, [filteredData, sortField, initialAscendingOrder]); | ||
|
||
const paginatedData = useMemo(() => { | ||
return sortedData.slice(page * pageSize, (1 + page) * pageSize); | ||
}, [page, pageSize, sortedData]); | ||
|
||
const handleChangePage = (newPage: number) => { | ||
setPage(newPage); | ||
}; | ||
|
||
const handleChangeRowsPerPage = (rowsPerPage: number) => { | ||
setPageSize(rowsPerPage); | ||
setPage(0); | ||
}; | ||
|
||
return { | ||
paginatedData, | ||
handleChangePage, | ||
handleChangeRowsPerPage, | ||
setSortField, | ||
setSortAscending, | ||
setPageSize, | ||
sortField, | ||
sortAscending, | ||
pageSize, | ||
page, | ||
}; | ||
} |