This repository has been archived by the owner on Nov 13, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Proposed table action group refactor (#448)
* Proposed table action group refactor * Support more functionality * PR feedback
- Loading branch information
1 parent
092f469
commit c845095
Showing
4 changed files
with
124 additions
and
24 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
95 changes: 95 additions & 0 deletions
95
src/components/dataDisplays/cellRenderers/ActionGroupRenderer.jsx
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,95 @@ | ||
import React, { useMemo } from 'react'; | ||
import ActionIcon from '../../ActionIcon'; | ||
|
||
function convertClickHandler(clickProp, value, datum) { | ||
if (clickProp && typeof clickProp === 'function') { | ||
return () => clickProp(value, datum); | ||
} | ||
return clickProp; | ||
} | ||
|
||
function convertHref(hrefProp, value, datum) { | ||
return typeof hrefProp === 'function' | ||
? hrefProp(value, datum) | ||
: hrefProp; | ||
} | ||
|
||
export default function ActionGroupRenderer({ | ||
value, | ||
datum, | ||
onView, | ||
viewHref, | ||
viewItemProps = {}, | ||
onEdit, | ||
editHref, | ||
editItemProps = {}, | ||
onDelete, | ||
deleteHref, | ||
deleteItemProps = {}, | ||
children, | ||
}) { | ||
const showView = viewHref || onView; | ||
const showEdit = editHref || onEdit; | ||
const showDelete = deleteHref || onDelete; | ||
|
||
const { | ||
handleView, | ||
handleEdit, | ||
handleDelete, | ||
finalViewHref, | ||
finalEditHref, | ||
finalDeleteHref, | ||
} = useMemo( | ||
() => ({ | ||
handleView: convertClickHandler(onView, value, datum), | ||
handleEdit: convertClickHandler(onEdit, value, datum), | ||
handleDelete: convertClickHandler(onDelete, value, datum), | ||
finalViewHref: convertHref(viewHref, value, datum), | ||
finalEditHref: convertHref(editHref, value, datum), | ||
finalDeleteHref: convertHref(deleteHref, value, datum), | ||
}), | ||
[ | ||
value, | ||
datum, | ||
onView, | ||
onEdit, | ||
onDelete, | ||
viewHref, | ||
editHref, | ||
deleteHref, | ||
], | ||
); | ||
|
||
return ( | ||
<div> | ||
{showView && ( | ||
<ActionIcon | ||
variant="view" | ||
labelId="VIEW" | ||
{...viewItemProps} | ||
href={finalViewHref} | ||
onClick={handleView} | ||
/> | ||
)} | ||
{showEdit && ( | ||
<ActionIcon | ||
variant="edit" | ||
labelId="EDIT" | ||
{...editItemProps} | ||
href={finalEditHref} | ||
onClick={handleEdit} | ||
/> | ||
)} | ||
{showDelete && ( | ||
<ActionIcon | ||
variant="delete" | ||
labelId="DELETE" | ||
{...deleteItemProps} | ||
href={finalDeleteHref} | ||
onClick={handleDelete} | ||
/> | ||
)} | ||
{children} | ||
</div> | ||
); | ||
} |
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