react-form-action 2.17.1 → 2.19.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 +34 -0
- package/dist/client.d.mts +26 -2
- package/dist/client.mjs +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -451,6 +451,40 @@ export function SignUpForm() {
|
|
|
451
451
|
}
|
|
452
452
|
```
|
|
453
453
|
|
|
454
|
+
#### `<Value>`
|
|
455
|
+
|
|
456
|
+
Renders a hidden `<input>` with a fixed value, so a form can submit a value without a visible field. Use it to send the same form with different values, e.g. one submit button per decision.
|
|
457
|
+
|
|
458
|
+
Props:
|
|
459
|
+
- `name` - the form field name. Like `<FieldError>`, it is typed to the paths of the action's input schema.
|
|
460
|
+
- `value` - the string value submitted with the form.
|
|
461
|
+
- Any other `<input>` attributes are passed through to the hidden input.
|
|
462
|
+
|
|
463
|
+
```tsx
|
|
464
|
+
import { Action, Form, createComponents } from "react-form-action/client";
|
|
465
|
+
|
|
466
|
+
import { decisionAction } from "./actions";
|
|
467
|
+
|
|
468
|
+
// 🌟 The name prop is bound to the decisionAction input schema
|
|
469
|
+
const { Value } = createComponents(decisionAction);
|
|
470
|
+
|
|
471
|
+
export function DecisionForm() {
|
|
472
|
+
return (
|
|
473
|
+
<Action action={decisionAction}>
|
|
474
|
+
<Form>
|
|
475
|
+
{/* 👉 Submits decision=accept when the button is clicked */}
|
|
476
|
+
<Value name="decision" value="accept" />
|
|
477
|
+
<button type="submit">Accept</button>
|
|
478
|
+
</Form>
|
|
479
|
+
<Form>
|
|
480
|
+
<Value name="decision" value="deny" />
|
|
481
|
+
<button type="submit">Deny</button>
|
|
482
|
+
</Form>
|
|
483
|
+
</Action>
|
|
484
|
+
);
|
|
485
|
+
}
|
|
486
|
+
```
|
|
487
|
+
|
|
454
488
|
#### `<Success>`
|
|
455
489
|
|
|
456
490
|
#### When children are JSX
|
package/dist/client.d.mts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { a as FormAction, c as SuccessState, i as FailureState, o as InitialState, r as ActionState, s as InvalidState, t as Flatten } from "./formAction-0eiUHrXx.mjs";
|
|
2
|
-
import React$1, { JSX, PropsWithChildren } from "react";
|
|
2
|
+
import React$1, { JSX, PropsWithChildren, ReactNode } from "react";
|
|
3
|
+
import { FormStatus } from "react-dom";
|
|
3
4
|
import { $ZodErrorTree } from "zod/v4/core";
|
|
4
5
|
//#region src/Action.d.ts
|
|
5
6
|
type ActionProps<Data, Error, ValidationError, Arguments extends unknown[] = []> = PropsWithChildren<{
|
|
@@ -106,6 +107,10 @@ declare function createComponents<Data, Error, ValidationError extends $ZodError
|
|
|
106
107
|
validationError: ValidationError;
|
|
107
108
|
}) => React$1.ReactNode;
|
|
108
109
|
}) => React$1.ReactNode;
|
|
110
|
+
Value: <Name extends "" | InferZodErrorPaths<ValidationError>>({ name, value, ...props }: {
|
|
111
|
+
name: Name;
|
|
112
|
+
value: string;
|
|
113
|
+
} & React$1.InputHTMLAttributes<HTMLInputElement>) => React$1.JSX.Element;
|
|
109
114
|
};
|
|
110
115
|
//#endregion
|
|
111
116
|
//#region src/Form.d.ts
|
|
@@ -130,6 +135,25 @@ declare function Pending({ children }: PropsWithChildren | {
|
|
|
130
135
|
}) => React.ReactNode;
|
|
131
136
|
}): import("react").ReactNode;
|
|
132
137
|
//#endregion
|
|
138
|
+
//#region src/Submit.d.ts
|
|
139
|
+
type SubmitChildrenProps = FormStatus & {
|
|
140
|
+
/**
|
|
141
|
+
* Whether the **action** is pending. It will disable the submit button when true.
|
|
142
|
+
*/
|
|
143
|
+
disabled: boolean;
|
|
144
|
+
};
|
|
145
|
+
type SubmitProps = {
|
|
146
|
+
/**
|
|
147
|
+
* A render prop to render the submit button.
|
|
148
|
+
*/
|
|
149
|
+
children: (props: SubmitChildrenProps) => ReactNode;
|
|
150
|
+
};
|
|
151
|
+
/**
|
|
152
|
+
* A submit status component that tracks both the action & form pending states.
|
|
153
|
+
* NOTE: must be rendered within a <form>.
|
|
154
|
+
*/
|
|
155
|
+
declare function Submit({ children }: SubmitProps): ReactNode;
|
|
156
|
+
//#endregion
|
|
133
157
|
//#region src/useResetAction.d.ts
|
|
134
158
|
/**
|
|
135
159
|
* Hook to automatically reset the action when the component unmounts, unless it is in the initial state.
|
|
@@ -140,4 +164,4 @@ declare function useResetAction(): void;
|
|
|
140
164
|
*/
|
|
141
165
|
declare function ResetAction(): null;
|
|
142
166
|
//#endregion
|
|
143
|
-
export { Action, ActionContextState, ActionProps, Form, FormProps, InferZodErrorPaths, Paths, Pending, ResetAction, SpreadActionContext, ZodFieldError, ZodFieldErrorChildrenProps, ZodFieldErrorProps, createComponents, noError, useActionContext, useResetAction };
|
|
167
|
+
export { Action, ActionContextState, ActionProps, Form, FormProps, InferZodErrorPaths, Paths, Pending, ResetAction, SpreadActionContext, Submit, 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,useEffect as s}from"react";import{Fragment as c,jsx as l,jsxs as u}from"react/jsx-runtime";function
|
|
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,jsxs as u}from"react/jsx-runtime";import{useFormStatus as d}from"react-dom";function f(){let{reset:e,isInitial:t}=g();s(()=>()=>{t||e()},[t,e])}function p(){return f(),null}const m={isInitial:!1,isInvalid:!1,isFailure:!1,isSuccess:!1},h=n(null);function g(e){let t=i(h);if(!t)throw Error(`ActionContext must be initialized before use. Is your useActionContext hook wrapped with an <Action> Component?`);return t}function _({children:n,action:i,initialData:s,args:c,permalink:d,validate:f,autoReset:g=!0}){let _=c?i.bind(null,...c):i,[v,y,b]=a(async(n,r)=>{if(r===null)return e(s);if(f){let e=await t(({invalid:e,success:t})=>async(n,r)=>{let i=f(r);return i?e(i):t(!0)})(n,r);if(e.type===`invalid`)return e}return _(n,r)},e(s),d),x={action:y,reset:o(()=>{r(()=>{y(null)})},[y]),isPending:b},S=v.type===`initial`?{...x,...v,...m,isInitial:!0}:v.type===`invalid`?{...x,...v,...m,isInvalid:!0}:v.type===`failure`?{...x,...v,...m,isFailure:!0}:{...x,...v,...m,isSuccess:!0};return u(h,{value:S,children:[g&&l(p,{}),n]})}const v=Object.freeze({errors:Object.freeze([])});function y({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=v;break}i=i.properties[e]??v}return n({name:t,error:i.errors[0],errors:i.errors})}function b(e,t={}){function n({name:e,value:t,...n}){return l(`input`,{type:`hidden`,name:e,defaultValue:t,...n})}function r({name:n,children:r}){let i=g(e),{isInvalid:a,validationError:o}=i,s=r??(({error:e})=>a&&l(c,{children:e}));return l(y,{errors:o??v,name:n,children:e=>{let n=Object.fromEntries(Object.entries(t.fieldProps??{}).map(([t,n])=>[t,n(i,e)]));return s({...e,...n})}})}function i({children:t}){let{isFailure:n,error:r}=g(e);return n?typeof t==`function`?t({isFailure:n,error:r}):t:null}function a({children:t}){let{isSuccess:n,data:r}=g(e);return typeof t==`function`?t({isSuccess:n,data:r}):n&&t}function o({children:t}){let{isInvalid:n,validationError:r}=g(e);return typeof t==`function`?t({isInvalid:n,validationError:r}):n&&t}return{FieldError:r,Failure:i,Success:a,Invalid:o,Value:n}}function x({autoReset:e=!1,...t}){let{action:n}=g();return l(`form`,{...e?{action:n}:{onSubmit:e=>{e.preventDefault();let t=e.currentTarget;r(()=>{n(new FormData(t))})}},...t})}function S({children:e}){let{isPending:t}=g();return typeof e==`function`?e({isPending:t}):t&&e}function C({children:e}){let{isPending:t}=g();return e({...d(),disabled:t})}export{_ as Action,x as Form,S as Pending,p as ResetAction,C as Submit,y as ZodFieldError,b as createComponents,v as noError,g as useActionContext,f as useResetAction};
|