@cedarjs/forms 7.0.0-canary.3198 → 7.0.0-canary.3200

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.
@@ -1,9 +1,35 @@
1
1
  import React from 'react';
2
+ export interface FieldErrorRenderProps {
3
+ /**
4
+ * The error message. When the failed validation rule has no message of its
5
+ * own this is the default message for the error's `type`.
6
+ */
7
+ message: string;
8
+ /**
9
+ * The message of every failed validation rule, keyed by rule. Only set when
10
+ * the form collects all errors: `<Form config={{ criteriaMode: 'all' }}>`.
11
+ */
12
+ messages?: Record<string, string | string[]>;
13
+ /**
14
+ * What caused the error: the name of the validation rule (`required`,
15
+ * `minLength`, ...), the key of the failing function in a `validate`
16
+ * object, `server` for errors from the API side, or whatever `type`
17
+ * `setError` was called with.
18
+ */
19
+ type?: string | number;
20
+ }
2
21
  export interface FieldErrorProps extends React.ComponentPropsWithoutRef<'span'> {
3
22
  /**
4
23
  * The name of the field the `<FieldError>`'s associated with.
24
+ * Use `root.<key>` for a form-level error set with
25
+ * `setError('root.<key>', ...)`.
5
26
  */
6
27
  name: string;
28
+ /**
29
+ * Render something other than a `<span>` with the message in it. Only
30
+ * called when there's an error.
31
+ */
32
+ render?: (props: FieldErrorRenderProps) => React.ReactNode;
7
33
  }
8
34
  /**
9
35
  * Renders a `<span>` with an error message if there's a validation error on the corresponding field.
@@ -25,6 +51,22 @@ export interface FieldErrorProps extends React.ComponentPropsWithoutRef<'span'>
25
51
  * <FieldError name="name" className="error" />
26
52
  * ```
27
53
  *
54
+ * @example Rendering every failed rule's message with `render`
55
+ *
56
+ * ```jsx
57
+ * <Form config={{ criteriaMode: 'all' }}>
58
+ * <FieldError
59
+ * name="password"
60
+ * render={({ messages }) => (
61
+ * <ul>
62
+ * {Object.entries(messages).map(([type, message]) => (
63
+ * <li key={type}>{message}</li>
64
+ * ))}
65
+ * </ul>
66
+ * )}
67
+ * />
68
+ * ```
69
+ *
28
70
  * @see {@link https://cedarjs.com/docs/tutorial/chapter3/forms#fielderror}
29
71
  *
30
72
  * @privateRemarks
@@ -39,5 +81,5 @@ export interface FieldErrorProps extends React.ComponentPropsWithoutRef<'span'>
39
81
  *
40
82
  * @see {@link https://react-hook-form.com/get-started#Handleerrors}
41
83
  */
42
- export declare const FieldError: ({ name, ...rest }: FieldErrorProps) => React.JSX.Element | null;
84
+ export declare const FieldError: ({ name, render, ...rest }: FieldErrorProps) => string | number | bigint | boolean | Iterable<React.ReactNode> | Promise<string | number | bigint | boolean | React.ReactPortal | React.ReactElement<unknown, string | React.JSXElementConstructor<any>> | Iterable<React.ReactNode> | null | undefined> | React.JSX.Element | null | undefined;
43
85
  //# sourceMappingURL=FieldError.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"FieldError.d.ts","sourceRoot":"","sources":["../src/FieldError.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAA;AAIzB,MAAM,WAAW,eAAgB,SAAQ,KAAK,CAAC,wBAAwB,CAAC,MAAM,CAAC;IAC7E;;OAEG;IACH,IAAI,EAAE,MAAM,CAAA;CACb;AAYD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AACH,eAAO,MAAM,UAAU,GAAI,mBAAmB,eAAe,6BAe5D,CAAA"}
1
+ {"version":3,"file":"FieldError.d.ts","sourceRoot":"","sources":["../src/FieldError.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAA;AAQzB,MAAM,WAAW,qBAAqB;IACpC;;;OAGG;IACH,OAAO,EAAE,MAAM,CAAA;IACf;;;OAGG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC,CAAA;IAC5C;;;;;OAKG;IACH,IAAI,CAAC,EAAE,MAAM,GAAG,MAAM,CAAA;CACvB;AAED,MAAM,WAAW,eAAgB,SAAQ,KAAK,CAAC,wBAAwB,CAAC,MAAM,CAAC;IAC7E;;;;OAIG;IACH,IAAI,EAAE,MAAM,CAAA;IACZ;;;OAGG;IACH,MAAM,CAAC,EAAE,CAAC,KAAK,EAAE,qBAAqB,KAAK,KAAK,CAAC,SAAS,CAAA;CAC3D;AAYD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiDG;AACH,eAAO,MAAM,UAAU,GAAI,2BAA2B,eAAe,oSAsDpE,CAAA"}
@@ -9,13 +9,41 @@ const DEFAULT_MESSAGES = {
9
9
  max: "is too high",
10
10
  validate: "is not valid"
11
11
  };
12
- const FieldError = ({ name, ...rest }) => {
12
+ const FieldError = ({ name, render, ...rest }) => {
13
13
  const {
14
14
  formState: { errors }
15
15
  } = useFormContext();
16
16
  const validationError = get(errors, name);
17
- const errorMessage = validationError && (validationError.message || `${name} ${DEFAULT_MESSAGES[validationError.type]}`);
18
- return validationError ? /* @__PURE__ */ React.createElement("span", { ...rest }, errorMessage) : null;
17
+ if (!validationError) {
18
+ return null;
19
+ }
20
+ const isRootError = name === "root" || name.startsWith("root.");
21
+ const defaultMessages = DEFAULT_MESSAGES;
22
+ const getDefaultMessage = (type) => {
23
+ if (isRootError) {
24
+ return "";
25
+ }
26
+ return `${name} ${defaultMessages[String(type)] ?? DEFAULT_MESSAGES.validate}`;
27
+ };
28
+ const message = validationError.message || getDefaultMessage(validationError.type);
29
+ if (render) {
30
+ const toMessage = (type, result) => {
31
+ if (Array.isArray(result) || typeof result === "string" && result) {
32
+ return result;
33
+ }
34
+ return getDefaultMessage(type);
35
+ };
36
+ const messages = validationError.types && Object.fromEntries(
37
+ Object.entries(validationError.types).map(
38
+ ([type, result]) => [
39
+ type,
40
+ toMessage(type, result)
41
+ ]
42
+ )
43
+ );
44
+ return render({ message, messages, type: validationError.type });
45
+ }
46
+ return message ? /* @__PURE__ */ React.createElement("span", { ...rest }, message) : null;
19
47
  };
20
48
  export {
21
49
  FieldError
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cedarjs/forms",
3
- "version": "7.0.0-canary.3198",
3
+ "version": "7.0.0-canary.3200",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/cedarjs/cedar.git",
@@ -36,7 +36,7 @@
36
36
  "react-hook-form": "7.88.0"
37
37
  },
38
38
  "devDependencies": {
39
- "@cedarjs/framework-tools": "7.0.0-canary.3198",
39
+ "@cedarjs/framework-tools": "7.0.0-canary.3200",
40
40
  "@testing-library/dom": "10.4.1",
41
41
  "@testing-library/jest-dom": "6.9.1",
42
42
  "@testing-library/react": "16.3.3",