Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Do not lose focus while typing

dominik-zeglenc358c461d3c8
diff --git a/saleor/static/dashboard-next/products/components/ProductCreatePage/ProductCreatePage.tsx b/saleor/static/dashboard-next/products/components/ProductCreatePage/ProductCreatePage.tsx
index 0fd7ee7555..32cb7c8c21 100644
--- a/saleor/static/dashboard-next/products/components/ProductCreatePage/ProductCreatePage.tsx
+++ b/saleor/static/dashboard-next/products/components/ProductCreatePage/ProductCreatePage.tsx
@@ -1,4 +1,4 @@
-import { RawDraftContentState } from "draft-js";
+import { ContentState, convertToRaw, RawDraftContentState } from "draft-js";
import React from "react";
import AppHeader from "@saleor/components/AppHeader";
@@ -95,6 +95,8 @@ export const ProductCreatePage: React.StatelessComponent<
onBack,
onSubmit
}: ProductCreatePageProps) => {
+ const initialDescription = convertToRaw(ContentState.createFromText(""));
+
const initialData: FormData = {
attributes: [],
basePrice: 0,
@@ -143,6 +145,7 @@ export const ProductCreatePage: React.StatelessComponent<
data={data}
disabled={disabled}
errors={errors}
+ initialDescription={initialDescription}
onChange={change}
/>
<CardSpacer />
diff --git a/saleor/static/dashboard-next/products/components/ProductDetailsForm/ProductDetailsForm.tsx b/saleor/static/dashboard-next/products/components/ProductDetailsForm/ProductDetailsForm.tsx
index 9a13586c3f..bf361d0d65 100644
--- a/saleor/static/dashboard-next/products/components/ProductDetailsForm/ProductDetailsForm.tsx
+++ b/saleor/static/dashboard-next/products/components/ProductDetailsForm/ProductDetailsForm.tsx
@@ -7,6 +7,7 @@ import {
WithStyles
} from "@material-ui/core/styles";
import TextField from "@material-ui/core/TextField";
+import { RawDraftContentState } from "draft-js";
import React from "react";
import CardTitle from "@saleor/components/CardTitle";
@@ -29,40 +30,53 @@ interface ProductDetailsFormProps extends WithStyles<typeof styles> {
data: CreateFormData & UpdateFormData;
disabled?: boolean;
errors: { [key: string]: string };
+ // Draftail isn't controlled - it needs only initial input
+ // because it's autosaving on its own.
+ // Ref https://github.com/mirumee/saleor/issues/4470
+ initialDescription: RawDraftContentState;
onChange(event: any);
}
export const ProductDetailsForm = withStyles(styles, {
name: "ProductDetailsForm"
-})(({ classes, data, disabled, errors, onChange }: ProductDetailsFormProps) => (
- <Card>
- <CardTitle title={i18n.t("General information")} />
- <CardContent>
- <div className={classes.root}>
- <TextField
- error={!!errors.name}
- helperText={errors.name}
+})(
+ ({
+ classes,
+ data,
+ disabled,
+ errors,
+ initialDescription,
+ onChange
+ }: ProductDetailsFormProps) => (
+ <Card>
+ <CardTitle title={i18n.t("General information")} />
+ <CardContent>
+ <div className={classes.root}>
+ <TextField
+ error={!!errors.name}
+ helperText={errors.name}
+ disabled={disabled}
+ fullWidth
+ label={i18n.t("Name")}
+ name="name"
+ rows={5}
+ value={data.name}
+ onChange={onChange}
+ />
+ </div>
+ <FormSpacer />
+ <RichTextEditor
disabled={disabled}
- fullWidth
- label={i18n.t("Name")}
- name="name"
- rows={5}
- value={data.name}
+ error={!!errors.descriptionJson}
+ helperText={errors.descriptionJson}
+ initial={initialDescription}
+ label={i18n.t("Description")}
+ name="description"
onChange={onChange}
/>
- </div>
- <FormSpacer />
- <RichTextEditor
- disabled={disabled}
- error={!!errors.descriptionJson}
- helperText={errors.descriptionJson}
- initial={data.description}
- label={i18n.t("Description")}
- name="description"
- onChange={onChange}
- />
- </CardContent>
- </Card>
-));
+ </CardContent>
+ </Card>
+ )
+);
ProductDetailsForm.displayName = "ProductDetailsForm";
export default ProductDetailsForm;
diff --git a/saleor/static/dashboard-next/products/components/ProductUpdatePage/ProductUpdatePage.tsx b/saleor/static/dashboard-next/products/components/ProductUpdatePage/ProductUpdatePage.tsx
index 70fade6d7c..5b1c2dd9f0 100644
--- a/saleor/static/dashboard-next/products/components/ProductUpdatePage/ProductUpdatePage.tsx
+++ b/saleor/static/dashboard-next/products/components/ProductUpdatePage/ProductUpdatePage.tsx
@@ -129,6 +129,10 @@ export const ProductUpdate: React.StatelessComponent<ProductUpdateProps> = ({
toggleAll,
toolbar
}) => {
+ const initialDescription = maybe<RawDraftContentState>(() =>
+ JSON.parse(product.descriptionJson)
+ );
+
const initialData: FormData = {
attributes: maybe(
() =>
@@ -150,7 +154,7 @@ export const ProductUpdate: React.StatelessComponent<ProductUpdateProps> = ({
value: collection.id
}))
: [],
- description: maybe(() => JSON.parse(product.descriptionJson)),
+ description: initialDescription,
isPublished: maybe(() => product.isPublished, false),
name: maybe(() => product.name, ""),
productType: maybe(() => ({
@@ -217,6 +221,7 @@ export const ProductUpdate: React.StatelessComponent<ProductUpdateProps> = ({
data={data}
disabled={disabled}
errors={errors}
+ initialDescription={initialDescription}
onChange={change}
/>
<CardSpacer />