Skip to content

Commit

Permalink
feat(formfield): support errorMessage array
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinWijnant committed Dec 13, 2020
1 parent c9db016 commit 0fd2f00
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/components/FormField/FormField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import styles from './FormField.module.css';
type Props = {
label?: string;
hint?: ReactNode;
errorMessage?: string;
errorMessage?: string | (string | undefined)[];
className?: string;
children: ReactNode;
};
Expand All @@ -16,11 +16,23 @@ const FormField: React.FC<Props> = ({ label, className, children, hint, errorMes
const mergedClassNames = classNames(cssReset.ventura, className, {
[styles.isSpaced]: Boolean(label),
});

let errorMessages: string[] = [];
if (Array.isArray(errorMessage)) {
errorMessages = errorMessage.filter(Boolean) as string[];
} else if (errorMessage) {
errorMessages = [errorMessage];
}

return (
<div className={mergedClassNames}>
{Boolean(label) && <span className={styles.label}>{label}</span>}
{children}
{Boolean(errorMessage) && <span className={styles.errorMessage}>{errorMessage}</span>}
{errorMessages.map((err) => (
<span key={err} className={styles.errorMessage}>
{err}
</span>
))}
{Boolean(hint) && <div className={styles.hint}>{hint}</div>}
</div>
);
Expand Down

0 comments on commit 0fd2f00

Please sign in to comment.