Enhance ObjectActions component to support dynamic action disabling based on objectData; refactor mapActionsToMenuItems function to include objectData parameter.

This commit is contained in:
Tom Butcher 2025-09-05 23:18:37 +01:00
parent 12a4d46da8
commit 475f06e5f0

View File

@ -35,18 +35,29 @@ function filterActionsByVisibility(actions, visibleActions) {
} }
// Recursively map actions to AntD Dropdown items // Recursively map actions to AntD Dropdown items
function mapActionsToMenuItems(actions, currentUrlWithActions, id) { function mapActionsToMenuItems(actions, currentUrlWithActions, id, objectData) {
return actions.map((action) => { return actions.map((action) => {
if (action.type === 'divider') { if (action.type === 'divider') {
return { type: 'divider' } return { type: 'divider' }
} }
const actionUrl = action.url ? action.url(id) : undefined const actionUrl = action.url ? action.url(id) : undefined
var disabled = actionUrl && actionUrl === currentUrlWithActions
if (action.disabled) {
if (typeof action.disabled === 'function') {
disabled = action.disabled(objectData)
} else {
disabled = action.disabled
}
}
const item = { const item = {
key: action.key || action.name, key: action.key || action.name,
label: action.label, label: action.label,
danger: action?.danger || false, danger: action?.danger || false,
icon: action.icon ? createElement(action.icon) : undefined, icon: action.icon ? createElement(action.icon) : undefined,
disabled: actionUrl && actionUrl === currentUrlWithActions disabled
} }
if (action.children && Array.isArray(action.children)) { if (action.children && Array.isArray(action.children)) {
item.children = mapActionsToMenuItems( item.children = mapActionsToMenuItems(
@ -69,6 +80,7 @@ const stripActionParam = (pathname, search) => {
const ObjectActions = ({ const ObjectActions = ({
type, type,
id, id,
objectData,
disabled = false, disabled = false,
buttonProps = {}, buttonProps = {},
visibleActions = {}, visibleActions = {},
@ -101,7 +113,12 @@ const ObjectActions = ({
// Compose AntD Dropdown menu items // Compose AntD Dropdown menu items
const menu = { const menu = {
items: mapActionsToMenuItems(filteredActions, currentUrlWithActions, id), items: mapActionsToMenuItems(
filteredActions,
currentUrlWithActions,
id,
objectData
),
onClick: (info) => { onClick: (info) => {
// Find the action by key // Find the action by key
const findAction = (acts, key) => { const findAction = (acts, key) => {
@ -132,6 +149,7 @@ const ObjectActions = ({
ObjectActions.propTypes = { ObjectActions.propTypes = {
type: PropTypes.string.isRequired, type: PropTypes.string.isRequired,
objectData: PropTypes.object.isRequired,
id: PropTypes.string.isRequired, id: PropTypes.string.isRequired,
disabled: PropTypes.bool, disabled: PropTypes.bool,
buttonProps: PropTypes.object, buttonProps: PropTypes.object,