-
Notifications
You must be signed in to change notification settings - Fork 659
/
base-action-component.tsx
99 lines (91 loc) · 2.47 KB
/
base-action-component.tsx
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 from 'react'
import { Trans } from 'react-i18next'
import { MessageBox, Link } from '@adminjs/design-system'
import ErrorBoundary from './error-boundary.js'
import { actions } from '../actions/index.js'
import { DOCS } from '../../../constants.js'
import { ActionProps } from '../actions/action.props.js'
import { useTranslation } from '../../hooks/index.js'
declare const AdminJS: {
UserComponents: Array<string>;
}
/**
* Component which renders all the default and custom actions for both the Resource and the Record.
*
* It passes all props down to the actual Action component.
*
* Example of creating your own actions:
* ```
* // AdminJS options
* const AdminJSOptions = {
* resources: [
* resource,
* options: {
* actions: {
* myNewAction: {
* label: 'amazing action',
* icon: 'Add',
* inVisible: (resource, record) => record.param('email') !== '',
* actionType: 'record',
* component: 'MyNewAction',
* handler: (request, response, data) => {
* return {
* ...
* }
* }
* }
* }
* }
* ]
* }
* ```
*
* ```
* // ./my-new-action.js
* import { Box } from 'adminjs'
*
* const MyNewAction = (props) => {
* const { resource, action, record } = props
* // do something with the props and render action
* return (
* <Box>Some Action Content</Box>
* )
* }
* ```
*
* @component
* @name BaseActionComponent
* @subcategory Application
*/
export const BaseActionComponent: React.FC<ActionProps> = (props) => {
const { resource, action, record, records, setTag } = props
const documentationLink = [DOCS, 'BaseAction.html'].join('/')
const { translateMessage } = useTranslation()
let Action = actions[action.name]
if (action.component) {
Action = AdminJS.UserComponents[action.component]
}
if (Action) {
return (
<ErrorBoundary>
<Action
action={action}
resource={resource}
record={record}
records={records}
setTag={setTag}
/>
</ErrorBoundary>
)
}
return Action || (
<MessageBox variant="danger">
{translateMessage('noActionComponent')}
<Trans key="messages.buttons.seeTheDocumentation">
See:
<Link ml="default" href={documentationLink}>the documentation</Link>
</Trans>
</MessageBox>
)
}
export default BaseActionComponent