Fix single autocomplete inputs
This commit is contained in:
parent
03aafd8b63
commit
bfdeb0d654
1 changed files with 31 additions and 12 deletions
|
@ -1,7 +1,6 @@
|
||||||
import { InputProps } from "@material-ui/core/Input";
|
import { InputProps } from "@material-ui/core/Input";
|
||||||
import { makeStyles } from "@material-ui/core/styles";
|
import { makeStyles } from "@material-ui/core/styles";
|
||||||
import TextField from "@material-ui/core/TextField";
|
import TextField from "@material-ui/core/TextField";
|
||||||
import useStateFromProps from "@saleor/hooks/useStateFromProps";
|
|
||||||
import { FetchMoreProps } from "@saleor/types";
|
import { FetchMoreProps } from "@saleor/types";
|
||||||
import classNames from "classnames";
|
import classNames from "classnames";
|
||||||
import Downshift, { ControllerStateAndHelpers } from "downshift";
|
import Downshift, { ControllerStateAndHelpers } from "downshift";
|
||||||
|
@ -74,8 +73,6 @@ const SingleAutocompleteSelectFieldComponent: React.FC<SingleAutocompleteSelectF
|
||||||
} = props;
|
} = props;
|
||||||
const classes = useStyles(props);
|
const classes = useStyles(props);
|
||||||
|
|
||||||
const [prevDisplayValue] = useStateFromProps(displayValue);
|
|
||||||
|
|
||||||
const handleChange = (
|
const handleChange = (
|
||||||
item: string,
|
item: string,
|
||||||
stateAndHelpers: ControllerStateAndHelpers
|
stateAndHelpers: ControllerStateAndHelpers
|
||||||
|
@ -116,23 +113,45 @@ const SingleAutocompleteSelectFieldComponent: React.FC<SingleAutocompleteSelectF
|
||||||
choices && selectedItem
|
choices && selectedItem
|
||||||
? choices.filter(c => c.value === selectedItem).length === 0
|
? choices.filter(c => c.value === selectedItem).length === 0
|
||||||
: false;
|
: false;
|
||||||
const hasInputValueChanged = prevDisplayValue !== displayValue;
|
|
||||||
|
|
||||||
if (hasInputValueChanged) {
|
const choiceFromInputValue = choices.find(
|
||||||
reset({ inputValue: displayValue });
|
({ value: choiceId }) => choiceId === inputValue
|
||||||
|
);
|
||||||
|
|
||||||
|
const isValueInValues = !!choiceFromInputValue;
|
||||||
|
|
||||||
|
const isValueInLabels = !!choices.find(
|
||||||
|
choice => choice.label === inputValue
|
||||||
|
);
|
||||||
|
|
||||||
|
const ensureProperValues = (alwaysCheck: boolean = false) => {
|
||||||
|
if (allowCustomValues && !alwaysCheck) {
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const displayCustomValue =
|
if (isValueInValues && !isValueInLabels) {
|
||||||
|
reset({ inputValue: choiceFromInputValue.label });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
reset({ inputValue: displayValue });
|
||||||
|
};
|
||||||
|
|
||||||
|
const displayCustomValue = !!(
|
||||||
inputValue &&
|
inputValue &&
|
||||||
inputValue.length > 0 &&
|
inputValue.length > 0 &&
|
||||||
allowCustomValues &&
|
allowCustomValues &&
|
||||||
!choices.find(
|
!isValueInLabels
|
||||||
choice =>
|
|
||||||
choice.label.toLowerCase() === inputValue.toLowerCase()
|
|
||||||
);
|
);
|
||||||
|
|
||||||
const handleBlur = () => {
|
const handleBlur = () => {
|
||||||
if(inputValue)
|
ensureProperValues(true);
|
||||||
|
closeMenu();
|
||||||
|
};
|
||||||
|
|
||||||
|
// fix for bug where input value is returned from debounce as id instead of label
|
||||||
|
if (value === inputValue && inputValue !== "") {
|
||||||
|
ensureProperValues();
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|
Loading…
Reference in a new issue