|
1 |
| -import { Typography, TypographyProps } from '@mui/material'; |
| 1 | +import { Alert, AlertProps, Collapse } from '@mui/material'; |
| 2 | +import { styled } from '@mui/material/styles'; |
| 3 | +import Markdown from 'markdown-to-jsx'; |
| 4 | +import { memo } from 'react'; |
2 | 5 | import { useFormState } from 'react-final-form';
|
| 6 | +import { extendSx } from '~/common'; |
3 | 7 |
|
4 | 8 | /**
|
5 | 9 | * Standard styling for displaying form submission errors.
|
6 |
| - * If no children are passed, component is display the form's submitErrors |
| 10 | + * If no children are passed, the component displays the form's submitErrors |
7 | 11 | * if it is a string.
|
8 | 12 | */
|
9 |
| -export const SubmitError = ({ children, ...rest }: TypographyProps) => { |
| 13 | +export const SubmitError = ({ children, ...rest }: AlertProps) => { |
10 | 14 | const { submitError } = useFormState({
|
11 | 15 | subscription: {
|
12 | 16 | submitError: true,
|
13 | 17 | },
|
14 | 18 | });
|
| 19 | + const error = children || submitError; |
15 | 20 | return (
|
16 |
| - <Typography color="error" variant="body2" align="center" {...rest}> |
17 |
| - {children || submitError || <br />} |
18 |
| - </Typography> |
| 21 | + <Collapse in={!!error}> |
| 22 | + <Alert severity="error" {...rest} sx={[{ mb: 2 }, ...extendSx(rest.sx)]}> |
| 23 | + {children || |
| 24 | + (typeof submitError === 'string' ? ( |
| 25 | + <MarkdownStyled>{submitError}</MarkdownStyled> |
| 26 | + ) : ( |
| 27 | + submitError |
| 28 | + ))} |
| 29 | + </Alert> |
| 30 | + </Collapse> |
19 | 31 | );
|
20 | 32 | };
|
| 33 | + |
| 34 | +const MarkdownStyled = memo(function MarkdownStyled({ |
| 35 | + children, |
| 36 | +}: { |
| 37 | + children: string; |
| 38 | +}) { |
| 39 | + return <Markdown options={{ wrapper: MkdownRoot }}>{children}</Markdown>; |
| 40 | +}); |
| 41 | + |
| 42 | +const MkdownRoot = styled('div')(({ theme }) => ({ |
| 43 | + '> :first-child': { |
| 44 | + marginBlockStart: 0, |
| 45 | + }, |
| 46 | + '> :last-child': { |
| 47 | + marginBlockEnd: 0, |
| 48 | + }, |
| 49 | + 'p, ul': { |
| 50 | + marginBlock: theme.spacing(1), |
| 51 | + }, |
| 52 | + ul: { |
| 53 | + paddingInlineStart: theme.spacing(4), |
| 54 | + }, |
| 55 | +})); |
0 commit comments