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" ;
63import PropTypes from "prop-types" ;
74
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+
924 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+ } }
1439 >
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 >
2342 ) ;
2443}
2544
2645AnalystForm . propTypes = {
2746 form : PropTypes . arrayOf ( PropTypes . object ) ,
2847 options : PropTypes . object ,
2948 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 ,
3066} ;
3167
3268function GridInput ( props ) {
69+ const { BaseAutoInput : AutoInput , Grid } = useComponents ( ) ;
3370 if ( props . type === "hidden" ) {
3471 return < AutoInput { ...props } /> ;
3572 } else if ( props . fullwidth ) {
@@ -51,3 +88,5 @@ GridInput.propTypes = {
5188 type : PropTypes . string ,
5289 fullwidth : PropTypes . bool ,
5390} ;
91+
92+ export default withWQ ( AnalystForm , { fallback : AnalystFormFallback } ) ;
0 commit comments