import React, { useRef } from 'react' import Block from '../Primitives/Block/Block' import { toPixels } from '../../global-utils/GlobalUtils' import { MultiSelectTokensType } from './multiSelect.tokens' import PrimitiveButton from '../Primitives/PrimitiveButton/PrimitiveButton' import FloatingLabels from '../Inputs/utils/FloatingLabels/FloatingLabels' import Text from '../Text/Text' import { FOUNDATION_THEME } from '../../tokens' import { ChevronDown, X } from 'lucide-react' import { MultiSelectMenuSize, MultiSelectSelectionTagType, MultiSelectVariant, } from './types' export type MultiSelectTriggerProps = { selectedValues: string[] slot: React.ReactNode variant: MultiSelectVariant size: MultiSelectMenuSize isSmallScreen: boolean onChange: (value: string) => void name: string label: string placeholder: string required: boolean selectionTagType: MultiSelectSelectionTagType valueLabelMap: Record open: boolean onClick?: React.MouseEventHandler multiSelectTokens: MultiSelectTokensType inline?: boolean error?: boolean disabled?: boolean maxTriggerWidth?: number minTriggerWidth?: number } & Omit< React.ButtonHTMLAttributes, 'slot' | 'onClick' | 'name' | 'disabled' | 'onChange' > const MultiSelectTrigger = ({ selectedValues, slot, variant, size, isSmallScreen, onChange, name, label, placeholder, required, open, onClick, multiSelectTokens, inline = false, error, maxTriggerWidth, minTriggerWidth, }: MultiSelectTriggerProps) => { const slotRef = useRef(null) const slotWidth = slotRef.current?.offsetWidth const showCancelButton = variant === MultiSelectVariant.CONTAINER && selectedValues.length > 0 const isItemSelected = selectedValues.length > 0 const isSmallScreenWithLargeSize = isSmallScreen && size === MultiSelectMenuSize.LARGE const borderRadius = multiSelectTokens.trigger.borderRadius[size][variant] const appliedBorderRadius = showCancelButton ? `${borderRadius} 0px 0px ${borderRadius}` : borderRadius const paddingX = toPixels( multiSelectTokens.trigger.padding[size][variant].x ) const paddingY = toPixels( multiSelectTokens.trigger.padding[size][variant].y ) const paddingInlineStart = slot && slotWidth ? paddingX + slotWidth + 8 : paddingX return ( {slot && ( {slot} )} {/* NO CONTAINER Label*/} {variant === MultiSelectVariant.NO_CONTAINER && ( {label} )} {isSmallScreenWithLargeSize && variant === MultiSelectVariant.CONTAINER && ( )} {variant === MultiSelectVariant.CONTAINER && (selectedValues.length > 0 || !isSmallScreen || size !== MultiSelectMenuSize.LARGE) && ( {placeholder} )} {selectedValues.length > 0 && ( {selectedValues.length} )} {variant === MultiSelectVariant.CONTAINER && selectedValues.length > 0 && ( onChange('')} aria-label={ label ? `Clear selection for ${label}` : 'Clear selection' } outline={ multiSelectTokens.trigger.outline[variant][ error ? 'error' : 'closed' ] } _hover={{ backgroundColor: multiSelectTokens.trigger.backgroundColor[ variant ].hover, }} _focus={{ backgroundColor: multiSelectTokens.trigger.backgroundColor[ variant ].focus, outline: `1px solid ${FOUNDATION_THEME.colors.gray[400]} !important`, }} > )} ) } export default MultiSelectTrigger