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

197 lines
5.6 KiB
TypeScript
Raw Normal View History

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