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

214 lines
6 KiB
TypeScript
Raw Normal View History

2019-06-19 14:40:52 +00:00
import { Omit } from "@material-ui/core";
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-08-09 10:26:22 +00:00
import React from "react";
2019-06-19 14:40:52 +00:00
import { compareTwoStrings } from "string-similarity";
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
}
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,
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,
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-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 =
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>
2019-10-14 14:17:03 +00:00
<ArrowDropdownIcon onClick={toggleMenu} />
2019-08-09 11:14:35 +00:00
</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-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>
);
}
);
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}
/>
);
}
}
2019-10-14 14:17:03 +00:00
2019-06-19 14:40:52 +00:00
export default SingleAutocompleteSelectField;