react-form-action 1.2.3 → 1.4.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 CHANGED
@@ -19,6 +19,11 @@ End-to-end typesafe success, error & validation state control for Next.js 14 for
19
19
  - ✅ `<Form />` component reads the action's response.
20
20
  - ✅ Computes progress meta-state like `isInvalid`, `isSuccess` and more.
21
21
 
22
+ **Context**
23
+
24
+ - ✅ Access the action state via `useFormContext()` hook.
25
+ - ✅ Keeps JSX markup free of render-props.
26
+
22
27
  ## Install
23
28
 
24
29
  ```
@@ -50,7 +55,7 @@ const i18nMiddleware = async () => {
50
55
  const authAction = formAction
51
56
  .use(i18nMiddleware)
52
57
  .use(async ({ ctx: { t } }) =>
53
- console.log("🎉 context enhanced by previous middlewares 🎉", t),
58
+ console.log("🎉 context enhanced by previous middlewares 🎉", t)
54
59
  )
55
60
  .error(({ error }) => {
56
61
  if (error instanceof DbError) {
@@ -85,7 +90,7 @@ export const signUp = authAction
85
90
  .refine((data) => data.password === data.confirm, {
86
91
  message: "Passwords don't match",
87
92
  path: ["confirm"],
88
- }),
93
+ })
89
94
  ) // if using refinement, only one input call is permited, as schema with ZodEffects is not extendable.
90
95
  .run(async ({ ctx: { t }, input: { email, password } }) => {
91
96
  // 🎉 passwords match!
@@ -161,7 +166,7 @@ export const updateUser = createFormAction<
161
166
 
162
167
  return failure({ message: "Failed to update user." });
163
168
  }
164
- },
169
+ }
165
170
  );
166
171
  ```
167
172
 
@@ -206,6 +211,50 @@ export function UpdateUserForm() {
206
211
  }
207
212
  ```
208
213
 
214
+ ### Context Form
215
+
216
+ ```tsx
217
+ // Example route: /app/auth/signup/SignUpForm.tsx
218
+ "use client";
219
+
220
+ import { createForm, Pending, useFormContext } from "react-form-action/client";
221
+ import { signupAction } from "./actions";
222
+
223
+ // This call will succeed only in client component
224
+ const { Form } = createForm(signupAction);
225
+
226
+ function Error() {
227
+ // read any state from the FormContext:
228
+ const {
229
+ error,
230
+ data,
231
+ validationError,
232
+ isPending,
233
+ isFailure,
234
+ isInvalid,
235
+ isSuccess,
236
+ isInitial
237
+ } = useFormContext();
238
+
239
+ return isFailure && "Failed to submit". // use the error somehow
240
+ }
241
+
242
+ // render this form on your RSC page (/app/auth/signup/page.tsx)
243
+ export function SignupForm() {
244
+ // This Form does not use render props.
245
+ return (
246
+ <Form>
247
+ <input name="email" />
248
+ <input name="password" />
249
+ <Pending>
250
+ {/* This renders only when the action is pending. */}
251
+ <p>Please wait...</p>
252
+ </Pending>
253
+ </Form>
254
+ );
255
+ }
256
+ ```
257
+
209
258
  ### `<ZodFieldError>` Component
210
259
 
211
260
  Actions created with `formAction` builder will have the `validationError` of [`ZodFormattedError`](https://zod.dev/ERROR_HANDLING?id=formatting-errors) type.
package/dist/client.d.mts CHANGED
@@ -1,29 +1,60 @@
1
- import * as React from 'react';
2
- import React__default, { FormHTMLAttributes } from 'react';
1
+ import * as react from 'react';
2
+ import react__default, { PropsWithChildren, FormHTMLAttributes } from 'react';
3
+ import { b as FormAction, A as ActionState, a as InitialState, I as InvalidState, F as FailureState, S as SuccessState } from './createFormAction-4e719362.js';
3
4
  import { RenderProp } from 'react-render-prop-type';
4
- import { F as FormState, b as InitialState, I as InvalidState, a as FailureState, S as SuccessState } from './createFormAction-3a51310f.js';
5
5
  import { useFormStatus } from 'react-dom';
6
6
  import { ZodFormattedError } from 'zod';
7
7
 
8
+ /**
9
+ * Creates a Form component which state is accessible via context - useFormContext() hook.
10
+ */
11
+ declare function createForm<Data, Error = Data, ValidationError = Record<string, never>>(action: FormAction<Data, Error, ValidationError>): {
12
+ Form: ({ children, ...props }: PropsWithChildren<{
13
+ initialData: Data;
14
+ }>) => react__default.JSX.Element;
15
+ };
16
+
8
17
  type FormStateProps<Data, Error, ValidationError, Payload> = {
9
- action: (state: FormState<Data, Error, ValidationError>, payload: Payload) => Promise<FormState<Data, Error, ValidationError>>;
18
+ action: (state: ActionState<Data, Error, ValidationError>, payload: Payload) => Promise<ActionState<Data, Error, ValidationError>>;
10
19
  initialData: Data;
11
20
  permalink?: string;
21
+ /**
22
+ * Opt-in into automatic form reset by using the form "action" prop.
23
+ * By default, the onSubmit with a custom transition which opts-out of the implicit form reset.
24
+ * See for more. https://github.com/facebook/react/issues/29034
25
+ * @default false
26
+ */
27
+ autoReset?: boolean;
12
28
  };
13
29
  declare function initial<Data>(data: Data): InitialState<Data>;
14
- type FormStatusFlags<T extends FormState<unknown, unknown, unknown>["type"] | unknown = unknown> = {
30
+ type FormStatusFlags<T extends ActionState<unknown, unknown, unknown>["type"] | unknown = unknown> = {
15
31
  isInitial: T extends "initial" ? true : false;
16
32
  isInvalid: T extends "invalid" ? true : false;
17
33
  isFailure: T extends "failure" ? true : false;
18
34
  isSuccess: T extends "success" ? true : false;
19
35
  };
20
- type FormMetaState<T extends FormState<unknown, unknown, unknown>> = T & FormStatusFlags<T["type"]> & {
36
+ type FormMetaState<T extends ActionState<unknown, unknown, unknown>> = T & FormStatusFlags<T["type"]> & {
21
37
  isPending: boolean;
22
38
  };
23
39
  type FormProps<Data, Error, ValidationError> = Omit<FormHTMLAttributes<HTMLFormElement>, "action" | "children"> & FormStateProps<Data, Error, ValidationError, FormData> & RenderProp<FormMetaState<InitialState<Data>> | FormMetaState<InvalidState<ValidationError>> | FormMetaState<FailureState<Error>> | FormMetaState<SuccessState<Data>>>;
24
- declare function Form<Data, Error, ValidationError>({ children, action, initialData, permalink, ...props }: FormProps<Data, Error, ValidationError>): React__default.JSX.Element;
40
+ declare function Form<Data, Error, ValidationError>({ children, action, initialData, permalink, autoReset, ...props }: FormProps<Data, Error, ValidationError>): react__default.JSX.Element;
41
+
42
+ /**
43
+ * A context exposing the form action state.
44
+ * @private You don't need to use this component directly. Use the `createForm` function instead.
45
+ */
46
+ declare const FormContext: react.Context<FormMetaState<ActionState<unknown, unknown, unknown>> | null>;
47
+ /**
48
+ * A hook to consume the form action state from the context.
49
+ */
50
+ declare const useFormContext: () => FormMetaState<ActionState<unknown, unknown, unknown>>;
51
+
52
+ declare function FormStatus({ children, }: RenderProp<ReturnType<typeof useFormStatus>>): react.ReactNode;
25
53
 
26
- declare function FormStatus({ children, }: RenderProp<ReturnType<typeof useFormStatus>>): React.ReactNode;
54
+ /**
55
+ * Conditionally renders the children, when the form action is in the "pending" state.
56
+ */
57
+ declare function Pending({ children }: PropsWithChildren): react.ReactNode;
27
58
 
28
59
  declare const SEPARATOR: ".";
29
60
  type Paths<Err> = {
@@ -36,6 +67,6 @@ type ZodFieldErrorProps<Errors extends ZodFormattedError<any>> = {
36
67
  } & Partial<RenderProp<{
37
68
  errors: readonly string[];
38
69
  }>>;
39
- declare function ZodFieldError<Errors extends ZodFormattedError<any>>({ errors, name, children, }: ZodFieldErrorProps<Errors>): React__default.ReactNode;
70
+ declare function ZodFieldError<Errors extends ZodFormattedError<any>>({ errors, name, children, }: ZodFieldErrorProps<Errors>): react__default.ReactNode;
40
71
 
41
- export { Form, FormProps, FormStateProps, FormStatus, InferZodErrorPaths, Paths, ZodFieldError, ZodFieldErrorProps, initial };
72
+ export { Form, FormContext, FormMetaState, FormProps, FormStateProps, FormStatus, InferZodErrorPaths, Paths, Pending, ZodFieldError, ZodFieldErrorProps, createForm, initial, useFormContext };
package/dist/client.mjs CHANGED
@@ -1 +1 @@
1
- "use client";import d from"react";import{useActionState as u}from"react";function c(t){return{type:"initial",data:t,error:null,validationError:null}}var i={isInitial:!1,isInvalid:!1,isFailure:!1,isSuccess:!1};function P({children:t,action:a,initialData:n,permalink:e,...o}){let[r,l,p]=u(a,c(n),e),m=r.type==="initial"?{...r,...i,isInitial:!0}:r.type==="invalid"?{...r,...i,isInvalid:!0}:r.type==="failure"?{...r,...i,isFailure:!0}:{...r,...i,isSuccess:!0};return d.createElement("form",{action:l,...o},t({...m,isPending:p}))}import{useFormStatus as E}from"react-dom";function D({children:t}){return t(E())}import s from"react";var f=".",F=Object.freeze({_errors:Object.freeze([])});function v({errors:t,name:a="",children:n=({errors:e})=>s.createElement(s.Fragment,null,e[0])}){if(a.length===0)return n({errors:t._errors});let e=a.split(f),o=t;for(;e.length;){let r=e.shift();o=o[r]??F}return n({errors:o._errors})}export{P as Form,D as FormStatus,v as ZodFieldError,c as initial};
1
+ "use client";import f from"react";import x,{useTransition as P}from"react";import{useActionState as D}from"react";function k(r){return{type:"initial",data:r,error:null,validationError:null}}var s={isInitial:!1,isInvalid:!1,isFailure:!1,isSuccess:!1};function c({children:r,action:e,initialData:a,permalink:t,autoReset:o=!1,...m}){let[n,l,i]=D(e,k(a),t),[,E]=P(),S=n.type==="initial"?{...n,...s,isPending:i,isInitial:!0}:n.type==="invalid"?{...n,...s,isPending:i,isInvalid:!0}:n.type==="failure"?{...n,...s,isPending:i,isFailure:!0}:{...n,...s,isPending:i,isSuccess:!0};return x.createElement("form",{...o?{action:l}:{onSubmit:u=>{u.preventDefault();let y=u.currentTarget;E(()=>{l(new FormData(y))})}},...m},r(S))}import{createContext as A,use as h}from"react";var p=A(null),d=()=>{let r=h(p);if(!r)throw new Error("Form Context must be initialized before use.");return r};function z(r){return{Form:({children:a,...t})=>f.createElement(c,{action:r,...t},o=>f.createElement(p,{value:o},a))}}import{useFormStatus as T}from"react-dom";function W({children:r}){return r(T())}function $({children:r}){let{isPending:e}=d();return e&&r}import F from"react";var g=".",v=Object.freeze({_errors:Object.freeze([])});function G({errors:r,name:e="",children:a=({errors:t})=>F.createElement(F.Fragment,null,t[0])}){if(e.length===0)return a({errors:r._errors});let t=e.split(g),o=r;for(;t.length;){let m=t.shift();o=o[m]??v}return a({errors:o._errors})}export{c as Form,p as FormContext,W as FormStatus,$ as Pending,G as ZodFieldError,z as createForm,k as initial,d as useFormContext};
@@ -22,11 +22,12 @@ type SuccessState<T> = {
22
22
  error: null;
23
23
  validationError: null;
24
24
  };
25
- type FormState<Data, Error, ValidationError = Record<string, never>> = InitialState<Data> | InvalidState<ValidationError> | FailureState<Error> | SuccessState<Data>;
25
+ type ActionState<Data, Error, ValidationError = Record<string, never>> = InitialState<Data> | InvalidState<ValidationError> | FailureState<Error> | SuccessState<Data>;
26
+ type FormAction<Data, Error = Data, ValidationError = Record<string, never>, Payload = FormData> = (state: ActionState<Data, Error, ValidationError>, payload: Payload) => Promise<ActionState<Data, Error, ValidationError>>;
26
27
  declare function createFormAction<Data, Error = Data, ValidationError = Record<string, never>, Payload = FormData>(formAction: (params: {
27
28
  success: (data: Data) => SuccessState<Data>;
28
29
  failure: (error: Error) => FailureState<Error>;
29
30
  invalid: (validationError: ValidationError) => InvalidState<ValidationError>;
30
- }) => (state: FormState<Data, Error, ValidationError>, payload: Payload) => Promise<FormState<Data, Error, ValidationError>>): (state: FormState<Data, Error, ValidationError>, payload: Payload) => Promise<FormState<Data, Error, ValidationError>>;
31
+ }) => FormAction<Data, Error, ValidationError, Payload>): (state: ActionState<Data, Error, ValidationError>, payload: Payload) => Promise<ActionState<Data, Error, ValidationError>>;
31
32
 
32
- export { FormState as F, InvalidState as I, SuccessState as S, FailureState as a, InitialState as b, createFormAction as c };
33
+ export { ActionState as A, FailureState as F, InvalidState as I, SuccessState as S, InitialState as a, FormAction as b, createFormAction as c };
package/dist/index.d.mts CHANGED
@@ -1,5 +1,5 @@
1
- import { F as FormState, I as InvalidState, a as FailureState, S as SuccessState } from './createFormAction-3a51310f.js';
2
- export { b as InitialState, c as createFormAction } from './createFormAction-3a51310f.js';
1
+ import { A as ActionState, I as InvalidState, F as FailureState, S as SuccessState } from './createFormAction-4e719362.js';
2
+ export { b as FormAction, a as InitialState, c as createFormAction } from './createFormAction-4e719362.js';
3
3
  import { ZodTypeAny, ZodType, ZodObject, ZodRawShape, objectUtil, z, ZodSchema, ZodEffects } from 'zod';
4
4
 
5
5
  declare const emptyInput: unique symbol;
@@ -30,7 +30,7 @@ type FormActionBuilder<Schema extends ZodTypeAny | EmptyInput, Err = Error, Cont
30
30
  * @param action async function to execute. It will receive parsed formData as input, when the builder was initialized with a schema.
31
31
  * @returns runnable server action exportable from a module.
32
32
  */
33
- run: Schema extends ZodTypeAny ? <Data>(action: SchemaAction<Data, Context, Schema>) => (state: FormState<Data, Err, z.inferFormattedError<Schema>>, payload: FormData) => Promise<Flatten<InvalidState<z.inferFormattedError<Schema>> | FailureState<Err> | SuccessState<Data>>> : <Data>(action: Action<Data, Context>) => (state: FormState<Data, Err>, payload: FormData) => Promise<Flatten<FailureState<Err> | SuccessState<Data>>>;
33
+ run: Schema extends ZodTypeAny ? <Data>(action: SchemaAction<Data, Context, Schema>) => (state: ActionState<Data, Err, z.inferFormattedError<Schema>>, payload: FormData) => Promise<Flatten<InvalidState<z.inferFormattedError<Schema>> | FailureState<Err> | SuccessState<Data>>> : <Data>(action: Action<Data, Context>) => (state: ActionState<Data, Err>, payload: FormData) => Promise<Flatten<FailureState<Err> | SuccessState<Data>>>;
34
34
  /**
35
35
  * A chainable context enhancing helper.
36
36
  * @param middleware A function which receives the current context, and returns an object which will be added to the context.
@@ -52,4 +52,4 @@ type FormActionBuilder<Schema extends ZodTypeAny | EmptyInput, Err = Error, Cont
52
52
  };
53
53
  declare const formAction: FormActionBuilder<typeof emptyInput, Error, InitialContext>;
54
54
 
55
- export { FailureState, FormState, InvalidState, SuccessState, formAction };
55
+ export { ActionState, FailureState, InvalidState, SuccessState, formAction };
package/dist/index.mjs CHANGED
@@ -1 +1 @@
1
- function S(e){function o(n){return{type:"success",data:n,error:null,validationError:null}}function a(n){return{type:"failure",data:null,error:n,validationError:null}}function s(n){return{type:"invalid",data:null,error:null,validationError:n}}return(n,f)=>e({success:o,failure:a,invalid:s})(n,f)}import{ZodFirstPartyTypeKind as C}from"zod";import{zfd as w}from"zod-form-data";var l=Symbol();function d(e,o=[],a){async function s(t){let r={formData:t};for(let i of o){let u=await i({ctx:r});r={...r,...u}}return r}return{input(t){if(e===l)return d(t,o,a);if(e._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(e._def.typeName===C.ZodObject){let r=e.merge(t);return d(r,o)}else throw Error("Merging inputs works only for object schemas without effects.")},use(t){return d(e,[...o,t],a)},error(t){return d(e,o,t)},run:e===l?t=>S(({success:r,failure:i})=>async(u,m)=>{let x=await s(m);try{return r(await t({ctx:x}))}catch(c){if(a)return i(a({error:c,ctx:x}));throw c}}):e===l?void 0:t=>S(({success:r,failure:i,invalid:u})=>{let m=w.formData(e);return async(x,c)=>{let y=await s(c),p=m.safeParse(c);if(!p.success)return u(p.error.format());let h=p.data;try{return r(await t({input:h,ctx:y}))}catch(E){if(a)return i(a({error:E,ctx:y}));throw E}}})}}var b=d(l);export{S as createFormAction,b as formAction};
1
+ function y(e){function o(n){return{type:"success",data:n,error:null,validationError:null}}function a(n){return{type:"failure",data:null,error:n,validationError:null}}function s(n){return{type:"invalid",data:null,error:null,validationError:n}}return(n,S)=>e({success:o,failure:a,invalid:s})(n,S)}import{ZodFirstPartyTypeKind as C}from"zod";import{zfd as w}from"zod-form-data";var l=Symbol();function d(e,o=[],a){async function s(t){let r={formData:t};for(let i of o){let u=await i({ctx:r});r={...r,...u}}return r}return{input(t){if(e===l)return d(t,o,a);if(e._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(e._def.typeName===C.ZodObject){let r=e.merge(t);return d(r,o)}else throw Error("Merging inputs works only for object schemas without effects.")},use(t){return d(e,[...o,t],a)},error(t){return d(e,o,t)},run:e===l?t=>y(({success:r,failure:i})=>async(u,x)=>{let m=await s(x);try{return r(await t({ctx:m}))}catch(c){if(a)return i(a({error:c,ctx:m}));throw c}}):e===l?void 0:t=>y(({success:r,failure:i,invalid:u})=>{let x=w.formData(e);return async(m,c)=>{let f=await s(c),p=x.safeParse(c);if(!p.success)return u(p.error.format());let h=p.data;try{return r(await t({input:h,ctx:f}))}catch(E){if(a)return i(a({error:E,ctx:f}));throw E}}})}}var P=d(l);export{y as createFormAction,P as formAction};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-form-action",
3
- "version": "1.2.3",
3
+ "version": "1.4.0",
4
4
  "description": "State management helpers for the react form actions.",
5
5
  "repository": {
6
6
  "type": "git",