react-form-action 1.2.1 → 1.2.3
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 +19 -16
- package/dist/client.d.mts +6 -5
- package/dist/client.mjs +1 -1
- package/package.json +4 -3
package/README.md
CHANGED
|
@@ -50,7 +50,7 @@ const i18nMiddleware = async () => {
|
|
|
50
50
|
const authAction = formAction
|
|
51
51
|
.use(i18nMiddleware)
|
|
52
52
|
.use(async ({ ctx: { t } }) =>
|
|
53
|
-
console.log("🎉 context enhanced by previous middlewares 🎉", t)
|
|
53
|
+
console.log("🎉 context enhanced by previous middlewares 🎉", t),
|
|
54
54
|
)
|
|
55
55
|
.error(({ error }) => {
|
|
56
56
|
if (error instanceof DbError) {
|
|
@@ -85,7 +85,7 @@ export const signUp = authAction
|
|
|
85
85
|
.refine((data) => data.password === data.confirm, {
|
|
86
86
|
message: "Passwords don't match",
|
|
87
87
|
path: ["confirm"],
|
|
88
|
-
})
|
|
88
|
+
}),
|
|
89
89
|
) // if using refinement, only one input call is permited, as schema with ZodEffects is not extendable.
|
|
90
90
|
.run(async ({ ctx: { t }, input: { email, password } }) => {
|
|
91
91
|
// 🎉 passwords match!
|
|
@@ -161,7 +161,7 @@ export const updateUser = createFormAction<
|
|
|
161
161
|
|
|
162
162
|
return failure({ message: "Failed to update user." });
|
|
163
163
|
}
|
|
164
|
-
}
|
|
164
|
+
},
|
|
165
165
|
);
|
|
166
166
|
```
|
|
167
167
|
|
|
@@ -237,19 +237,22 @@ export const signUp = authAction
|
|
|
237
237
|
export function SignUpForm() {
|
|
238
238
|
return (
|
|
239
239
|
<Form action={signUp} initialData="">
|
|
240
|
-
{({
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
240
|
+
{({ isInvalid, validationError }) =>
|
|
241
|
+
{/* Render ZodFieldError behind the isInvalid flag to narrow type (omits the possibility of null) */}
|
|
242
|
+
isInvalid && (
|
|
243
|
+
<>
|
|
244
|
+
{/*
|
|
245
|
+
When the "name" prop is ommited, the top-level error will be rendered e.g.:
|
|
246
|
+
"Passwords don't match"
|
|
247
|
+
*/}
|
|
248
|
+
<ZodFieldError errors={validationError} />
|
|
249
|
+
{/* Access fields by their name: */}
|
|
250
|
+
<ZodFieldError errors={validationError} name="password" />
|
|
251
|
+
{/* Access nested fields by dot access notation: */}
|
|
252
|
+
<ZodFieldError errors={validationError} name="user.email" />
|
|
253
|
+
</>
|
|
254
|
+
)
|
|
255
|
+
}
|
|
253
256
|
</Form>
|
|
254
257
|
);
|
|
255
258
|
}
|
package/dist/client.d.mts
CHANGED
|
@@ -26,15 +26,16 @@ declare function Form<Data, Error, ValidationError>({ children, action, initialD
|
|
|
26
26
|
declare function FormStatus({ children, }: RenderProp<ReturnType<typeof useFormStatus>>): React.ReactNode;
|
|
27
27
|
|
|
28
28
|
declare const SEPARATOR: ".";
|
|
29
|
-
type Paths<Err
|
|
29
|
+
type Paths<Err> = {
|
|
30
30
|
[k in keyof Err]: k extends string ? Err[k] extends object ? `${k}${typeof SEPARATOR}${Paths<Err[k]>}` : k : never;
|
|
31
31
|
}[keyof Err];
|
|
32
|
-
type
|
|
32
|
+
type InferZodErrorPaths<Errors> = Errors extends ZodFormattedError<infer E> ? Paths<E> : never;
|
|
33
|
+
type ZodFieldErrorProps<Errors extends ZodFormattedError<any>> = {
|
|
33
34
|
errors: Errors;
|
|
34
|
-
name?: "" |
|
|
35
|
+
name?: "" | InferZodErrorPaths<Errors>;
|
|
35
36
|
} & Partial<RenderProp<{
|
|
36
37
|
errors: readonly string[];
|
|
37
|
-
}
|
|
38
|
+
}>>;
|
|
38
39
|
declare function ZodFieldError<Errors extends ZodFormattedError<any>>({ errors, name, children, }: ZodFieldErrorProps<Errors>): React__default.ReactNode;
|
|
39
40
|
|
|
40
|
-
export { Form, FormProps, FormStateProps, FormStatus, ZodFieldError, ZodFieldErrorProps, initial };
|
|
41
|
+
export { Form, FormProps, FormStateProps, FormStatus, InferZodErrorPaths, Paths, ZodFieldError, ZodFieldErrorProps, initial };
|
package/dist/client.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use client";import
|
|
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};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-form-action",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.3",
|
|
4
4
|
"description": "State management helpers for the react form actions.",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -54,19 +54,20 @@
|
|
|
54
54
|
"@testing-library/dom": "^10.4.0",
|
|
55
55
|
"@testing-library/jest-dom": "^6.6.3",
|
|
56
56
|
"@testing-library/react": "^16.1.0",
|
|
57
|
+
"@testing-library/user-event": "^14.5.2",
|
|
57
58
|
"@types/react": "^19.0.0",
|
|
58
59
|
"@types/react-dom": "^19.0.1",
|
|
59
60
|
"@vitejs/plugin-react": "^4.3.4",
|
|
60
61
|
"happy-dom": "^15.11.7",
|
|
61
62
|
"npm-check-updates": "^17.1.11",
|
|
62
|
-
"prettier": "^3.
|
|
63
|
+
"prettier": "^3.4.2",
|
|
63
64
|
"react": "^19.0.0",
|
|
64
65
|
"react-dom": "^19.0.0",
|
|
65
66
|
"react-render-prop-type": "^0.1.0",
|
|
66
67
|
"tsup": "^7.2.0",
|
|
67
68
|
"typescript": "^5.2.2",
|
|
68
69
|
"vitest": "^1.3.1",
|
|
69
|
-
"zod": "^3.
|
|
70
|
+
"zod": "^3.23.8",
|
|
70
71
|
"zod-form-data": "^2.0.2"
|
|
71
72
|
}
|
|
72
73
|
}
|