2019-08-09 10:17:04 +00:00
|
|
|
import { MultiAutocompleteChoiceType } from "@saleor/components/MultiAutocompleteSelectField";
|
|
|
|
import { maybe } from "@saleor/misc";
|
2020-05-14 09:30:32 +00:00
|
|
|
|
2019-08-09 10:17:04 +00:00
|
|
|
import useListActions from "./useListActions";
|
|
|
|
|
|
|
|
function useMultiAutocomplete(initial: MultiAutocompleteChoiceType[] = []) {
|
|
|
|
const { listElements, toggle } = useListActions<MultiAutocompleteChoiceType>(
|
|
|
|
initial,
|
|
|
|
(a, b) => a.value === b.value
|
|
|
|
);
|
|
|
|
const handleSelect = (
|
|
|
|
event: React.ChangeEvent<any>,
|
|
|
|
choices: MultiAutocompleteChoiceType[]
|
|
|
|
) => {
|
|
|
|
const value: string = event.target.value;
|
|
|
|
const match = choices.find(choice => choice.value === value);
|
|
|
|
toggle({
|
|
|
|
label: maybe(() => match.label, value),
|
|
|
|
value
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
change: handleSelect,
|
|
|
|
data: listElements
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export default useMultiAutocomplete;
|