import { Button, ButtonProps, chakra, FormControl, FormHelperText, FormLabel, HStack, Menu, MenuButton, MenuItem, MenuList, Portal, Stack, } from '@chakra-ui/react' import { ChevronLeftIcon } from '@/components/icons' import React, { ReactNode } from 'react' import { MoreInfoTooltip } from './MoreInfoTooltip' type Item = | string | number | { label: string value: string } // eslint-disable-next-line @typescript-eslint/no-explicit-any type Props = { currentItem: string | number | undefined onItemSelect: ( value: T extends string ? T : T extends number ? T : string, item?: T ) => void items: readonly T[] placeholder?: string label?: string isRequired?: boolean direction?: 'row' | 'column' helperText?: ReactNode moreInfoTooltip?: string } // eslint-disable-next-line @typescript-eslint/no-explicit-any export const DropdownList = ({ currentItem, onItemSelect, items, placeholder, label, isRequired, direction = 'column', helperText, moreInfoTooltip, ...props }: Props & ButtonProps) => { const handleMenuItemClick = (item: T) => () => { if (typeof item === 'string' || typeof item === 'number') onItemSelect(item as T extends string ? T : T extends number ? T : string) else onItemSelect( item.value as T extends string ? T : T extends number ? T : string, item ) } return ( {label && ( {label}{' '} {moreInfoTooltip && ( {moreInfoTooltip} )} )} } colorScheme="gray" justifyContent="space-between" textAlign="left" w="full" {...props} > {currentItem ? getItemLabel( items?.find((item) => typeof item === 'string' || typeof item === 'number' ? currentItem === item : currentItem === item.value ) ) : placeholder ?? 'Select an item'} {items.map((item) => ( {typeof item === 'object' ? item.label : item} ))} {helperText && {helperText}} ) } const getItemLabel = (item?: Item) => { if (!item) return '' if (typeof item === 'object') return item.label return item }