Skip to content

Commit 59835b7

Browse files
committed
Allow error handlers to return a React element as the form error
1 parent 1bad246 commit 59835b7

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

src/api/errorHandling/form-error-handling.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { isNotFalsy, mapValues } from '@seedcompany/common';
22
import { FORM_ERROR, FormApi, setIn, SubmissionErrors } from 'final-form';
3+
import { isValidElement, ReactElement } from 'react';
34
import { Promisable } from 'type-fest';
45
import { ErrorMap, getErrorInfo, ValidationError } from './error.types';
56

@@ -53,7 +54,11 @@ export type ErrorHandler<E> =
5354
) => Promisable<ErrorHandlerResult>);
5455

5556
type NextHandler<E> = (e: E) => Promisable<ErrorHandlerResult>;
56-
export type ErrorHandlerResult = string | SubmissionErrors | undefined;
57+
export type ErrorHandlerResult =
58+
| string
59+
| ReactElement
60+
| SubmissionErrors
61+
| undefined;
5762

5863
interface HandlerUtils {
5964
/** Does the form have this field registered? */
@@ -140,5 +145,7 @@ const resolveHandler =
140145
typeof handler === 'function'
141146
? await handler(error, next, utils)
142147
: handler;
143-
return typeof result === 'string' ? { [FORM_ERROR]: result } : result;
148+
return typeof result === 'string' || isValidElement(result)
149+
? { [FORM_ERROR]: result }
150+
: result;
144151
};

0 commit comments

Comments
 (0)