react-form-action 1.2.1 → 1.2.2

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 CHANGED
@@ -237,19 +237,22 @@ export const signUp = authAction
237
237
  export function SignUpForm() {
238
238
  return (
239
239
  <Form action={signUp} initialData="">
240
- {({ error, data, validationError }) => (
241
- <>
242
- {/*
243
- When the "name" prop is ommited, the top-level error will be rendered e.g.:
244
- "Passwords don't match"
245
- */}
246
- <ZodFieldError errors={validationError} />
247
- {/* Access fields by their name: */}
248
- <ZodFieldError errors={validationError} name="password" />
249
- {/* Access nested fields by dot access notation: */}
250
- <ZodFieldError errors={validationError} name="user.email" />
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 extends object> = {
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 ZodFieldErrorProps<Errors extends ZodFormattedError<any>> = Errors extends ZodFormattedError<infer E extends object> ? {
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?: "" | Paths<E>;
35
+ name?: "" | InferZodErrorPaths<Errors>;
35
36
  } & Partial<RenderProp<{
36
37
  errors: readonly string[];
37
- }>> : any;
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 l from"react";import{useActionState as F}from"react";import{useFormStatus as c}from"react-dom";function s({children:t}){return t(c())}function f(t){return{type:"initial",data:t,error:null,validationError:null}}var i={isInitial:!1,isInvalid:!1,isFailure:!1,isSuccess:!1};function T({children:t,action:a,initialData:n,permalink:e,...o}){let[r,m]=F(a,f(n),e),d=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 l.createElement("form",{action:m,...o},l.createElement(s,null,({pending:u})=>t({...d,isPending:u})))}import p from"react";var E=".",S=Object.freeze({_errors:Object.freeze([])});function v({errors:t,name:a="",children:n=({errors:e})=>p.createElement(p.Fragment,null,e[0])}){if(a.length===0)return n({errors:t._errors});let e=a.split(E),o=t;for(;e.length;){let r=e.shift();o=o[r]??S}return n({errors:o._errors})}export{T as Form,s as FormStatus,v as ZodFieldError,f as initial};
1
+ "use client";import p from"react";import{useActionState as E}from"react";import{useFormStatus as c}from"react-dom";function s({children:t}){return t(c())}function F(t){return{type:"initial",data:t,error:null,validationError:null}}var i={isInitial:!1,isInvalid:!1,isFailure:!1,isSuccess:!1};function T({children:t,action:a,initialData:n,permalink:e,...o}){let[r,m]=E(a,F(n),e),d=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 p.createElement("form",{action:m,...o},p.createElement(s,null,({pending:u})=>t({...d,isPending:u})))}import l from"react";var f=".",S=Object.freeze({_errors:Object.freeze([])});function g({errors:t,name:a="",children:n=({errors:e})=>l.createElement(l.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]??S}return n({errors:o._errors})}export{T as Form,s as FormStatus,g as ZodFieldError,F as initial};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-form-action",
3
- "version": "1.2.1",
3
+ "version": "1.2.2",
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.3.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.22.4",
70
+ "zod": "^3.23.8",
70
71
  "zod-form-data": "^2.0.2"
71
72
  }
72
73
  }