Skip to content

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

Refactor single autocomplete select

dominik-zeglen822143560bfd
diff --git a/saleor/static/dashboard-next/components/SingleAutocompleteSelectField/SingleAutocompleteSelectField.tsx b/saleor/static/dashboard-next/components/SingleAutocompleteSelectField/SingleAutocompleteSelectField.tsx
index 99e965f7e9..840490c71c 100644
--- a/saleor/static/dashboard-next/components/SingleAutocompleteSelectField/SingleAutocompleteSelectField.tsx
+++ b/saleor/static/dashboard-next/components/SingleAutocompleteSelectField/SingleAutocompleteSelectField.tsx
@@ -48,12 +48,12 @@ export interface SingleAutocompleteSelectFieldProps {
disabled?: boolean;
loading?: boolean;
placeholder?: string;
- custom?: boolean;
+ allowCustomValues?: boolean;
helperText?: string;
label?: string;
InputProps?: InputProps;
- fetchChoices?(value: string);
- onChange(event);
+ fetchChoices?: (value: string) => void;
+ onChange: (event: React.ChangeEvent<any>) => void;
}
interface SingleAutocompleteSelectFieldState {
@@ -73,7 +73,7 @@ const SingleAutocompleteSelectFieldComponent = withStyles(styles, {
({
choices,
classes,
- custom,
+ allowCustomValues: custom,
disabled,
displayValue,
error,
@@ -87,7 +87,13 @@ const SingleAutocompleteSelectFieldComponent = withStyles(styles, {
fetchChoices,
onChange
}: SingleAutocompleteSelectFieldProps & WithStyles<typeof styles>) => {
- const handleChange = item => onChange({ target: { name, value: item } });
+ const handleChange = item =>
+ onChange({
+ target: {
+ name,
+ value: item
+ }
+ } as any);
return (
<DebounceAutocomplete debounceFn={fetchChoices}>
@@ -109,11 +115,11 @@ const SingleAutocompleteSelectFieldComponent = withStyles(styles, {
closeMenu,
highlightedIndex
}) => {
- const isCustom =
+ const isCustomValueSelected =
choices && selectedItem
- ? choices.filter(c => c.value === selectedItem.value)
- .length === 0
+ ? choices.filter(c => c.value === selectedItem).length === 0
: false;
+
return (
<div className={classes.container}>
<TextField
@@ -141,32 +147,44 @@ const SingleAutocompleteSelectFieldComponent = withStyles(styles, {
label={label}
fullWidth={true}
/>
- {isOpen && (
+ {isOpen && (inputValue || choices.length) && (
<Paper className={classes.paper} square>
{choices.length > 0 || custom ? (
<>
{choices.map((suggestion, index) => (
<MenuItem
key={JSON.stringify(suggestion)}
- selected={highlightedIndex === index}
+ selected={
+ highlightedIndex === index ||
+ selectedItem === suggestion.value
+ }
component="div"
{...getItemProps({ item: suggestion.value })}
>
{suggestion.label}
</MenuItem>
))}
- {custom && (
- <MenuItem
- key={"customValue"}
- selected={isCustom}
- component="div"
- {...getItemProps({
- item: { label: inputValue, value: inputValue }
- })}
- >
- {i18n.t("Add custom value")}
- </MenuItem>
- )}
+ {custom &&
+ inputValue &&
+ !choices.find(
+ choice =>
+ choice.label.toLowerCase() ===
+ inputValue.toLowerCase()
+ ) && (
+ <MenuItem
+ key={"customValue"}
+ selected={isCustomValueSelected}
+ component="div"
+ {...getItemProps({
+ item: inputValue
+ })}
+ >
+ {i18n.t("Add new value: {{ value }}", {
+ context: "add custom option",
+ value: inputValue
+ })}
+ </MenuItem>
+ )}
</>
) : (
<MenuItem disabled={true} component="div">
diff --git a/saleor/static/dashboard-next/orders/components/OrderCustomer/OrderCustomer.tsx b/saleor/static/dashboard-next/orders/components/OrderCustomer/OrderCustomer.tsx
index ea42bf8f78..54be95db30 100644
--- a/saleor/static/dashboard-next/orders/components/OrderCustomer/OrderCustomer.tsx
+++ b/saleor/static/dashboard-next/orders/components/OrderCustomer/OrderCustomer.tsx
@@ -112,7 +112,7 @@ const OrderCustomer = withStyles(styles, { name: "OrderCustomer" })(
};
return (
<SingleAutocompleteSelectField
- custom={true}
+ allowCustomValues={true}
choices={maybe(() => users, []).map(user => ({
label: user.email,
value: user.id
diff --git a/saleor/static/dashboard-next/orders/components/OrderCustomerEditDialog/OrderCustomerEditDialog.tsx b/saleor/static/dashboard-next/orders/components/OrderCustomerEditDialog/OrderCustomerEditDialog.tsx
index 3cd9804bb9..0c1e7921c2 100644
--- a/saleor/static/dashboard-next/orders/components/OrderCustomerEditDialog/OrderCustomerEditDialog.tsx
+++ b/saleor/static/dashboard-next/orders/components/OrderCustomerEditDialog/OrderCustomerEditDialog.tsx
@@ -81,7 +81,7 @@ const OrderCustomerEditDialog = withStyles(styles, {
<DialogContent className={classes.root}>
<SingleAutocompleteSelectField
choices={choices}
- custom
+ allowCustomValues
loading={loading}
name="user"
value={user}
diff --git a/saleor/static/dashboard-next/products/components/ProductVariantAttributes/ProductVariantAttributes.tsx b/saleor/static/dashboard-next/products/components/ProductVariantAttributes/ProductVariantAttributes.tsx
index b90cbcd041..89230dca7c 100644
--- a/saleor/static/dashboard-next/products/components/ProductVariantAttributes/ProductVariantAttributes.tsx
+++ b/saleor/static/dashboard-next/products/components/ProductVariantAttributes/ProductVariantAttributes.tsx
@@ -128,7 +128,7 @@ const ProductVariantAttributes = withStyles(styles, {
onChange={handleAttributeValueSelect}
value={getAttributeValue(attribute.slug)}
choices={getAttributeValues(attribute.slug)}
- custom
+ allowCustomValues
/>
);
})
diff --git a/saleor/static/dashboard-next/storybook/stories/components/SingleAutocompleteSelectField.tsx b/saleor/static/dashboard-next/storybook/stories/components/SingleAutocompleteSelectField.tsx
index 32e8fbc6f3..0827f0b706 100644
--- a/saleor/static/dashboard-next/storybook/stories/components/SingleAutocompleteSelectField.tsx
+++ b/saleor/static/dashboard-next/storybook/stories/components/SingleAutocompleteSelectField.tsx
@@ -2,7 +2,10 @@ import { storiesOf } from "@storybook/react";
import React from "react";
import Form from "@saleor/components/Form";
-import SingleAutocompleteSelectField from "@saleor/components/SingleAutocompleteSelectField";
+import SingleAutocompleteSelectField, {
+ SingleAutocompleteSelectFieldProps
+} from "@saleor/components/SingleAutocompleteSelectField";
+import { maybe } from "@saleor/misc";
import CardDecorator from "../../CardDecorator";
import Decorator from "../../Decorator";
import { ChoiceProvider } from "../../mock";
@@ -22,70 +25,57 @@ const suggestions = [
"Zimbabwe"
].map(c => ({ label: c, value: c.toLocaleLowerCase().replace(/\s+/, "_") }));
-const props = {
+const props: SingleAutocompleteSelectFieldProps = {
+ choices: undefined,
+ displayValue: undefined,
label: "Country",
loading: false,
name: "country",
+ onChange: () => undefined,
placeholder: "Select country"
};
-storiesOf("Generics / SingleAutocompleteSelectField", module)
- .addDecorator(CardDecorator)
- .addDecorator(Decorator)
- .add("with loading data", () => (
- <Form initial={{ country: suggestions[0] }}>
- {({ change, data }) => (
- <ChoiceProvider choices={suggestions}>
- {({ choices, fetchChoices }) => (
- <SingleAutocompleteSelectField
- {...props}
- choices={choices}
- fetchChoices={fetchChoices}
- helperText={`Value: ${data.country.value}`}
- loading={true}
- onChange={change}
- value={data.country}
- />
- )}
- </ChoiceProvider>
- )}
- </Form>
- ))
- .add("with loaded data", () => (
- <Form initial={{ country: suggestions[0] }}>
- {({ change, data }) => (
- <ChoiceProvider choices={suggestions}>
- {({ choices, fetchChoices }) => (
- <SingleAutocompleteSelectField
- {...props}
- choices={choices}
- fetchChoices={fetchChoices}
- helperText={`Value: ${data.country.value}`}
- onChange={change}
- value={data.country}
- />
- )}
- </ChoiceProvider>
- )}
- </Form>
- ))
- .add("with custom option", () => (
- <Form initial={{ country: suggestions[0] }}>
+const Story: React.FC<
+ Partial<SingleAutocompleteSelectFieldProps>
+> = storyProps => {
+ const [displayValue, setDisplayValue] = React.useState(suggestions[0].label);
+
+ return (
+ <Form initial={{ country: suggestions[0].value }}>
{({ change, data }) => (
<ChoiceProvider choices={suggestions}>
- {({ choices, fetchChoices, loading }) => (
- <SingleAutocompleteSelectField
- {...props}
- choices={choices}
- fetchChoices={fetchChoices}
- helperText={`Value: ${data.country.value}`}
- loading={loading}
- onChange={change}
- value={data.country}
- custom={true}
- />
- )}
+ {({ choices, loading, fetchChoices }) => {
+ const handleSelect = (event: React.ChangeEvent<any>) => {
+ const value: string = event.target.value;
+ const match = choices.find(choice => choice.value === value);
+ const label = maybe(() => match.label, value);
+ setDisplayValue(label);
+ change(event);
+ };
+
+ return (
+ <SingleAutocompleteSelectField
+ {...props}
+ displayValue={displayValue}
+ choices={choices}
+ fetchChoices={fetchChoices}
+ helperText={`Value: ${data.country}`}
+ onChange={handleSelect}
+ value={data.country}
+ loading={loading}
+ {...storyProps}
+ />
+ );
+ }}
</ChoiceProvider>
)}
</Form>
- ));
+ );
+};
+
+storiesOf("Generics / SingleAutocompleteSelectField", module)
+ .addDecorator(CardDecorator)
+ .addDecorator(Decorator)
+ .add("with loaded data", () => <Story />)
+ .add("with loading data", () => <Story loading={true} />)
+ .add("with custom option", () => <Story allowCustomValues={true} />);