import SelectMenu from './SelectMenu' import PrimitiveButton from '../Primitives/PrimitiveButton/PrimitiveButton' import Block from '../Primitives/Block/Block' import { ChevronDownIcon, HelpCircleIcon, X } from 'lucide-react' import { type SelectMenuGroupType, type SelectMenuItemType, SelectMenuSize, SelectMenuVariant, } from './types' import Text, { type VariantType } from '../Text/Text' import type { SelectTokensType } from './select.tokens.types' import { Tooltip, TooltipSize } from '../Tooltip' import React from 'react' import { useTheme } from '../../context/ThemeContext' import { useResponsiveTokens } from '../../hooks/useResponsiveTokens' export enum SelectionTagType { COUNT = 'count', TEXT = 'text', } type SelectProps = { label: string subLabel?: string hintText?: string required?: boolean helpIconText?: string placeholder: string size?: SelectMenuSize items: SelectMenuGroupType[] variant?: SelectMenuVariant selected: string | string[] onSelectChange: (value: string | string[]) => void allowMultiSelect?: boolean enableSearch?: boolean selectionTagType?: SelectionTagType slot?: React.ReactNode } const map = function getValueLabelMap( groups: SelectMenuGroupType[] ): Record { const map: Record = {} function traverse(items: SelectMenuItemType[]) { for (const item of items) { map[item.value] = item.label if (item.subMenu) { traverse(item.subMenu) } } } for (const group of groups) { traverse(group.items) } return map } const Select = ({ items, variant = SelectMenuVariant.CONTAINER, label = 'Your favorite color', subLabel = 'Select an option', hintText = 'Hint text', required = true, helpIconText = 'Help icon text', placeholder = 'Select an option', size = SelectMenuSize.MEDIUM, selected, onSelectChange, allowMultiSelect = false, enableSearch = false, selectionTagType = SelectionTagType.COUNT, slot, }: SelectProps) => { const { foundationTokens } = useTheme() const selectTokens = useResponsiveTokens('SELECT') const valueLabelMap = map(items) const getLabelsForSelectedValues = (values: string[]) => { return values.map((val) => valueLabelMap[val] || val) } const showCancelButton = variant === SelectMenuVariant.CONTAINER && allowMultiSelect && selected.length > 0 return ( {variant === SelectMenuVariant.CONTAINER && ( {variant === SelectMenuVariant.CONTAINER && ( {label} )} {subLabel && ( ({subLabel}) )} {variant === SelectMenuVariant.CONTAINER && required && ( * )} {variant === SelectMenuVariant.CONTAINER && helpIconText && ( )} )} {slot && ( {slot} )} {allowMultiSelect === false && ( {selected ? ( { valueLabelMap[ selected as string ] } ) : ( {placeholder} )} )} {allowMultiSelect && ( {placeholder} {selected && selected.length > 0 && ( {selectionTagType === SelectionTagType.TEXT ? ( {getLabelsForSelectedValues( selected as string[] ).join(', ')} ) : ( { selected.length } )} )} )} } /> {variant === SelectMenuVariant.CONTAINER && allowMultiSelect && selected.length > 0 && ( onSelectChange([])} outline={`${selectTokens.clearButton.outlineBorder} !important`} > )} {variant === SelectMenuVariant.CONTAINER && hintText && ( {hintText} )} ) } export default Select