From 678139015706a00b032aa9ed8bb10912f4edb8c0 Mon Sep 17 00:00:00 2001 From: Iacopo Leardini Date: Wed, 3 Jan 2024 17:19:11 +0100 Subject: [PATCH] Add ErrorMessageWrapper component and update Form.stories.tsx --- src/stories/form/Form.stories.tsx | 5 +++- src/stories/form/Form.tsx | 40 ++++++++++++++----------------- 2 files changed, 22 insertions(+), 23 deletions(-) diff --git a/src/stories/form/Form.stories.tsx b/src/stories/form/Form.stories.tsx index 9deaecc..78dbb0e 100644 --- a/src/stories/form/Form.stories.tsx +++ b/src/stories/form/Form.stories.tsx @@ -1,4 +1,4 @@ -import { Field } from "./Form"; +import { ErrorMessageWrapper, Field } from "./Form"; import { Checkbox } from "./checkBox/Checkbox"; import { Story, Meta } from "@storybook/react"; import { Formik, Form, FormikProps } from "formik"; @@ -69,5 +69,8 @@ export const Template: Story = () => ( )} +
+ Generic Error outside Formik +
); diff --git a/src/stories/form/Form.tsx b/src/stories/form/Form.tsx index b149269..974c7d5 100644 --- a/src/stories/form/Form.tsx +++ b/src/stories/form/Form.tsx @@ -1,4 +1,5 @@ import { + ErrorMessageProps, FieldProps, ErrorMessage as FormikErrorMessage, Field as FormikField, @@ -12,32 +13,21 @@ import FormLabel from "./formlabel/FormLabel"; import Input from "./input/Input"; import { Radio } from "./radio/Radio"; -const ErrorMessageWrapper = styled.div` - display: flex; - align-items: center; - gap: ${(props) => props.theme.grid.sizes[2]}; -`; - -const BasicErrorMessage = ({ - name, - className, +export const ErrorMessageWrapper = ({ + children, }: { - name: string; - className?: string; + children: React.ReactNode; }) => ( -
- - {(msg) => ( - - - {msg} - - )} - -
+ + + {children} + ); -export const ErrorMessage = styled(BasicErrorMessage)` +const StyledErrorMessage = styled.div` + display: flex; + align-items: center; + gap: ${(props) => props.theme.grid.sizes[2]}; color: ${(props) => props.theme.palette.danger}; width: 100%; margin-top: 0.25rem; @@ -47,6 +37,12 @@ export const ErrorMessage = styled(BasicErrorMessage)` } `; +export const ErrorMessage = ({ ...props }: ErrorMessageProps) => ( + + {(msg) => {msg}} + +); + export const Field = ({ type = "text", placeholder,