import React, { useRef, useState, useEffect, useMemo } from 'react';
import Select from '@/_ui/Select';
import { components } from 'react-select';
import defaultStyles from '@/_ui/Select/styles';
import SolidIcon from '@/_ui/Icon/SolidIcons';
import { Checkbox } from '@/_ui/CheckBox/CheckBox';
import OverlayTrigger from 'react-bootstrap/OverlayTrigger';
import { isArray, isString } from 'lodash';
const { MenuList } = components;
const SCALING_FACTOR = 0.6;
const FONT_SIZE = 12;
const PADDING = 12;
const MARGIN = 10; // including left and right side
export const CustomSelect = ({
options,
value,
onChange,
fuzzySearch = false,
placeholder,
disabled,
className,
darkMode,
defaultOptionsList,
textColor = '',
isMulti,
containerWidth,
optionsLoadingState = false,
horizontalAlignment = 'left',
isEditable,
isMaxRowHeightAuto,
}) => {
const containerRef = useRef(null);
const inputRef = useRef(null); // Ref for the input search box
const [isFocused, setIsFocused] = useState(false);
useEffect(() => {
const handleDocumentClick = (event) => {
if (isMulti && !containerRef.current?.contains(event.target)) {
setIsFocused(false);
}
};
document.addEventListener('mousedown', handleDocumentClick);
return () => {
document.removeEventListener('mousedown', handleDocumentClick);
};
}, []);
useEffect(() => {
// Focus the input search box when the menu list is open and the component is focused
if (isFocused && inputRef.current) {
inputRef.current.focus();
}
}, [isFocused]);
const customStyles = {
...defaultStyles(darkMode, '100%'),
...(isMulti && {
multiValue: (provided) => ({
...provided,
display: 'inline-block', // Display selected options inline
marginRight: '4px', // Add some space between options
}),
}),
valueContainer: (provided) => ({
...provided,
...(isMulti && {
marginBottom: '0',
display: 'flex',
flexWrap: 'no-wrap',
overflow: 'hidden',
flexDirection: 'row',
}),
justifyContent: horizontalAlignment,
}),
menuList: (base) => ({
...base,
backgroundColor: 'var(--surfaces-surface-01) ',
color: 'var(--text-primary)',
cursor: 'pointer',
overflow: 'auto',
}),
multiValueLabel: (provided) => ({
...provided,
padding: '2px 6px',
background: 'var(--surfaces-surface-03)',
margin: '0 5px',
borderRadius: '6px',
color: textColor || 'var(--text-primary)',
fontSize: '12px',
}),
singleValue: (provided) => ({
...provided,
padding: '2px 6px',
background: 'var(--surfaces-surface-03)',
margin: '0 5px',
borderRadius: '6px',
color: textColor || 'var(--text-primary)',
fontSize: '12px',
}),
};
const customComponents = {
MenuList: (props) =>