import React, { useCallback } from 'react'; import { DivInnerStyledOptionsContainer, DivStyledNoData, DivStyledOptionItem, DivStyledOptionsContainer, DivStyledSelected, StyledSelectParentDiv, } from './Dropdown.styles'; import { useState, useEffect } from 'react'; import { IDropdown } from './types'; import { Illustration } from '../Illustrations'; import { Icon } from '../Icon'; import { OptionProps } from '../Select'; import { Typography } from '../Typography'; const Dropdown: React.FC = ({ defaultOptionIndex, hasOutline = true, hideSelected = true, icon, isDisabled = false, isLabelFixed = true, isLabelVisible = true, label, onChange, options, selectedState, showSelected = true, width = '250px', dropdownArrowType = 'normal', ...props }) => { const [isOpen, setIsOpen] = useState(false); const [selectedIndex, setSelectedIndex] = useState(defaultOptionIndex); useEffect(() => { if (isDisabled) { setIsOpen(false); } }, [isDisabled, isOpen]); useEffect(() => { if (typeof selectedState == 'number') { setSelectedIndex(selectedState); } }, [selectedState, selectedIndex]); const handleSelectOptionClick = (selectedOption: OptionProps) => { setIsOpen(false); const indexOf = options.indexOf(selectedOption); setSelectedIndex(indexOf); onChange(selectedOption); }; const RenderOptions = useCallback(() => { if ( options.length == 0 || (options.length == 1 && selectedIndex != null && hideSelected) ) { return ( No Data ); } return ( {options .filter( (optionItem) => options.indexOf(optionItem) != selectedIndex || !hideSelected, ) .map((option) => ( { handleSelectOptionClick(option); }} key={option.id} > {option?.prefix} {isLabelVisible && ( {option.label} )} ))} ); }, [isOpen]); return ( { if (!isDisabled) { setIsOpen(!isOpen); } }} isOpen={!!isOpen} hasOutline={hasOutline} >
{icon && ( )} {typeof selectedIndex == 'number' && options[selectedIndex]?.prefix && options[selectedIndex]?.prefix} {isLabelVisible && ( {(isLabelFixed || typeof selectedIndex != 'number') && label} {typeof selectedIndex === 'number' && showSelected && options[selectedIndex]?.label} )}
); }; export default Dropdown;