react-form-action 2.15.1 → 2.16.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 +39 -0
- package/dist/client.d.mts +11 -1
- package/dist/client.mjs +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -24,6 +24,7 @@ End-to-end typesafe success, error & validation state control for Next.js form a
|
|
|
24
24
|
- ✅ Binds the action with the arguments from the _args_ prop.
|
|
25
25
|
- ✅ Has an optional `validate` prop to validate the form data client-side before calling the server action.
|
|
26
26
|
- ✅ Enables you to reset the action state with the `reset()` callback.
|
|
27
|
+
- ✅ `useResetAction()` hook (or `<ResetAction />` component) to automatically initialize your action when the hook/component unmounts.
|
|
27
28
|
|
|
28
29
|
**Context-bound `<Form />` component**
|
|
29
30
|
|
|
@@ -335,6 +336,44 @@ export function SignUpAction({ children }: PropsWithChildren) {
|
|
|
335
336
|
}
|
|
336
337
|
```
|
|
337
338
|
|
|
339
|
+
#### Automatic action state reset (when unmounting component)
|
|
340
|
+
|
|
341
|
+
Works with Next.js routing, where routes are wrapped in `<Activity />`.
|
|
342
|
+
|
|
343
|
+
```tsx
|
|
344
|
+
"use client";
|
|
345
|
+
|
|
346
|
+
import type { PropsWithChildren } from "react";
|
|
347
|
+
import { Action, ResetAction } from "react-form-action/client";
|
|
348
|
+
|
|
349
|
+
import { updateUser } from "./action";
|
|
350
|
+
|
|
351
|
+
export function UpdateUserAction({ children }: PropsWithChildren) {
|
|
352
|
+
return (
|
|
353
|
+
<Action
|
|
354
|
+
action={updateUser}
|
|
355
|
+
initialData={undefined}
|
|
356
|
+
>
|
|
357
|
+
{/* IMPORTANT: must be rendered as a child of the <Action /> context. */}
|
|
358
|
+
<ResetAction />
|
|
359
|
+
{children}
|
|
360
|
+
</Action>
|
|
361
|
+
);
|
|
362
|
+
}
|
|
363
|
+
```
|
|
364
|
+
|
|
365
|
+
When you prefer hooks to JSX, you can use the `useResetAction()` usually in your forms:
|
|
366
|
+
|
|
367
|
+
```tsx
|
|
368
|
+
import { Form, useResetAction } from "react-form-action/client";
|
|
369
|
+
|
|
370
|
+
function MyForm() {
|
|
371
|
+
useResetAction();
|
|
372
|
+
|
|
373
|
+
return <Form>...</Form>
|
|
374
|
+
}
|
|
375
|
+
```
|
|
376
|
+
|
|
338
377
|
|
|
339
378
|
### `<Form>` Component
|
|
340
379
|
|
package/dist/client.d.mts
CHANGED
|
@@ -123,4 +123,14 @@ declare function Pending({ children }: PropsWithChildren | {
|
|
|
123
123
|
}) => React.ReactNode;
|
|
124
124
|
}): import("react").ReactNode;
|
|
125
125
|
//#endregion
|
|
126
|
-
|
|
126
|
+
//#region src/useResetAction.d.ts
|
|
127
|
+
/**
|
|
128
|
+
* Hook to automatically reset the action when the component unmounts, unless it is in the initial state.
|
|
129
|
+
*/
|
|
130
|
+
declare function useResetAction(): void;
|
|
131
|
+
/**
|
|
132
|
+
* Component that automatically resets the action when it unmounts, unless it is in the initial state.
|
|
133
|
+
*/
|
|
134
|
+
declare function ResetAction(): null;
|
|
135
|
+
//#endregion
|
|
136
|
+
export { Action, ActionContextState, ActionProps, Form, FormProps, InferZodErrorPaths, Paths, Pending, ResetAction, SpreadActionContext, ZodFieldError, ZodFieldErrorChildrenProps, ZodFieldErrorProps, createComponents, noError, useActionContext, useResetAction };
|
package/dist/client.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use client";import{n as e,t}from"./createFormAction-BwMYAzm2.mjs";import{createContext as n,startTransition as r,use as i,useActionState as a,useCallback as o}from"react";import{Fragment as
|
|
1
|
+
"use client";import{n as e,t}from"./createFormAction-BwMYAzm2.mjs";import{createContext as n,startTransition as r,use as i,useActionState as a,useCallback as o,useEffect as s}from"react";import{Fragment as c,jsx as l}from"react/jsx-runtime";const u={isInitial:!1,isInvalid:!1,isFailure:!1,isSuccess:!1},d=n(null);function f(e){let t=i(d);if(!t)throw Error(`ActionContext must be initialized before use. Is your useActionContext hook wrapped with an <Action> Component?`);return t}function p({children:n,action:i,initialData:s,args:c,permalink:f,validate:p}){let m=c?i.bind(null,...c):i,[h,g,_]=a(async(n,r)=>{if(r===null)return e(s);if(p){let e=await t(({invalid:e,success:t})=>async(n,r)=>{let i=p(r);return i?e(i):t(!0)})(n,r);if(e.type===`invalid`)return e}return m(n,r)},e(s),f),v=o(()=>{r(()=>{g(null)})},[g]),y=h.type===`initial`?{...h,...u,action:g,reset:v,isPending:_,isInitial:!0}:h.type===`invalid`?{...h,...u,action:g,reset:v,isPending:_,isInvalid:!0}:h.type===`failure`?{...h,...u,action:g,reset:v,isPending:_,isFailure:!0}:{...h,...u,action:g,reset:v,isPending:_,isSuccess:!0};return l(d,{value:y,children:n})}const m=Object.freeze({errors:Object.freeze([])});function h({errors:e,name:t,children:n=({error:e})=>l(c,{children:e})}){if(t.length===0)return n({name:t,error:e.errors[0],errors:e.errors});let r=t.split(`.`),i=e;for(let e of r){if(!i.properties){i=m;break}i=i.properties[e]??m}return n({name:t,error:i.errors[0],errors:i.errors})}function g(e,t={}){function n({name:n,children:r}){let i=f(e),{isInvalid:a,validationError:o}=i,s=r??(({error:e})=>a&&l(c,{children:e}));return l(h,{errors:o??m,name:n,children:e=>{let n=Object.fromEntries(Object.entries(t.fieldProps??{}).map(([t,n])=>[t,n(i,e)]));return s({...e,...n})}})}function r({children:t}){let{isFailure:n,error:r}=f(e);return n?typeof t==`function`?t({isFailure:n,error:r}):t:null}function i({children:t}){let{isSuccess:n,data:r}=f(e);return typeof t==`function`?t({isSuccess:n,data:r}):n&&t}function a({children:t}){let{isInvalid:n,validationError:r}=f(e);return typeof t==`function`?t({isInvalid:n,validationError:r}):n&&t}return{FieldError:n,Failure:r,Success:i,Invalid:a}}function _({autoReset:e=!1,...t}){let{action:n}=f();return l(`form`,{...e?{action:n}:{onSubmit:e=>{e.preventDefault();let t=e.currentTarget;r(()=>{n(new FormData(t))})}},...t})}function v({children:e}){let{isPending:t}=f();return typeof e==`function`?e({isPending:t}):t&&e}function y(){let{reset:e,isInitial:t}=f();s(()=>()=>{t||e()},[t,e])}function b(){return y(),null}export{p as Action,_ as Form,v as Pending,b as ResetAction,h as ZodFieldError,g as createComponents,m as noError,f as useActionContext,y as useResetAction};
|