prom-pal-ui 1.2.2 → 1.2.4
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.
- package/build/types/ui/fields/form.d.ts +11 -6
- package/build/ui/cjs/index.js +162 -12
- package/build/ui/esm/index.js +162 -12
- package/package.json +1 -1
|
@@ -2,27 +2,32 @@ import { ReactNode } from "react";
|
|
|
2
2
|
import { type ControllerProps, type FieldPath, type FieldValues, UseFormReturn } from "react-hook-form";
|
|
3
3
|
import { z } from "zod";
|
|
4
4
|
import { CommonPromComponentProps } from "../types";
|
|
5
|
+
interface ExtendedFormMethods<T extends FieldValues = FieldValues> extends UseFormReturn<T> {
|
|
6
|
+
setServerError: (error: string) => void;
|
|
7
|
+
getServerError: () => string;
|
|
8
|
+
}
|
|
5
9
|
type PromFormRenderProps = {
|
|
6
10
|
isValid: boolean;
|
|
7
|
-
serverError?:
|
|
11
|
+
serverError?: string;
|
|
8
12
|
isSubmitting: boolean;
|
|
9
13
|
errors: Record<string, any>;
|
|
10
|
-
form:
|
|
14
|
+
form: ExtendedFormMethods<z.infer<any>>;
|
|
11
15
|
};
|
|
12
16
|
interface PromFromProps extends CommonPromComponentProps {
|
|
13
17
|
children?: ReactNode;
|
|
14
18
|
schema?: z.ZodObject<any>;
|
|
15
19
|
defaultValues?: z.infer<any>;
|
|
16
|
-
form?:
|
|
20
|
+
form?: ExtendedFormMethods<z.infer<any>>;
|
|
17
21
|
onSubmit?: (data: z.infer<any>) => Promise<void>;
|
|
18
22
|
render?: (props: PromFormRenderProps) => ReactNode;
|
|
19
23
|
}
|
|
20
24
|
declare const PromFrom: ({ schema, render, children, onSubmit, defaultValues, form: externalForm, }: PromFromProps) => import("react/jsx-runtime").JSX.Element;
|
|
21
25
|
declare const usePromForm: <T extends FieldValues>() => {
|
|
22
|
-
form:
|
|
26
|
+
form: ExtendedFormMethods<T>;
|
|
23
27
|
errors: import("react-hook-form").FieldErrors<T>;
|
|
24
28
|
isValid: boolean;
|
|
25
29
|
isSubmitting: boolean;
|
|
30
|
+
serverError: string;
|
|
26
31
|
reset: import("react-hook-form").UseFormReset<T>;
|
|
27
32
|
watch: import("react-hook-form").UseFormWatch<T>;
|
|
28
33
|
control: import("react-hook-form").Control<T, any, T>;
|
|
@@ -30,10 +35,10 @@ declare const usePromForm: <T extends FieldValues>() => {
|
|
|
30
35
|
register: import("react-hook-form").UseFormRegister<T>;
|
|
31
36
|
setValue: import("react-hook-form").UseFormSetValue<T>;
|
|
32
37
|
getValues: import("react-hook-form").UseFormGetValues<T>;
|
|
38
|
+
setServerError: (error: string) => void;
|
|
33
39
|
handleSubmit: import("react-hook-form").UseFormHandleSubmit<T, T>;
|
|
34
40
|
};
|
|
35
|
-
|
|
36
|
-
declare const PromFormFiled: <TFieldValues extends FieldValues = FieldValues, TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>>({ ...props }: PromFormFiledProps<TFieldValues, TName>) => import("react/jsx-runtime").JSX.Element;
|
|
41
|
+
declare const PromFormFiled: <TFieldValues extends FieldValues = FieldValues, TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>>({ ...props }: ControllerProps<TFieldValues, TName>) => import("react/jsx-runtime").JSX.Element;
|
|
37
42
|
declare const PromMessage: () => import("react/jsx-runtime").JSX.Element;
|
|
38
43
|
declare const useCreatePromForm: ({ schema, defaultValues, }: {
|
|
39
44
|
schema: z.ZodObject<any>;
|
package/build/ui/cjs/index.js
CHANGED
|
@@ -23637,31 +23637,134 @@ const r=(t,r,o)=>{if(t&&"reportValidity"in t){const s=reactHookForm.get(o,r);t.s
|
|
|
23637
23637
|
|
|
23638
23638
|
function t(r,e){try{var o=r();}catch(r){return e(r)}return o&&o.then?o.then(void 0,e):o}function s(r,e){for(var n={};r.length;){var t=r[0],s=t.code,i=t.message,a=t.path.join(".");if(!n[a])if("unionErrors"in t){var u=t.unionErrors[0].errors[0];n[a]={message:u.message,type:u.code};}else n[a]={message:i,type:s};if("unionErrors"in t&&t.unionErrors.forEach(function(e){return e.errors.forEach(function(e){return r.push(e)})}),e){var c=n[a].types,f=c&&c[t.code];n[a]=reactHookForm.appendErrors(a,e,n,s,f?[].concat(f,t.message):t.message);}r.shift();}return n}function i(r,e){for(var n={};r.length;){var t=r[0],s=t.code,i=t.message,a=t.path.join(".");if(!n[a])if("invalid_union"===t.code&&t.errors.length>0){var u=t.errors[0][0];n[a]={message:u.message,type:u.code};}else n[a]={message:i,type:s};if("invalid_union"===t.code&&t.errors.forEach(function(e){return e.forEach(function(e){return r.push(e)})}),e){var c=n[a].types,f=c&&c[t.code];n[a]=reactHookForm.appendErrors(a,e,n,s,f?[].concat(f,t.message):t.message);}r.shift();}return n}function a(o$1,a,u){if(void 0===u&&(u={}),function(r){return "_def"in r&&"object"==typeof r._def&&"typeName"in r._def}(o$1))return function(n,i,c){try{return Promise.resolve(t(function(){return Promise.resolve(o$1["sync"===u.mode?"parse":"parseAsync"](n,a)).then(function(e){return c.shouldUseNativeValidation&&o({},c),{errors:{},values:u.raw?Object.assign({},n):e}})},function(r){if(function(r){return Array.isArray(null==r?void 0:r.issues)}(r))return {values:{},errors:s$1(s(r.errors,!c.shouldUseNativeValidation&&"all"===c.criteriaMode),c)};throw r}))}catch(r){return Promise.reject(r)}};if(function(r){return "_zod"in r&&"object"==typeof r._zod}(o$1))return function(s,c,f){try{return Promise.resolve(t(function(){return Promise.resolve(("sync"===u.mode?parse$2:parseAsync$1)(o$1,s,a)).then(function(e){return f.shouldUseNativeValidation&&o({},f),{errors:{},values:u.raw?Object.assign({},s):e}})},function(r){if(function(r){return r instanceof $ZodError}(r))return {values:{},errors:s$1(i(r.issues,!f.shouldUseNativeValidation&&"all"===f.criteriaMode),f)};throw r}))}catch(r){return Promise.reject(r)}};throw new Error("Invalid input: not a Zod schema")}
|
|
23639
23639
|
|
|
23640
|
+
// "use client";
|
|
23641
|
+
// import { ReactNode, useState } from "react";
|
|
23642
|
+
// import {
|
|
23643
|
+
// useForm,
|
|
23644
|
+
// Controller,
|
|
23645
|
+
// FormProvider,
|
|
23646
|
+
// useFormState,
|
|
23647
|
+
// useFormContext,
|
|
23648
|
+
// type ControllerProps,
|
|
23649
|
+
// type FieldPath,
|
|
23650
|
+
// type FieldValues,
|
|
23651
|
+
// UseFormReturn,
|
|
23652
|
+
// } from "react-hook-form";
|
|
23653
|
+
// import { zodResolver } from "@hookform/resolvers/zod";
|
|
23654
|
+
// import { z } from "zod";
|
|
23655
|
+
// import { CommonPromComponentProps } from "../types";
|
|
23656
|
+
// type PromFormRenderProps = {
|
|
23657
|
+
// isValid: boolean;
|
|
23658
|
+
// serverError?: any;
|
|
23659
|
+
// isSubmitting: boolean;
|
|
23660
|
+
// errors: Record<string, any>;
|
|
23661
|
+
// form: UseFormReturn<z.infer<any>>;
|
|
23662
|
+
// };
|
|
23663
|
+
// interface PromFromProps extends CommonPromComponentProps {
|
|
23664
|
+
// children?: ReactNode;
|
|
23665
|
+
// schema?: z.ZodObject<any>;
|
|
23666
|
+
// defaultValues?: z.infer<any>;
|
|
23667
|
+
// form?: UseFormReturn<z.infer<any>>;
|
|
23668
|
+
// onSubmit?: (data: z.infer<any>) => Promise<void>;
|
|
23669
|
+
// render?: (props: PromFormRenderProps) => ReactNode;
|
|
23670
|
+
// }
|
|
23671
|
+
// const PromFrom = ({
|
|
23672
|
+
// schema,
|
|
23673
|
+
// render,
|
|
23674
|
+
// children,
|
|
23675
|
+
// onSubmit,
|
|
23676
|
+
// defaultValues = {},
|
|
23677
|
+
// form: externalForm,
|
|
23678
|
+
// }: PromFromProps) => {
|
|
23679
|
+
// type FormData = z.infer<typeof schema>;
|
|
23680
|
+
// const internalForm = useForm<FormData>({
|
|
23681
|
+
// resolver: schema ? zodResolver(schema) : undefined,
|
|
23682
|
+
// defaultValues: defaultValues,
|
|
23683
|
+
// });
|
|
23684
|
+
// const form = externalForm || internalForm;
|
|
23685
|
+
// const { errors, isSubmitting, isValid } = form.formState;
|
|
23686
|
+
// const [serverError, setServerError] = useState<string>("");
|
|
23687
|
+
// const handleOnSubmit = async (data: FormData) => {
|
|
23688
|
+
// try {
|
|
23689
|
+
// await onSubmit(data);
|
|
23690
|
+
// } catch (error) {
|
|
23691
|
+
// let errorMessage = "Произошла ошибка при отправке формы";
|
|
23692
|
+
// setServerError(error || errorMessage);
|
|
23693
|
+
// throw error;
|
|
23694
|
+
// }
|
|
23695
|
+
// };
|
|
23696
|
+
// const handleSubmit = handleOnSubmit
|
|
23697
|
+
// ? form.handleSubmit(handleOnSubmit)
|
|
23698
|
+
// : undefined;
|
|
23699
|
+
// return (
|
|
23700
|
+
// <FormProvider {...form}>
|
|
23701
|
+
// <form
|
|
23702
|
+
// onSubmit={handleSubmit}
|
|
23703
|
+
// className="space-y-4"
|
|
23704
|
+
// onKeyDown={(e) => {
|
|
23705
|
+
// if (e.key === "Enter" && !onSubmit) {
|
|
23706
|
+
// e.preventDefault();
|
|
23707
|
+
// }
|
|
23708
|
+
// }}
|
|
23709
|
+
// >
|
|
23710
|
+
// {render
|
|
23711
|
+
// ? render({ form, errors, isSubmitting, isValid, serverError })
|
|
23712
|
+
// : children}
|
|
23713
|
+
// </form>
|
|
23714
|
+
// </FormProvider>
|
|
23715
|
+
// );
|
|
23716
|
+
// };
|
|
23717
|
+
// const usePromForm = <T extends FieldValues>() => {
|
|
23718
|
+
// const form = useFormContext<T>();
|
|
23719
|
+
// const { errors, isSubmitting, isValid } = form.formState;
|
|
23720
|
+
// return {
|
|
23721
|
+
// form,
|
|
23722
|
+
// errors,
|
|
23723
|
+
// isValid,
|
|
23724
|
+
// isSubmitting,
|
|
23725
|
+
// reset: form.reset,
|
|
23726
|
+
// watch: form.watch,
|
|
23727
|
+
// control: form.control,
|
|
23728
|
+
// trigger: form.trigger,
|
|
23729
|
+
// register: form.register,
|
|
23730
|
+
// setValue: form.setValue,
|
|
23731
|
+
// getValues: form.getValues,
|
|
23732
|
+
// handleSubmit: form.handleSubmit,
|
|
23733
|
+
// };
|
|
23734
|
+
// };
|
|
23640
23735
|
var PromFrom = function (_a) {
|
|
23641
23736
|
var schema = _a.schema, render = _a.render, children = _a.children, onSubmit = _a.onSubmit, _b = _a.defaultValues, defaultValues = _b === void 0 ? {} : _b, externalForm = _a.form;
|
|
23642
23737
|
var internalForm = reactHookForm.useForm({
|
|
23643
23738
|
resolver: schema ? a(schema) : undefined,
|
|
23644
23739
|
defaultValues: defaultValues,
|
|
23645
23740
|
});
|
|
23646
|
-
var
|
|
23647
|
-
var
|
|
23648
|
-
var _d =
|
|
23741
|
+
var _c = React.useState(""), serverError = _c[0], setServerError = _c[1];
|
|
23742
|
+
var form = externalForm || tslib.__assign(tslib.__assign({}, internalForm), { setServerError: setServerError, getServerError: function () { return serverError; } });
|
|
23743
|
+
var _d = form.formState, errors = _d.errors, isSubmitting = _d.isSubmitting, isValid = _d.isValid;
|
|
23649
23744
|
var handleOnSubmit = function (data) { return tslib.__awaiter(void 0, void 0, void 0, function () {
|
|
23650
23745
|
var error_1, errorMessage;
|
|
23651
23746
|
return tslib.__generator(this, function (_a) {
|
|
23652
23747
|
switch (_a.label) {
|
|
23653
23748
|
case 0:
|
|
23654
|
-
|
|
23655
|
-
|
|
23749
|
+
setServerError("");
|
|
23750
|
+
_a.label = 1;
|
|
23656
23751
|
case 1:
|
|
23657
|
-
_a.
|
|
23658
|
-
return [3 /*break*/, 3];
|
|
23752
|
+
_a.trys.push([1, 4, , 5]);
|
|
23753
|
+
if (!onSubmit) return [3 /*break*/, 3];
|
|
23754
|
+
return [4 /*yield*/, onSubmit(data)];
|
|
23659
23755
|
case 2:
|
|
23756
|
+
_a.sent();
|
|
23757
|
+
_a.label = 3;
|
|
23758
|
+
case 3: return [3 /*break*/, 5];
|
|
23759
|
+
case 4:
|
|
23660
23760
|
error_1 = _a.sent();
|
|
23661
23761
|
errorMessage = "Произошла ошибка при отправке формы";
|
|
23662
|
-
|
|
23663
|
-
|
|
23664
|
-
|
|
23762
|
+
if (error_1 instanceof Error) {
|
|
23763
|
+
errorMessage = error_1.message;
|
|
23764
|
+
}
|
|
23765
|
+
setServerError(errorMessage);
|
|
23766
|
+
return [3 /*break*/, 5];
|
|
23767
|
+
case 5: return [2 /*return*/];
|
|
23665
23768
|
}
|
|
23666
23769
|
});
|
|
23667
23770
|
}); };
|
|
@@ -23673,17 +23776,25 @@ var PromFrom = function (_a) {
|
|
|
23673
23776
|
e.preventDefault();
|
|
23674
23777
|
}
|
|
23675
23778
|
}, children: render
|
|
23676
|
-
? render({
|
|
23779
|
+
? render({
|
|
23780
|
+
form: form,
|
|
23781
|
+
errors: errors,
|
|
23782
|
+
isSubmitting: isSubmitting,
|
|
23783
|
+
isValid: isValid,
|
|
23784
|
+
serverError: serverError,
|
|
23785
|
+
})
|
|
23677
23786
|
: children }) })));
|
|
23678
23787
|
};
|
|
23679
23788
|
var usePromForm = function () {
|
|
23789
|
+
var _a;
|
|
23680
23790
|
var form = reactHookForm.useFormContext();
|
|
23681
|
-
var
|
|
23791
|
+
var _b = form.formState, errors = _b.errors, isSubmitting = _b.isSubmitting, isValid = _b.isValid;
|
|
23682
23792
|
return {
|
|
23683
23793
|
form: form,
|
|
23684
23794
|
errors: errors,
|
|
23685
23795
|
isValid: isValid,
|
|
23686
23796
|
isSubmitting: isSubmitting,
|
|
23797
|
+
serverError: ((_a = form.getServerError) === null || _a === void 0 ? void 0 : _a.call(form)) || "",
|
|
23687
23798
|
reset: form.reset,
|
|
23688
23799
|
watch: form.watch,
|
|
23689
23800
|
control: form.control,
|
|
@@ -23691,9 +23802,47 @@ var usePromForm = function () {
|
|
|
23691
23802
|
register: form.register,
|
|
23692
23803
|
setValue: form.setValue,
|
|
23693
23804
|
getValues: form.getValues,
|
|
23805
|
+
setServerError: form.setServerError,
|
|
23694
23806
|
handleSubmit: form.handleSubmit,
|
|
23695
23807
|
};
|
|
23696
23808
|
};
|
|
23809
|
+
// const PromFormFiled = <
|
|
23810
|
+
// TFieldValues extends FieldValues = FieldValues,
|
|
23811
|
+
// TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>,
|
|
23812
|
+
// >({
|
|
23813
|
+
// ...props
|
|
23814
|
+
// }: PromFormFiledProps<TFieldValues, TName>) => {
|
|
23815
|
+
// return <Controller {...props} />;
|
|
23816
|
+
// };
|
|
23817
|
+
// const PromMessage = () => {
|
|
23818
|
+
// const { control } = useFormContext();
|
|
23819
|
+
// const { errors } = useFormState({ control });
|
|
23820
|
+
// if (Object.keys(errors).length > 0) {
|
|
23821
|
+
// console.log(errors);
|
|
23822
|
+
// return <p>Ошибка</p>;
|
|
23823
|
+
// }
|
|
23824
|
+
// };
|
|
23825
|
+
// const useCreatePromForm = ({
|
|
23826
|
+
// schema,
|
|
23827
|
+
// defaultValues,
|
|
23828
|
+
// }: {
|
|
23829
|
+
// schema: z.ZodObject<any>;
|
|
23830
|
+
// defaultValues?: z.infer<any>;
|
|
23831
|
+
// }) => {
|
|
23832
|
+
// return useForm({
|
|
23833
|
+
// resolver: schema ? zodResolver(schema) : undefined,
|
|
23834
|
+
// defaultValues,
|
|
23835
|
+
// });
|
|
23836
|
+
// };
|
|
23837
|
+
// export {
|
|
23838
|
+
// PromFrom,
|
|
23839
|
+
// usePromForm,
|
|
23840
|
+
// PromMessage,
|
|
23841
|
+
// PromFormFiled,
|
|
23842
|
+
// useCreatePromForm,
|
|
23843
|
+
// type PromFromProps,
|
|
23844
|
+
// type PromFormRenderProps,
|
|
23845
|
+
// };
|
|
23697
23846
|
var PromFormFiled = function (_a) {
|
|
23698
23847
|
var props = tslib.__rest(_a, []);
|
|
23699
23848
|
return jsxRuntimeExports.jsx(reactHookForm.Controller, tslib.__assign({}, props));
|
|
@@ -23705,6 +23854,7 @@ var PromMessage = function () {
|
|
|
23705
23854
|
console.log(errors);
|
|
23706
23855
|
return jsxRuntimeExports.jsx("p", { children: "\u041E\u0448\u0438\u0431\u043A\u0430" });
|
|
23707
23856
|
}
|
|
23857
|
+
return null;
|
|
23708
23858
|
};
|
|
23709
23859
|
var useCreatePromForm = function (_a) {
|
|
23710
23860
|
var schema = _a.schema, defaultValues = _a.defaultValues;
|
package/build/ui/esm/index.js
CHANGED
|
@@ -23617,31 +23617,134 @@ const r=(t,r,o)=>{if(t&&"reportValidity"in t){const s=get(o,r);t.setCustomValidi
|
|
|
23617
23617
|
|
|
23618
23618
|
function t(r,e){try{var o=r();}catch(r){return e(r)}return o&&o.then?o.then(void 0,e):o}function s(r,e){for(var n={};r.length;){var t=r[0],s=t.code,i=t.message,a=t.path.join(".");if(!n[a])if("unionErrors"in t){var u=t.unionErrors[0].errors[0];n[a]={message:u.message,type:u.code};}else n[a]={message:i,type:s};if("unionErrors"in t&&t.unionErrors.forEach(function(e){return e.errors.forEach(function(e){return r.push(e)})}),e){var c=n[a].types,f=c&&c[t.code];n[a]=appendErrors(a,e,n,s,f?[].concat(f,t.message):t.message);}r.shift();}return n}function i(r,e){for(var n={};r.length;){var t=r[0],s=t.code,i=t.message,a=t.path.join(".");if(!n[a])if("invalid_union"===t.code&&t.errors.length>0){var u=t.errors[0][0];n[a]={message:u.message,type:u.code};}else n[a]={message:i,type:s};if("invalid_union"===t.code&&t.errors.forEach(function(e){return e.forEach(function(e){return r.push(e)})}),e){var c=n[a].types,f=c&&c[t.code];n[a]=appendErrors(a,e,n,s,f?[].concat(f,t.message):t.message);}r.shift();}return n}function a(o$1,a,u){if(void 0===u&&(u={}),function(r){return "_def"in r&&"object"==typeof r._def&&"typeName"in r._def}(o$1))return function(n,i,c){try{return Promise.resolve(t(function(){return Promise.resolve(o$1["sync"===u.mode?"parse":"parseAsync"](n,a)).then(function(e){return c.shouldUseNativeValidation&&o({},c),{errors:{},values:u.raw?Object.assign({},n):e}})},function(r){if(function(r){return Array.isArray(null==r?void 0:r.issues)}(r))return {values:{},errors:s$1(s(r.errors,!c.shouldUseNativeValidation&&"all"===c.criteriaMode),c)};throw r}))}catch(r){return Promise.reject(r)}};if(function(r){return "_zod"in r&&"object"==typeof r._zod}(o$1))return function(s,c,f){try{return Promise.resolve(t(function(){return Promise.resolve(("sync"===u.mode?parse$2:parseAsync$1)(o$1,s,a)).then(function(e){return f.shouldUseNativeValidation&&o({},f),{errors:{},values:u.raw?Object.assign({},s):e}})},function(r){if(function(r){return r instanceof $ZodError}(r))return {values:{},errors:s$1(i(r.issues,!f.shouldUseNativeValidation&&"all"===f.criteriaMode),f)};throw r}))}catch(r){return Promise.reject(r)}};throw new Error("Invalid input: not a Zod schema")}
|
|
23619
23619
|
|
|
23620
|
+
// "use client";
|
|
23621
|
+
// import { ReactNode, useState } from "react";
|
|
23622
|
+
// import {
|
|
23623
|
+
// useForm,
|
|
23624
|
+
// Controller,
|
|
23625
|
+
// FormProvider,
|
|
23626
|
+
// useFormState,
|
|
23627
|
+
// useFormContext,
|
|
23628
|
+
// type ControllerProps,
|
|
23629
|
+
// type FieldPath,
|
|
23630
|
+
// type FieldValues,
|
|
23631
|
+
// UseFormReturn,
|
|
23632
|
+
// } from "react-hook-form";
|
|
23633
|
+
// import { zodResolver } from "@hookform/resolvers/zod";
|
|
23634
|
+
// import { z } from "zod";
|
|
23635
|
+
// import { CommonPromComponentProps } from "../types";
|
|
23636
|
+
// type PromFormRenderProps = {
|
|
23637
|
+
// isValid: boolean;
|
|
23638
|
+
// serverError?: any;
|
|
23639
|
+
// isSubmitting: boolean;
|
|
23640
|
+
// errors: Record<string, any>;
|
|
23641
|
+
// form: UseFormReturn<z.infer<any>>;
|
|
23642
|
+
// };
|
|
23643
|
+
// interface PromFromProps extends CommonPromComponentProps {
|
|
23644
|
+
// children?: ReactNode;
|
|
23645
|
+
// schema?: z.ZodObject<any>;
|
|
23646
|
+
// defaultValues?: z.infer<any>;
|
|
23647
|
+
// form?: UseFormReturn<z.infer<any>>;
|
|
23648
|
+
// onSubmit?: (data: z.infer<any>) => Promise<void>;
|
|
23649
|
+
// render?: (props: PromFormRenderProps) => ReactNode;
|
|
23650
|
+
// }
|
|
23651
|
+
// const PromFrom = ({
|
|
23652
|
+
// schema,
|
|
23653
|
+
// render,
|
|
23654
|
+
// children,
|
|
23655
|
+
// onSubmit,
|
|
23656
|
+
// defaultValues = {},
|
|
23657
|
+
// form: externalForm,
|
|
23658
|
+
// }: PromFromProps) => {
|
|
23659
|
+
// type FormData = z.infer<typeof schema>;
|
|
23660
|
+
// const internalForm = useForm<FormData>({
|
|
23661
|
+
// resolver: schema ? zodResolver(schema) : undefined,
|
|
23662
|
+
// defaultValues: defaultValues,
|
|
23663
|
+
// });
|
|
23664
|
+
// const form = externalForm || internalForm;
|
|
23665
|
+
// const { errors, isSubmitting, isValid } = form.formState;
|
|
23666
|
+
// const [serverError, setServerError] = useState<string>("");
|
|
23667
|
+
// const handleOnSubmit = async (data: FormData) => {
|
|
23668
|
+
// try {
|
|
23669
|
+
// await onSubmit(data);
|
|
23670
|
+
// } catch (error) {
|
|
23671
|
+
// let errorMessage = "Произошла ошибка при отправке формы";
|
|
23672
|
+
// setServerError(error || errorMessage);
|
|
23673
|
+
// throw error;
|
|
23674
|
+
// }
|
|
23675
|
+
// };
|
|
23676
|
+
// const handleSubmit = handleOnSubmit
|
|
23677
|
+
// ? form.handleSubmit(handleOnSubmit)
|
|
23678
|
+
// : undefined;
|
|
23679
|
+
// return (
|
|
23680
|
+
// <FormProvider {...form}>
|
|
23681
|
+
// <form
|
|
23682
|
+
// onSubmit={handleSubmit}
|
|
23683
|
+
// className="space-y-4"
|
|
23684
|
+
// onKeyDown={(e) => {
|
|
23685
|
+
// if (e.key === "Enter" && !onSubmit) {
|
|
23686
|
+
// e.preventDefault();
|
|
23687
|
+
// }
|
|
23688
|
+
// }}
|
|
23689
|
+
// >
|
|
23690
|
+
// {render
|
|
23691
|
+
// ? render({ form, errors, isSubmitting, isValid, serverError })
|
|
23692
|
+
// : children}
|
|
23693
|
+
// </form>
|
|
23694
|
+
// </FormProvider>
|
|
23695
|
+
// );
|
|
23696
|
+
// };
|
|
23697
|
+
// const usePromForm = <T extends FieldValues>() => {
|
|
23698
|
+
// const form = useFormContext<T>();
|
|
23699
|
+
// const { errors, isSubmitting, isValid } = form.formState;
|
|
23700
|
+
// return {
|
|
23701
|
+
// form,
|
|
23702
|
+
// errors,
|
|
23703
|
+
// isValid,
|
|
23704
|
+
// isSubmitting,
|
|
23705
|
+
// reset: form.reset,
|
|
23706
|
+
// watch: form.watch,
|
|
23707
|
+
// control: form.control,
|
|
23708
|
+
// trigger: form.trigger,
|
|
23709
|
+
// register: form.register,
|
|
23710
|
+
// setValue: form.setValue,
|
|
23711
|
+
// getValues: form.getValues,
|
|
23712
|
+
// handleSubmit: form.handleSubmit,
|
|
23713
|
+
// };
|
|
23714
|
+
// };
|
|
23620
23715
|
var PromFrom = function (_a) {
|
|
23621
23716
|
var schema = _a.schema, render = _a.render, children = _a.children, onSubmit = _a.onSubmit, _b = _a.defaultValues, defaultValues = _b === void 0 ? {} : _b, externalForm = _a.form;
|
|
23622
23717
|
var internalForm = useForm({
|
|
23623
23718
|
resolver: schema ? a(schema) : undefined,
|
|
23624
23719
|
defaultValues: defaultValues,
|
|
23625
23720
|
});
|
|
23626
|
-
var
|
|
23627
|
-
var
|
|
23628
|
-
var _d =
|
|
23721
|
+
var _c = useState(""), serverError = _c[0], setServerError = _c[1];
|
|
23722
|
+
var form = externalForm || __assign(__assign({}, internalForm), { setServerError: setServerError, getServerError: function () { return serverError; } });
|
|
23723
|
+
var _d = form.formState, errors = _d.errors, isSubmitting = _d.isSubmitting, isValid = _d.isValid;
|
|
23629
23724
|
var handleOnSubmit = function (data) { return __awaiter(void 0, void 0, void 0, function () {
|
|
23630
23725
|
var error_1, errorMessage;
|
|
23631
23726
|
return __generator(this, function (_a) {
|
|
23632
23727
|
switch (_a.label) {
|
|
23633
23728
|
case 0:
|
|
23634
|
-
|
|
23635
|
-
|
|
23729
|
+
setServerError("");
|
|
23730
|
+
_a.label = 1;
|
|
23636
23731
|
case 1:
|
|
23637
|
-
_a.
|
|
23638
|
-
return [3 /*break*/, 3];
|
|
23732
|
+
_a.trys.push([1, 4, , 5]);
|
|
23733
|
+
if (!onSubmit) return [3 /*break*/, 3];
|
|
23734
|
+
return [4 /*yield*/, onSubmit(data)];
|
|
23639
23735
|
case 2:
|
|
23736
|
+
_a.sent();
|
|
23737
|
+
_a.label = 3;
|
|
23738
|
+
case 3: return [3 /*break*/, 5];
|
|
23739
|
+
case 4:
|
|
23640
23740
|
error_1 = _a.sent();
|
|
23641
23741
|
errorMessage = "Произошла ошибка при отправке формы";
|
|
23642
|
-
|
|
23643
|
-
|
|
23644
|
-
|
|
23742
|
+
if (error_1 instanceof Error) {
|
|
23743
|
+
errorMessage = error_1.message;
|
|
23744
|
+
}
|
|
23745
|
+
setServerError(errorMessage);
|
|
23746
|
+
return [3 /*break*/, 5];
|
|
23747
|
+
case 5: return [2 /*return*/];
|
|
23645
23748
|
}
|
|
23646
23749
|
});
|
|
23647
23750
|
}); };
|
|
@@ -23653,17 +23756,25 @@ var PromFrom = function (_a) {
|
|
|
23653
23756
|
e.preventDefault();
|
|
23654
23757
|
}
|
|
23655
23758
|
}, children: render
|
|
23656
|
-
? render({
|
|
23759
|
+
? render({
|
|
23760
|
+
form: form,
|
|
23761
|
+
errors: errors,
|
|
23762
|
+
isSubmitting: isSubmitting,
|
|
23763
|
+
isValid: isValid,
|
|
23764
|
+
serverError: serverError,
|
|
23765
|
+
})
|
|
23657
23766
|
: children }) })));
|
|
23658
23767
|
};
|
|
23659
23768
|
var usePromForm = function () {
|
|
23769
|
+
var _a;
|
|
23660
23770
|
var form = useFormContext();
|
|
23661
|
-
var
|
|
23771
|
+
var _b = form.formState, errors = _b.errors, isSubmitting = _b.isSubmitting, isValid = _b.isValid;
|
|
23662
23772
|
return {
|
|
23663
23773
|
form: form,
|
|
23664
23774
|
errors: errors,
|
|
23665
23775
|
isValid: isValid,
|
|
23666
23776
|
isSubmitting: isSubmitting,
|
|
23777
|
+
serverError: ((_a = form.getServerError) === null || _a === void 0 ? void 0 : _a.call(form)) || "",
|
|
23667
23778
|
reset: form.reset,
|
|
23668
23779
|
watch: form.watch,
|
|
23669
23780
|
control: form.control,
|
|
@@ -23671,9 +23782,47 @@ var usePromForm = function () {
|
|
|
23671
23782
|
register: form.register,
|
|
23672
23783
|
setValue: form.setValue,
|
|
23673
23784
|
getValues: form.getValues,
|
|
23785
|
+
setServerError: form.setServerError,
|
|
23674
23786
|
handleSubmit: form.handleSubmit,
|
|
23675
23787
|
};
|
|
23676
23788
|
};
|
|
23789
|
+
// const PromFormFiled = <
|
|
23790
|
+
// TFieldValues extends FieldValues = FieldValues,
|
|
23791
|
+
// TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>,
|
|
23792
|
+
// >({
|
|
23793
|
+
// ...props
|
|
23794
|
+
// }: PromFormFiledProps<TFieldValues, TName>) => {
|
|
23795
|
+
// return <Controller {...props} />;
|
|
23796
|
+
// };
|
|
23797
|
+
// const PromMessage = () => {
|
|
23798
|
+
// const { control } = useFormContext();
|
|
23799
|
+
// const { errors } = useFormState({ control });
|
|
23800
|
+
// if (Object.keys(errors).length > 0) {
|
|
23801
|
+
// console.log(errors);
|
|
23802
|
+
// return <p>Ошибка</p>;
|
|
23803
|
+
// }
|
|
23804
|
+
// };
|
|
23805
|
+
// const useCreatePromForm = ({
|
|
23806
|
+
// schema,
|
|
23807
|
+
// defaultValues,
|
|
23808
|
+
// }: {
|
|
23809
|
+
// schema: z.ZodObject<any>;
|
|
23810
|
+
// defaultValues?: z.infer<any>;
|
|
23811
|
+
// }) => {
|
|
23812
|
+
// return useForm({
|
|
23813
|
+
// resolver: schema ? zodResolver(schema) : undefined,
|
|
23814
|
+
// defaultValues,
|
|
23815
|
+
// });
|
|
23816
|
+
// };
|
|
23817
|
+
// export {
|
|
23818
|
+
// PromFrom,
|
|
23819
|
+
// usePromForm,
|
|
23820
|
+
// PromMessage,
|
|
23821
|
+
// PromFormFiled,
|
|
23822
|
+
// useCreatePromForm,
|
|
23823
|
+
// type PromFromProps,
|
|
23824
|
+
// type PromFormRenderProps,
|
|
23825
|
+
// };
|
|
23677
23826
|
var PromFormFiled = function (_a) {
|
|
23678
23827
|
var props = __rest(_a, []);
|
|
23679
23828
|
return jsxRuntimeExports.jsx(Controller, __assign({}, props));
|
|
@@ -23685,6 +23834,7 @@ var PromMessage = function () {
|
|
|
23685
23834
|
console.log(errors);
|
|
23686
23835
|
return jsxRuntimeExports.jsx("p", { children: "\u041E\u0448\u0438\u0431\u043A\u0430" });
|
|
23687
23836
|
}
|
|
23837
|
+
return null;
|
|
23688
23838
|
};
|
|
23689
23839
|
var useCreatePromForm = function (_a) {
|
|
23690
23840
|
var schema = _a.schema, defaultValues = _a.defaultValues;
|