Skip to content

Commit

Permalink
implement field template local error functionality, fix snapshots
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben Loe committed May 1, 2024
1 parent 5ef4b3e commit 11dbe95
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 86 deletions.
21 changes: 21 additions & 0 deletions src/__snapshots__/Storyshots.test.js.snap

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions src/components/form/ConnectedFieldTemplate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,6 @@ function FormikFieldTemplate<Values>({
return annotations;
}, [analysisAnnotations, error, formik, showUntouchedErrors, touched, value]);

console.log({ analysisAnnotations, fieldAnnotations });

return (
<FieldTemplate
value={value}
Expand Down
34 changes: 28 additions & 6 deletions src/components/form/FieldTemplate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import React, { type ReactNode } from "react";
import React, { type ReactNode, useMemo } from "react";
import {
type FormControlProps,
Collapse,
Expand Down Expand Up @@ -69,6 +69,7 @@ export type CustomFieldWidgetProps<
disabled?: boolean;
value: TValue;
onChange: React.ChangeEventHandler<TInputElement>;
setLocalError: (error: string | null) => void;
};
export type CustomFieldWidget<
TValue = string | string[] | number,
Expand All @@ -87,16 +88,36 @@ const FieldTemplate: <As extends React.ElementType, T = Element>(
fitLabelWidth,
widerLabel,
description,
annotations,
annotations = [],
value,
children,
blankValue = "",
as: AsControl,
className,
...restFieldProps
}) => {
const [localError, setLocalError] = React.useState<string | null>(null);
const localErrorAnnotation = useMemo<FieldAnnotation | null>(() => {
if (localError == null) {
return null;
}

return {
message: localError,
type: AnnotationType.Error,
};
}, [localError]);

const fieldAnnotations = useMemo<FieldAnnotation[]>(
() => [
...annotations,
...(localErrorAnnotation ? [localErrorAnnotation] : []),
],
[annotations, localErrorAnnotation],
);

const isInvalid = !isEmpty(
annotations?.filter(
fieldAnnotations?.filter(
(annotation) => annotation.type === AnnotationType.Error,
),
);
Expand Down Expand Up @@ -145,6 +166,7 @@ const FieldTemplate: <As extends React.ElementType, T = Element>(
name={name}
isInvalid={isInvalid}
value={nonUndefinedValue}
setLocalError={setLocalError}
{...restFieldProps}
>
{children}
Expand All @@ -153,12 +175,12 @@ const FieldTemplate: <As extends React.ElementType, T = Element>(

return (
<FormGroup className={cx(styles.formGroup, className)}>
<Collapse in={!isEmpty(annotations)}>
<Collapse in={fieldAnnotations.length > 0}>
<div className="mb-2 w-100">
{isEmpty(annotations) ? (
{isEmpty(fieldAnnotations) ? (
<div className={styles.annotationPlaceholder} />
) : (
annotations?.map(({ message, type, actions }, index) => (
fieldAnnotations?.map(({ message, type, actions }, index) => (
<FieldAnnotationAlert
// eslint-disable-next-line react/no-array-index-key -- Requires a refactor of the `FieldAnnotation` component to require specifying a key
key={index}
Expand Down
Loading

0 comments on commit 11dbe95

Please sign in to comment.