mtxuilib 0.0.317 → 0.0.319

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,13 @@
1
+ /// <reference types="react/experimental" />
2
+ import React from "react";
3
+ import { AnyZodForm } from "./ZodForm";
4
+ export declare const EditFormToolbar: React.ForwardRefExoticComponent<{
5
+ onCancel?: (() => void) | undefined;
6
+ zodForm?: AnyZodForm | undefined;
7
+ submitText?: string | undefined;
8
+ enableCancelConform?: boolean | undefined;
9
+ enableDeleteButton?: boolean | undefined;
10
+ onDelete?: (() => void) | undefined;
11
+ } & {
12
+ children?: React.ReactNode;
13
+ } & React.HTMLAttributes<HTMLDivElement> & React.RefAttributes<HTMLDivElement>>;
@@ -0,0 +1,106 @@
1
+ "use client";
2
+ var __async = (__this, __arguments, generator) => {
3
+ return new Promise((resolve, reject) => {
4
+ var fulfilled = (value) => {
5
+ try {
6
+ step(generator.next(value));
7
+ } catch (e) {
8
+ reject(e);
9
+ }
10
+ };
11
+ var rejected = (value) => {
12
+ try {
13
+ step(generator.throw(value));
14
+ } catch (e) {
15
+ reject(e);
16
+ }
17
+ };
18
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
19
+ step((generator = generator.apply(__this, __arguments)).next());
20
+ });
21
+ };
22
+ import { jsx, jsxs } from "react/jsx-runtime";
23
+ import React, { useState } from "react";
24
+ import { useFormContext } from "react-hook-form";
25
+ import { cn } from "../lib/utils";
26
+ import { Button } from "../ui/button";
27
+ import { ConformDeleteBtn } from "./deleteConform";
28
+ const EditFormToolbar = React.forwardRef(
29
+ (props, forwardedRef) => {
30
+ const { submitText, onCancel, children, zodForm, enableCancelConform, enableDeleteButton, onDelete } = props;
31
+ const form = useFormContext();
32
+ const isDirty = form.formState.isDirty;
33
+ const formState = form.formState;
34
+ const [openConform, setOpenConform] = useState(false);
35
+ return /* @__PURE__ */ jsxs("div", { className: "flex w-full flex-col p-1", children: [
36
+ !openConform && /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
37
+ /* @__PURE__ */ jsx(
38
+ Button,
39
+ {
40
+ variant: "outline",
41
+ className: " min-w-24",
42
+ onClick: (e) => {
43
+ e.preventDefault();
44
+ if (enableCancelConform && isDirty) {
45
+ setOpenConform(true);
46
+ } else {
47
+ onCancel && onCancel();
48
+ }
49
+ },
50
+ children: "cancel"
51
+ }
52
+ ),
53
+ /* @__PURE__ */ jsx(
54
+ Button,
55
+ {
56
+ form: zodForm == null ? void 0 : zodForm.id,
57
+ type: "submit",
58
+ disabled: formState.isSubmitting,
59
+ className: " min-w-24",
60
+ children: submitText || "submit"
61
+ }
62
+ ),
63
+ enableDeleteButton && /* @__PURE__ */ jsx(
64
+ ConformDeleteBtn,
65
+ {
66
+ callback: () => __async(void 0, null, function* () {
67
+ onDelete && onDelete();
68
+ }),
69
+ disabled: form.formState.isSubmitting,
70
+ variant: "destructive",
71
+ children: "delete"
72
+ }
73
+ )
74
+ ] }),
75
+ openConform && /* @__PURE__ */ jsx(
76
+ ComformCancel,
77
+ {
78
+ onBack: () => {
79
+ setOpenConform(false);
80
+ },
81
+ onContinue: () => {
82
+ onCancel && onCancel();
83
+ }
84
+ }
85
+ )
86
+ ] });
87
+ }
88
+ );
89
+ EditFormToolbar.displayName = "EditFormToolbar";
90
+ const ComformCancel = (props) => {
91
+ const { onContinue, onBack } = props;
92
+ return /* @__PURE__ */ jsxs("div", { className: "flex gap-2", children: [
93
+ "\u672A\u4FDD\u5B58\u66F4\u6539\uFF0C\u786E\u5B9A\u7EE7\u7EED\u5417\uFF1F",
94
+ /* @__PURE__ */ jsx(Button, { onClick: (e) => {
95
+ e.preventDefault();
96
+ onBack();
97
+ }, children: "\u7EE7\u7EED\u7F16\u8F91" }),
98
+ /* @__PURE__ */ jsx(Button, { variant: "destructive", className: cn(""), onClick: (e) => {
99
+ e.preventDefault();
100
+ onContinue();
101
+ }, children: "\u5173\u95ED" })
102
+ ] });
103
+ };
104
+ export {
105
+ EditFormToolbar
106
+ };
@@ -0,0 +1,13 @@
1
+ /// <reference types="react" />
2
+ import { PlainMessage } from "@bufbuild/protobuf";
3
+ import { FormSchema } from "../lib/mtmclient/mtm/sppb/mtm_pb";
4
+ import { Renderable } from "../lib/render";
5
+ import { MaybePromise } from "../types/common";
6
+ type submitHandler = (values: any) => MaybePromise<void>;
7
+ export declare const usePopupForm: () => {
8
+ openSchemaForm: (schema: PlainMessage<FormSchema>, values: any, onSutmit: submitHandler) => void;
9
+ setOpen: (args_0: boolean | ((prev: boolean) => boolean)) => void;
10
+ openComponent: (rand: Renderable<any>) => void;
11
+ };
12
+ export declare function MtFormPopupHolder(): import("react").JSX.Element | undefined;
13
+ export {};
@@ -0,0 +1,163 @@
1
+ "use client";
2
+ var __defProp = Object.defineProperty;
3
+ var __defProps = Object.defineProperties;
4
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
5
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
8
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
9
+ var __spreadValues = (a, b) => {
10
+ for (var prop in b || (b = {}))
11
+ if (__hasOwnProp.call(b, prop))
12
+ __defNormalProp(a, prop, b[prop]);
13
+ if (__getOwnPropSymbols)
14
+ for (var prop of __getOwnPropSymbols(b)) {
15
+ if (__propIsEnum.call(b, prop))
16
+ __defNormalProp(a, prop, b[prop]);
17
+ }
18
+ return a;
19
+ };
20
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
21
+ import { jsx, jsxs } from "react/jsx-runtime";
22
+ import { atom, useAtom, useSetAtom } from "jotai";
23
+ import { useCallback } from "react";
24
+ import { FormProvider, useForm } from "react-hook-form";
25
+ import { useIsDesktop } from "../hooks/use-media-query";
26
+ import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle } from "../ui/dialog";
27
+ import { Drawer, DrawerClose, DrawerContent, DrawerDescription, DrawerFooter, DrawerHeader, DrawerTitle } from "../ui/drawer";
28
+ import { MtButton } from "../ui/ui-mt/Button";
29
+ import { EditFormToolbar } from "./EditFormToolbar";
30
+ import { SchemaFormFieldsRender } from "./SchemaFormViewV2";
31
+ const isOpenAtom = atom(false);
32
+ const defaultValuesAtom = atom({});
33
+ const formSchemaAtom = atom(null);
34
+ const formSubmitAtom = atom(null);
35
+ const popupInnerComponentAtom = atom(null);
36
+ const usePopupForm = () => {
37
+ const setDefaultValues = useSetAtom(defaultValuesAtom);
38
+ const setFormSchema = useSetAtom(formSchemaAtom);
39
+ const setOnSubmit = useSetAtom(formSubmitAtom);
40
+ const setOpen = useSetAtom(isOpenAtom);
41
+ const setPopupInnerComponent = useSetAtom(popupInnerComponentAtom);
42
+ const openSchemaForm = useCallback((schema, values, onSutmit) => {
43
+ console.log("openPopform", schema, values);
44
+ setOnSubmit(() => onSutmit);
45
+ setDefaultValues(values);
46
+ setFormSchema(schema);
47
+ setOpen(true);
48
+ }, [setDefaultValues, setFormSchema, setOnSubmit, setOpen]);
49
+ const openComponent = useCallback((rand) => {
50
+ setPopupInnerComponent(() => rand);
51
+ setOpen(true);
52
+ }, [setOpen, setPopupInnerComponent]);
53
+ return { openSchemaForm, setOpen, openComponent };
54
+ };
55
+ function MtFormPopupHolder() {
56
+ const [isOpen, setOpen] = useAtom(isOpenAtom);
57
+ if (isOpen) {
58
+ return /* @__PURE__ */ jsx(MtFormPopupHolderInner, {});
59
+ }
60
+ }
61
+ function MtFormPopupHolderInner() {
62
+ const isDesktop = useIsDesktop();
63
+ const [defaultvalues] = useAtom(defaultValuesAtom);
64
+ const [formSchema] = useAtom(formSchemaAtom);
65
+ const [onSubmit] = useAtom(formSubmitAtom);
66
+ const [isOpen, setOpen] = useAtom(isOpenAtom);
67
+ const [popupInnerComponent, setPopupInnerComponent] = useAtom(popupInnerComponentAtom);
68
+ const form = useForm({
69
+ defaultValues: defaultvalues
70
+ });
71
+ const handleSubmit = (values) => {
72
+ onSubmit && onSubmit(values);
73
+ };
74
+ if (isOpen && popupInnerComponent) {
75
+ return /* @__PURE__ */ jsx(MtFormPopupHolderForComponent, {});
76
+ }
77
+ if (isDesktop) {
78
+ return /* @__PURE__ */ jsx(
79
+ Dialog,
80
+ {
81
+ open: isOpen,
82
+ onOpenChange: setOpen,
83
+ children: /* @__PURE__ */ jsxs(DialogContent, { className: "sm:max-w-[425px]", children: [
84
+ /* @__PURE__ */ jsxs(DialogHeader, { children: [
85
+ /* @__PURE__ */ jsx(DialogTitle, { children: formSchema == null ? void 0 : formSchema.title }),
86
+ /* @__PURE__ */ jsx(DialogDescription, { children: formSchema == null ? void 0 : formSchema.description })
87
+ ] }),
88
+ /* @__PURE__ */ jsx(FormProvider, __spreadProps(__spreadValues({}, form), { children: /* @__PURE__ */ jsxs("form", { onSubmit: form.handleSubmit(handleSubmit), children: [
89
+ /* @__PURE__ */ jsx(SchemaFormFieldsRender, { schema: formSchema }),
90
+ /* @__PURE__ */ jsx(DialogFooter, { children: /* @__PURE__ */ jsx(EditFormToolbar, { onCancel: () => setOpen(false) }) })
91
+ ] }) }))
92
+ ] })
93
+ }
94
+ );
95
+ }
96
+ return /* @__PURE__ */ jsx(
97
+ Drawer,
98
+ {
99
+ open: isOpen,
100
+ onOpenChange: setOpen,
101
+ children: /* @__PURE__ */ jsx(DrawerContent, { children: /* @__PURE__ */ jsxs("div", { className: "mx-auto w-full max-w-sm", children: [
102
+ /* @__PURE__ */ jsxs(DrawerHeader, { children: [
103
+ /* @__PURE__ */ jsx(DrawerTitle, { children: formSchema == null ? void 0 : formSchema.title }),
104
+ /* @__PURE__ */ jsx(DrawerDescription, { children: formSchema == null ? void 0 : formSchema.description })
105
+ ] }),
106
+ /* @__PURE__ */ jsx(FormProvider, __spreadProps(__spreadValues({}, form), { children: /* @__PURE__ */ jsxs("form", { onSubmit: form.handleSubmit(handleSubmit), children: [
107
+ /* @__PURE__ */ jsx(SchemaFormFieldsRender, { schema: formSchema }),
108
+ /* @__PURE__ */ jsxs(DrawerFooter, { children: [
109
+ /* @__PURE__ */ jsx(MtButton, { type: "submit", children: "Submit" }),
110
+ /* @__PURE__ */ jsx(DrawerClose, { asChild: true, children: /* @__PURE__ */ jsx(MtButton, { variant: "outline", children: "Cancel" }) })
111
+ ] })
112
+ ] }) }))
113
+ ] }) })
114
+ }
115
+ );
116
+ }
117
+ function MtFormPopupHolderForComponent() {
118
+ const isDesktop = useIsDesktop();
119
+ const [defaultvalues] = useAtom(defaultValuesAtom);
120
+ const [formSchema] = useAtom(formSchemaAtom);
121
+ const [onSubmit] = useAtom(formSubmitAtom);
122
+ const [isOpen, setOpen] = useAtom(isOpenAtom);
123
+ const form = useForm({
124
+ defaultValues: defaultvalues
125
+ });
126
+ const handleSubmit = (values) => {
127
+ onSubmit && onSubmit(values);
128
+ };
129
+ if (isDesktop) {
130
+ return /* @__PURE__ */ jsx(
131
+ Dialog,
132
+ {
133
+ open: isOpen,
134
+ onOpenChange: setOpen,
135
+ children: /* @__PURE__ */ jsxs(DialogContent, { className: "sm:max-w-[425px]", children: [
136
+ /* @__PURE__ */ jsxs(DialogHeader, { children: [
137
+ /* @__PURE__ */ jsx(DialogTitle, { children: formSchema == null ? void 0 : formSchema.title }),
138
+ /* @__PURE__ */ jsx(DialogDescription, { children: formSchema == null ? void 0 : formSchema.description })
139
+ ] }),
140
+ /* @__PURE__ */ jsx(FormProvider, __spreadProps(__spreadValues({}, form), { children: /* @__PURE__ */ jsx("form", { onSubmit: form.handleSubmit(handleSubmit), children: "component todo" }) }))
141
+ ] })
142
+ }
143
+ );
144
+ }
145
+ return /* @__PURE__ */ jsx(
146
+ Drawer,
147
+ {
148
+ open: isOpen,
149
+ onOpenChange: setOpen,
150
+ children: /* @__PURE__ */ jsx(DrawerContent, { children: /* @__PURE__ */ jsxs("div", { className: "mx-auto w-full max-w-sm", children: [
151
+ /* @__PURE__ */ jsxs(DrawerHeader, { children: [
152
+ /* @__PURE__ */ jsx(DrawerTitle, { children: formSchema == null ? void 0 : formSchema.title }),
153
+ /* @__PURE__ */ jsx(DrawerDescription, { children: formSchema == null ? void 0 : formSchema.description })
154
+ ] }),
155
+ /* @__PURE__ */ jsx(FormProvider, __spreadProps(__spreadValues({}, form), { children: /* @__PURE__ */ jsx("form", { onSubmit: form.handleSubmit(handleSubmit), children: "component todo" }) }))
156
+ ] }) })
157
+ }
158
+ );
159
+ }
160
+ export {
161
+ MtFormPopupHolder,
162
+ usePopupForm
163
+ };
@@ -0,0 +1,24 @@
1
+ import * as DialogPrimitive from "@radix-ui/react-dialog";
2
+ import { Dispatch, PropsWithChildren, SetStateAction } from "react";
3
+ export declare const MtFormV3Trigger: import("react").ForwardRefExoticComponent<DialogPrimitive.DialogTriggerProps & import("react").RefAttributes<HTMLButtonElement>>;
4
+ interface MtFormV3Props {
5
+ onSubmit: (values: any) => Promise<void>;
6
+ container?: "modal" | "card" | "none";
7
+ defaultValues?: any;
8
+ title?: string;
9
+ description?: string;
10
+ open?: boolean;
11
+ onOpenChange?: Dispatch<SetStateAction<boolean>>;
12
+ }
13
+ export declare function MtFormV3(props: MtFormV3Props & PropsWithChildren): import("react").JSX.Element;
14
+ export declare const MtFormV3Content: (props: PropsWithChildren) => import("react").JSX.Element;
15
+ export declare function useMtformV3(): {
16
+ onSubmit: (values: any) => Promise<void>;
17
+ container?: "card" | "none" | "modal" | undefined;
18
+ defaultValues?: any;
19
+ title?: string | undefined;
20
+ description?: string | undefined;
21
+ open?: boolean | undefined;
22
+ onOpenChange?: Dispatch<SetStateAction<boolean>> | undefined;
23
+ };
24
+ export {};
@@ -0,0 +1,90 @@
1
+ "use client";
2
+ var __defProp = Object.defineProperty;
3
+ var __defProps = Object.defineProperties;
4
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
5
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
8
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
9
+ var __spreadValues = (a, b) => {
10
+ for (var prop in b || (b = {}))
11
+ if (__hasOwnProp.call(b, prop))
12
+ __defNormalProp(a, prop, b[prop]);
13
+ if (__getOwnPropSymbols)
14
+ for (var prop of __getOwnPropSymbols(b)) {
15
+ if (__propIsEnum.call(b, prop))
16
+ __defNormalProp(a, prop, b[prop]);
17
+ }
18
+ return a;
19
+ };
20
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
21
+ import { jsx, jsxs } from "react/jsx-runtime";
22
+ import * as DialogPrimitive from "@radix-ui/react-dialog";
23
+ import { createContext, useContext, useMemo, useState } from "react";
24
+ import { FormProvider, useForm } from "react-hook-form";
25
+ import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader } from "../ui/dialog";
26
+ import { EditFormToolbar } from "./EditFormToolbar";
27
+ const MtFormV3Trigger = DialogPrimitive.Trigger;
28
+ const mtformV3Context = createContext(void 0);
29
+ function MtFormV3(props) {
30
+ const { container, defaultValues, children, open, onOpenChange, onSubmit } = props;
31
+ const [innerOpen, setInnerOpen] = useState(false);
32
+ const [mode, setMode] = useState(container || "none");
33
+ const form = useForm({
34
+ defaultValues
35
+ });
36
+ const __setOpen = useMemo(() => {
37
+ if (onOpenChange) {
38
+ return onOpenChange;
39
+ }
40
+ return setInnerOpen;
41
+ }, [onOpenChange]);
42
+ const __open = useMemo(() => {
43
+ if (open !== void 0) {
44
+ return open;
45
+ }
46
+ return innerOpen;
47
+ }, [innerOpen, open]);
48
+ return /* @__PURE__ */ jsx(mtformV3Context.Provider, { value: {
49
+ onSubmit,
50
+ defaultValues
51
+ }, children: /* @__PURE__ */ jsx(Dialog, { open: __open, onOpenChange: __setOpen, children: /* @__PURE__ */ jsx(FormProvider, __spreadProps(__spreadValues({}, form), { children })) }) });
52
+ }
53
+ const MtFormV3Content = (props) => {
54
+ const { children } = props;
55
+ const mtformv3 = useMtformV3();
56
+ return /* @__PURE__ */ jsx(DialogContent, { className: "sm:max-w-[425px]", children: /* @__PURE__ */ jsxs(
57
+ FormMtFormV3,
58
+ {
59
+ defaultValues: mtformv3.defaultValues,
60
+ onSubmit: mtformv3.onSubmit,
61
+ children: [
62
+ /* @__PURE__ */ jsx(DialogHeader, { children: /* @__PURE__ */ jsx(DialogDescription, {}) }),
63
+ children,
64
+ /* @__PURE__ */ jsx(DialogFooter, { className: "flex justify-end gap-2", children: /* @__PURE__ */ jsx(EditFormToolbar, { submitText: "ok" }) })
65
+ ]
66
+ }
67
+ ) });
68
+ };
69
+ const FormMtFormV3 = (props) => {
70
+ const { defaultValues, children, onSubmit } = props;
71
+ const form = useForm(
72
+ {
73
+ defaultValues
74
+ }
75
+ );
76
+ return /* @__PURE__ */ jsx(FormProvider, __spreadProps(__spreadValues({}, form), { children: /* @__PURE__ */ jsx("form", { onSubmit: form.handleSubmit(onSubmit), children }) }));
77
+ };
78
+ function useMtformV3() {
79
+ const context = useContext(mtformV3Context);
80
+ if (context === void 0) {
81
+ throw new Error("useMtformV3 must be used within a MtFormV3");
82
+ }
83
+ return __spreadValues({}, context);
84
+ }
85
+ export {
86
+ MtFormV3,
87
+ MtFormV3Content,
88
+ MtFormV3Trigger,
89
+ useMtformV3
90
+ };
@@ -0,0 +1,27 @@
1
+ import { PlainMessage } from "@bufbuild/protobuf";
2
+ import { Dispatch, PropsWithChildren } from "react";
3
+ import { FormSchema } from "../lib/mtmclient/mtm/sppb/mtm_pb";
4
+ import { MaybePromise } from "../types/common";
5
+ type SchemaFormViewVaranit = "auto" | "modal" | "card";
6
+ interface SchemaFormViewProps {
7
+ formSchema: PlainMessage<FormSchema>;
8
+ defaultValues?: any;
9
+ onSubmit: (values: any) => MaybePromise<void>;
10
+ onCancel?: () => void;
11
+ isLoading?: boolean;
12
+ variants?: SchemaFormViewVaranit;
13
+ }
14
+ export declare const SchemaFormViewV2: (props: SchemaFormViewProps & PropsWithChildren) => import("react").JSX.Element;
15
+ export declare function useSchemaFormViewV2(): {
16
+ formSchema: PlainMessage<FormSchema>;
17
+ defaultValues?: any;
18
+ onSubmit: (values: any) => MaybePromise<void>;
19
+ onCancel?: (() => void) | undefined;
20
+ isLoading?: boolean | undefined;
21
+ variants?: SchemaFormViewVaranit | undefined;
22
+ setDefaultValues: Dispatch<any>;
23
+ };
24
+ export declare const SchemaFormFieldsRender: (props: {
25
+ schema: PlainMessage<FormSchema>;
26
+ }) => import("react").JSX.Element;
27
+ export {};
@@ -0,0 +1,140 @@
1
+ "use client";
2
+ var __defProp = Object.defineProperty;
3
+ var __defProps = Object.defineProperties;
4
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
5
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
8
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
9
+ var __spreadValues = (a, b) => {
10
+ for (var prop in b || (b = {}))
11
+ if (__hasOwnProp.call(b, prop))
12
+ __defNormalProp(a, prop, b[prop]);
13
+ if (__getOwnPropSymbols)
14
+ for (var prop of __getOwnPropSymbols(b)) {
15
+ if (__propIsEnum.call(b, prop))
16
+ __defNormalProp(a, prop, b[prop]);
17
+ }
18
+ return a;
19
+ };
20
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
21
+ import { Fragment, jsx, jsxs } from "react/jsx-runtime";
22
+ import { createContext, useContext, useState } from "react";
23
+ import { FormProvider, useForm, useFormContext } from "react-hook-form";
24
+ import { useCurdViewShow } from "../curd/curd-detail";
25
+ import { useIsDesktop } from "../hooks/use-media-query";
26
+ import { Card, CardContent } from "../ui/card";
27
+ import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle } from "../ui/dialog";
28
+ import { Drawer, DrawerClose, DrawerContent, DrawerDescription, DrawerFooter, DrawerHeader, DrawerTitle } from "../ui/drawer";
29
+ import { FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage } from "../ui/form";
30
+ import { Input } from "../ui/input";
31
+ import { MtButton } from "../ui/ui-mt/Button";
32
+ import { EditFormToolbar } from "./EditFormToolbar";
33
+ const CurdContext = createContext(void 0);
34
+ const SchemaFormViewV2 = (props) => {
35
+ const { formSchema, children, onSubmit, onCancel, variants } = props;
36
+ const [defaultValues, setDefaultValues] = useState({});
37
+ const curdDetailView = useCurdViewShow();
38
+ const form = useForm({
39
+ defaultValues
40
+ });
41
+ return /* @__PURE__ */ jsxs(CurdContext.Provider, { value: {
42
+ formSchema,
43
+ setDefaultValues,
44
+ onSubmit,
45
+ variants
46
+ }, children: [
47
+ variants == "auto" && /* @__PURE__ */ jsx(FormProvider, __spreadProps(__spreadValues({}, form), { children: /* @__PURE__ */ jsx(Card, { className: "p-2", children: /* @__PURE__ */ jsx(CardContent, { children: /* @__PURE__ */ jsxs("form", { onSubmit, children: [
48
+ /* @__PURE__ */ jsx(SchemaFormFieldsRender, { schema: formSchema }),
49
+ /* @__PURE__ */ jsx(DialogFooter, { children: /* @__PURE__ */ jsx(EditFormToolbar, { onCancel: () => onCancel && onCancel() }) })
50
+ ] }) }) }) })),
51
+ variants == "modal" && /* @__PURE__ */ jsx(
52
+ MtFormPopup,
53
+ {
54
+ open: curdDetailView.isOpenEdit,
55
+ onOpenChange: curdDetailView.setIsOpenEdit
56
+ }
57
+ )
58
+ ] });
59
+ };
60
+ function useSchemaFormViewV2() {
61
+ const context = useContext(CurdContext);
62
+ if (context === void 0) {
63
+ throw new Error("useSchemaFormViewV2 must be used within a SchemaFormViewV2");
64
+ }
65
+ return __spreadValues({}, context);
66
+ }
67
+ function MtFormPopup(props) {
68
+ const { open, onOpenChange } = props;
69
+ const isDesktop = useIsDesktop();
70
+ const formView = useSchemaFormViewV2();
71
+ const form = useForm({
72
+ defaultValues: formView.defaultValues
73
+ });
74
+ if (isDesktop) {
75
+ return /* @__PURE__ */ jsx(
76
+ Dialog,
77
+ {
78
+ open,
79
+ onOpenChange,
80
+ children: /* @__PURE__ */ jsxs(DialogContent, { className: "sm:max-w-[425px]", children: [
81
+ /* @__PURE__ */ jsxs(DialogHeader, { children: [
82
+ /* @__PURE__ */ jsx(DialogTitle, { children: formView.formSchema.title }),
83
+ /* @__PURE__ */ jsx(DialogDescription, { children: formView.formSchema.description })
84
+ ] }),
85
+ /* @__PURE__ */ jsx(FormProvider, __spreadProps(__spreadValues({}, form), { children: /* @__PURE__ */ jsxs("form", { onSubmit: form.handleSubmit(formView.onSubmit), children: [
86
+ /* @__PURE__ */ jsx(SchemaFormFieldsRender, { schema: formView.formSchema }),
87
+ /* @__PURE__ */ jsx(DialogFooter, { children: /* @__PURE__ */ jsx(EditFormToolbar, { onCancel: () => onOpenChange(false) }) })
88
+ ] }) }))
89
+ ] })
90
+ }
91
+ );
92
+ }
93
+ return /* @__PURE__ */ jsx(
94
+ Drawer,
95
+ {
96
+ open,
97
+ onOpenChange,
98
+ children: /* @__PURE__ */ jsx(DrawerContent, { children: /* @__PURE__ */ jsxs("div", { className: "mx-auto w-full max-w-sm", children: [
99
+ /* @__PURE__ */ jsxs(DrawerHeader, { children: [
100
+ /* @__PURE__ */ jsx(DrawerTitle, { children: formView.formSchema.title }),
101
+ /* @__PURE__ */ jsx(DrawerDescription, { children: formView.formSchema.description })
102
+ ] }),
103
+ /* @__PURE__ */ jsx(FormProvider, __spreadProps(__spreadValues({}, form), { children: /* @__PURE__ */ jsxs("form", { onSubmit: form.handleSubmit(formView.onSubmit), children: [
104
+ /* @__PURE__ */ jsx(SchemaFormFieldsRender, { schema: formView.formSchema }),
105
+ /* @__PURE__ */ jsxs(DrawerFooter, { children: [
106
+ /* @__PURE__ */ jsx(MtButton, { type: "submit", children: "Submit" }),
107
+ /* @__PURE__ */ jsx(DrawerClose, { asChild: true, children: /* @__PURE__ */ jsx(MtButton, { variant: "outline", children: "Cancel" }) })
108
+ ] })
109
+ ] }) }))
110
+ ] }) })
111
+ }
112
+ );
113
+ }
114
+ const SchemaFormFieldsRender = (props) => {
115
+ var _a;
116
+ const { schema } = props;
117
+ const form = useFormContext();
118
+ return /* @__PURE__ */ jsx(Fragment, { children: (_a = schema.fields) == null ? void 0 : _a.map((formField, i) => {
119
+ return /* @__PURE__ */ jsx(
120
+ FormField,
121
+ {
122
+ control: form.control,
123
+ name: formField.name,
124
+ defaultValue: formField.defaultValue || "",
125
+ render: ({ field }) => /* @__PURE__ */ jsxs(FormItem, { children: [
126
+ formField.label && /* @__PURE__ */ jsx(FormLabel, { children: formField.label }),
127
+ /* @__PURE__ */ jsx(FormControl, { children: /* @__PURE__ */ jsx(Fragment, { children: /* @__PURE__ */ jsx(Input, __spreadValues({ placeholder: formField.placeholder }, field)) }) }),
128
+ formField.description && /* @__PURE__ */ jsx(FormDescription, { children: formField.description }),
129
+ /* @__PURE__ */ jsx(FormMessage, {})
130
+ ] })
131
+ },
132
+ i
133
+ );
134
+ }) });
135
+ };
136
+ export {
137
+ SchemaFormFieldsRender,
138
+ SchemaFormViewV2,
139
+ useSchemaFormViewV2
140
+ };
@@ -0,0 +1,14 @@
1
+ /// <reference types="react" />
2
+ import { FieldValues, SubmitHandler, UseFormProps, UseFormReturn } from 'react-hook-form';
3
+ import { z } from 'zod';
4
+ export type UseZodForm<TInput extends FieldValues> = UseFormReturn<TInput> & {
5
+ id: string;
6
+ };
7
+ export declare function useZodForm<TSchema extends z.ZodType>(props: Omit<UseFormProps<TSchema['_input']>, 'resolver'> & {
8
+ schema?: TSchema;
9
+ }): UseZodForm<TSchema["_input"]>;
10
+ export type AnyZodForm = UseZodForm<any>;
11
+ export declare function ZForm<TInput extends FieldValues>(props: Omit<React.ComponentProps<'form'>, 'onSubmit' | 'id'> & {
12
+ handleSubmit: SubmitHandler<TInput>;
13
+ form: UseZodForm<TInput>;
14
+ }): import("react").JSX.Element;