File size: 2,416 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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'

/**
 * @alias ActionButtonProps
 * @memberof ActionButton
 */
export type ActionButtonProps = {
  /** Action to which button should redirect */
  action: ActionJSON
  /** Id of a resource of an action */
  resourceId: string
  /** Optional recordId for _record_ action */
  recordId?: string
  /** Optional recordIds for _bulk_ action */
  recordIds?: Array<string>
  /** optional callback function which will be triggered when action is performed */
  actionPerformed?: (action: ActionResponse) => any
  children?: React.ReactNode
  search?: string
  queryParams?: Record<string, unknown>
}

/**
 * Renders Button which redirects to given action
 *
 * ### Usage
 *
 * ```
 * import { ActionButton } from 'adminjs'
 * ```
 *
 * @component
 * @subcategory Application
 */
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,
}