react-form-action 2.1.0 → 2.2.0
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 +50 -8
- package/dist/client.d.mts +7 -0
- package/dist/client.mjs +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -328,7 +328,7 @@ export function UpdateUserForm() {
|
|
|
328
328
|
|
|
329
329
|
Use the `createComponents(action)` helper to create components which use the ActionContext and have types bound to the action type.
|
|
330
330
|
|
|
331
|
-
|
|
331
|
+
#### `<FielError>` Component
|
|
332
332
|
|
|
333
333
|
```tsx
|
|
334
334
|
"use client";
|
|
@@ -338,7 +338,7 @@ import { Form, createComponents } from "react-form-action/client";
|
|
|
338
338
|
|
|
339
339
|
import { authAction } from "./actions";
|
|
340
340
|
|
|
341
|
-
export const
|
|
341
|
+
export const signUpAction = authAction
|
|
342
342
|
.input(
|
|
343
343
|
z
|
|
344
344
|
.object({
|
|
@@ -357,13 +357,13 @@ export const signUp = authAction
|
|
|
357
357
|
return null;
|
|
358
358
|
});
|
|
359
359
|
|
|
360
|
-
// 🌟 The FieldError is now bound do the
|
|
360
|
+
// 🌟 The FieldError is now bound do the signUpAction input schema which allows autocompletion for its "name" prop
|
|
361
361
|
// ⚠️ Usable only with actions created with the formAction builder
|
|
362
|
-
const { FieldError } = createComponents(
|
|
362
|
+
const { FieldError } = createComponents(signUpAction);
|
|
363
363
|
|
|
364
364
|
export function SignUpForm() {
|
|
365
365
|
return (
|
|
366
|
-
<Action action={
|
|
366
|
+
<Action action={signUpAction} initialData={null}>
|
|
367
367
|
<Form>
|
|
368
368
|
{/* 1️⃣ When the "name" prop is ommited, the top-level error will be rendered e.g.:
|
|
369
369
|
"Passwords don't match" */}
|
|
@@ -378,6 +378,48 @@ export function SignUpForm() {
|
|
|
378
378
|
}
|
|
379
379
|
```
|
|
380
380
|
|
|
381
|
+
#### `<Success>`
|
|
382
|
+
|
|
383
|
+
#### When children are JSX
|
|
384
|
+
|
|
385
|
+
```tsx
|
|
386
|
+
import { Action, createComponents } from "react-form-action/client";
|
|
387
|
+
|
|
388
|
+
const { Success } = createComponents(signUpAction);
|
|
389
|
+
|
|
390
|
+
function MyForm() {
|
|
391
|
+
return (
|
|
392
|
+
<Action action={signUpAction}>
|
|
393
|
+
<Success>
|
|
394
|
+
{/* 👉 The message will render only after the action has succeeded */}
|
|
395
|
+
<p>You've been signed up!</p>
|
|
396
|
+
</Success>
|
|
397
|
+
</Action>
|
|
398
|
+
);
|
|
399
|
+
}
|
|
400
|
+
```
|
|
401
|
+
|
|
402
|
+
#### When children is a render prop
|
|
403
|
+
|
|
404
|
+
```tsx
|
|
405
|
+
import { createComponents } from "react-form-action/client";
|
|
406
|
+
|
|
407
|
+
const { Success } = createComponents(signUpAction);
|
|
408
|
+
|
|
409
|
+
function Label({children}: PropsWithChildren) {
|
|
410
|
+
return (
|
|
411
|
+
<Success>
|
|
412
|
+
{({ isSuccess, data }) => (
|
|
413
|
+
{/* 👉 With a render prop, the children are always mounted, regardles of the isSuccess flag */}
|
|
414
|
+
<label className={isSuccess ? "green" : ""}>
|
|
415
|
+
{children}
|
|
416
|
+
</label>
|
|
417
|
+
)}
|
|
418
|
+
</Success>
|
|
419
|
+
);
|
|
420
|
+
};
|
|
421
|
+
```
|
|
422
|
+
|
|
381
423
|
### `<Pending>`
|
|
382
424
|
|
|
383
425
|
Render children when the action is pending:
|
|
@@ -389,7 +431,7 @@ import { Action, Pending } from "react-form-action/client";
|
|
|
389
431
|
|
|
390
432
|
import { Spinner } from "./components";
|
|
391
433
|
|
|
392
|
-
|
|
434
|
+
function MyForm() {
|
|
393
435
|
return (
|
|
394
436
|
<Action action={action}>
|
|
395
437
|
{/* 👉 Unlike the React.useFormStatus() hook, we don't need here the <form> element at all. */}
|
|
@@ -399,7 +441,7 @@ return function MyForm() {
|
|
|
399
441
|
</Pending>
|
|
400
442
|
</Action>
|
|
401
443
|
);
|
|
402
|
-
}
|
|
444
|
+
}
|
|
403
445
|
```
|
|
404
446
|
|
|
405
447
|
#### When children is a render prop
|
|
@@ -407,7 +449,7 @@ return function MyForm() {
|
|
|
407
449
|
```tsx
|
|
408
450
|
import { Pending } from "react-form-action/client";
|
|
409
451
|
|
|
410
|
-
|
|
452
|
+
function SubmitButton() {
|
|
411
453
|
return (
|
|
412
454
|
<Pending>
|
|
413
455
|
{({ 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};
|