react-form-action 1.2.0 → 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
@@ -230,26 +230,29 @@ export const signUp = authAction
230
230
  message: "Passwords don't match",
231
231
  })
232
232
  )
233
- .run(async ({ ctx: { t }, input: { email, password } }) => {
233
+ .run(async ({ ctx, input }) => {
234
234
  // implementation
235
235
  });
236
236
 
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
@@ -3,6 +3,7 @@ import React__default, { FormHTMLAttributes } from 'react';
3
3
  import { RenderProp } from 'react-render-prop-type';
4
4
  import { F as FormState, b as InitialState, I as InvalidState, a as FailureState, S as SuccessState } from './createFormAction-3a51310f.js';
5
5
  import { useFormStatus } from 'react-dom';
6
+ import { ZodFormattedError } from 'zod';
6
7
 
7
8
  type FormStateProps<Data, Error, ValidationError, Payload> = {
8
9
  action: (state: FormState<Data, Error, ValidationError>, payload: Payload) => Promise<FormState<Data, Error, ValidationError>>;
@@ -24,4 +25,17 @@ declare function Form<Data, Error, ValidationError>({ children, action, initialD
24
25
 
25
26
  declare function FormStatus({ children, }: RenderProp<ReturnType<typeof useFormStatus>>): React.ReactNode;
26
27
 
27
- export { Form, FormProps, FormStateProps, FormStatus, initial };
28
+ declare const SEPARATOR: ".";
29
+ type Paths<Err> = {
30
+ [k in keyof Err]: k extends string ? Err[k] extends object ? `${k}${typeof SEPARATOR}${Paths<Err[k]>}` : k : never;
31
+ }[keyof Err];
32
+ type InferZodErrorPaths<Errors> = Errors extends ZodFormattedError<infer E> ? Paths<E> : never;
33
+ type ZodFieldErrorProps<Errors extends ZodFormattedError<any>> = {
34
+ errors: Errors;
35
+ name?: "" | InferZodErrorPaths<Errors>;
36
+ } & Partial<RenderProp<{
37
+ errors: readonly string[];
38
+ }>>;
39
+ declare function ZodFieldError<Errors extends ZodFormattedError<any>>({ errors, name, children, }: ZodFieldErrorProps<Errors>): React__default.ReactNode;
40
+
41
+ export { Form, FormProps, FormStateProps, FormStatus, InferZodErrorPaths, Paths, ZodFieldError, ZodFieldErrorProps, initial };
package/dist/client.mjs CHANGED
@@ -1 +1 @@
1
- "use client";import o from"react";import{useActionState as F}from"react";import{useFormStatus as S}from"react-dom";function e({children:r}){return r(S())}function c(r){return{type:"initial",data:r,error:null,validationError:null}}var a={isInitial:!1,isInvalid:!1,isFailure:!1,isSuccess:!1};function x({children:r,action:i,initialData:n,permalink:s,...l}){let[t,m]=F(i,c(n),s),u=t.type==="initial"?{...t,...a,isInitial:!0}:t.type==="invalid"?{...t,...a,isInvalid:!0}:t.type==="failure"?{...t,...a,isFailure:!0}:{...t,...a,isSuccess:!0};return o.createElement("form",{action:m,...l},o.createElement(e,null,({pending:p})=>r({...u,isPending:p})))}export{x as Form,e as FormStatus,c 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.0",
3
+ "version": "1.2.2",
4
4
  "description": "State management helpers for the react form actions.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -54,20 +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
- "next": "^14.0.2",
62
62
  "npm-check-updates": "^17.1.11",
63
- "prettier": "^3.3.3",
63
+ "prettier": "^3.4.2",
64
64
  "react": "^19.0.0",
65
65
  "react-dom": "^19.0.0",
66
66
  "react-render-prop-type": "^0.1.0",
67
67
  "tsup": "^7.2.0",
68
68
  "typescript": "^5.2.2",
69
69
  "vitest": "^1.3.1",
70
- "zod": "^3.22.4",
70
+ "zod": "^3.23.8",
71
71
  "zod-form-data": "^2.0.2"
72
72
  }
73
73
  }