Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow clicking table row to invoke callback #705

Merged
merged 2 commits into from
Jul 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions client/src/components/generic/ReusableTable/Table.story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,26 @@ type Story = StoryObj<typeof Table>
export const FullTable: Story = {
decorators: [(Story) => <Story />],
args: {
onRowClick: (id) => console.log("table row clicked", id),
data: [
{
uid: "",
uid: "John",
Name: "John Doe",
Email: "[email protected]",
Status: "Member",
"Membership type": "UOA Student",
"Date Joined": "12-7-22"
},
{
uid: "",
uid: "Straight Zhao",
Name: "Ray Zhao",
Email: "[email protected]",
Status: "Superior Controller",
"Membership type": "UOA Student",
"Date Joined": "12-7-22"
},
{
uid: "",
uid: "Doeshin",
Name: "Johnathan Doeshin",
Email: "[email protected]",
Status: "Member",
Expand Down Expand Up @@ -134,6 +135,7 @@ export const MultipleOperations = () => {
<p>Last deleted id: {deleteMessage}</p>
<Table<(typeof data)[0], "multiple-operations">
data={data}
onRowClick={() => alert("Row Callback")}
operationType="multiple-operations"
rowOperations={[
{
Expand Down
25 changes: 22 additions & 3 deletions client/src/components/generic/ReusableTable/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ interface ITable<
* Pass in a callback for when the last page of the table is reached (i.e go to the next offset if paginating)
*/
onPageChange?: (isLastPage: boolean) => void
/**
* **Optional** callback for when a row is clicked.
*
* @param uid the identifier for the row, typically an id associated with a user
* @example
* onRowClick={(uid) => openEditPanelForUid(uid)}
*/
onRowClick?: (uid: string) => void
/**
* decides if clicking on the row options will give multiple options or a single one
*/
Expand Down Expand Up @@ -154,6 +162,7 @@ const Table = <
operationType = "none",
rowOperations,
onPageChange,
onRowClick,
groupSameRows = false
}: ITable<T & TableRowObjectWithIdentifier, S>) => {
// Needs to be zero-indexed
Expand Down Expand Up @@ -220,12 +229,15 @@ const Table = <
currentGroup % 2 === 0 ? "" : "text-dark-blue-100 font-bold"

return (
<tr key={index} className="">
<tr key={index}>
{dataKeys.map((key) => {
return (
<td
onClick={() => {
onRowClick?.(obj[TABLE_ROW_IDENTIFIER_KEY])
}}
className={`break-keep pb-2 pl-4 pt-2
${groupSameRows && rowClass}`}
${groupSameRows && rowClass} ${!!onRowClick && "cursor-pointer"}`}
key={key}
>
{obj[key] || ""}
Expand All @@ -240,7 +252,14 @@ const Table = <
</tr>
)
})
}, [operationType, rowOperations, dataKeys, currentDataSlice, groupSameRows])
}, [
operationType,
rowOperations,
dataKeys,
currentDataSlice,
groupSameRows,
onRowClick
])

return (
<div className="border-gray-3 h-full w-full overflow-y-visible rounded-t-sm border bg-white">
Expand Down
Loading