2023-06-21 09:28:00 +00:00
|
|
|
// @ts-strict-ignore
|
2023-01-16 09:45:12 +00:00
|
|
|
import { MultiAutocompleteChoiceType } from "@dashboard/components/MultiAutocompleteSelectField";
|
|
|
|
import { maybe } from "@dashboard/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,
|
2022-06-21 09:36:55 +00:00
|
|
|
(a, b) => a.value === b.value,
|
2019-08-09 10:17:04 +00:00
|
|
|
);
|
|
|
|
const handleSelect = (
|
|
|
|
event: React.ChangeEvent<any>,
|
2022-06-21 09:36:55 +00:00
|
|
|
choices: MultiAutocompleteChoiceType[],
|
2019-08-09 10:17:04 +00:00
|
|
|
) => {
|
|
|
|
const value: string = event.target.value;
|
|
|
|
const match = choices.find(choice => choice.value === value);
|
|
|
|
toggle({
|
|
|
|
label: maybe(() => match.label, value),
|
2022-06-21 09:36:55 +00:00
|
|
|
value,
|
2019-08-09 10:17:04 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
change: handleSelect,
|
2022-06-21 09:36:55 +00:00
|
|
|
data: listElements,
|
2019-08-09 10:17:04 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export default useMultiAutocomplete;
|