Display error message if invalid content is passed
This commit is contained in:
parent
fd73e69870
commit
c9cf31851e
2 changed files with 230 additions and 192 deletions
|
@ -1,6 +1,7 @@
|
||||||
import { makeStyles } from "@material-ui/core/styles";
|
import { makeStyles } from "@material-ui/core/styles";
|
||||||
import { fade } from "@material-ui/core/styles/colorManipulator";
|
import { fade } from "@material-ui/core/styles/colorManipulator";
|
||||||
import Typography from "@material-ui/core/Typography";
|
import Typography from "@material-ui/core/Typography";
|
||||||
|
import { FormattedMessage } from "react-intl";
|
||||||
import classNames from "classnames";
|
import classNames from "classnames";
|
||||||
import { RawDraftContentState } from "draft-js";
|
import { RawDraftContentState } from "draft-js";
|
||||||
import {
|
import {
|
||||||
|
@ -13,6 +14,7 @@ import isEqual from "lodash-es/isEqual";
|
||||||
import React from "react";
|
import React from "react";
|
||||||
|
|
||||||
import { ChangeEvent } from "@saleor/hooks/useForm";
|
import { ChangeEvent } from "@saleor/hooks/useForm";
|
||||||
|
import ErrorBoundary from "react-error-boundary";
|
||||||
import BoldIcon from "../../icons/BoldIcon";
|
import BoldIcon from "../../icons/BoldIcon";
|
||||||
import HeaderTwo from "../../icons/HeaderTwo";
|
import HeaderTwo from "../../icons/HeaderTwo";
|
||||||
import HeaderThree from "../../icons/HeaderThree";
|
import HeaderThree from "../../icons/HeaderThree";
|
||||||
|
@ -38,7 +40,17 @@ export interface RichTextEditorProps {
|
||||||
}
|
}
|
||||||
|
|
||||||
const useStyles = makeStyles(
|
const useStyles = makeStyles(
|
||||||
theme => ({
|
theme => {
|
||||||
|
const editorContainer: React.CSSProperties = {
|
||||||
|
border: `1px ${theme.palette.divider} solid`,
|
||||||
|
borderRadius: 4,
|
||||||
|
padding: "27px 12px 10px",
|
||||||
|
position: "relative",
|
||||||
|
transition: theme.transitions.duration.shortest + "ms"
|
||||||
|
};
|
||||||
|
|
||||||
|
return {
|
||||||
|
editorContainer,
|
||||||
error: {
|
error: {
|
||||||
color: theme.palette.error.main
|
color: theme.palette.error.main
|
||||||
},
|
},
|
||||||
|
@ -93,11 +105,7 @@ const useStyles = makeStyles(
|
||||||
"&:hover": {
|
"&:hover": {
|
||||||
borderColor: theme.palette.primary.main
|
borderColor: theme.palette.primary.main
|
||||||
},
|
},
|
||||||
border: `1px ${theme.palette.divider} solid`,
|
...editorContainer
|
||||||
borderRadius: 4,
|
|
||||||
padding: "27px 12px 10px",
|
|
||||||
position: "relative",
|
|
||||||
transition: theme.transitions.duration.shortest + "ms"
|
|
||||||
},
|
},
|
||||||
"&-Toolbar": {
|
"&-Toolbar": {
|
||||||
"&Button": {
|
"&Button": {
|
||||||
|
@ -189,7 +197,8 @@ const useStyles = makeStyles(
|
||||||
smallIcon: {
|
smallIcon: {
|
||||||
marginLeft: 10
|
marginLeft: 10
|
||||||
}
|
}
|
||||||
}),
|
};
|
||||||
|
},
|
||||||
{ name: "RichTextEditor" }
|
{ name: "RichTextEditor" }
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -226,6 +235,18 @@ const RichTextEditor: React.FC<RichTextEditorProps> = props => {
|
||||||
<Typography className={classes.label} variant="caption" color="primary">
|
<Typography className={classes.label} variant="caption" color="primary">
|
||||||
{label}
|
{label}
|
||||||
</Typography>
|
</Typography>
|
||||||
|
<ErrorBoundary
|
||||||
|
FallbackComponent={() => (
|
||||||
|
<div className={classes.editorContainer}>
|
||||||
|
<Typography color="error">
|
||||||
|
<FormattedMessage
|
||||||
|
defaultMessage="Invalid content"
|
||||||
|
description="rich text error"
|
||||||
|
/>
|
||||||
|
</Typography>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
>
|
||||||
<DraftailEditor
|
<DraftailEditor
|
||||||
key={JSON.stringify(initial)}
|
key={JSON.stringify(initial)}
|
||||||
rawContentState={
|
rawContentState={
|
||||||
|
@ -278,6 +299,7 @@ const RichTextEditor: React.FC<RichTextEditorProps> = props => {
|
||||||
// }
|
// }
|
||||||
]}
|
]}
|
||||||
/>
|
/>
|
||||||
|
</ErrorBoundary>
|
||||||
</div>
|
</div>
|
||||||
{helperText && (
|
{helperText && (
|
||||||
<Typography
|
<Typography
|
||||||
|
|
|
@ -116,4 +116,20 @@ storiesOf("Generics / Rich text editor", module)
|
||||||
name="content"
|
name="content"
|
||||||
onChange={() => undefined}
|
onChange={() => undefined}
|
||||||
/>
|
/>
|
||||||
|
))
|
||||||
|
.add("invalid content", () => (
|
||||||
|
<RichTextEditor
|
||||||
|
disabled={false}
|
||||||
|
error={false}
|
||||||
|
helperText={""}
|
||||||
|
initial={
|
||||||
|
{
|
||||||
|
content: "dummy",
|
||||||
|
key2: "this is not valid draftjs content"
|
||||||
|
} as any
|
||||||
|
}
|
||||||
|
label="Content"
|
||||||
|
name="content"
|
||||||
|
onChange={() => undefined}
|
||||||
|
/>
|
||||||
));
|
));
|
||||||
|
|
Loading…
Reference in a new issue