react-form-action 2.5.2 → 2.6.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 +543 -543
- package/dist/client.d.mts +28 -23
- package/dist/client.mjs +1 -1
- package/dist/index.d.mts +12 -12
- package/dist/index.mjs +1 -1
- package/package.json +76 -74
package/dist/client.d.mts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import * as React from 'react';
|
|
1
|
+
import * as React$1 from 'react';
|
|
2
2
|
import React__default, { PropsWithChildren, JSX } from 'react';
|
|
3
3
|
import { F as FormAction, A as ActionState, I as InitialState, a as InvalidState, b as FailureState, S as SuccessState } from './createFormAction-DjhozgiG.mjs';
|
|
4
|
-
import {
|
|
5
|
-
import { RenderProp, RP } from 'react-render-prop-type';
|
|
4
|
+
import { $ZodErrorTree } from 'zod/v4/core';
|
|
6
5
|
|
|
7
6
|
type ActionProps<Data, Error, ValidationError> = PropsWithChildren<{
|
|
8
7
|
action: FormAction<Data, Error, ValidationError>;
|
|
@@ -44,45 +43,51 @@ type FormProps = Omit<JSX.IntrinsicElements["form"], "action"> & {
|
|
|
44
43
|
declare function Form({ autoReset, ...props }: FormProps): JSX.Element;
|
|
45
44
|
|
|
46
45
|
declare const SEPARATOR: ".";
|
|
47
|
-
type Paths<
|
|
48
|
-
[k in keyof
|
|
49
|
-
}[keyof
|
|
50
|
-
type InferZodErrorPaths<
|
|
51
|
-
type ZodFieldErrorProps<Errors extends
|
|
46
|
+
type Paths<Shape extends $ZodErrorTree<object>, Props = Required<NonNullable<Shape["properties"]>>> = {
|
|
47
|
+
[k in keyof Props]: k extends string ? NonNullable<Props[k]> extends $ZodErrorTree<object> ? "properties" extends keyof NonNullable<Props[k]> ? `${k}${typeof SEPARATOR}${Paths<NonNullable<Props[k]>>}` : k : k : never;
|
|
48
|
+
}[keyof Props];
|
|
49
|
+
type InferZodErrorPaths<T> = T extends $ZodErrorTree<object> ? Paths<T> : never;
|
|
50
|
+
type ZodFieldErrorProps<Errors extends $ZodErrorTree<any>, Name extends "" | InferZodErrorPaths<Errors>> = {
|
|
52
51
|
errors: Errors;
|
|
53
52
|
name: Name;
|
|
54
|
-
|
|
53
|
+
children?: (props: ZodFieldErrorChildrenProps<Name>) => React__default.ReactNode;
|
|
54
|
+
};
|
|
55
55
|
type ZodFieldErrorChildrenProps<Name> = {
|
|
56
56
|
name: Name;
|
|
57
57
|
error?: string;
|
|
58
58
|
errors: readonly string[];
|
|
59
59
|
};
|
|
60
60
|
declare const noError: Readonly<{
|
|
61
|
-
|
|
61
|
+
errors: readonly string[];
|
|
62
62
|
}>;
|
|
63
|
-
declare function ZodFieldError<Errors extends
|
|
63
|
+
declare function ZodFieldError<Errors extends $ZodErrorTree<object>, Name extends "" | InferZodErrorPaths<Errors> = "">({ errors, name, children, }: ZodFieldErrorProps<Errors, Name>): React__default.ReactNode;
|
|
64
64
|
|
|
65
65
|
/**
|
|
66
66
|
* Creates a typed components for actions created with the formAction builder.
|
|
67
67
|
*/
|
|
68
|
-
declare function createComponents<Data, Error, ValidationError extends
|
|
68
|
+
declare function createComponents<Data, Error, ValidationError extends $ZodErrorTree<any>>(action: FormAction<Data, Error, ValidationError>): {
|
|
69
69
|
FieldError: <Name extends "" | InferZodErrorPaths<ValidationError>>({ name, children, }: {
|
|
70
70
|
name: Name;
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
71
|
+
children?: (props: ZodFieldErrorChildrenProps<Name>) => React__default.ReactNode;
|
|
72
|
+
}) => React__default.JSX.Element;
|
|
73
|
+
Success: ({ children, }: PropsWithChildren | {
|
|
74
|
+
children?: (props: {
|
|
75
|
+
isSuccess: false;
|
|
76
|
+
data: Data | null;
|
|
77
|
+
} | {
|
|
78
|
+
isSuccess: true;
|
|
79
|
+
data: Data;
|
|
80
|
+
}) => React__default.ReactNode;
|
|
81
|
+
}) => React__default.ReactNode;
|
|
79
82
|
};
|
|
80
83
|
|
|
81
84
|
/**
|
|
82
85
|
* Conditionally renders the children, when the form action is in the "pending" state.
|
|
83
86
|
*/
|
|
84
|
-
declare function Pending({ children, }: PropsWithChildren |
|
|
85
|
-
|
|
86
|
-
|
|
87
|
+
declare function Pending({ children, }: PropsWithChildren | {
|
|
88
|
+
children?: (props: {
|
|
89
|
+
isPending: boolean;
|
|
90
|
+
}) => React.ReactNode;
|
|
91
|
+
}): React$1.ReactNode;
|
|
87
92
|
|
|
88
93
|
export { Action, type ActionContextState, type ActionProps, Form, type FormProps, type InferZodErrorPaths, type Paths, Pending, ZodFieldError, type ZodFieldErrorChildrenProps, type ZodFieldErrorProps, createComponents, noError, useActionContext };
|
package/dist/client.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use client";import{a as
|
|
1
|
+
"use client";import{a as u}from"./chunk-LPB4R2UB.mjs";import S from"react";import{createContext as A,use as P}from"react";import{useActionState as y}from"react";var c={isInitial:!1,isInvalid:!1,isFailure:!1,isSuccess:!1},m=A(null);function a(o){let t=P(m);if(!t)throw new Error("ActionContext must be initialized before use. Is your useActionContext hook wrapped with an <Action> Component?");return t}function b({children:o,action:t,initialData:i,permalink:e}){let[r,n,s]=y(t,u(i),e),d=r.type==="initial"?{...r,...c,action:n,isPending:s,isInitial:!0}:r.type==="invalid"?{...r,...c,action:n,isPending:s,isInvalid:!0}:r.type==="failure"?{...r,...c,action:n,isPending:s,isFailure:!0}:{...r,...c,action:n,isPending:s,isSuccess:!0};return S.createElement(m,{value:d},o)}import h,{startTransition as F}from"react";function I({autoReset:o=!1,...t}){let{action:i}=a();return h.createElement("form",{...o?{action:i}:{onSubmit:r=>{r.preventDefault();let n=r.currentTarget;F(()=>{i(new FormData(n))})}},...t})}import p from"react";import f from"react";var C=".",l=Object.freeze({errors:Object.freeze([])});function E({errors:o,name:t,children:i=({error:e})=>f.createElement(f.Fragment,null,e)}){if(t.length===0)return i({name:t,error:o.errors[0],errors:o.errors});let e=t.split(C),r=o;for(;e.length;){let n=e.shift();if(!r.properties){r=l;break}r=r.properties[n]??l}return i({name:t,error:r.errors[0],errors:r.errors})}function J(o){function t({name:e,children:r}){let{isInvalid:n,validationError:s}=a(o);return p.createElement(E,{errors:s??l,name:e},r??(({error:x})=>n&&p.createElement(p.Fragment,null,x)))}function i({children:e}){let{isSuccess:r,data:n}=a(o);return typeof e=="function"?e(r?{isSuccess:r,data:n}:{isSuccess:r,data:n}):r&&e}return{FieldError:t,Success:i}}function q({children:o}){let{isPending:t}=a();return typeof o=="function"?o({isPending:t}):t&&o}export{b as Action,I as Form,q as Pending,E as ZodFieldError,J as createComponents,l as noError,a as useActionContext};
|
package/dist/index.d.mts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { A as ActionState, a as InvalidState, b as FailureState, S as SuccessState } from './createFormAction-DjhozgiG.mjs';
|
|
2
2
|
export { F as FormAction, I as InitialState, c as createFormAction, i as initial } from './createFormAction-DjhozgiG.mjs';
|
|
3
|
-
import {
|
|
3
|
+
import { ZodType, ZodTuple, ZodObject, z } from 'zod/v4';
|
|
4
|
+
import { $ZodShape, $ZodErrorTree } from 'zod/v4/core';
|
|
4
5
|
|
|
5
6
|
declare const emptyInput: unique symbol;
|
|
6
7
|
type EmptyInput = typeof emptyInput;
|
|
@@ -16,36 +17,35 @@ type InitialContext = Record<"formData", FormData>;
|
|
|
16
17
|
/**
|
|
17
18
|
* Action without schema has no input.
|
|
18
19
|
*/
|
|
19
|
-
type Action<Data, Context, Args extends
|
|
20
|
+
type Action<Data, Context, Args extends ZodTuple> = (params: {
|
|
20
21
|
args: z.infer<Args>;
|
|
21
22
|
ctx: Context;
|
|
22
23
|
}) => Promise<Data>;
|
|
23
24
|
/**
|
|
24
25
|
* Action with schema will get parsed formData as the input.
|
|
25
26
|
*/
|
|
26
|
-
type SchemaAction<Data, Context, Args extends
|
|
27
|
+
type SchemaAction<Data, Context, Args extends ZodTuple, Schema extends ZodType> = (params: {
|
|
27
28
|
args: z.infer<Args>;
|
|
28
29
|
ctx: Context;
|
|
29
30
|
input: z.infer<Schema>;
|
|
30
31
|
}) => Promise<Data>;
|
|
31
|
-
type
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
input: <T extends ZodTypeAny>(newInput: T extends ZodType<infer Out extends Record<string, unknown>> ? Schema extends AnyZodEffects ? "Extending schema with effect is not possible." : Schema extends EmptyInput ? T : Schema extends ZodTypeAny ? T extends AnyZodEffects ? "Your input contains effect which prevents merging it with the previous inputs." : T : T : "The schema output must be an object.") => FormActionBuilder<Schema extends ZodObject<infer O1 extends ZodRawShape> ? T extends ZodObject<infer O2 extends ZodRawShape> ? ZodObject<objectUtil.extendShape<O1, O2>> : never : T, TErr, Context, Args>;
|
|
32
|
+
type FormActionBuilder<Schema extends ZodType | EmptyInput, TErr = unknown, Context = InitialContext, Args extends ZodTuple = ZodTuple<[], null>> = {
|
|
33
|
+
args: <T extends [ZodType, ...ZodType[]]>(args: T) => FormActionBuilder<Schema, TErr, Context, ZodTuple<T, null>>;
|
|
34
|
+
input: <T extends ZodObject>(newInput: T extends ZodObject ? T : "The schema output must be an object.") => FormActionBuilder<Schema extends ZodObject<infer O1 extends $ZodShape> ? T extends ZodObject<infer O2 extends $ZodShape> ? ZodObject<O1 & O2> : never : T, TErr, Context, Args>;
|
|
35
35
|
/**
|
|
36
36
|
* A finishing call on the builder completing your action.
|
|
37
37
|
* @param action async function to execute. It will receive parsed formData as input, when the builder was initialized with a schema.
|
|
38
38
|
* @returns runnable server action exportable from a module.
|
|
39
39
|
*/
|
|
40
|
-
run: Schema extends
|
|
40
|
+
run: Schema extends ZodType ? <Data>(action: SchemaAction<Data, Flatten<Context>, Args, Schema>) => (...args: [
|
|
41
41
|
...z.infer<Args>,
|
|
42
|
-
state: ActionState<Data, TErr, z.
|
|
42
|
+
state: ActionState<Data, TErr, $ZodErrorTree<z.output<Schema>> & $ZodErrorTree<z.output<Args>>>,
|
|
43
43
|
payload: FormData
|
|
44
|
-
]) => Promise<Flatten<InvalidState<z.
|
|
44
|
+
]) => Promise<Flatten<InvalidState<$ZodErrorTree<z.output<Schema>> & $ZodErrorTree<z.output<Args>>> | FailureState<TErr> | SuccessState<Data>>> : <Data>(action: Action<Data, Flatten<Context>, Args>) => (...args: [
|
|
45
45
|
...z.infer<Args>,
|
|
46
|
-
state: ActionState<Data, TErr, z.
|
|
46
|
+
state: ActionState<Data, TErr, $ZodErrorTree<z.output<Args>>>,
|
|
47
47
|
payload: FormData
|
|
48
|
-
]) => Promise<Flatten<InvalidState<z.
|
|
48
|
+
]) => Promise<Flatten<InvalidState<$ZodErrorTree<z.output<Args>>> | FailureState<TErr> | SuccessState<Data>>>;
|
|
49
49
|
/**
|
|
50
50
|
* A chainable context enhancing helper.
|
|
51
51
|
* @param middleware A function which receives the current context, and returns an object which will be added to the context.
|
package/dist/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{a as
|
|
1
|
+
import{a as h,b as f}from"./chunk-LPB4R2UB.mjs";import{z as T}from"zod/v4";import{zfd as g}from"zod-form-data";var m=Symbol();function c(e,s=[],n,a){async function A(t){let r={formData:t};for(let i of s){let u=await i({ctx:r});r={...r,...u}}return r}return{args(t){return c(e,s,n,T.tuple(t))},input(t){if(e===m)return c(t,s,n,a);if(e._zod.def.checks)throw new Error("Previous input is not augmentable because it contains an effect.");if(t._zod.def.checks)throw new Error("Your input contains effect which prevents merging it with the previous inputs.");if(e._zod.def.type==="object"){let r=T.object({...e.shape,...t.shape});return c(r,s)}else throw Error("Merging inputs works only for object schemas without effects.")},use(t){return c(e,[...s,t],n,a)},error(t){return c(e,s,t,a)},run:e===m?t=>f(({success:r,failure:i,invalid:u},...d)=>async(E,y)=>{let p=await A(y);if(a){let o=a.safeParse(d);if(!o.success)return u(T.treeifyError(o.error))}try{return r(await t({args:d,ctx:p}))}catch(o){if(n)return i(await n({error:o,ctx:p}));throw o}}):e===m?void 0:t=>f(({success:r,failure:i,invalid:u},...d)=>{let E=g.formData(e);return async(y,p)=>{let o=await A(p);if(a){let x=a.safeParse(d);if(!x.success)return u(T.treeifyError(x.error))}let l=E.safeParse(p);if(!l.success)return u(T.treeifyError(l.error));let C=l.data;try{return r(await t({args:d,input:C,ctx:o}))}catch(x){if(n)return i(await n({error:x,ctx:o}));throw x}}})}}var j=c(m);export{f as createFormAction,j as formAction,h as initial};
|
package/package.json
CHANGED
|
@@ -1,74 +1,76 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "react-form-action",
|
|
3
|
-
"version": "2.
|
|
4
|
-
"description": "State management helpers for the react form actions.",
|
|
5
|
-
"repository": {
|
|
6
|
-
"type": "git",
|
|
7
|
-
"url": "https://github.com/MiroslavPetrik/react-form-action.git"
|
|
8
|
-
},
|
|
9
|
-
"main": "dist/index.js",
|
|
10
|
-
"module": "dist/index.mjs",
|
|
11
|
-
"files": [
|
|
12
|
-
"/dist"
|
|
13
|
-
],
|
|
14
|
-
"scripts": {
|
|
15
|
-
"build": "tsup",
|
|
16
|
-
"test": "vitest",
|
|
17
|
-
"typecheck": "vitest --typecheck",
|
|
18
|
-
"format": "prettier . --write",
|
|
19
|
-
"format:check": "prettier . --check",
|
|
20
|
-
"ncu": "ncu --interactive --format group",
|
|
21
|
-
"pub:next": "npm publish --tag next"
|
|
22
|
-
},
|
|
23
|
-
"exports": {
|
|
24
|
-
".": {
|
|
25
|
-
"import": "./dist/index.mjs",
|
|
26
|
-
"module": "./dist/index.mjs",
|
|
27
|
-
"types": "./dist/index.d.mts",
|
|
28
|
-
"default": "./dist/index.mjs"
|
|
29
|
-
},
|
|
30
|
-
"./client": {
|
|
31
|
-
"import": "./dist/client.mjs",
|
|
32
|
-
"module": "./dist/client.mjs",
|
|
33
|
-
"types": "./dist/client.d.mts",
|
|
34
|
-
"default": "./dist/client.mjs"
|
|
35
|
-
}
|
|
36
|
-
},
|
|
37
|
-
"
|
|
38
|
-
"
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
"
|
|
42
|
-
"
|
|
43
|
-
"
|
|
44
|
-
"
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
"
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
"
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
"@testing-library/
|
|
59
|
-
"@
|
|
60
|
-
"@
|
|
61
|
-
"@
|
|
62
|
-
"
|
|
63
|
-
"
|
|
64
|
-
"
|
|
65
|
-
"
|
|
66
|
-
"
|
|
67
|
-
"
|
|
68
|
-
"
|
|
69
|
-
"
|
|
70
|
-
"
|
|
71
|
-
"
|
|
72
|
-
"
|
|
73
|
-
|
|
74
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "react-form-action",
|
|
3
|
+
"version": "2.6.0",
|
|
4
|
+
"description": "State management helpers for the react form actions.",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "https://github.com/MiroslavPetrik/react-form-action.git"
|
|
8
|
+
},
|
|
9
|
+
"main": "dist/index.js",
|
|
10
|
+
"module": "dist/index.mjs",
|
|
11
|
+
"files": [
|
|
12
|
+
"/dist"
|
|
13
|
+
],
|
|
14
|
+
"scripts": {
|
|
15
|
+
"build": "tsup",
|
|
16
|
+
"test": "vitest",
|
|
17
|
+
"typecheck": "vitest --typecheck",
|
|
18
|
+
"format": "prettier . --write",
|
|
19
|
+
"format:check": "prettier . --check",
|
|
20
|
+
"ncu": "ncu --interactive --format group",
|
|
21
|
+
"pub:next": "npm publish --tag next"
|
|
22
|
+
},
|
|
23
|
+
"exports": {
|
|
24
|
+
".": {
|
|
25
|
+
"import": "./dist/index.mjs",
|
|
26
|
+
"module": "./dist/index.mjs",
|
|
27
|
+
"types": "./dist/index.d.mts",
|
|
28
|
+
"default": "./dist/index.mjs"
|
|
29
|
+
},
|
|
30
|
+
"./client": {
|
|
31
|
+
"import": "./dist/client.mjs",
|
|
32
|
+
"module": "./dist/client.mjs",
|
|
33
|
+
"types": "./dist/client.d.mts",
|
|
34
|
+
"default": "./dist/client.mjs"
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
"publishConfig": {
|
|
38
|
+
"access": "public"
|
|
39
|
+
},
|
|
40
|
+
"keywords": [
|
|
41
|
+
"react",
|
|
42
|
+
"form-action",
|
|
43
|
+
"react-form-action",
|
|
44
|
+
"next",
|
|
45
|
+
"next-form-action",
|
|
46
|
+
"nextjs",
|
|
47
|
+
"server-action"
|
|
48
|
+
],
|
|
49
|
+
"author": "Miroslav Petrik",
|
|
50
|
+
"license": "MIT",
|
|
51
|
+
"peerDependencies": {
|
|
52
|
+
"react": ">=19",
|
|
53
|
+
"react-dom": ">=19",
|
|
54
|
+
"zod": "^3.25.0 || ^4.0.0",
|
|
55
|
+
"zod-form-data": "^3.0.0"
|
|
56
|
+
},
|
|
57
|
+
"devDependencies": {
|
|
58
|
+
"@testing-library/dom": "^10.4.1",
|
|
59
|
+
"@testing-library/jest-dom": "^6.9.1",
|
|
60
|
+
"@testing-library/react": "^16.3.0",
|
|
61
|
+
"@testing-library/user-event": "^14.6.1",
|
|
62
|
+
"@types/react": "^19.2.2",
|
|
63
|
+
"@types/react-dom": "^19.2.2",
|
|
64
|
+
"@vitejs/plugin-react": "^5.1.0",
|
|
65
|
+
"happy-dom": "^20.0.8",
|
|
66
|
+
"npm-check-updates": "^19.1.1",
|
|
67
|
+
"prettier": "^3.6.2",
|
|
68
|
+
"react": "^19.2.0",
|
|
69
|
+
"react-dom": "^19.2.0",
|
|
70
|
+
"tsup": "^8.5.0",
|
|
71
|
+
"typescript": "^5.9.3",
|
|
72
|
+
"vitest": "^4.0.4",
|
|
73
|
+
"zod": "^3.25.0",
|
|
74
|
+
"zod-form-data": "^3.0.1"
|
|
75
|
+
}
|
|
76
|
+
}
|