saleor-dashboard/src/components/SeoForm/SeoForm.tsx

194 lines
5.5 KiB
TypeScript
Raw Normal View History

2019-06-19 14:40:52 +00:00
import Button from "@material-ui/core/Button";
import Card from "@material-ui/core/Card";
import CardContent from "@material-ui/core/CardContent";
import {
createStyles,
Theme,
withStyles,
WithStyles
} from "@material-ui/core/styles";
import TextField from "@material-ui/core/TextField";
import Typography from "@material-ui/core/Typography";
2019-08-09 11:14:35 +00:00
import classNames from "classnames";
2019-08-09 10:26:22 +00:00
import React from "react";
import { FormattedMessage, useIntl } from "react-intl";
2019-06-19 14:40:52 +00:00
import CardTitle from "../CardTitle";
import FormSpacer from "../FormSpacer";
2019-10-28 16:16:49 +00:00
const styles = theme =>
2019-06-19 14:40:52 +00:00
createStyles({
addressBar: {
color: "#006621",
fontSize: "13px",
lineHeight: "16px",
marginBottom: "2px",
overflow: "hidden",
textOverflow: "ellipsis",
whiteSpace: "nowrap"
},
container: {
width: "100%"
},
descriptionBar: {
color: "#545454",
fontSize: "13px",
lineHeight: "18px",
overflowWrap: "break-word"
},
helperText: {
2019-10-28 16:16:49 +00:00
marginBottom: theme.spacing(3)
2019-06-19 14:40:52 +00:00
},
label: {
flex: 1
},
labelContainer: {
2019-08-09 11:14:35 +00:00
"& span": {
paddingRight: 30
},
2019-06-19 14:40:52 +00:00
display: "flex"
},
preview: {
2019-10-28 16:16:49 +00:00
minHeight: theme.spacing(10)
2019-06-19 14:40:52 +00:00
},
title: {
padding: 0
},
titleBar: {
color: "#1a0dab",
fontSize: "18px",
lineHeight: "21px",
overflowWrap: "break-word",
textDecoration: "none",
wordWrap: "break-word"
}
});
interface SeoFormProps extends WithStyles<typeof styles> {
description?: string;
descriptionPlaceholder: string;
disabled?: boolean;
loading?: boolean;
helperText?: string;
title: string;
titlePlaceholder: string;
onChange(event: any);
onClick?();
}
const SeoForm = withStyles(styles, { name: "SeoForm" })(
({
classes,
description,
descriptionPlaceholder,
disabled,
helperText,
loading,
title,
titlePlaceholder,
onChange
}: SeoFormProps) => {
const intl = useIntl();
2019-06-19 14:40:52 +00:00
const [expanded, setExpansionStatus] = React.useState(false);
const toggleExpansion = () => setExpansionStatus(!expanded);
return (
<Card>
<CardTitle
title={intl.formatMessage({
defaultMessage: "Search Engine Preview"
})}
2019-06-19 14:40:52 +00:00
toolbar={
<Button color="primary" variant="text" onClick={toggleExpansion}>
<FormattedMessage
defaultMessage="Edit website SEO"
description="button"
/>
2019-06-19 14:40:52 +00:00
</Button>
}
/>
<CardContent>
{helperText && (
<Typography
className={classNames({ [classes.helperText]: expanded })}
>
{helperText}
</Typography>
)}
{expanded && (
<div className={classes.container}>
<TextField
name="seoTitle"
label={
<div className={classes.labelContainer}>
<div className={classes.label}>
<FormattedMessage defaultMessage="Search engine title" />
2019-06-19 14:40:52 +00:00
</div>
2019-10-28 16:16:49 +00:00
{title.length > 0 && (
<span>
<FormattedMessage
defaultMessage="{numberOfCharacters} of {maxCharacters} characters"
description="character limit"
values={{
maxCharacters: 70,
numberOfCharacters: title.length
}}
/>
</span>
)}
2019-06-19 14:40:52 +00:00
</div>
}
helperText={intl.formatMessage({
defaultMessage:
"If empty, the preview shows what will be autogenerated."
})}
2019-06-19 14:40:52 +00:00
value={title.slice(0, 69)}
disabled={loading || disabled}
placeholder={titlePlaceholder}
onChange={onChange}
fullWidth
/>
<FormSpacer />
<TextField
name="seoDescription"
label={
<div className={classes.labelContainer}>
<div className={classes.label}>
<FormattedMessage defaultMessage="Search engine description" />
2019-06-19 14:40:52 +00:00
</div>
2019-10-28 16:16:49 +00:00
{description.length > 0 && (
<span>
<FormattedMessage
defaultMessage="{numberOfCharacters} of {maxCharacters} characters"
description="character limit"
values={{
maxCharacters: 300,
numberOfCharacters: description.length
}}
/>
</span>
)}
2019-06-19 14:40:52 +00:00
</div>
}
helperText={intl.formatMessage({
defaultMessage:
"If empty, the preview shows what will be autogenerated."
})}
2019-06-19 14:40:52 +00:00
value={description ? description.slice(0, 299) : undefined}
onChange={onChange}
disabled={loading || disabled}
fullWidth
multiline
placeholder={descriptionPlaceholder}
rows={10}
/>
</div>
)}
</CardContent>
</Card>
);
}
);
SeoForm.displayName = "SeoForm";
export default SeoForm;