react-form-action 1.4.0 → 1.4.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/dist/client.d.mts CHANGED
@@ -1,17 +1,52 @@
1
- import * as react from 'react';
2
- import react__default, { PropsWithChildren, FormHTMLAttributes } from 'react';
1
+ import * as React from 'react';
2
+ import React__default, { PropsWithChildren, FormHTMLAttributes } from 'react';
3
3
  import { b as FormAction, A as ActionState, a as InitialState, I as InvalidState, F as FailureState, S as SuccessState } from './createFormAction-4e719362.js';
4
4
  import { RenderProp } from 'react-render-prop-type';
5
5
  import { useFormStatus } from 'react-dom';
6
6
  import { ZodFormattedError } from 'zod';
7
7
 
8
+ type ActionProps<Data, Error, ValidationError> = PropsWithChildren<{
9
+ action: FormAction<Data, Error, ValidationError>;
10
+ initialData: Data;
11
+ permalink?: string;
12
+ }>;
13
+ type ActionStatusFlags<T extends ActionState<unknown, unknown, unknown>["type"] | unknown = unknown> = {
14
+ isInitial: T extends "initial" ? true : false;
15
+ isInvalid: T extends "invalid" ? true : false;
16
+ isFailure: T extends "failure" ? true : false;
17
+ isSuccess: T extends "success" ? true : false;
18
+ };
19
+ type ActionContextState<T extends ActionState<unknown, unknown, unknown>> = T & ActionStatusFlags<T["type"]> & {
20
+ /**
21
+ * The dispatch function returned from React.useActionState()
22
+ */
23
+ action: (payload: FormData) => void;
24
+ isPending: boolean;
25
+ };
26
+ /**
27
+ * A hook to consume the form action state from the context.
28
+ */
29
+ declare const useActionContext: () => ActionContextState<ActionState<unknown, unknown, unknown>>;
30
+ declare function Action<Data, Error, ValidationError>({ children, action: formAction, initialData, permalink, }: ActionProps<Data, Error, ValidationError>): React__default.JSX.Element;
31
+
32
+ type ActionFormProps = Omit<FormHTMLAttributes<HTMLFormElement>, "action"> & {
33
+ /**
34
+ * Opt-in into automatic form reset by using the form "action" prop.
35
+ * By default, the onSubmit with a custom transition which opts-out of the implicit form reset.
36
+ * See for more. https://github.com/facebook/react/issues/29034
37
+ * @default false
38
+ */
39
+ autoReset?: boolean;
40
+ };
41
+ declare function ActionForm({ autoReset, ...props }: ActionFormProps): React__default.JSX.Element;
42
+
8
43
  /**
9
44
  * Creates a Form component which state is accessible via context - useFormContext() hook.
10
45
  */
11
46
  declare function createForm<Data, Error = Data, ValidationError = Record<string, never>>(action: FormAction<Data, Error, ValidationError>): {
12
47
  Form: ({ children, ...props }: PropsWithChildren<{
13
48
  initialData: Data;
14
- }>) => react__default.JSX.Element;
49
+ }>) => React__default.JSX.Element;
15
50
  };
16
51
 
17
52
  type FormStateProps<Data, Error, ValidationError, Payload> = {
@@ -37,24 +72,24 @@ type FormMetaState<T extends ActionState<unknown, unknown, unknown>> = T & FormS
37
72
  isPending: boolean;
38
73
  };
39
74
  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>>>;
40
- declare function Form<Data, Error, ValidationError>({ children, action, initialData, permalink, autoReset, ...props }: FormProps<Data, Error, ValidationError>): react__default.JSX.Element;
75
+ declare function Form<Data, Error, ValidationError>({ children, action, initialData, permalink, autoReset, ...props }: FormProps<Data, Error, ValidationError>): React__default.JSX.Element;
41
76
 
42
77
  /**
43
78
  * A context exposing the form action state.
44
79
  * @private You don't need to use this component directly. Use the `createForm` function instead.
45
80
  */
46
- declare const FormContext: react.Context<FormMetaState<ActionState<unknown, unknown, unknown>> | null>;
81
+ declare const FormContext: React.Context<FormMetaState<ActionState<unknown, unknown, unknown>> | null>;
47
82
  /**
48
83
  * A hook to consume the form action state from the context.
49
84
  */
50
85
  declare const useFormContext: () => FormMetaState<ActionState<unknown, unknown, unknown>>;
51
86
 
52
- declare function FormStatus({ children, }: RenderProp<ReturnType<typeof useFormStatus>>): react.ReactNode;
87
+ declare function FormStatus({ children, }: RenderProp<ReturnType<typeof useFormStatus>>): React.ReactNode;
53
88
 
54
89
  /**
55
90
  * Conditionally renders the children, when the form action is in the "pending" state.
56
91
  */
57
- declare function Pending({ children }: PropsWithChildren): react.ReactNode;
92
+ declare function Pending({ children }: PropsWithChildren): React.ReactNode;
58
93
 
59
94
  declare const SEPARATOR: ".";
60
95
  type Paths<Err> = {
@@ -67,6 +102,6 @@ type ZodFieldErrorProps<Errors extends ZodFormattedError<any>> = {
67
102
  } & Partial<RenderProp<{
68
103
  errors: readonly string[];
69
104
  }>>;
70
- declare function ZodFieldError<Errors extends ZodFormattedError<any>>({ errors, name, children, }: ZodFieldErrorProps<Errors>): react__default.ReactNode;
105
+ declare function ZodFieldError<Errors extends ZodFormattedError<any>>({ errors, name, children, }: ZodFieldErrorProps<Errors>): React__default.ReactNode;
71
106
 
72
- export { Form, FormContext, FormMetaState, FormProps, FormStateProps, FormStatus, InferZodErrorPaths, Paths, Pending, ZodFieldError, ZodFieldErrorProps, createForm, initial, useFormContext };
107
+ export { Action, ActionContextState, ActionForm, ActionFormProps, ActionProps, Form, FormContext, FormMetaState, FormProps, FormStateProps, FormStatus, InferZodErrorPaths, Paths, Pending, ZodFieldError, ZodFieldErrorProps, createForm, initial, useActionContext, useFormContext };
package/dist/client.mjs CHANGED
@@ -1 +1 @@
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};
1
+ "use client";import k from"react";import{createContext as D,use as T}from"react";import{useActionState as w}from"react";function h(t){return{type:"initial",data:t,error:null,validationError:null}}var l={isInitial:!1,isInvalid:!1,isFailure:!1,isSuccess:!1},d=D(null),f=()=>{let t=T(d);if(!t)throw new Error("ActionContext must be initialized before use. Is your useActionContext hook wrapped with an <Action> Component?");return t};function _({children:t,action:e,initialData:n,permalink:a}){let[r,i,o]=w(e,h(n),a),s=r.type==="initial"?{...r,...l,action:i,isPending:o,isInitial:!0}:r.type==="invalid"?{...r,...l,action:i,isPending:o,isInvalid:!0}:r.type==="failure"?{...r,...l,action:i,isPending:o,isFailure:!0}:{...r,...l,action:i,isPending:o,isSuccess:!0};return k.createElement(d,{value:s},t)}import v,{startTransition as g}from"react";function G({autoReset:t=!1,...e}){let{action:n}=f();return v.createElement("form",{...t?{action:n}:{onSubmit:r=>{r.preventDefault();let i=r.currentTarget;g(()=>{n(new FormData(i))})}},...e})}import S from"react";import C,{useTransition as I}from"react";import{useActionState as b}from"react";function M(t){return{type:"initial",data:t,error:null,validationError:null}}var u={isInitial:!1,isInvalid:!1,isFailure:!1,isSuccess:!1};function F({children:t,action:e,initialData:n,permalink:a,autoReset:r=!1,...i}){let[o,s,m]=b(e,M(n),a),[,y]=I(),A=o.type==="initial"?{...o,...u,isPending:m,isInitial:!0}:o.type==="invalid"?{...o,...u,isPending:m,isInvalid:!0}:o.type==="failure"?{...o,...u,isPending:m,isFailure:!0}:{...o,...u,isPending:m,isSuccess:!0};return C.createElement("form",{...r?{action:s}:{onSubmit:p=>{p.preventDefault();let P=p.currentTarget;y(()=>{s(new FormData(P))})}},...i},t(A))}import{createContext as V,use as R}from"react";var c=V(null),E=()=>{let t=R(c);if(!t)throw new Error("Form Context must be initialized before use.");return t};function ot(t){return{Form:({children:n,...a})=>S.createElement(F,{action:t,...a},r=>S.createElement(c,{value:r},n))}}import{useFormStatus as Z}from"react-dom";function at({children:t}){return t(Z())}function mt({children:t}){let{isPending:e}=E();return e&&t}import x from"react";var H=".",L=Object.freeze({_errors:Object.freeze([])});function ct({errors:t,name:e="",children:n=({errors:a})=>x.createElement(x.Fragment,null,a[0])}){if(e.length===0)return n({errors:t._errors});let a=e.split(H),r=t;for(;a.length;){let i=a.shift();r=r[i]??L}return n({errors:r._errors})}export{_ as Action,G as ActionForm,F as Form,c as FormContext,at as FormStatus,mt as Pending,ct as ZodFieldError,ot as createForm,M as initial,f as useActionContext,E as useFormContext};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-form-action",
3
- "version": "1.4.0",
3
+ "version": "1.4.1",
4
4
  "description": "State management helpers for the react form actions.",
5
5
  "repository": {
6
6
  "type": "git",