feat: add auto form value provider
This commit is contained in:
		
							parent
							
								
									2d033570f4
								
							
						
					
					
						commit
						e3a1d24304
					
				@ -23,6 +23,33 @@ import * as React from "react";
 | 
				
			|||||||
import { useState } from "react";
 | 
					import { useState } from "react";
 | 
				
			||||||
import type { UnknownKeysParam, ZodObject, ZodRawShape, z } from "zod";
 | 
					import type { UnknownKeysParam, ZodObject, ZodRawShape, z } from "zod";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type ContextType = [Partial<any>, React.Dispatch<React.SetStateAction<any>>];
 | 
				
			||||||
 | 
					const AutoFormValueContext = React.createContext<ContextType | null>(null);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function AutoFormValueProvider<Z extends ZodObject<any, any>>({
 | 
				
			||||||
 | 
					  children,
 | 
				
			||||||
 | 
					  value,
 | 
				
			||||||
 | 
					}: {
 | 
				
			||||||
 | 
					  children: React.ReactNode;
 | 
				
			||||||
 | 
					  value: ContextType;
 | 
				
			||||||
 | 
					}) {
 | 
				
			||||||
 | 
					  return (
 | 
				
			||||||
 | 
					    <AutoFormValueContext.Provider value={value}>
 | 
				
			||||||
 | 
					      {children}
 | 
				
			||||||
 | 
					    </AutoFormValueContext.Provider>
 | 
				
			||||||
 | 
					  );
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export function useAutoFormValueContext<Z extends ZodObject<any, any>>() {
 | 
				
			||||||
 | 
					  const context = React.useContext(AutoFormValueContext);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  // if (!context) {
 | 
				
			||||||
 | 
					  //   throw new Error("useInsertModal must be used within a InsertModalProvider");
 | 
				
			||||||
 | 
					  // }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  return context;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export function InsertModal<
 | 
					export function InsertModal<
 | 
				
			||||||
  K extends ZodRawShape,
 | 
					  K extends ZodRawShape,
 | 
				
			||||||
  Y extends UnknownKeysParam,
 | 
					  Y extends UnknownKeysParam,
 | 
				
			||||||
@ -41,7 +68,10 @@ export function InsertModal<
 | 
				
			|||||||
  const [open, setOpen] = React.useState(false);
 | 
					  const [open, setOpen] = React.useState(false);
 | 
				
			||||||
  const [isLoading, setIsLoading] = React.useState(false);
 | 
					  const [isLoading, setIsLoading] = React.useState(false);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  const a = useState<Partial<z.infer<Z>>>({});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  return (
 | 
					  return (
 | 
				
			||||||
 | 
					    <AutoFormValueProvider value={a}>
 | 
				
			||||||
      <Dialog open={open} onOpenChange={setOpen}>
 | 
					      <Dialog open={open} onOpenChange={setOpen}>
 | 
				
			||||||
        {/* <DialogTrigger disabled={props.disabled}> */}
 | 
					        {/* <DialogTrigger disabled={props.disabled}> */}
 | 
				
			||||||
        {props.tooltip ? (
 | 
					        {props.tooltip ? (
 | 
				
			||||||
@ -74,13 +104,17 @@ export function InsertModal<
 | 
				
			|||||||
          </Button>
 | 
					          </Button>
 | 
				
			||||||
        )}
 | 
					        )}
 | 
				
			||||||
        {/* </DialogTrigger> */}
 | 
					        {/* </DialogTrigger> */}
 | 
				
			||||||
      <DialogContent className={cn("sm:max-w-[425px]", props.dialogClassName)}>
 | 
					        <DialogContent
 | 
				
			||||||
 | 
					          className={cn("sm:max-w-[425px]", props.dialogClassName)}
 | 
				
			||||||
 | 
					        >
 | 
				
			||||||
          <DialogHeader>
 | 
					          <DialogHeader>
 | 
				
			||||||
            <DialogTitle>{props.title}</DialogTitle>
 | 
					            <DialogTitle>{props.title}</DialogTitle>
 | 
				
			||||||
            <DialogDescription>{props.description}</DialogDescription>
 | 
					            <DialogDescription>{props.description}</DialogDescription>
 | 
				
			||||||
          </DialogHeader>
 | 
					          </DialogHeader>
 | 
				
			||||||
          {/* <ScrollArea> */}
 | 
					          {/* <ScrollArea> */}
 | 
				
			||||||
          <AutoForm
 | 
					          <AutoForm
 | 
				
			||||||
 | 
					            values={values}
 | 
				
			||||||
 | 
					            onValuesChange={setValues}
 | 
				
			||||||
            fieldConfig={props.fieldConfig}
 | 
					            fieldConfig={props.fieldConfig}
 | 
				
			||||||
            formSchema={props.formSchema}
 | 
					            formSchema={props.formSchema}
 | 
				
			||||||
            onSubmit={async (data) => {
 | 
					            onSubmit={async (data) => {
 | 
				
			||||||
@ -100,6 +134,7 @@ export function InsertModal<
 | 
				
			|||||||
          {/* </ScrollArea> */}
 | 
					          {/* </ScrollArea> */}
 | 
				
			||||||
        </DialogContent>
 | 
					        </DialogContent>
 | 
				
			||||||
      </Dialog>
 | 
					      </Dialog>
 | 
				
			||||||
 | 
					    </AutoFormValueProvider>
 | 
				
			||||||
  );
 | 
					  );
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -138,6 +173,7 @@ export function UpdateModal<
 | 
				
			|||||||
  }, [props.data]);
 | 
					  }, [props.data]);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  return (
 | 
					  return (
 | 
				
			||||||
 | 
					    <AutoFormValueProvider value={[values, setValues]}>
 | 
				
			||||||
      <Dialog open={open} onOpenChange={setOpen}>
 | 
					      <Dialog open={open} onOpenChange={setOpen}>
 | 
				
			||||||
        {props.trigger ?? (
 | 
					        {props.trigger ?? (
 | 
				
			||||||
          <DialogTrigger
 | 
					          <DialogTrigger
 | 
				
			||||||
@ -150,7 +186,9 @@ export function UpdateModal<
 | 
				
			|||||||
            {props.trigger}
 | 
					            {props.trigger}
 | 
				
			||||||
          </DialogTrigger>
 | 
					          </DialogTrigger>
 | 
				
			||||||
        )}
 | 
					        )}
 | 
				
			||||||
      <DialogContent className={cn("sm:max-w-[425px]", props.dialogClassName)}>
 | 
					        <DialogContent
 | 
				
			||||||
 | 
					          className={cn("sm:max-w-[425px]", props.dialogClassName)}
 | 
				
			||||||
 | 
					        >
 | 
				
			||||||
          <DialogHeader>
 | 
					          <DialogHeader>
 | 
				
			||||||
            <DialogTitle>{props.title}</DialogTitle>
 | 
					            <DialogTitle>{props.title}</DialogTitle>
 | 
				
			||||||
            <DialogDescription>{props.description}</DialogDescription>
 | 
					            <DialogDescription>{props.description}</DialogDescription>
 | 
				
			||||||
@ -182,5 +220,6 @@ export function UpdateModal<
 | 
				
			|||||||
          </AutoForm>
 | 
					          </AutoForm>
 | 
				
			||||||
        </DialogContent>
 | 
					        </DialogContent>
 | 
				
			||||||
      </Dialog>
 | 
					      </Dialog>
 | 
				
			||||||
 | 
					    </AutoFormValueProvider>
 | 
				
			||||||
  );
 | 
					  );
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user