From e3a1d24304fe1e0d207c6d1931483e524006de18 Mon Sep 17 00:00:00 2001 From: bennykok Date: Wed, 31 Jan 2024 11:39:13 +0800 Subject: [PATCH] feat: add auto form value provider --- web/src/components/InsertModal.tsx | 239 +++++++++++++++++------------ 1 file changed, 139 insertions(+), 100 deletions(-) diff --git a/web/src/components/InsertModal.tsx b/web/src/components/InsertModal.tsx index 523bbc8..5f13b5f 100644 --- a/web/src/components/InsertModal.tsx +++ b/web/src/components/InsertModal.tsx @@ -23,6 +23,33 @@ import * as React from "react"; import { useState } from "react"; import type { UnknownKeysParam, ZodObject, ZodRawShape, z } from "zod"; +type ContextType = [Partial, React.Dispatch>]; +const AutoFormValueContext = React.createContext(null); + +function AutoFormValueProvider>({ + children, + value, +}: { + children: React.ReactNode; + value: ContextType; +}) { + return ( + + {children} + + ); +} + +export function useAutoFormValueContext>() { + const context = React.useContext(AutoFormValueContext); + + // if (!context) { + // throw new Error("useInsertModal must be used within a InsertModalProvider"); + // } + + return context; +} + export function InsertModal< K extends ZodRawShape, Y extends UnknownKeysParam, @@ -41,65 +68,73 @@ export function InsertModal< const [open, setOpen] = React.useState(false); const [isLoading, setIsLoading] = React.useState(false); + const a = useState>>({}); + return ( - - {/* */} - {props.tooltip ? ( - - - - - -

{props.tooltip}

-
-
- ) : ( - + + +

{props.tooltip}

+
+ + ) : ( + + )} + {/*
*/} + - {props.title} - - )} - {/* */} - - - {props.title} - {props.description} - - {/* */} - { - setIsLoading(true); - await callServerPromise(props.serverAction(data)); - setIsLoading(false); - setOpen(false); - }} - > -
- - Save Changes - {isLoading && } - -
-
- {/*
*/} -
-
+ + {props.title} + {props.description} + + {/* */} + { + setIsLoading(true); + await callServerPromise(props.serverAction(data)); + setIsLoading(false); + setOpen(false); + }} + > +
+ + Save Changes + {isLoading && } + +
+
+ {/*
*/} + + + ); } @@ -138,49 +173,53 @@ export function UpdateModal< }, [props.data]); return ( - - {props.trigger ?? ( - { - setOpen(true); - }} + + + {props.trigger ?? ( + { + setOpen(true); + }} + > + {props.trigger} + + )} + - {props.trigger} - - )} - - - {props.title} - {props.description} - - { - setIsLoading(true); - await callServerPromise( - props.serverAction({ - ...data, - id: props.data.id, - }), - ); - setIsLoading(false); - setOpen(false); - }} - > -
- {props.extraButtons} - - Save Changes - {isLoading && } - -
-
-
-
+ + {props.title} + {props.description} + + { + setIsLoading(true); + await callServerPromise( + props.serverAction({ + ...data, + id: props.data.id, + }), + ); + setIsLoading(false); + setOpen(false); + }} + > +
+ {props.extraButtons} + + Save Changes + {isLoading && } + +
+
+ + + ); }