react-form-action 1.4.1 → 1.5.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/dist/client.d.mts +25 -21
- package/dist/client.mjs +1 -1
- package/package.json +1 -1
package/dist/client.d.mts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import * as React from 'react';
|
|
2
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
|
+
import { ZodFormattedError } from 'zod';
|
|
4
5
|
import { RenderProp } from 'react-render-prop-type';
|
|
5
6
|
import { useFormStatus } from 'react-dom';
|
|
6
|
-
import { ZodFormattedError } from 'zod';
|
|
7
7
|
|
|
8
8
|
type ActionProps<Data, Error, ValidationError> = PropsWithChildren<{
|
|
9
9
|
action: FormAction<Data, Error, ValidationError>;
|
|
@@ -23,10 +23,14 @@ type ActionContextState<T extends ActionState<unknown, unknown, unknown>> = T &
|
|
|
23
23
|
action: (payload: FormData) => void;
|
|
24
24
|
isPending: boolean;
|
|
25
25
|
};
|
|
26
|
+
/**
|
|
27
|
+
* NOTE: ActionContextState<ActionState<...>> would not allow discriminate the union inside of the ActionState.
|
|
28
|
+
*/
|
|
29
|
+
type SpreadActionContext<Data = unknown, Error = unknown, ValidationError = unknown> = ActionContextState<InitialState<Data>> | ActionContextState<InvalidState<ValidationError>> | ActionContextState<FailureState<Error>> | ActionContextState<SuccessState<Data>>;
|
|
26
30
|
/**
|
|
27
31
|
* A hook to consume the form action state from the context.
|
|
28
32
|
*/
|
|
29
|
-
declare
|
|
33
|
+
declare function useActionContext<Data, Error, ValidationError>(): SpreadActionContext<Data, Error, ValidationError>;
|
|
30
34
|
declare function Action<Data, Error, ValidationError>({ children, action: formAction, initialData, permalink, }: ActionProps<Data, Error, ValidationError>): React__default.JSX.Element;
|
|
31
35
|
|
|
32
36
|
type ActionFormProps = Omit<FormHTMLAttributes<HTMLFormElement>, "action"> & {
|
|
@@ -40,13 +44,26 @@ type ActionFormProps = Omit<FormHTMLAttributes<HTMLFormElement>, "action"> & {
|
|
|
40
44
|
};
|
|
41
45
|
declare function ActionForm({ autoReset, ...props }: ActionFormProps): React__default.JSX.Element;
|
|
42
46
|
|
|
47
|
+
declare const SEPARATOR: ".";
|
|
48
|
+
type Paths<Err> = {
|
|
49
|
+
[k in keyof Err]: k extends string ? Err[k] extends object ? `${k}${typeof SEPARATOR}${Paths<Err[k]>}` : k : never;
|
|
50
|
+
}[keyof Err];
|
|
51
|
+
type InferZodErrorPaths<Errors> = Errors extends ZodFormattedError<infer E> ? Paths<E> : never;
|
|
52
|
+
type ZodFieldErrorProps<Errors extends ZodFormattedError<any>> = {
|
|
53
|
+
errors: Errors;
|
|
54
|
+
name?: "" | InferZodErrorPaths<Errors>;
|
|
55
|
+
} & Partial<RenderProp<{
|
|
56
|
+
errors: readonly string[];
|
|
57
|
+
}>>;
|
|
58
|
+
declare function ZodFieldError<Errors extends ZodFormattedError<any>>({ errors, name, children, }: ZodFieldErrorProps<Errors>): React__default.ReactNode;
|
|
59
|
+
|
|
43
60
|
/**
|
|
44
|
-
* Creates a
|
|
61
|
+
* Creates a typed components for actions created with the formAction builder.
|
|
45
62
|
*/
|
|
46
|
-
declare function
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
}>) => React__default.JSX.Element;
|
|
63
|
+
declare function createComponents<Data, Error, ValidationError extends ZodFormattedError<any>>(action: FormAction<Data, Error, ValidationError>): {
|
|
64
|
+
FieldError: ({ name, }: React__default.PropsWithChildren<{
|
|
65
|
+
name?: "" | InferZodErrorPaths<ValidationError> | undefined;
|
|
66
|
+
}>) => false | React__default.JSX.Element;
|
|
50
67
|
};
|
|
51
68
|
|
|
52
69
|
type FormStateProps<Data, Error, ValidationError, Payload> = {
|
|
@@ -91,17 +108,4 @@ declare function FormStatus({ children, }: RenderProp<ReturnType<typeof useFormS
|
|
|
91
108
|
*/
|
|
92
109
|
declare function Pending({ children }: PropsWithChildren): React.ReactNode;
|
|
93
110
|
|
|
94
|
-
|
|
95
|
-
type Paths<Err> = {
|
|
96
|
-
[k in keyof Err]: k extends string ? Err[k] extends object ? `${k}${typeof SEPARATOR}${Paths<Err[k]>}` : k : never;
|
|
97
|
-
}[keyof Err];
|
|
98
|
-
type InferZodErrorPaths<Errors> = Errors extends ZodFormattedError<infer E> ? Paths<E> : never;
|
|
99
|
-
type ZodFieldErrorProps<Errors extends ZodFormattedError<any>> = {
|
|
100
|
-
errors: Errors;
|
|
101
|
-
name?: "" | InferZodErrorPaths<Errors>;
|
|
102
|
-
} & Partial<RenderProp<{
|
|
103
|
-
errors: readonly string[];
|
|
104
|
-
}>>;
|
|
105
|
-
declare function ZodFieldError<Errors extends ZodFormattedError<any>>({ errors, name, children, }: ZodFieldErrorProps<Errors>): React__default.ReactNode;
|
|
106
|
-
|
|
107
|
-
export { Action, ActionContextState, ActionForm, ActionFormProps, ActionProps, Form, FormContext, FormMetaState, FormProps, FormStateProps, FormStatus, InferZodErrorPaths, Paths, Pending, ZodFieldError, ZodFieldErrorProps, createForm, initial, useActionContext, useFormContext };
|
|
111
|
+
export { Action, ActionContextState, ActionForm, ActionFormProps, ActionProps, Form, FormContext, FormMetaState, FormProps, FormStateProps, FormStatus, InferZodErrorPaths, Paths, Pending, ZodFieldError, ZodFieldErrorProps, createComponents, initial, useActionContext, useFormContext };
|
package/dist/client.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use client";import
|
|
1
|
+
"use client";import y from"react";import{createContext as A,use as P}from"react";import{useActionState as D}from"react";function k(t){return{type:"initial",data:t,error:null,validationError:null}}var u={isInitial:!1,isInvalid:!1,isFailure:!1,isSuccess:!1},d=A(null);function s(){let t=P(d);if(!t)throw new Error("ActionContext must be initialized before use. Is your useActionContext hook wrapped with an <Action> Component?");return t}function j({children:t,action:o,initialData:n,permalink:a}){let[r,i,e]=D(o,k(n),a),l=r.type==="initial"?{...r,...u,action:i,isPending:e,isInitial:!0}:r.type==="invalid"?{...r,...u,action:i,isPending:e,isInvalid:!0}:r.type==="failure"?{...r,...u,action:i,isPending:e,isFailure:!0}:{...r,...u,action:i,isPending:e,isSuccess:!0};return y.createElement(d,{value:l},t)}import T,{startTransition as w}from"react";function B({autoReset:t=!1,...o}){let{action:n}=s();return T.createElement("form",{...t?{action:n}:{onSubmit:r=>{r.preventDefault();let i=r.currentTarget;w(()=>{n(new FormData(i))})}},...o})}import v from"react";import f from"react";var C=".",h=Object.freeze({_errors:Object.freeze([])});function E({errors:t,name:o="",children:n=({errors:a})=>f.createElement(f.Fragment,null,a[0])}){if(o.length===0)return n({errors:t._errors});let a=o.split(C),r=t;for(;a.length;){let i=a.shift();r=r[i]??h}return n({errors:r._errors})}function Y(t){function o({name:n}){let{isInvalid:a,validationError:r}=s();return a&&v.createElement(E,{errors:r,name:n})}return{FieldError:o}}import I,{useTransition as g}from"react";import{useActionState as b}from"react";function V(t){return{type:"initial",data:t,error:null,validationError:null}}var m={isInitial:!1,isInvalid:!1,isFailure:!1,isSuccess:!1};function et({children:t,action:o,initialData:n,permalink:a,autoReset:r=!1,...i}){let[e,l,c]=b(o,V(n),a),[,S]=g(),F=e.type==="initial"?{...e,...m,isPending:c,isInitial:!0}:e.type==="invalid"?{...e,...m,isPending:c,isInvalid:!0}:e.type==="failure"?{...e,...m,isPending:c,isFailure:!0}:{...e,...m,isPending:c,isSuccess:!0};return I.createElement("form",{...r?{action:l}:{onSubmit:p=>{p.preventDefault();let x=p.currentTarget;S(()=>{l(new FormData(x))})}},...i},t(F))}import{createContext as M,use as Z}from"react";var R=M(null),it=()=>{let t=Z(R);if(!t)throw new Error("Form Context must be initialized before use.");return t};import{useFormStatus as H}from"react-dom";function ct({children:t}){return t(H())}function pt({children:t}){let{isPending:o}=s();return o&&t}export{j as Action,B as ActionForm,et as Form,R as FormContext,ct as FormStatus,pt as Pending,E as ZodFieldError,Y as createComponents,V as initial,s as useActionContext,it as useFormContext};
|