muffin-admin 3.6.3 → 3.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/types.d.ts CHANGED
@@ -7,7 +7,7 @@ export type AdminAction = {
7
7
  label: string;
8
8
  title: string;
9
9
  icon: string;
10
- action: string;
10
+ paths: string[];
11
11
  confirm?: string | false;
12
12
  schema?: AdminInput[];
13
13
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "muffin-admin",
3
- "version": "3.6.3",
3
+ "version": "3.7.1",
4
4
  "description": "NPM package for https://github.com/klen/muffin-admin",
5
5
  "files": [
6
6
  "src",
package/src/buildRA.tsx CHANGED
@@ -90,8 +90,6 @@ export function buildRAComponent(name, props) {
90
90
  delete props.required
91
91
  }
92
92
 
93
- props.fullWidth = props.fullWidth ?? true
94
-
95
93
  if (props.children) props.children = buildRA(props.children)[0]
96
94
 
97
95
  return <Item key={props.source} {...props} />
@@ -1,11 +1,13 @@
1
1
  import {
2
2
  Button,
3
- Form,
3
+ FormGroupsProvider,
4
+ useAugmentedForm,
4
5
  useListContext,
5
6
  useRecordContext,
6
7
  useResourceContext,
7
8
  useTranslate,
8
9
  } from "react-admin"
10
+ import { FormProvider } from "react-hook-form"
9
11
 
10
12
  import { useState } from "react"
11
13
  import { buildRA } from "../buildRA"
@@ -20,11 +22,12 @@ export type ActionPayloadProps = {
20
22
  onHandle: (payload: any) => void
21
23
  }
22
24
 
23
- export function ActionButton({ action, confirm, ...props }: AdminAction) {
25
+ export function ActionButton({ paths, confirm, ...props }: AdminAction) {
24
26
  const translate = useTranslate()
25
27
  const record = useRecordContext()
26
28
  const confirmation = useConfirmation()
27
- const { mutate, isPending } = useAction(action)
29
+ const path = paths.find((p) => p.includes("{id}")) || paths[paths.length - 1]
30
+ const { mutate, isPending } = useAction(path)
28
31
 
29
32
  const confirmMessage =
30
33
  typeof confirm === "string" ? translate(confirm) : "Do you confirm this action?"
@@ -37,10 +40,11 @@ export function ActionButton({ action, confirm, ...props }: AdminAction) {
37
40
  return <ActionButtonBase {...props} isPending={isPending} onHandle={onHandle} />
38
41
  }
39
42
 
40
- export function BulkActionButton({ action, confirm, ...props }: AdminAction) {
43
+ export function BulkActionButton({ paths, confirm, ...props }: AdminAction) {
41
44
  const translate = useTranslate()
42
45
  const { selectedIds } = useListContext()
43
- const { mutate, isPending } = useAction(action)
46
+ const path = paths.find((p) => !p.includes("{id}")) || paths[0]
47
+ const { mutate, isPending } = useAction(path)
44
48
  const confirmation = useConfirmation()
45
49
 
46
50
  const confirmMessage =
@@ -54,29 +58,6 @@ export function BulkActionButton({ action, confirm, ...props }: AdminAction) {
54
58
  return <ActionButtonBase {...props} onHandle={onHandle} isPending={isPending} />
55
59
  }
56
60
 
57
- export function CommonPayload({ active, onClose, onHandle, schema, title }) {
58
- const inputs = buildRA(schema)
59
- const translate = useTranslate()
60
- return (
61
- <AdminModal
62
- maxWidth="sm"
63
- open={active}
64
- onClose={onClose}
65
- title={translate(title, { _: title })}
66
- >
67
- <Form
68
- onSubmit={(data) => {
69
- onClose()
70
- onHandle(data)
71
- }}
72
- >
73
- {inputs}
74
- <PayloadButtons onClose={onClose} />
75
- </Form>
76
- </AdminModal>
77
- )
78
- }
79
-
80
61
  function ActionButtonBase({
81
62
  label,
82
63
  title,
@@ -85,7 +66,7 @@ function ActionButtonBase({
85
66
  icon,
86
67
  schema,
87
68
  id,
88
- }: Omit<AdminAction, "action"> & {
69
+ }: Omit<AdminAction, "paths"> & {
89
70
  isPending?: boolean
90
71
  onHandle: (payload?: any) => void
91
72
  }) {
@@ -120,3 +101,35 @@ function ActionButtonBase({
120
101
  </>
121
102
  )
122
103
  }
104
+
105
+ export function CommonPayload({ active, onClose, onHandle, schema, title }) {
106
+ schema[0][1] = { ...schema[0][1], autoFocus: true }
107
+ const inputs = buildRA(schema)
108
+ const translate = useTranslate()
109
+ const { form, formHandleSubmit } = useAugmentedForm({
110
+ onSubmit: (data) => {
111
+ onClose()
112
+ onHandle(data)
113
+ form.reset()
114
+ },
115
+ record: {},
116
+ })
117
+
118
+ return (
119
+ <AdminModal
120
+ maxWidth="sm"
121
+ open={active}
122
+ onClose={onClose}
123
+ title={translate(title, { _: title })}
124
+ >
125
+ <FormProvider {...form}>
126
+ <FormGroupsProvider>
127
+ <form onSubmit={formHandleSubmit}>
128
+ {inputs}
129
+ <PayloadButtons onClose={onClose} />
130
+ </form>
131
+ </FormGroupsProvider>
132
+ </FormProvider>
133
+ </AdminModal>
134
+ )
135
+ }
package/src/types.ts CHANGED
@@ -8,7 +8,7 @@ export type AdminAction = {
8
8
  label: string
9
9
  title: string
10
10
  icon: string
11
- action: string
11
+ paths: string[]
12
12
  confirm?: string | false
13
13
  schema?: AdminInput[]
14
14
  }
@@ -35,11 +35,11 @@ export type AdminResourceProps = {
35
35
  actions: AdminAction[]
36
36
  create: AdminInput[] | false
37
37
  edit:
38
- | {
39
- inputs: AdminInput[]
40
- remove?: boolean
41
- }
42
- | false
38
+ | {
39
+ inputs: AdminInput[]
40
+ remove?: boolean
41
+ }
42
+ | false
43
43
  list: {
44
44
  limit: number
45
45
  limitMax: number