2019-06-19 14:40:52 +00:00
|
|
|
import { Omit } from "@material-ui/core";
|
2019-08-09 11:14:35 +00:00
|
|
|
import CircularProgress from "@material-ui/core/CircularProgress";
|
2019-06-19 14:40:52 +00:00
|
|
|
import { InputProps } from "@material-ui/core/Input";
|
|
|
|
import MenuItem from "@material-ui/core/MenuItem";
|
|
|
|
import Paper from "@material-ui/core/Paper";
|
|
|
|
import {
|
|
|
|
createStyles,
|
|
|
|
Theme,
|
|
|
|
withStyles,
|
|
|
|
WithStyles
|
|
|
|
} from "@material-ui/core/styles";
|
|
|
|
import TextField from "@material-ui/core/TextField";
|
2019-08-09 11:14:35 +00:00
|
|
|
import Typography from "@material-ui/core/Typography";
|
2019-06-19 14:40:52 +00:00
|
|
|
import Downshift from "downshift";
|
2019-08-09 10:26:22 +00:00
|
|
|
import React from "react";
|
2019-08-26 21:54:03 +00:00
|
|
|
import { FormattedMessage } from "react-intl";
|
2019-06-19 14:40:52 +00:00
|
|
|
import { compareTwoStrings } from "string-similarity";
|
|
|
|
|
2019-08-09 11:14:35 +00:00
|
|
|
import useStateFromProps from "@saleor/hooks/useStateFromProps";
|
2019-06-19 14:40:52 +00:00
|
|
|
import ArrowDropdownIcon from "../../icons/ArrowDropdown";
|
|
|
|
import Debounce, { DebounceProps } from "../Debounce";
|
|
|
|
|
|
|
|
const styles = (theme: Theme) =>
|
|
|
|
createStyles({
|
|
|
|
container: {
|
|
|
|
flexGrow: 1,
|
|
|
|
position: "relative"
|
|
|
|
},
|
2019-08-09 11:14:35 +00:00
|
|
|
menuItem: {
|
|
|
|
height: "auto",
|
|
|
|
whiteSpace: "normal"
|
|
|
|
},
|
2019-06-19 14:40:52 +00:00
|
|
|
paper: {
|
|
|
|
borderRadius: 4,
|
|
|
|
left: 0,
|
|
|
|
marginTop: theme.spacing.unit,
|
|
|
|
padding: 8,
|
|
|
|
position: "absolute",
|
|
|
|
right: 0,
|
2019-09-04 13:43:18 +00:00
|
|
|
zIndex: 22
|
2019-06-19 14:40:52 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-08-09 11:14:35 +00:00
|
|
|
export interface SingleAutocompleteChoiceType {
|
|
|
|
label: string;
|
|
|
|
value: any;
|
|
|
|
}
|
2019-06-19 14:40:52 +00:00
|
|
|
export interface SingleAutocompleteSelectFieldProps {
|
|
|
|
error?: boolean;
|
|
|
|
name: string;
|
2019-08-09 11:14:35 +00:00
|
|
|
displayValue: string;
|
|
|
|
emptyOption?: boolean;
|
|
|
|
choices: SingleAutocompleteChoiceType[];
|
|
|
|
value?: string;
|
2019-06-19 14:40:52 +00:00
|
|
|
disabled?: boolean;
|
|
|
|
loading?: 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
|
|
|
}
|
|
|
|
|
|
|
|
interface SingleAutocompleteSelectFieldState {
|
|
|
|
choices: Array<{
|
|
|
|
label: string;
|
|
|
|
value: string;
|
|
|
|
}>;
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
helperText,
|
|
|
|
label,
|
|
|
|
loading,
|
|
|
|
name,
|
|
|
|
placeholder,
|
|
|
|
value,
|
|
|
|
InputProps,
|
|
|
|
fetchChoices,
|
2019-08-27 13:29:00 +00:00
|
|
|
onChange,
|
|
|
|
...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);
|
|
|
|
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,
|
|
|
|
openMenu,
|
|
|
|
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-08-09 11:14:35 +00:00
|
|
|
|
|
|
|
if (prevDisplayValue !== displayValue) {
|
|
|
|
reset({ inputValue: displayValue });
|
|
|
|
}
|
|
|
|
|
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>
|
|
|
|
{loading ? (
|
|
|
|
<CircularProgress size={20} />
|
|
|
|
) : (
|
|
|
|
<ArrowDropdownIcon onClick={toggleMenu} />
|
|
|
|
)}
|
|
|
|
</div>
|
2019-06-19 14:40:52 +00:00
|
|
|
),
|
|
|
|
error,
|
|
|
|
id: undefined,
|
|
|
|
onBlur: closeMenu,
|
|
|
|
onFocus: openMenu
|
|
|
|
}}
|
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-06-19 14:40:52 +00:00
|
|
|
<Paper className={classes.paper} square>
|
2019-08-09 11:14:35 +00:00
|
|
|
{choices.length > 0 || allowCustomValues ? (
|
2019-06-19 14:40:52 +00:00
|
|
|
<>
|
2019-08-09 11:14:35 +00:00
|
|
|
{emptyOption && (
|
2019-06-19 14:40:52 +00:00
|
|
|
<MenuItem
|
2019-08-09 11:14:35 +00:00
|
|
|
className={classes.menuItem}
|
2019-06-19 14:40:52 +00:00
|
|
|
component="div"
|
|
|
|
{...getItemProps({
|
2019-08-09 11:14:35 +00:00
|
|
|
item: ""
|
2019-06-19 14:40:52 +00:00
|
|
|
})}
|
2019-08-27 13:29:00 +00:00
|
|
|
data-tc="singleautocomplete-select-option"
|
2019-06-19 14:40:52 +00:00
|
|
|
>
|
2019-08-09 11:14:35 +00:00
|
|
|
<Typography color="textSecondary">
|
2019-08-26 21:54:03 +00:00
|
|
|
<FormattedMessage defaultMessage="None" />
|
2019-08-09 11:14:35 +00:00
|
|
|
</Typography>
|
2019-06-19 14:40:52 +00:00
|
|
|
</MenuItem>
|
|
|
|
)}
|
2019-08-09 11:14:35 +00:00
|
|
|
{choices.map((suggestion, index) => {
|
|
|
|
const choiceIndex = index + (emptyOption ? 1 : 0);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<MenuItem
|
|
|
|
className={classes.menuItem}
|
|
|
|
key={JSON.stringify(suggestion)}
|
|
|
|
selected={
|
|
|
|
highlightedIndex === choiceIndex ||
|
|
|
|
selectedItem === suggestion.value
|
|
|
|
}
|
|
|
|
component="div"
|
|
|
|
{...getItemProps({
|
|
|
|
index: choiceIndex,
|
|
|
|
item: suggestion.value
|
|
|
|
})}
|
2019-08-27 13:29:00 +00:00
|
|
|
data-tc="singleautocomplete-select-option"
|
2019-08-09 11:14:35 +00:00
|
|
|
>
|
|
|
|
{suggestion.label}
|
|
|
|
</MenuItem>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
{allowCustomValues &&
|
|
|
|
!!inputValue &&
|
|
|
|
!choices.find(
|
|
|
|
choice =>
|
|
|
|
choice.label.toLowerCase() ===
|
|
|
|
inputValue.toLowerCase()
|
|
|
|
) && (
|
|
|
|
<MenuItem
|
|
|
|
className={classes.menuItem}
|
|
|
|
key={"customValue"}
|
|
|
|
selected={isCustomValueSelected}
|
|
|
|
component="div"
|
|
|
|
{...getItemProps({
|
|
|
|
item: inputValue
|
|
|
|
})}
|
2019-08-27 13:29:00 +00:00
|
|
|
data-tc="singleautocomplete-select-option"
|
2019-08-09 11:14:35 +00:00
|
|
|
>
|
2019-08-26 21:54:03 +00:00
|
|
|
<FormattedMessage
|
|
|
|
defaultMessage="Add new value: {value}"
|
|
|
|
description="add custom select input option"
|
|
|
|
values={{
|
|
|
|
value: inputValue
|
|
|
|
}}
|
|
|
|
/>
|
2019-08-09 11:14:35 +00:00
|
|
|
</MenuItem>
|
|
|
|
)}
|
2019-06-19 14:40:52 +00:00
|
|
|
</>
|
|
|
|
) : (
|
2019-08-27 13:29:00 +00:00
|
|
|
<MenuItem
|
|
|
|
disabled={true}
|
|
|
|
component="div"
|
|
|
|
data-tc="singleautocomplete-select-no-options"
|
|
|
|
>
|
2019-08-26 21:54:03 +00:00
|
|
|
<FormattedMessage defaultMessage="No results found" />
|
2019-06-19 14:40:52 +00:00
|
|
|
</MenuItem>
|
|
|
|
)}
|
|
|
|
</Paper>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
</Downshift>
|
|
|
|
)}
|
|
|
|
</DebounceAutocomplete>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
export class SingleAutocompleteSelectField extends React.Component<
|
|
|
|
Omit<SingleAutocompleteSelectFieldProps, "classes">,
|
|
|
|
SingleAutocompleteSelectFieldState
|
|
|
|
> {
|
|
|
|
state = { choices: this.props.choices };
|
|
|
|
|
|
|
|
handleInputChange = (value: string) =>
|
|
|
|
this.setState((_, props) => ({
|
|
|
|
choices: props.choices
|
|
|
|
.sort((a, b) => {
|
|
|
|
const ratingA = compareTwoStrings(value || "", a.label);
|
|
|
|
const ratingB = compareTwoStrings(value || "", b.label);
|
|
|
|
if (ratingA > ratingB) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (ratingA < ratingB) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
})
|
|
|
|
.slice(0, 5)
|
|
|
|
}));
|
|
|
|
|
|
|
|
render() {
|
|
|
|
if (!!this.props.fetchChoices) {
|
|
|
|
return <SingleAutocompleteSelectFieldComponent {...this.props} />;
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<SingleAutocompleteSelectFieldComponent
|
|
|
|
{...this.props}
|
|
|
|
choices={this.state.choices}
|
|
|
|
fetchChoices={this.handleInputChange}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
export default SingleAutocompleteSelectField;
|