react-form-action 2.1.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 +76 -40
- package/dist/client.d.mts +7 -0
- package/dist/client.mjs +1 -1
- 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
|
|
|
@@ -328,7 +322,7 @@ export function UpdateUserForm() {
|
|
|
328
322
|
|
|
329
323
|
Use the `createComponents(action)` helper to create components which use the ActionContext and have types bound to the action type.
|
|
330
324
|
|
|
331
|
-
|
|
325
|
+
#### `<FielError>` Component
|
|
332
326
|
|
|
333
327
|
```tsx
|
|
334
328
|
"use client";
|
|
@@ -338,7 +332,7 @@ import { Form, createComponents } from "react-form-action/client";
|
|
|
338
332
|
|
|
339
333
|
import { authAction } from "./actions";
|
|
340
334
|
|
|
341
|
-
export const
|
|
335
|
+
export const signUpAction = authAction
|
|
342
336
|
.input(
|
|
343
337
|
z
|
|
344
338
|
.object({
|
|
@@ -357,13 +351,13 @@ export const signUp = authAction
|
|
|
357
351
|
return null;
|
|
358
352
|
});
|
|
359
353
|
|
|
360
|
-
// 🌟 The FieldError is now bound do the
|
|
354
|
+
// 🌟 The FieldError is now bound do the signUpAction input schema which allows autocompletion for its "name" prop
|
|
361
355
|
// ⚠️ Usable only with actions created with the formAction builder
|
|
362
|
-
const { FieldError } = createComponents(
|
|
356
|
+
const { FieldError } = createComponents(signUpAction);
|
|
363
357
|
|
|
364
358
|
export function SignUpForm() {
|
|
365
359
|
return (
|
|
366
|
-
<Action action={
|
|
360
|
+
<Action action={signUpAction} initialData={null}>
|
|
367
361
|
<Form>
|
|
368
362
|
{/* 1️⃣ When the "name" prop is ommited, the top-level error will be rendered e.g.:
|
|
369
363
|
"Passwords don't match" */}
|
|
@@ -378,6 +372,48 @@ export function SignUpForm() {
|
|
|
378
372
|
}
|
|
379
373
|
```
|
|
380
374
|
|
|
375
|
+
#### `<Success>`
|
|
376
|
+
|
|
377
|
+
#### When children are JSX
|
|
378
|
+
|
|
379
|
+
```tsx
|
|
380
|
+
import { Action, createComponents } from "react-form-action/client";
|
|
381
|
+
|
|
382
|
+
const { Success } = createComponents(signUpAction);
|
|
383
|
+
|
|
384
|
+
function MyForm() {
|
|
385
|
+
return (
|
|
386
|
+
<Action action={signUpAction}>
|
|
387
|
+
<Success>
|
|
388
|
+
{/* 👉 The message will render only after the action has succeeded */}
|
|
389
|
+
<p>You've been signed up!</p>
|
|
390
|
+
</Success>
|
|
391
|
+
</Action>
|
|
392
|
+
);
|
|
393
|
+
}
|
|
394
|
+
```
|
|
395
|
+
|
|
396
|
+
#### When children is a render prop
|
|
397
|
+
|
|
398
|
+
```tsx
|
|
399
|
+
import { createComponents } from "react-form-action/client";
|
|
400
|
+
|
|
401
|
+
const { Success } = createComponents(signUpAction);
|
|
402
|
+
|
|
403
|
+
function Label({children}: PropsWithChildren) {
|
|
404
|
+
return (
|
|
405
|
+
<Success>
|
|
406
|
+
{({ isSuccess, data }) => (
|
|
407
|
+
{/* 👉 With a render prop, the children are always mounted, regardles of the isSuccess flag */}
|
|
408
|
+
<label className={isSuccess ? "green" : ""}>
|
|
409
|
+
{children}
|
|
410
|
+
</label>
|
|
411
|
+
)}
|
|
412
|
+
</Success>
|
|
413
|
+
);
|
|
414
|
+
};
|
|
415
|
+
```
|
|
416
|
+
|
|
381
417
|
### `<Pending>`
|
|
382
418
|
|
|
383
419
|
Render children when the action is pending:
|
|
@@ -389,7 +425,7 @@ import { Action, Pending } from "react-form-action/client";
|
|
|
389
425
|
|
|
390
426
|
import { Spinner } from "./components";
|
|
391
427
|
|
|
392
|
-
|
|
428
|
+
function MyForm() {
|
|
393
429
|
return (
|
|
394
430
|
<Action action={action}>
|
|
395
431
|
{/* 👉 Unlike the React.useFormStatus() hook, we don't need here the <form> element at all. */}
|
|
@@ -399,7 +435,7 @@ return function MyForm() {
|
|
|
399
435
|
</Pending>
|
|
400
436
|
</Action>
|
|
401
437
|
);
|
|
402
|
-
}
|
|
438
|
+
}
|
|
403
439
|
```
|
|
404
440
|
|
|
405
441
|
#### When children is a render prop
|
|
@@ -407,7 +443,7 @@ return function MyForm() {
|
|
|
407
443
|
```tsx
|
|
408
444
|
import { Pending } from "react-form-action/client";
|
|
409
445
|
|
|
410
|
-
|
|
446
|
+
function SubmitButton() {
|
|
411
447
|
return (
|
|
412
448
|
<Pending>
|
|
413
449
|
{({ isPending }) => (
|
package/dist/client.d.mts
CHANGED
|
@@ -64,6 +64,13 @@ declare function createComponents<Data, Error, ValidationError extends ZodFormat
|
|
|
64
64
|
FieldError: ({ name, }: PropsWithChildren<{
|
|
65
65
|
name?: "" | InferZodErrorPaths<ValidationError>;
|
|
66
66
|
}>) => false | React__default.JSX.Element;
|
|
67
|
+
Success: ({ children, }: PropsWithChildren | RP<{
|
|
68
|
+
isSuccess: false;
|
|
69
|
+
data: Data | null;
|
|
70
|
+
} | {
|
|
71
|
+
isSuccess: true;
|
|
72
|
+
data: Data;
|
|
73
|
+
}>) => React__default.ReactNode;
|
|
67
74
|
};
|
|
68
75
|
|
|
69
76
|
declare function FormStatus({ children, }: RenderProp<ReturnType<typeof useFormStatus>>): React.ReactNode;
|
package/dist/client.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use client";import{a as c}from"./chunk-HL47DG4L.mjs";import f from"react";import{createContext as E,use as x}from"react";import{useActionState as y}from"react";var p={isInitial:!1,isInvalid:!1,isFailure:!1,isSuccess:!1},
|
|
1
|
+
"use client";import{a as c}from"./chunk-HL47DG4L.mjs";import f from"react";import{createContext as E,use as x}from"react";import{useActionState as y}from"react";var p={isInitial:!1,isInvalid:!1,isFailure:!1,isSuccess:!1},u=E(null);function a(t){let o=x(u);if(!o)throw new Error("ActionContext must be initialized before use. Is your useActionContext hook wrapped with an <Action> Component?");return o}function g({children:t,action:o,initialData:i,permalink:e}){let[r,n,s]=y(o,c(i),e),d=r.type==="initial"?{...r,...p,action:n,isPending:s,isInitial:!0}:r.type==="invalid"?{...r,...p,action:n,isPending:s,isInvalid:!0}:r.type==="failure"?{...r,...p,action:n,isPending:s,isFailure:!0}:{...r,...p,action:n,isPending:s,isSuccess:!0};return f.createElement(u,{value:d},t)}import S,{startTransition as A}from"react";function R({autoReset:t=!1,...o}){let{action:i}=a();return S.createElement("form",{...t?{action:i}:{onSubmit:r=>{r.preventDefault();let n=r.currentTarget;A(()=>{i(new FormData(n))})}},...o})}import C from"react";import m from"react";var F=".",P=Object.freeze({_errors:Object.freeze([])});function l({errors:t,name:o="",children:i=({errors:e})=>m.createElement(m.Fragment,null,e[0])}){if(o.length===0)return i({errors:t._errors});let e=o.split(F),r=t;for(;e.length;){let n=e.shift();r=r[n]??P}return i({errors:r._errors})}function $(t){function o({name:e}){let{isInvalid:r,validationError:n}=a(t);return r&&C.createElement(l,{errors:n,name:e})}function i({children:e}){let{isSuccess:r,data:n}=a(t);return typeof e=="function"?e(r?{isSuccess:r,data:n}:{isSuccess:r,data:n}):r&&e}return{FieldError:o,Success:i}}import{useFormStatus as h}from"react-dom";function X({children:t}){return t(h())}function q({children:t}){let{isPending:o}=a();return typeof t=="function"?t({isPending:o}):o&&t}export{g as Action,R as Form,X as FormStatus,q as Pending,l as ZodFieldError,$ as createComponents,a as useActionContext};
|
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};
|