| import React, { ReactElement } from 'react' |
| import { stringify } from 'qs' |
|
|
| import { ActionResponse } from '../../../../backend/actions/action.interface.js' |
| import allowOverride from '../../../hoc/allow-override.js' |
| import { useAction } from '../../../hooks/index.js' |
| import { ActionJSON, buildActionTestId } from '../../../interfaces/index.js' |
| import { getActionElementCss } from '../../../utils/index.js' |
|
|
| |
| |
| |
| |
| export type ActionButtonProps = { |
| |
| action: ActionJSON |
| |
| resourceId: string |
| |
| recordId?: string |
| |
| recordIds?: Array<string> |
| |
| actionPerformed?: (action: ActionResponse) => any |
| children?: React.ReactNode |
| search?: string |
| queryParams?: Record<string, unknown> |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| const ActionButton: React.FC<ActionButtonProps> = (props) => { |
| const { |
| children, |
| action, |
| actionPerformed, |
| resourceId, |
| recordId, |
| recordIds, |
| search, |
| queryParams, |
| } = props |
|
|
| const { href, handleClick } = useAction( |
| action, |
| { |
| resourceId, |
| recordId, |
| recordIds, |
| search: stringify(queryParams, { addQueryPrefix: true }) || search, |
| }, |
| actionPerformed, |
| ) |
|
|
| if (!action) { |
| return null |
| } |
|
|
| const firstChild = React.Children.toArray(children)[0] |
|
|
| if ( |
| !firstChild |
| || typeof firstChild === 'string' |
| || typeof firstChild === 'number' |
| || typeof firstChild === 'boolean' |
| ) { |
| throw new Error('ActionButton has to have one child') |
| } |
|
|
| const contentTag = getActionElementCss(resourceId, action.name, 'button') |
|
|
| const WrappedElement = React.cloneElement(firstChild as ReactElement<any>, { |
| onClick: handleClick, |
| 'data-testid': buildActionTestId(action), |
| 'data-css': contentTag, |
| href, |
| }) |
|
|
| return WrappedElement |
| } |
|
|
| const OverridableActionButton = allowOverride(ActionButton, 'ActionButton') |
|
|
| export { |
| OverridableActionButton as default, |
| OverridableActionButton as ActionButton, |
| ActionButton as OriginalActionButton, |
| } |
|
|