Skip to content

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

Refactor Mutli Autocomplete Select

dominik-zeglenca330dd578b2
diff --git a/saleor/static/dashboard-next/components/MultiAutocompleteSelectField/MultiAutocompleteSelectField.tsx b/saleor/static/dashboard-next/components/MultiAutocompleteSelectField/MultiAutocompleteSelectField.tsx
index 1ae702d0b4..40ebb8fdf0 100644
--- a/saleor/static/dashboard-next/components/MultiAutocompleteSelectField/MultiAutocompleteSelectField.tsx
+++ b/saleor/static/dashboard-next/components/MultiAutocompleteSelectField/MultiAutocompleteSelectField.tsx
@@ -1,4 +1,4 @@
-import Chip from "@material-ui/core/Chip";
+import CircularProgress from "@material-ui/core/CircularProgress";
import MenuItem from "@material-ui/core/MenuItem";
import Paper from "@material-ui/core/Paper";
import {
@@ -34,12 +34,22 @@ const styles = (theme: Theme) =>
marginTop: theme.spacing.unit,
position: "absolute",
right: 0,
- zIndex: 1
+ zIndex: 2
}
});
-interface MultiAutocompleteSelectFieldProps extends WithStyles<typeof styles> {
+export interface MultiAutocompleteSelectFieldChildrenFunc {
+ deleteItem: (item: ChoiceType) => void;
+ items: ChoiceType[];
+}
+export type MultiAutocompleteSelectFieldChildren = (
+ props: MultiAutocompleteSelectFieldChildrenFunc
+) => React.ReactNode;
+
+export interface MultiAutocompleteSelectFieldProps
+ extends WithStyles<typeof styles> {
name: string;
+ children: MultiAutocompleteSelectFieldChildren;
choices: ChoiceType[];
value?: ChoiceType[];
loading?: boolean;
@@ -58,6 +68,7 @@ export const MultiAutocompleteSelectField = withStyles(styles, {
name: "MultiAutocompleteSelectField"
})(
({
+ children,
choices,
classes,
helperText,
@@ -76,7 +87,7 @@ export const MultiAutocompleteSelectField = withStyles(styles, {
reset({ inputValue: "" });
onChange({ target: { name, value: [...value, item] } });
};
- const handleDelete = (item: ChoiceType) => () => {
+ const handleDelete = (item: ChoiceType) => {
const newValue = value.slice();
newValue.splice(
value.findIndex(listItem => listItem.value === item.value),
@@ -84,23 +95,6 @@ export const MultiAutocompleteSelectField = withStyles(styles, {
);
onChange({ target: { name, value: newValue } });
};
- const handleKeyDown = (inputValue: string | null) => (
- event: React.KeyboardEvent<any>
- ) => {
- switch (event.keyCode) {
- // Backspace
- case 8:
- if (!inputValue) {
- onChange({
- target: {
- name,
- value: value.slice(0, value.length - 1)
- }
- });
- break;
- }
- }
- };
const filteredChoices = choices.filter(
suggestion => value.map(v => v.value).indexOf(suggestion.value) === -1
@@ -131,22 +125,20 @@ export const MultiAutocompleteSelectField = withStyles(styles, {
<TextField
InputProps={{
...getInputProps({
- onKeyDown: handleKeyDown(inputValue),
placeholder
}),
- endAdornment: <ArrowDropdownIcon onClick={toggleMenu} />,
+ endAdornment: (
+ <div>
+ {loading ? (
+ <CircularProgress size={20} />
+ ) : (
+ <ArrowDropdownIcon onClick={toggleMenu} />
+ )}
+ </div>
+ ),
id: undefined,
onBlur: closeMenu,
- onFocus: openMenu,
- startAdornment: selectedItem.map(item => (
- <Chip
- key={item.value}
- tabIndex={-1}
- label={item.label}
- className={classes.chip}
- onDelete={handleDelete(item)}
- />
- ))
+ onFocus: openMenu
}}
helperText={helperText}
label={label}
@@ -154,32 +146,28 @@ export const MultiAutocompleteSelectField = withStyles(styles, {
/>
{isOpen && (
<Paper className={classes.paper} square>
- {loading ? (
- <MenuItem disabled={true} component="div">
- {i18n.t("Loading...")}
- </MenuItem>
- ) : (
- <>
- {filteredChoices.length > 0
- ? filteredChoices.map((suggestion, index) => (
- <MenuItem
- key={suggestion.value}
- selected={highlightedIndex === index}
- component="div"
- {...getItemProps({ item: suggestion })}
- >
- {suggestion.label}
- </MenuItem>
- ))
- : !loading && (
- <MenuItem disabled={true} component="div">
- {i18n.t("No results found")}
- </MenuItem>
- )}
- </>
- )}
+ {!loading && filteredChoices.length > 0
+ ? filteredChoices.map((suggestion, index) => (
+ <MenuItem
+ key={suggestion.value}
+ selected={highlightedIndex === index}
+ component="div"
+ {...getItemProps({ item: suggestion })}
+ >
+ {suggestion.label}
+ </MenuItem>
+ ))
+ : !loading && (
+ <MenuItem disabled={true} component="div">
+ {i18n.t("No results found")}
+ </MenuItem>
+ )}
</Paper>
)}
+ {children({
+ deleteItem: handleDelete,
+ items: selectedItem
+ })}
</div>
);
}}
diff --git a/saleor/static/dashboard-next/products/components/ProductOrganization/ProductOrganization.tsx b/saleor/static/dashboard-next/products/components/ProductOrganization/ProductOrganization.tsx
index 23f50b6b0f..bd59d767fb 100644
--- a/saleor/static/dashboard-next/products/components/ProductOrganization/ProductOrganization.tsx
+++ b/saleor/static/dashboard-next/products/components/ProductOrganization/ProductOrganization.tsx
@@ -18,6 +18,7 @@ import Skeleton from "../../../components/Skeleton";
import i18n from "../../../i18n";
import { maybe } from "../../../misc";
import { ProductCreateData_productTypes_edges_node_productAttributes } from "../../types/ProductCreateData";
+import Chip from "../../../components/Chip";
interface ChoiceType {
label: string;
@@ -214,7 +215,6 @@ const ProductOrganization = withStyles(styles, { name: "ProductOrganization" })(
}
value={data.productType}
onChange={handleProductTypeSelect}
-
/>
) : (
<>
@@ -294,7 +294,22 @@ const ProductOrganization = withStyles(styles, { name: "ProductOrganization" })(
value={data.collections}
onChange={onChange}
fetchChoices={fetchCollections}
- />
+ >
+ {({ deleteItem, items }) => (
+ <>
+ <FormSpacer />
+ <div>
+ {items.map(item => (
+ <Chip
+ key={item.value}
+ label={item.label}
+ onClose={() => deleteItem(item)}
+ />
+ ))}
+ </div>
+ </>
+ )}
+ </MultiAutocompleteSelectField>
</CardContent>
</Card>
);
diff --git a/saleor/static/dashboard-next/storybook/stories/components/MultiAutocompleteSelectField.tsx b/saleor/static/dashboard-next/storybook/stories/components/MultiAutocompleteSelectField.tsx
index b6fa8c2736..8c7b03f46c 100644
--- a/saleor/static/dashboard-next/storybook/stories/components/MultiAutocompleteSelectField.tsx
+++ b/saleor/static/dashboard-next/storybook/stories/components/MultiAutocompleteSelectField.tsx
@@ -1,8 +1,14 @@
+import IconButton from "@material-ui/core/IconButton";
+import Typography from "@material-ui/core/Typography";
+import DeleteIcon from "@material-ui/icons/Close";
import { storiesOf } from "@storybook/react";
import * as React from "react";
import Form from "../../../components/Form";
-import MultiAutocompleteSelectField from "../../../components/MultiAutocompleteSelectField";
+import MultiAutocompleteSelectField, {
+ MultiAutocompleteSelectFieldChildrenFunc
+} from "../../../components/MultiAutocompleteSelectField";
+import CardDecorator from "../../CardDecorator";
import Decorator from "../../Decorator";
import { ChoiceProvider } from "../../mock";
@@ -21,7 +27,35 @@ const suggestions = [
"Zimbabwe"
].map(c => ({ label: c, value: c.toLocaleLowerCase().replace(/\s+/, "_") }));
+const Choices: React.FC<MultiAutocompleteSelectFieldChildrenFunc> = ({
+ deleteItem,
+ items
+}) => (
+ <div style={{ marginTop: 16 }}>
+ {items.map(item => (
+ <div
+ style={{
+ alignItems: "center",
+ display: "flex",
+ justifyContent: "space-between"
+ }}
+ >
+ <Typography>{item.label}</Typography>
+ <div
+ style={{
+ flex: 1
+ }}
+ />
+ <IconButton onClick={() => deleteItem(item)}>
+ <DeleteIcon style={{ fontSize: 16 }} />
+ </IconButton>
+ </div>
+ ))}
+ </div>
+);
+
storiesOf("Generics / MultiAutocompleteSelectField", module)
+ .addDecorator(CardDecorator)
.addDecorator(Decorator)
.add("with loading data", () => (
<Form initial={{ countries: [suggestions[0]] }}>
@@ -32,12 +66,15 @@ storiesOf("Generics / MultiAutocompleteSelectField", module)
choices={choices}
fetchChoices={fetchChoices}
helperText={`Value: ${data.countries.map(c => c.value)}`}
+ label="Countries"
loading={true}
name="countries"
onChange={change}
placeholder="Select country"
value={data.countries}
- />
+ >
+ {selectInput => <Choices {...selectInput} />}
+ </MultiAutocompleteSelectField>
)}
</ChoiceProvider>
)}
@@ -52,12 +89,15 @@ storiesOf("Generics / MultiAutocompleteSelectField", module)
choices={choices}
fetchChoices={fetchChoices}
helperText={`Value: ${data.countries.map(c => c.value)}`}
+ label="Countries"
loading={false}
name="countries"
onChange={change}
placeholder="Select country"
value={data.countries}
- />
+ >
+ {selectInput => <Choices {...selectInput} />}
+ </MultiAutocompleteSelectField>
)}
</ChoiceProvider>
)}