1
- import React from "react" ;
2
- import { Formik } from "formik" ;
3
- import { Grid } from "@mui/material" ;
4
- import { AutoInput } from "@wq/react" ;
5
- import { View } from "@wq/material" ;
1
+ import React , { useCallback } from "react" ;
2
+ import { useComponents , withWQ , createFallbackComponent } from "@wq/react" ;
6
3
import PropTypes from "prop-types" ;
7
4
8
- export default function AnalystForm ( { form, options, setOptions } ) {
5
+ const AnalystFormFallback = {
6
+ components : {
7
+ Grid : createFallbackComponent ( "Grid" , "@wq/material" ) ,
8
+ View : createFallbackComponent ( "View" , "@wq/material" ) ,
9
+ AutoForm : createFallbackComponent ( "AutoForm" , "@wq/form" , "AutoForm" ) ,
10
+ AutoInput : createFallbackComponent ( "AutoInput" , "@wq/form" , "AutoForm" ) ,
11
+ } ,
12
+ } ;
13
+
14
+ function AnalystForm ( { form, options, setOptions, wq, children } ) {
15
+ const useValidate = useCallback ( ( ) => {
16
+ function validate ( newOptions ) {
17
+ setOptions ( newOptions ) ;
18
+ }
19
+ validate . onChange = true ;
20
+ return validate ;
21
+ } , [ setOptions ] ) ,
22
+ { AutoForm, AutoInput } = useComponents ( ) ;
23
+
9
24
return (
10
- < Formik
11
- initialValues = { options }
12
- enableReinitialize = { true }
13
- validate = { setOptions }
25
+ < AutoForm
26
+ form = { form }
27
+ data = { options }
28
+ hideSubmit
29
+ wq = { {
30
+ ...wq ,
31
+ components : {
32
+ ...( wq || { } ) . components ,
33
+ AutoInput : GridInput ,
34
+ FormRoot : GridFormRoot ,
35
+ BaseAutoInput : AutoInput ,
36
+ useValidate,
37
+ } ,
38
+ } }
14
39
>
15
- < View sx = { { p : 2 } } >
16
- < Grid container spacing = { 1 } sx = { { } } >
17
- { form . map ( ( field ) => (
18
- < GridInput key = { field . name } { ...field } />
19
- ) ) }
20
- </ Grid >
21
- </ View >
22
- </ Formik >
40
+ { children }
41
+ </ AutoForm >
23
42
) ;
24
43
}
25
44
26
45
AnalystForm . propTypes = {
27
46
form : PropTypes . arrayOf ( PropTypes . object ) ,
28
47
options : PropTypes . object ,
29
48
setOptions : PropTypes . func ,
49
+ wq : PropTypes . object ,
50
+ children : PropTypes . node ,
51
+ } ;
52
+
53
+ function GridFormRoot ( { children } ) {
54
+ const { View, Grid } = useComponents ( ) ;
55
+ return (
56
+ < View sx = { { p : 2 } } >
57
+ < Grid container spacing = { 1 } >
58
+ { children }
59
+ </ Grid >
60
+ </ View >
61
+ ) ;
62
+ }
63
+
64
+ GridFormRoot . propTypes = {
65
+ children : PropTypes . node ,
30
66
} ;
31
67
32
68
function GridInput ( props ) {
69
+ const { BaseAutoInput : AutoInput , Grid } = useComponents ( ) ;
33
70
if ( props . type === "hidden" ) {
34
71
return < AutoInput { ...props } /> ;
35
72
} else if ( props . fullwidth ) {
@@ -51,3 +88,5 @@ GridInput.propTypes = {
51
88
type : PropTypes . string ,
52
89
fullwidth : PropTypes . bool ,
53
90
} ;
91
+
92
+ export default withWQ ( AnalystForm , { fallback : AnalystFormFallback } ) ;
0 commit comments