react-form-action 2.2.0 → 2.2.1
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/README.md +26 -32
- package/dist/index.d.mts +5 -4
- package/dist/index.mjs +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -60,59 +60,53 @@ export const subscribeAction = formAction
|
|
|
60
60
|
"use client";
|
|
61
61
|
|
|
62
62
|
import {
|
|
63
|
-
Action,
|
|
64
63
|
Form,
|
|
65
64
|
Pending,
|
|
65
|
+
createComponents,
|
|
66
66
|
useActionContext,
|
|
67
67
|
} from "react-form-action/client";
|
|
68
68
|
|
|
69
69
|
import { subscribeAction } from "./action";
|
|
70
70
|
|
|
71
|
-
const { FieldError } = createComponents(subscribeAction);
|
|
71
|
+
const { FieldError, Success } = createComponents(subscribeAction);
|
|
72
72
|
|
|
73
73
|
export function SubscribeForm() {
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
<SuccessMessage />
|
|
77
|
-
<Form>
|
|
78
|
-
<input name="email" />
|
|
79
|
-
{/*💡 The name prop supports autocompletion */}
|
|
80
|
-
<FieldError name="email" />
|
|
81
|
-
</Form>
|
|
82
|
-
<SubmitButton />
|
|
83
|
-
<Pending>Please wait...</Pending>
|
|
84
|
-
</Action>
|
|
85
|
-
);
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
function SuccessMessage() {
|
|
89
|
-
// Pass action to cast the type of the "data" value
|
|
90
|
-
const { isSuccess, data } = useActionContext(subscribeAction);
|
|
91
|
-
|
|
92
|
-
return isSuccess && <p>Email {data} was registered.</p>;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
function SubmitButton() {
|
|
96
|
-
// no need for action when reading a generic value
|
|
97
|
-
const { isPending } = useActionContext();
|
|
74
|
+
const { isPending, isFailure, error, data } =
|
|
75
|
+
useActionContext(subscribeAction);
|
|
98
76
|
|
|
99
77
|
return (
|
|
100
|
-
<
|
|
101
|
-
|
|
102
|
-
|
|
78
|
+
<Form>
|
|
79
|
+
<Success>
|
|
80
|
+
<p>✅ Email {data} was registered.</p>
|
|
81
|
+
</Success>
|
|
82
|
+
<input name="email" />
|
|
83
|
+
{/*💡 The FieldError "name" prop supports autocompletion */}
|
|
84
|
+
<FieldError name="email" />
|
|
85
|
+
<button type="submit" disabled={isPending}>
|
|
86
|
+
{isPending ? "🌀 Submitting..." : "Submit"}
|
|
87
|
+
</button>
|
|
88
|
+
<Pending>Please wait...</Pending>
|
|
89
|
+
</Form>
|
|
103
90
|
);
|
|
104
91
|
}
|
|
105
92
|
```
|
|
106
93
|
|
|
107
|
-
#### 3️⃣
|
|
94
|
+
#### 3️⃣ Provide the `<Action>` context on a Page
|
|
108
95
|
|
|
109
96
|
```tsx
|
|
110
97
|
// app/subscribe/page.tsx
|
|
111
98
|
|
|
99
|
+
import { Action } from "react-form-action/client";
|
|
100
|
+
|
|
112
101
|
import { SubscribeForm } from "./SubscribeForm";
|
|
102
|
+
import { subscribeAction } from "./action";
|
|
113
103
|
|
|
114
|
-
export function Page() {
|
|
115
|
-
return
|
|
104
|
+
export default function Page() {
|
|
105
|
+
return (
|
|
106
|
+
<Action action={subscribeAction} initialData="">
|
|
107
|
+
<SubscribeForm />
|
|
108
|
+
</Action>
|
|
109
|
+
);
|
|
116
110
|
}
|
|
117
111
|
```
|
|
118
112
|
|
package/dist/index.d.mts
CHANGED
|
@@ -8,6 +8,10 @@ type Flatten<T> = Identity<{
|
|
|
8
8
|
[K in keyof T]: T[K];
|
|
9
9
|
}>;
|
|
10
10
|
type Identity<T> = T;
|
|
11
|
+
type ErrorHandler<Context, Err> = (params: {
|
|
12
|
+
error: unknown;
|
|
13
|
+
ctx: Context;
|
|
14
|
+
}) => Promise<Err>;
|
|
11
15
|
type InitialContext = Record<"formData", FormData>;
|
|
12
16
|
/**
|
|
13
17
|
* Action without schema has no input.
|
|
@@ -45,10 +49,7 @@ type FormActionBuilder<Schema extends ZodTypeAny | EmptyInput, Err = Error, Cont
|
|
|
45
49
|
* @param processError A function which receives action's errors.
|
|
46
50
|
* @returns FormActionBuilder
|
|
47
51
|
*/
|
|
48
|
-
error: <Err>(processError:
|
|
49
|
-
error: unknown;
|
|
50
|
-
ctx: Context;
|
|
51
|
-
}) => Err) => FormActionBuilder<Schema, Err, Context>;
|
|
52
|
+
error: <Err>(processError: ErrorHandler<Context, Err>) => FormActionBuilder<Schema, Err, Context>;
|
|
52
53
|
};
|
|
53
54
|
declare const formAction: FormActionBuilder<typeof emptyInput, Error, InitialContext>;
|
|
54
55
|
|
package/dist/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{a as A,b as p}from"./chunk-HL47DG4L.mjs";import{ZodFirstPartyTypeKind as C}from"zod";import{zfd as
|
|
1
|
+
import{a as A,b as p}from"./chunk-HL47DG4L.mjs";import{ZodFirstPartyTypeKind as C}from"zod";import{zfd as E}from"zod-form-data";var d=Symbol();function i(n,o=[],r){async function f(t){let e={formData:t};for(let a of o){let s=await a({ctx:e});e={...e,...s}}return e}return{input(t){if(n===d)return i(t,o,r);if(n._def.effect)throw new Error("Previous input is not augmentable because it contains an effect.");if(t._def.effect)throw new Error("Your input contains effect which prevents merging it with the previous inputs.");if(n._def.typeName===C.ZodObject){let e=n.merge(t);return i(e,o)}else throw Error("Merging inputs works only for object schemas without effects.")},use(t){return i(n,[...o,t],r)},error(t){return i(n,o,t)},run:n===d?t=>p(({success:e,failure:a})=>async(s,x)=>{let m=await f(x);try{return e(await t({ctx:m}))}catch(c){if(r)return a(await r({error:c,ctx:m}));throw c}}):n===d?void 0:t=>p(({success:e,failure:a,invalid:s})=>{let x=E.formData(n);return async(m,c)=>{let y=await f(c),u=x.safeParse(c);if(!u.success)return s(u.error.format());let h=u.data;try{return e(await t({input:h,ctx:y}))}catch(S){if(r)return a(await r({error:S,ctx:y}));throw S}}})}}var N=i(d);export{p as createFormAction,N as formAction,A as initial};
|