Enhance ObjectActions component to support dynamic action disabling based on objectData; refactor mapActionsToMenuItems function to include objectData parameter.
This commit is contained in:
parent
12a4d46da8
commit
475f06e5f0
@ -35,18 +35,29 @@ function filterActionsByVisibility(actions, visibleActions) {
|
||||
}
|
||||
|
||||
// Recursively map actions to AntD Dropdown items
|
||||
function mapActionsToMenuItems(actions, currentUrlWithActions, id) {
|
||||
function mapActionsToMenuItems(actions, currentUrlWithActions, id, objectData) {
|
||||
return actions.map((action) => {
|
||||
if (action.type === 'divider') {
|
||||
return { type: 'divider' }
|
||||
}
|
||||
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 = {
|
||||
key: action.key || action.name,
|
||||
label: action.label,
|
||||
danger: action?.danger || false,
|
||||
icon: action.icon ? createElement(action.icon) : undefined,
|
||||
disabled: actionUrl && actionUrl === currentUrlWithActions
|
||||
disabled
|
||||
}
|
||||
if (action.children && Array.isArray(action.children)) {
|
||||
item.children = mapActionsToMenuItems(
|
||||
@ -69,6 +80,7 @@ const stripActionParam = (pathname, search) => {
|
||||
const ObjectActions = ({
|
||||
type,
|
||||
id,
|
||||
objectData,
|
||||
disabled = false,
|
||||
buttonProps = {},
|
||||
visibleActions = {},
|
||||
@ -101,7 +113,12 @@ const ObjectActions = ({
|
||||
|
||||
// Compose AntD Dropdown menu items
|
||||
const menu = {
|
||||
items: mapActionsToMenuItems(filteredActions, currentUrlWithActions, id),
|
||||
items: mapActionsToMenuItems(
|
||||
filteredActions,
|
||||
currentUrlWithActions,
|
||||
id,
|
||||
objectData
|
||||
),
|
||||
onClick: (info) => {
|
||||
// Find the action by key
|
||||
const findAction = (acts, key) => {
|
||||
@ -132,6 +149,7 @@ const ObjectActions = ({
|
||||
|
||||
ObjectActions.propTypes = {
|
||||
type: PropTypes.string.isRequired,
|
||||
objectData: PropTypes.object.isRequired,
|
||||
id: PropTypes.string.isRequired,
|
||||
disabled: PropTypes.bool,
|
||||
buttonProps: PropTypes.object,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user