2022-04-28 08:43:05 +00:00
|
|
|
import {
|
|
|
|
Popper,
|
|
|
|
PopperPlacementType,
|
|
|
|
TextField,
|
|
|
|
Typography
|
|
|
|
} from "@material-ui/core";
|
2020-05-14 09:30:32 +00:00
|
|
|
import { fade } from "@material-ui/core/styles/colorManipulator";
|
2019-08-09 11:14:35 +00:00
|
|
|
import CloseIcon from "@material-ui/icons/Close";
|
2020-05-14 09:30:32 +00:00
|
|
|
import Debounce, { DebounceProps } from "@saleor/components/Debounce";
|
2022-03-25 12:37:01 +00:00
|
|
|
import { ChevronIcon, IconButton, makeStyles } from "@saleor/macaw-ui";
|
2020-05-14 09:30:32 +00:00
|
|
|
import { FetchMoreProps } from "@saleor/types";
|
2022-03-25 12:37:01 +00:00
|
|
|
import classNames from "classnames";
|
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-10-15 15:23:33 +00:00
|
|
|
import MultiAutocompleteSelectFieldContent, {
|
2020-05-14 09:30:32 +00:00
|
|
|
MultiAutocompleteActionType,
|
|
|
|
MultiAutocompleteChoiceType
|
2019-10-15 15:23:33 +00:00
|
|
|
} 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"
|
2020-04-23 15:43:08 +00:00
|
|
|
},
|
|
|
|
disabledChipInner: {
|
|
|
|
"& svg": {
|
2020-04-27 07:53:34 +00:00
|
|
|
color: theme.palette.grey[200]
|
2020-04-23 15:43:08 +00:00
|
|
|
},
|
|
|
|
alignItems: "center",
|
2020-04-27 07:53:34 +00:00
|
|
|
background: fade(theme.palette.grey[400], 0.8),
|
2020-04-23 15:43:08 +00:00
|
|
|
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)
|
2021-07-29 12:15:14 +00:00
|
|
|
},
|
|
|
|
adornment: {
|
2022-03-25 12:37:01 +00:00
|
|
|
color: theme.palette.saleor.main[3],
|
|
|
|
cursor: "pointer",
|
|
|
|
userSelect: "none",
|
2021-07-29 12:15:14 +00:00
|
|
|
display: "flex",
|
2021-10-04 07:40:24 +00:00
|
|
|
alignItems: "center",
|
2022-03-25 12:37:01 +00:00
|
|
|
"& svg": {
|
|
|
|
transition: theme.transitions.duration.shorter + "ms"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
adornmentRotate: {
|
|
|
|
"& svg": {
|
|
|
|
transform: "rotate(180deg)"
|
2021-10-04 07:40:24 +00:00
|
|
|
}
|
2019-12-03 15:28:40 +00:00
|
|
|
}
|
|
|
|
}),
|
|
|
|
{ name: "MultiAutocompleteSelectField" }
|
|
|
|
);
|
2019-06-19 14:40:52 +00:00
|
|
|
|
2019-10-15 15:23:33 +00:00
|
|
|
export interface MultiAutocompleteSelectFieldProps
|
|
|
|
extends Partial<FetchMoreProps> {
|
2020-03-18 17:24:55 +00:00
|
|
|
add?: MultiAutocompleteActionType;
|
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;
|
2020-12-16 10:53:28 +00:00
|
|
|
disabled?: boolean;
|
2021-04-21 08:02:48 +00:00
|
|
|
testId?: string;
|
2019-08-09 11:14:35 +00:00
|
|
|
fetchChoices?: (value: string) => void;
|
|
|
|
onChange: (event: React.ChangeEvent<any>) => void;
|
2021-08-11 09:47:01 +00:00
|
|
|
onBlur?: () => void;
|
2021-06-14 12:19:23 +00:00
|
|
|
fetchOnFocus?: boolean;
|
2021-07-29 12:15:14 +00:00
|
|
|
endAdornment?: React.ReactNode;
|
2022-04-28 08:43:05 +00:00
|
|
|
popperPlacement?: PopperPlacementType;
|
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 {
|
2020-03-18 17:24:55 +00:00
|
|
|
add,
|
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,
|
2020-12-16 10:53:28 +00:00
|
|
|
disabled,
|
2021-04-21 08:02:48 +00:00
|
|
|
testId,
|
2019-06-19 14:40:52 +00:00
|
|
|
fetchChoices,
|
2019-08-27 13:29:00 +00:00
|
|
|
onChange,
|
2021-08-11 09:47:01 +00:00
|
|
|
onBlur,
|
2019-10-15 15:23:33 +00:00
|
|
|
onFetchMore,
|
2021-06-14 12:19:23 +00:00
|
|
|
fetchOnFocus,
|
2021-07-29 12:15:14 +00:00
|
|
|
endAdornment,
|
2022-04-28 08:43:05 +00:00
|
|
|
popperPlacement = "bottom-end",
|
2019-10-30 14:34:24 +00:00
|
|
|
...rest
|
|
|
|
} = props;
|
|
|
|
const classes = useStyles(props);
|
2022-01-28 12:34:20 +00:00
|
|
|
const anchor = React.useRef<HTMLInputElement | null>(null);
|
2019-06-19 14:40:52 +00:00
|
|
|
|
2019-10-30 14:34:24 +00:00
|
|
|
const handleSelect = (
|
|
|
|
item: string,
|
2022-03-01 15:04:44 +00:00
|
|
|
downshiftOpts?: ControllerStateAndHelpers<string>
|
2019-10-30 14:34:24 +00:00
|
|
|
) => {
|
|
|
|
if (downshiftOpts) {
|
2021-09-02 07:01:57 +00:00
|
|
|
downshiftOpts.reset({ inputValue: "", isOpen: true });
|
2019-10-30 14:34:24 +00:00
|
|
|
}
|
|
|
|
onChange({
|
|
|
|
target: { name, value: item }
|
|
|
|
} as any);
|
|
|
|
};
|
2019-10-15 15:23:33 +00:00
|
|
|
|
2019-10-30 14:34:24 +00:00
|
|
|
return (
|
|
|
|
<>
|
2021-06-14 12:19:23 +00:00
|
|
|
<DebounceAutocomplete debounceFn={fetchChoices}>
|
|
|
|
{debounceFn => (
|
|
|
|
<Downshift
|
|
|
|
onInputValueChange={value => debounceFn(value)}
|
|
|
|
onSelect={handleSelect}
|
|
|
|
itemToString={() => ""}
|
2021-08-11 09:47:01 +00:00
|
|
|
// this is to prevent unwanted state updates when the dropdown is closed with an empty value,
|
|
|
|
// which downshift interprets as the value being updated with an empty string, causing side-effects
|
|
|
|
stateReducer={(state, changes) => {
|
|
|
|
if (changes.isOpen === false && state.inputValue === "") {
|
|
|
|
delete changes.inputValue;
|
|
|
|
}
|
|
|
|
return changes;
|
|
|
|
}}
|
2021-06-14 12:19:23 +00:00
|
|
|
>
|
|
|
|
{({
|
|
|
|
closeMenu,
|
|
|
|
getInputProps,
|
|
|
|
getItemProps,
|
|
|
|
isOpen,
|
|
|
|
toggleMenu,
|
2022-03-01 15:04:44 +00:00
|
|
|
getMenuProps,
|
2021-06-14 12:19:23 +00:00
|
|
|
highlightedIndex,
|
2022-03-25 12:37:01 +00:00
|
|
|
inputValue,
|
|
|
|
getToggleButtonProps
|
2021-06-14 12:19:23 +00:00
|
|
|
}) => {
|
|
|
|
const displayCustomValue =
|
|
|
|
inputValue &&
|
|
|
|
inputValue.length > 0 &&
|
|
|
|
allowCustomValues &&
|
|
|
|
!choices.find(
|
|
|
|
choice =>
|
|
|
|
choice.label.toLowerCase() === inputValue.toLowerCase()
|
|
|
|
);
|
2019-10-30 14:34:24 +00:00
|
|
|
|
2021-06-14 12:19:23 +00:00
|
|
|
return (
|
|
|
|
<div className={classes.container} {...rest}>
|
|
|
|
<TextField
|
|
|
|
InputProps={{
|
|
|
|
endAdornment: (
|
2022-03-25 12:37:01 +00:00
|
|
|
<div
|
|
|
|
{...getToggleButtonProps()}
|
|
|
|
className={classNames(classes.adornment, {
|
|
|
|
[classes.adornmentRotate]: isOpen
|
|
|
|
})}
|
|
|
|
>
|
2021-07-29 12:15:14 +00:00
|
|
|
{endAdornment}
|
2022-03-25 12:37:01 +00:00
|
|
|
<ChevronIcon />
|
2021-06-14 12:19:23 +00:00
|
|
|
</div>
|
|
|
|
),
|
2022-03-01 15:04:44 +00:00
|
|
|
ref: anchor,
|
2021-06-14 12:19:23 +00:00
|
|
|
onFocus: () => {
|
|
|
|
if (fetchOnFocus) {
|
|
|
|
fetchChoices(inputValue);
|
|
|
|
}
|
2022-03-01 15:04:44 +00:00
|
|
|
}
|
|
|
|
}}
|
|
|
|
inputProps={{
|
|
|
|
...getInputProps({
|
|
|
|
placeholder,
|
2022-04-28 08:56:56 +00:00
|
|
|
testId,
|
2022-03-01 15:04:44 +00:00
|
|
|
onClick: toggleMenu
|
|
|
|
}),
|
|
|
|
...getMenuProps()
|
2021-06-14 12:19:23 +00:00
|
|
|
}}
|
|
|
|
error={error}
|
|
|
|
helperText={helperText}
|
|
|
|
label={label}
|
|
|
|
fullWidth={true}
|
|
|
|
disabled={disabled}
|
2022-03-25 12:37:01 +00:00
|
|
|
onBlur={onBlur}
|
2021-06-14 12:19:23 +00:00
|
|
|
/>
|
2021-07-01 08:20:35 +00:00
|
|
|
{isOpen && (
|
2022-01-28 12:34:20 +00:00
|
|
|
<Popper
|
|
|
|
anchorEl={anchor.current}
|
|
|
|
open={isOpen}
|
|
|
|
style={{
|
|
|
|
width: anchor.current.clientWidth,
|
|
|
|
zIndex: 1301
|
|
|
|
}}
|
2022-04-28 08:43:05 +00:00
|
|
|
placement={popperPlacement}
|
2022-01-28 12:34:20 +00:00
|
|
|
>
|
|
|
|
<MultiAutocompleteSelectFieldContent
|
|
|
|
add={
|
|
|
|
add && {
|
|
|
|
...add,
|
|
|
|
onClick: () => {
|
|
|
|
add.onClick();
|
|
|
|
closeMenu();
|
|
|
|
}
|
2021-06-14 12:19:23 +00:00
|
|
|
}
|
|
|
|
}
|
2022-03-25 12:46:43 +00:00
|
|
|
choices={choices?.filter(
|
2022-01-28 12:34:20 +00:00
|
|
|
choice => !value.includes(choice.value)
|
|
|
|
)}
|
|
|
|
displayCustomValue={displayCustomValue}
|
|
|
|
displayValues={displayValues}
|
|
|
|
getItemProps={getItemProps}
|
|
|
|
hasMore={hasMore}
|
|
|
|
highlightedIndex={highlightedIndex}
|
|
|
|
loading={loading}
|
|
|
|
inputValue={inputValue}
|
|
|
|
onFetchMore={onFetchMore}
|
|
|
|
/>
|
|
|
|
</Popper>
|
2019-10-30 14:34:24 +00:00
|
|
|
)}
|
2021-06-14 12:19:23 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
</Downshift>
|
|
|
|
)}
|
|
|
|
</DebounceAutocomplete>
|
2019-10-30 14:34:24 +00:00
|
|
|
<div className={classes.chipContainer}>
|
|
|
|
{displayValues.map(value => (
|
|
|
|
<div className={classes.chip} key={value.value}>
|
2020-04-23 15:43:08 +00:00
|
|
|
<div
|
|
|
|
className={
|
|
|
|
!value.disabled ? classes.chipInner : classes.disabledChipInner
|
|
|
|
}
|
|
|
|
>
|
2019-10-30 14:34:24 +00:00
|
|
|
<Typography className={classes.chipLabel}>
|
|
|
|
{value.label}
|
|
|
|
</Typography>
|
2020-04-27 07:53:34 +00:00
|
|
|
|
|
|
|
<IconButton
|
2022-01-28 12:34:20 +00:00
|
|
|
hoverOutline={false}
|
|
|
|
variant="secondary"
|
2022-02-11 11:28:55 +00:00
|
|
|
data-test-id={testId ? `${testId}-remove` : "remove"}
|
2020-04-27 07:53:34 +00:00
|
|
|
className={classes.chipClose}
|
|
|
|
disabled={value.disabled}
|
|
|
|
onClick={() => handleSelect(value.value)}
|
|
|
|
>
|
|
|
|
<CloseIcon fontSize="small" />
|
|
|
|
</IconButton>
|
2019-10-30 14:34:24 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
))}
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2020-03-06 11:09:55 +00:00
|
|
|
const MultiAutocompleteSelectField: React.FC<MultiAutocompleteSelectFieldProps> = ({
|
|
|
|
choices,
|
|
|
|
fetchChoices,
|
2021-04-21 08:02:48 +00:00
|
|
|
testId,
|
2020-03-06 11:09:55 +00:00
|
|
|
...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 (
|
2021-07-01 08:20:35 +00:00
|
|
|
<MultiAutocompleteSelectFieldComponent
|
|
|
|
testId={testId}
|
|
|
|
choices={choices}
|
|
|
|
{...props}
|
|
|
|
fetchChoices={fetchChoices}
|
|
|
|
/>
|
2019-06-19 14:40:52 +00:00
|
|
|
);
|
|
|
|
}
|
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;
|