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

230 lines
6.1 KiB
TypeScript
Raw Normal View History

2019-06-19 14:40:52 +00:00
import { InputProps } from "@material-ui/core/Input";
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";
import useStateFromProps from "@saleor/hooks/useStateFromProps";
import { FetchMoreProps } from "@saleor/types";
2020-09-23 08:29:13 +00:00
import classNames from "classnames";
2020-10-12 10:55:57 +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
import ArrowDropdownIcon from "../../icons/ArrowDropdown";
import Debounce, { DebounceProps } from "../Debounce";
2019-12-02 10:49:14 +00:00
import SingleAutocompleteSelectFieldContent, {
SingleAutocompleteActionType,
SingleAutocompleteChoiceType
2019-12-02 10:49:14 +00:00
} from "./SingleAutocompleteSelectFieldContent";
2019-06-19 14:40:52 +00:00
2019-12-03 15:28:40 +00:00
const useStyles = makeStyles(
{
container: {
flexGrow: 1,
position: "relative"
}
},
{ name: "SingleAutocompleteSelectField" }
);
2019-06-19 14:40:52 +00:00
2019-10-14 14:17:03 +00:00
export interface SingleAutocompleteSelectFieldProps
extends Partial<FetchMoreProps> {
add?: SingleAutocompleteActionType;
2020-09-23 08:29:13 +00:00
className?: string;
2019-06-19 14:40:52 +00:00
error?: boolean;
name: string;
2019-08-09 11:14:35 +00:00
displayValue: string;
emptyOption?: boolean;
choices: SingleAutocompleteChoiceType[];
2019-10-14 14:17:03 +00:00
value: string;
2019-06-19 14:40:52 +00:00
disabled?: boolean;
placeholder?: string;
2019-08-09 11:14:35 +00:00
allowCustomValues?: boolean;
2019-06-19 14:40:52 +00:00
helperText?: string;
label?: string;
InputProps?: InputProps;
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-01-15 15:36:45 +00:00
const DebounceAutocomplete: React.ComponentType<DebounceProps<
string
>> = Debounce;
2019-06-19 14:40:52 +00:00
2020-01-15 15:36:45 +00:00
const SingleAutocompleteSelectFieldComponent: React.FC<SingleAutocompleteSelectFieldProps> = props => {
2019-10-30 14:34:24 +00:00
const {
2020-02-10 16:07:17 +00:00
add,
2019-08-09 11:14:35 +00:00
allowCustomValues,
2020-09-23 08:29:13 +00:00
className,
2020-01-15 15:36:45 +00:00
choices,
2019-06-19 14:40:52 +00:00
disabled,
2019-08-09 11:14:35 +00:00
displayValue,
emptyOption,
2019-06-19 14:40:52 +00:00
error,
2019-10-14 14:17:03 +00:00
hasMore,
2019-06-19 14:40:52 +00:00
helperText,
label,
loading,
name,
placeholder,
value,
InputProps,
fetchChoices,
2019-08-27 13:29:00 +00:00
onChange,
2019-10-14 14:17:03 +00:00
onFetchMore,
2019-10-30 14:34:24 +00:00
...rest
} = props;
const classes = useStyles(props);
2019-10-14 11:57:08 +00:00
2019-10-30 14:34:24 +00:00
const [prevDisplayValue] = useStateFromProps(displayValue);
2019-06-19 14:40:52 +00:00
2020-10-12 10:45:07 +00:00
const handleChange = (
item: string,
stateAndHelpers: ControllerStateAndHelpers
) => {
2019-10-30 14:34:24 +00:00
onChange({
target: {
name,
value: item
}
} as any);
2020-10-12 10:45:07 +00:00
stateAndHelpers.reset({
inputValue: item
});
};
2019-08-09 11:14:35 +00:00
2019-10-30 14:34:24 +00:00
return (
<DebounceAutocomplete debounceFn={fetchChoices}>
{debounceFn => (
<Downshift
defaultInputValue={displayValue}
2020-10-12 10:45:07 +00:00
itemToString={() => displayValue || ""}
2019-10-30 14:34:24 +00:00
onInputValueChange={value => debounceFn(value)}
onSelect={handleChange}
selectedItem={value}
>
{({
getInputProps,
getItemProps,
isOpen,
inputValue,
selectedItem,
toggleMenu,
closeMenu,
highlightedIndex,
reset
}) => {
const isCustomValueSelected =
choices && selectedItem
? choices.filter(c => c.value === selectedItem).length === 0
: false;
const hasInputValueChanged = prevDisplayValue !== displayValue;
2019-08-09 11:14:35 +00:00
2019-10-30 14:34:24 +00:00
if (hasInputValueChanged) {
reset({ inputValue: displayValue });
}
2019-10-14 11:57:08 +00:00
2019-10-30 14:34:24 +00:00
const displayCustomValue =
inputValue &&
inputValue.length > 0 &&
allowCustomValues &&
!choices.find(
choice =>
choice.label.toLowerCase() === inputValue.toLowerCase()
2019-06-19 14:40:52 +00:00
);
2019-10-30 14:34:24 +00:00
2020-10-21 13:07:54 +00:00
const handleBlur = () => {
if(inputValue)
}
2019-10-30 14:34:24 +00:00
return (
2020-09-23 08:29:13 +00:00
<div
className={classNames(classes.container, className)}
{...rest}
>
2019-10-30 14:34:24 +00:00
<TextField
InputProps={{
...InputProps,
...getInputProps({
placeholder
}),
endAdornment: (
<div>
<ArrowDropdownIcon />
</div>
),
error,
id: undefined,
2020-10-21 13:07:54 +00:00
onBlur: handleBlur,
2019-10-30 14:34:24 +00:00
onClick: toggleMenu
}}
error={error}
disabled={disabled}
helperText={helperText}
label={label}
fullWidth={true}
/>
{isOpen && (!!inputValue || !!choices.length) && (
<SingleAutocompleteSelectFieldContent
add={
!!add && {
...add,
onClick: () => {
add.onClick();
closeMenu();
}
2020-02-10 16:07:17 +00:00
}
}
2019-10-30 14:34:24 +00:00
choices={choices}
displayCustomValue={displayCustomValue}
emptyOption={emptyOption}
getItemProps={getItemProps}
hasMore={hasMore}
highlightedIndex={highlightedIndex}
loading={loading}
inputValue={inputValue}
isCustomValueSelected={isCustomValueSelected}
selectedItem={selectedItem}
onFetchMore={onFetchMore}
/>
)}
</div>
);
}}
</Downshift>
)}
</DebounceAutocomplete>
);
};
2019-06-19 14:40:52 +00:00
2020-01-15 15:36:45 +00:00
const SingleAutocompleteSelectField: React.FC<SingleAutocompleteSelectFieldProps> = ({
choices,
fetchChoices,
...rest
}) => {
2019-10-16 15:51:53 +00:00
const [query, setQuery] = React.useState("");
2020-10-12 10:45:07 +00:00
2019-10-16 15:51:53 +00:00
if (fetchChoices) {
2019-06-19 14:40:52 +00:00
return (
2019-10-16 15:51:53 +00:00
<DebounceAutocomplete debounceFn={fetchChoices}>
{debounceFn => (
<SingleAutocompleteSelectFieldComponent
choices={choices}
2019-10-30 14:34:24 +00:00
{...rest}
2019-10-16 15:51:53 +00:00
fetchChoices={debounceFn}
/>
)}
</DebounceAutocomplete>
2019-06-19 14:40:52 +00:00
);
}
2019-10-14 14:17:03 +00:00
2019-10-16 15:51:53 +00:00
return (
<SingleAutocompleteSelectFieldComponent
fetchChoices={q => setQuery(q || "")}
choices={filter(choices, query, {
key: "label"
})}
2019-10-30 14:34:24 +00:00
{...rest}
2019-10-16 15:51:53 +00:00
/>
);
};
2019-06-19 14:40:52 +00:00
export default SingleAutocompleteSelectField;