Skip to content

Commit 1bad246

Browse files
committed
Better SubmitError styling w/ Alert & Markdown
1 parent 3023c40 commit 1bad246

File tree

3 files changed

+43
-8
lines changed

3 files changed

+43
-8
lines changed
Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,55 @@
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';
25
import { useFormState } from 'react-final-form';
6+
import { extendSx } from '~/common';
37

48
/**
59
* 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
711
* if it is a string.
812
*/
9-
export const SubmitError = ({ children, ...rest }: TypographyProps) => {
13+
export const SubmitError = ({ children, ...rest }: AlertProps) => {
1014
const { submitError } = useFormState({
1115
subscription: {
1216
submitError: true,
1317
},
1418
});
19+
const error = children || submitError;
1520
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>
1931
);
2032
};
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+
}));

src/scenes/Authentication/ChangePassword/ChangePassword.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export const ChangePassword = (props: ChangePasswordProps) => {
4242
});
4343
}}
4444
>
45-
<SubmitError align="left" />
45+
<SubmitError />
4646
<PasswordField
4747
name="oldPassword"
4848
label="Old Password"

src/scenes/Languages/LanguageForm/LanguageForm.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ export const LanguageForm = <Mutation extends LanguageMutation>({
9696
return (
9797
<Grid container spacing={3} className={classes.content}>
9898
<Grid item xs={12}>
99-
<SubmitError align="left" />
99+
<SubmitError />
100100
</Grid>
101101
{canReadAny(
102102
language,

0 commit comments

Comments
 (0)