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

227 lines
6 KiB
TypeScript
Raw Normal View History

2019-08-09 11:14:35 +00:00
import IconButton from "@material-ui/core/IconButton";
2019-10-30 14:34:24 +00:00
import { makeStyles } from "@material-ui/core/styles";
2019-06-19 14:40:52 +00:00
import TextField from "@material-ui/core/TextField";
2019-08-09 11:14:35 +00:00
import Typography from "@material-ui/core/Typography";
import CloseIcon from "@material-ui/icons/Close";
2019-06-19 14:40:52 +00:00
import Downshift, { ControllerStateAndHelpers } from "downshift";
2019-10-16 15:51:53 +00:00
import { filter } from "fuzzaldrin";
2019-08-09 10:26:22 +00:00
import React from "react";
2019-06-19 14:40:52 +00:00
2019-08-09 11:14:35 +00:00
import { fade } from "@material-ui/core/styles/colorManipulator";
import Debounce, { DebounceProps } from "@saleor/components/Debounce";
import ArrowDropdownIcon from "@saleor/icons/ArrowDropdown";
2019-10-15 15:23:33 +00:00
import { FetchMoreProps } from "@saleor/types";
import MultiAutocompleteSelectFieldContent, {
MultiAutocompleteChoiceType
} from "./MultiAutocompleteSelectFieldContent";
2019-06-19 14:40:52 +00:00
2019-12-03 15:28:40 +00:00
const useStyles = makeStyles(
theme => ({
chip: {
width: "100%"
},
chipClose: {
height: 32,
padding: 0,
width: 32
},
chipContainer: {
display: "flex",
flexDirection: "column",
marginTop: theme.spacing(1)
},
chipInner: {
"& svg": {
color: theme.palette.primary.contrastText
},
alignItems: "center",
background: fade(theme.palette.primary.main, 0.8),
borderRadius: 18,
color: theme.palette.primary.contrastText,
display: "flex",
justifyContent: "space-between",
margin: theme.spacing(1, 0),
paddingLeft: theme.spacing(2),
paddingRight: theme.spacing(1)
},
chipLabel: {
2019-09-12 14:38:40 +00:00
color: theme.palette.primary.contrastText
2019-06-19 14:40:52 +00:00
},
2019-12-03 15:28:40 +00:00
container: {
flexGrow: 1,
position: "relative"
}
}),
{ name: "MultiAutocompleteSelectField" }
);
2019-06-19 14:40:52 +00:00
2019-10-15 15:23:33 +00:00
export interface MultiAutocompleteSelectFieldProps
extends Partial<FetchMoreProps> {
2019-08-09 11:14:35 +00:00
allowCustomValues?: boolean;
displayValues: MultiAutocompleteChoiceType[];
2020-03-06 11:09:55 +00:00
error?: boolean;
2019-06-19 14:40:52 +00:00
name: string;
2019-08-09 11:14:35 +00:00
choices: MultiAutocompleteChoiceType[];
value: string[];
2019-06-19 14:40:52 +00:00
loading?: boolean;
placeholder?: string;
helperText?: string;
label?: string;
2019-08-09 11:14:35 +00:00
fetchChoices?: (value: string) => void;
onChange: (event: React.ChangeEvent<any>) => void;
2019-06-19 14:40:52 +00:00
}
2020-03-06 11:09:55 +00:00
const DebounceAutocomplete: React.ComponentType<DebounceProps<
string
>> = Debounce;
2019-06-19 14:40:52 +00:00
2020-03-06 11:09:55 +00:00
const MultiAutocompleteSelectFieldComponent: React.FC<MultiAutocompleteSelectFieldProps> = props => {
2019-10-30 14:34:24 +00:00
const {
2019-08-09 11:14:35 +00:00
allowCustomValues,
2019-06-19 14:40:52 +00:00
choices,
2019-08-09 11:14:35 +00:00
displayValues,
2020-03-06 11:09:55 +00:00
error,
2019-10-15 15:23:33 +00:00
hasMore,
2019-06-19 14:40:52 +00:00
helperText,
label,
loading,
name,
placeholder,
value,
fetchChoices,
2019-08-27 13:29:00 +00:00
onChange,
2019-10-15 15:23:33 +00:00
onFetchMore,
2019-10-30 14:34:24 +00:00
...rest
} = props;
const classes = useStyles(props);
2019-06-19 14:40:52 +00:00
2019-10-30 14:34:24 +00:00
const handleSelect = (
item: string,
downshiftOpts?: ControllerStateAndHelpers
) => {
if (downshiftOpts) {
downshiftOpts.reset({ inputValue: "" });
}
onChange({
target: { name, value: item }
} as any);
};
2019-10-15 15:23:33 +00:00
2019-10-30 14:34:24 +00:00
return (
<>
<Downshift
onInputValueChange={fetchChoices}
onSelect={handleSelect}
itemToString={() => ""}
>
{({
getInputProps,
getItemProps,
isOpen,
toggleMenu,
highlightedIndex,
inputValue
}) => {
const displayCustomValue =
inputValue &&
inputValue.length > 0 &&
allowCustomValues &&
!choices.find(
choice => choice.label.toLowerCase() === inputValue.toLowerCase()
2019-10-15 15:23:33 +00:00
);
2019-10-30 14:34:24 +00:00
return (
<div className={classes.container} {...rest}>
<TextField
InputProps={{
...getInputProps({
placeholder
}),
endAdornment: (
<div>
<ArrowDropdownIcon onClick={() => toggleMenu()} />
</div>
),
id: undefined,
onClick: toggleMenu
}}
2020-03-06 11:09:55 +00:00
error={error}
2019-10-30 14:34:24 +00:00
helperText={helperText}
label={label}
fullWidth={true}
/>
{isOpen && (!!inputValue || !!choices.length) && (
<MultiAutocompleteSelectFieldContent
choices={choices.filter(
choice => !value.includes(choice.value)
)}
displayCustomValue={displayCustomValue}
displayValues={displayValues}
getItemProps={getItemProps}
hasMore={hasMore}
highlightedIndex={highlightedIndex}
loading={loading}
inputValue={inputValue}
onFetchMore={onFetchMore}
/>
)}
2019-08-09 11:14:35 +00:00
</div>
2019-10-30 14:34:24 +00:00
);
}}
</Downshift>
<div className={classes.chipContainer}>
{displayValues.map(value => (
<div className={classes.chip} key={value.value}>
<div className={classes.chipInner}>
<Typography className={classes.chipLabel}>
{value.label}
</Typography>
<IconButton
className={classes.chipClose}
onClick={() => handleSelect(value.value)}
>
<CloseIcon fontSize="small" />
</IconButton>
</div>
</div>
))}
</div>
</>
);
};
2020-03-06 11:09:55 +00:00
const MultiAutocompleteSelectField: React.FC<MultiAutocompleteSelectFieldProps> = ({
choices,
fetchChoices,
...props
}) => {
2019-08-09 11:14:35 +00:00
const [query, setQuery] = React.useState("");
2019-10-30 14:34:24 +00:00
2019-08-09 11:14:35 +00:00
if (fetchChoices) {
2019-06-19 14:40:52 +00:00
return (
<DebounceAutocomplete debounceFn={fetchChoices}>
2019-08-09 11:14:35 +00:00
{debounceFn => (
<MultiAutocompleteSelectFieldComponent
choices={choices}
{...props}
fetchChoices={debounceFn}
/>
2019-06-19 14:40:52 +00:00
)}
</DebounceAutocomplete>
);
}
2019-08-09 11:14:35 +00:00
return (
<MultiAutocompleteSelectFieldComponent
fetchChoices={q => setQuery(q || "")}
2019-10-16 15:51:53 +00:00
choices={filter(choices, query, {
key: "label"
})}
2019-08-09 11:14:35 +00:00
{...props}
/>
);
};
2019-10-30 14:34:24 +00:00
2019-06-19 14:40:52 +00:00
MultiAutocompleteSelectField.displayName = "MultiAutocompleteSelectField";
export default MultiAutocompleteSelectField;