@@ -2,8 +2,7 @@ import * as React from 'react';
2
2
import { useEffect , useState } from 'react' ;
3
3
import inflection from 'inflection' ;
4
4
import {
5
- useEditController ,
6
- EditContextProvider ,
5
+ EditBase ,
7
6
InferredElement ,
8
7
useResourceContext ,
9
8
useEditContext ,
@@ -14,12 +13,45 @@ import { EditProps } from '../types';
14
13
import { EditView } from './EditView' ;
15
14
import { editFieldTypes } from './editFieldTypes' ;
16
15
16
+ export const EditGuesser = ( props : EditProps ) => {
17
+ const {
18
+ resource,
19
+ id,
20
+ mutationMode,
21
+ mutationOptions,
22
+ queryOptions,
23
+ redirect,
24
+ transform,
25
+ disableAuthentication,
26
+ ...rest
27
+ } = props ;
28
+ return (
29
+ < EditBase
30
+ resource = { resource }
31
+ id = { id }
32
+ mutationMode = { mutationMode }
33
+ mutationOptions = { mutationOptions }
34
+ queryOptions = { queryOptions }
35
+ redirect = { redirect }
36
+ transform = { transform }
37
+ disableAuthentication = { disableAuthentication }
38
+ >
39
+ < EditViewGuesser { ...rest } />
40
+ </ EditBase >
41
+ ) ;
42
+ } ;
43
+
17
44
const EditViewGuesser = props => {
18
45
const resource = useResourceContext ( props ) ;
19
46
const { record } = useEditContext ( ) ;
20
- const [ inferredChild , setInferredChild ] = useState ( null ) ;
47
+ const [ child , setChild ] = useState ( null ) ;
48
+
49
+ useEffect ( ( ) => {
50
+ setChild ( null ) ;
51
+ } , [ resource ] ) ;
52
+
21
53
useEffect ( ( ) => {
22
- if ( record && ! inferredChild ) {
54
+ if ( record && ! child ) {
23
55
const inferredElements = getElementsFromRecords (
24
56
[ record ] ,
25
57
editFieldTypes
@@ -29,34 +61,42 @@ const EditViewGuesser = props => {
29
61
null ,
30
62
inferredElements
31
63
) ;
64
+ setChild ( inferredChild . getElement ( ) ) ;
65
+
66
+ if ( process . env . NODE_ENV === 'production' ) return ;
32
67
33
- process . env . NODE_ENV !== 'production' &&
34
- // eslint-disable-next-line no-console
35
- console . log (
36
- `Guessed Edit:
68
+ const representation = inferredChild . getRepresentation ( ) ;
69
+
70
+ const components = [ 'Edit' ]
71
+ . concat (
72
+ Array . from (
73
+ new Set (
74
+ Array . from ( representation . matchAll ( / < ( [ ^ / \s > ] + ) / g) )
75
+ . map ( match => match [ 1 ] )
76
+ . filter ( component => component !== 'span' )
77
+ )
78
+ )
79
+ )
80
+ . sort ( ) ;
81
+
82
+ // eslint-disable-next-line no-console
83
+ console . log (
84
+ `Guessed Edit:
85
+
86
+ import { ${ components . join ( ', ' ) } } from 'react-admin';
37
87
38
88
export const ${ inflection . capitalize (
39
- inflection . singularize ( resource )
40
- ) } Edit = () => (
89
+ inflection . singularize ( resource )
90
+ ) } Edit = () => (
41
91
<Edit>
42
- ${ inferredChild . getRepresentation ( ) }
92
+ ${ representation }
43
93
</Edit>
44
94
);`
45
- ) ;
46
- setInferredChild ( inferredChild . getElement ( ) ) ;
95
+ ) ;
47
96
}
48
- } , [ record , inferredChild , resource ] ) ;
97
+ } , [ record , child , resource ] ) ;
49
98
50
- return < EditView { ...props } > { inferredChild } </ EditView > ;
99
+ return < EditView { ...props } > { child } </ EditView > ;
51
100
} ;
52
101
53
102
EditViewGuesser . propTypes = EditView . propTypes ;
54
-
55
- export const EditGuesser = ( props : EditProps ) => {
56
- const controllerProps = useEditController ( props ) ;
57
- return (
58
- < EditContextProvider value = { controllerProps } >
59
- < EditViewGuesser { ...props } />
60
- </ EditContextProvider >
61
- ) ;
62
- } ;
0 commit comments