2023-06-12 14:27:40 +02:00
|
|
|
import React, { useEffect, useRef, useState } from "react";
|
|
|
|
|
import { useCombobox } from "downshift";
|
|
|
|
|
import { useCunningham } from ":/components/Provider";
|
|
|
|
|
import {
|
|
|
|
|
getOptionsFilter,
|
|
|
|
|
optionToString,
|
2023-09-15 11:00:44 +02:00
|
|
|
optionToValue,
|
2023-06-12 14:27:40 +02:00
|
|
|
SelectMonoAux,
|
|
|
|
|
SubProps,
|
|
|
|
|
} from ":/components/Forms/Select/mono-common";
|
|
|
|
|
|
|
|
|
|
export const SelectMonoSearchable = (props: SubProps) => {
|
|
|
|
|
const { t } = useCunningham();
|
|
|
|
|
const [optionsToDisplay, setOptionsToDisplay] = useState(props.options);
|
|
|
|
|
const [hasInputFocused, setHasInputFocused] = useState(false);
|
|
|
|
|
const inputRef = useRef<HTMLInputElement>(null);
|
|
|
|
|
const downshiftReturn = useCombobox({
|
|
|
|
|
...props.downshiftProps,
|
|
|
|
|
items: optionsToDisplay,
|
|
|
|
|
itemToString: optionToString,
|
|
|
|
|
onInputValueChange: (e) => {
|
|
|
|
|
setOptionsToDisplay(props.options.filter(getOptionsFilter(e.inputValue)));
|
|
|
|
|
if (!e.inputValue) {
|
|
|
|
|
downshiftReturn.selectItem(null);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const [labelAsPlaceholder, setLabelAsPlaceholder] = useState(
|
2023-07-18 15:43:56 +02:00
|
|
|
!downshiftReturn.selectedItem,
|
2023-06-12 14:27:40 +02:00
|
|
|
);
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (hasInputFocused || downshiftReturn.inputValue) {
|
|
|
|
|
setLabelAsPlaceholder(false);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
setLabelAsPlaceholder(!downshiftReturn.selectedItem);
|
|
|
|
|
}, [
|
|
|
|
|
downshiftReturn.selectedItem,
|
|
|
|
|
hasInputFocused,
|
|
|
|
|
downshiftReturn.inputValue,
|
|
|
|
|
]);
|
|
|
|
|
|
2023-09-15 11:00:44 +02:00
|
|
|
// When component is controlled, this useEffect will update the local selected item.
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (props.downshiftProps.initialSelectedItem !== undefined) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const optionToSelect = props.options.find(
|
|
|
|
|
(option) => optionToValue(option) === props.value,
|
|
|
|
|
);
|
|
|
|
|
downshiftReturn.selectItem(optionToSelect ?? null);
|
|
|
|
|
}, [props.value, props.options, props.downshiftProps]);
|
|
|
|
|
|
2023-06-12 14:27:40 +02:00
|
|
|
const inputProps = downshiftReturn.getInputProps({
|
|
|
|
|
ref: inputRef,
|
|
|
|
|
disabled: props.disabled,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<SelectMonoAux
|
|
|
|
|
{...props}
|
|
|
|
|
downshiftReturn={{
|
|
|
|
|
...downshiftReturn,
|
|
|
|
|
wrapperProps: {
|
|
|
|
|
onClick: () => {
|
|
|
|
|
inputRef.current?.focus();
|
2023-07-18 16:27:48 +02:00
|
|
|
downshiftReturn.openMenu();
|
2023-06-12 14:27:40 +02:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
toggleButtonProps: downshiftReturn.getToggleButtonProps({
|
|
|
|
|
disabled: props.disabled,
|
|
|
|
|
"aria-label": t("components.forms.select.toggle_button_aria_label"),
|
|
|
|
|
}),
|
|
|
|
|
}}
|
|
|
|
|
labelAsPlaceholder={labelAsPlaceholder}
|
|
|
|
|
options={optionsToDisplay}
|
|
|
|
|
>
|
|
|
|
|
<input
|
|
|
|
|
{...inputProps}
|
2023-07-18 16:27:48 +02:00
|
|
|
onFocus={() => {
|
2023-06-12 14:27:40 +02:00
|
|
|
setHasInputFocused(true);
|
|
|
|
|
}}
|
2023-07-18 16:27:48 +02:00
|
|
|
onBlur={() => {
|
2023-06-12 14:27:40 +02:00
|
|
|
setHasInputFocused(false);
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
</SelectMonoAux>
|
|
|
|
|
);
|
|
|
|
|
};
|